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(final 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(final 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(final 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:     if ("/".equals(this.contentId) == false)
 112:     {
 113:       try
 114:       {
 115:         final ZipEntry entry = new ZipEntry(contentId);
 116:         repository.writeDirectory(entry);
 117:       }
 118:       catch (IOException e)
 119:       {
 120:         throw new ContentCreationException("Failed to create directory.");
 121:       }
 122:     }
 123:     return item;
 124:   }
 125: 
 126:   public boolean exists(final String name)
 127:   {
 128:     return entries.containsKey(name);
 129:   }
 130: 
 131:   public String getName()
 132:   {
 133:     return name;
 134:   }
 135: 
 136:   public Object getContentId()
 137:   {
 138:     return contentId;
 139:   }
 140: 
 141:   public Object getAttribute(final String domain, final String key)
 142:   {
 143:     return null;
 144:   }
 145: 
 146:   public boolean setAttribute(final String domain, final String key, final Object value)
 147:   {
 148:     return false;
 149:   }
 150: 
 151:   public ContentLocation getParent()
 152:   {
 153:     return parent;
 154:   }
 155: 
 156:   public Repository getRepository()
 157:   {
 158:     return repository;
 159:   }
 160: 
 161:   public boolean delete()
 162:   {
 163:     return false;
 164:   }
 165: }