Source for org.jfree.repository.zipwriter.ZipContentLocation

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * ZipContentLocation.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository.zipwriter;
  32: 
  33: import java.util.HashMap;
  34: import java.util.zip.ZipEntry;
  35: import java.io.IOException;
  36: 
  37: import org.jfree.repository.ContentLocation;
  38: import org.jfree.repository.ContentEntity;
  39: import org.jfree.repository.ContentIOException;
  40: import org.jfree.repository.ContentItem;
  41: import org.jfree.repository.ContentCreationException;
  42: import org.jfree.repository.Repository;
  43: import org.jfree.repository.RepositoryUtilities;
  44: 
  45: /**
  46:  * Creation-Date: 01.12.2006, 21:13:24
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public class ZipContentLocation implements ContentLocation
  51: {
  52:   private HashMap entries;
  53:   private String name;
  54:   private String contentId;
  55:   private ContentLocation parent;
  56:   private ZipRepository repository;
  57: 
  58:   public ZipContentLocation(final ZipRepository repository,
  59:                             final ContentLocation parent,
  60:                             final String name)
  61:   {
  62:     this.repository = repository;
  63:     this.parent = parent;
  64:     this.name = name;
  65:     this.entries = new HashMap();
  66:     this.contentId = RepositoryUtilities.buildName(this, "/") + "/";
  67:   }
  68: 
  69:   public ContentEntity[] listContents() throws ContentIOException
  70:   {
  71:     return (ContentEntity[]) entries.values().toArray
  72:         (new ContentEntity[entries.size()]);
  73:   }
  74: 
  75:   public ContentEntity getEntry(String name) throws ContentIOException
  76:   {
  77:     return (ContentEntity) entries.get(name);
  78:   }
  79: 
  80:   /**
  81:    * Creates a new data item in the current location. This method must never
  82:    * return null.
  83:    *
  84:    * @param name
  85:    * @return
  86:    * @throws org.jfree.repository.ContentCreationException
  87:    *          if the item could not be created.
  88:    */
  89:   public ContentItem createItem(String name) throws ContentCreationException
  90:   {
  91:     if (entries.containsKey(name))
  92:     {
  93:       throw new ContentCreationException("Entry already exists");
  94:     }
  95: 
  96:     final ZipContentItem item = new ZipContentItem(name, repository, this);
  97:     entries.put (name, item);
  98:     return item;
  99:   }
 100: 
 101:   public ContentLocation createLocation(String name)
 102:       throws ContentCreationException
 103:   {
 104:     if (entries.containsKey(name))
 105:     {
 106:       throw new ContentCreationException("Entry already exists");
 107:     }
 108: 
 109:     final ZipContentLocation item = new ZipContentLocation(repository, this, name);
 110:     entries.put (name, item);
 111: 
 112:     try
 113:     {
 114:       final ZipEntry entry = new ZipEntry(contentId);
 115:       repository.writeDirectory(entry);
 116:       return item;
 117:     }
 118:     catch (IOException e)
 119:     {
 120:       throw new ContentCreationException("Failed to create directory.");
 121:     }
 122:   }
 123: 
 124:   public boolean exists(final String name)
 125:   {
 126:     return entries.containsKey(name);
 127:   }
 128: 
 129:   public String getName()
 130:   {
 131:     return name;
 132:   }
 133: 
 134:   public Object getContentId()
 135:   {
 136:     return contentId;
 137:   }
 138: 
 139:   public Object getAttribute(String domain, String key)
 140:   {
 141:     return null;
 142:   }
 143: 
 144:   public boolean setAttribute(String domain, String key, Object value)
 145:   {
 146:     return false;
 147:   }
 148: 
 149:   public ContentLocation getParent()
 150:   {
 151:     return parent;
 152:   }
 153: 
 154:   public Repository getRepository()
 155:   {
 156:     return repository;
 157:   }
 158: 
 159:   public boolean delete()
 160:   {
 161:     return false;
 162:   }
 163: }