001 /**
002 * ===========================================================
003 * LibRepository : a free Java content repository access layer
004 * ===========================================================
005 *
006 * Project Info: http://jfreereport.pentaho.org/librepository/
007 *
008 * (C) Copyright 2006, by Pentaho Corporation and Contributors.
009 *
010 * This library is free software; you can redistribute it and/or modify it under the terms
011 * of the GNU Lesser General Public License as published by the Free Software Foundation;
012 * either version 2.1 of the License, or (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016 * See the GNU Lesser General Public License for more details.
017 *
018 * You should have received a copy of the GNU Lesser General Public License along with this
019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020 * Boston, MA 02111-1307, USA.
021 *
022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023 * in the United States and other countries.]
024 *
025 * ------------
026 * ZipContentLocation.java
027 * ------------
028 * (C) Copyright 2006, by Pentaho Corporation.
029 */
030
031 package org.jfree.repository.zipwriter;
032
033 import java.util.HashMap;
034 import java.util.zip.ZipEntry;
035 import java.io.IOException;
036
037 import org.jfree.repository.ContentLocation;
038 import org.jfree.repository.ContentEntity;
039 import org.jfree.repository.ContentIOException;
040 import org.jfree.repository.ContentItem;
041 import org.jfree.repository.ContentCreationException;
042 import org.jfree.repository.Repository;
043 import org.jfree.repository.RepositoryUtilities;
044
045 /**
046 * Creation-Date: 01.12.2006, 21:13:24
047 *
048 * @author Thomas Morgner
049 */
050 public class ZipContentLocation implements ContentLocation
051 {
052 private HashMap entries;
053 private String name;
054 private String contentId;
055 private ContentLocation parent;
056 private ZipRepository repository;
057
058 public ZipContentLocation(final ZipRepository repository,
059 final ContentLocation parent,
060 final String name)
061 {
062 this.repository = repository;
063 this.parent = parent;
064 this.name = name;
065 this.entries = new HashMap();
066 this.contentId = RepositoryUtilities.buildName(this, "/") + "/";
067 }
068
069 public ContentEntity[] listContents() throws ContentIOException
070 {
071 return (ContentEntity[]) entries.values().toArray
072 (new ContentEntity[entries.size()]);
073 }
074
075 public ContentEntity getEntry(final String name) throws ContentIOException
076 {
077 return (ContentEntity) entries.get(name);
078 }
079
080 /**
081 * Creates a new data item in the current location. This method must never
082 * return null.
083 *
084 * @param name
085 * @return
086 * @throws org.jfree.repository.ContentCreationException
087 * if the item could not be created.
088 */
089 public ContentItem createItem(final String name) throws ContentCreationException
090 {
091 if (entries.containsKey(name))
092 {
093 throw new ContentCreationException("Entry already exists");
094 }
095
096 final ZipContentItem item = new ZipContentItem(name, repository, this);
097 entries.put (name, item);
098 return item;
099 }
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 }