001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.localhistory.store;
042:
043: import java.io.BufferedInputStream;
044: import java.io.BufferedOutputStream;
045: import java.io.File;
046: import java.io.FileNotFoundException;
047: import java.io.FileOutputStream;
048: import java.io.FileInputStream;
049: import java.io.IOException;
050: import java.io.InputStream;
051: import java.io.OutputStream;
052: import java.sql.Date;
053: import java.util.zip.ZipEntry;
054: import java.util.zip.ZipInputStream;
055: import java.util.zip.ZipOutputStream;
056: import org.openide.filesystems.FileObject;
057: import org.openide.filesystems.FileUtil;
058:
059: /**
060: *
061: * @author Tomas Stupka
062: *
063: * // XXX override for folder and to be deleted
064: * // XXX status or isDeleted
065: *
066: */
067: public abstract class StoreEntry {
068:
069: private final File file;
070: private final File storeFile;
071: private final long ts;
072: private final String label;
073: private final Date date;
074: private String mimeType = null;
075:
076: public static StoreEntry createStoreEntry(File file,
077: File storeFile, long ts, String label) {
078: return new DefaultStoreEntry(file, storeFile, ts, label);
079: }
080:
081: public static StoreEntry createDeletedStoreEntry(File file, long ts) {
082: return new DeletedStoreEntry(file, ts);
083: }
084:
085: public static StoreEntry createFakeStoreEntry(File file, long ts) {
086: return new FakeStoreEntry(file, ts);
087: }
088:
089: private StoreEntry(File file, File storeFile, long ts, String label) {
090: this .file = file;
091: this .storeFile = storeFile;
092: this .ts = ts;
093: this .label = label;
094: this .date = new Date(ts);
095: }
096:
097: public File getStoreFile() {
098: return storeFile;
099: }
100:
101: public File getFile() {
102: return file;
103: }
104:
105: public long getTimestamp() {
106: return ts;
107: }
108:
109: public String getLabel() {
110: return label != null ? label : "";
111: }
112:
113: public Date getDate() {
114: return date;
115: }
116:
117: public boolean representsFile() {
118: return storeFile.isFile();
119: }
120:
121: public String getMIMEType() {
122: if (mimeType == null) {
123: FileObject fo = FileUtil.toFileObject(getFile());
124: if (fo != null) {
125: mimeType = fo.getMIMEType();
126: } else {
127: mimeType = "content/unknown";
128: }
129: }
130: return mimeType;
131: }
132:
133: static OutputStream createStoreFileOutputStream(File storeFile)
134: throws FileNotFoundException, IOException {
135: ZipOutputStream zos = new ZipOutputStream(
136: new BufferedOutputStream(
137: new FileOutputStream(storeFile)));
138: ZipEntry entry = new ZipEntry(storeFile.getName());
139: zos.putNextEntry(entry);
140: return zos;
141: }
142:
143: abstract OutputStream getStoreFileOutputStream()
144: throws FileNotFoundException, IOException;
145:
146: public abstract InputStream getStoreFileInputStream()
147: throws FileNotFoundException, IOException;
148:
149: private static class DefaultStoreEntry extends StoreEntry {
150:
151: private DefaultStoreEntry(File file, File storeFile, long ts,
152: String label) {
153: super (file, storeFile, ts, label);
154: }
155:
156: OutputStream getStoreFileOutputStream()
157: throws FileNotFoundException, IOException {
158: return createStoreFileOutputStream(getStoreFile());
159: }
160:
161: public InputStream getStoreFileInputStream()
162: throws FileNotFoundException, IOException {
163: ZipInputStream zis = new ZipInputStream(
164: new BufferedInputStream(new FileInputStream(
165: getStoreFile())));
166: ZipEntry entry;
167: while ((entry = zis.getNextEntry()) != null) {
168: if (entry.getName().equals(getStoreFile().getName())) {
169: return zis;
170: }
171: }
172: throw new FileNotFoundException();
173: }
174: }
175:
176: private static class DeletedStoreEntry extends StoreEntry {
177: public DeletedStoreEntry(File file, long ts) {
178: super (file, null, ts, "");
179: }
180:
181: OutputStream getStoreFileOutputStream()
182: throws FileNotFoundException, IOException {
183: throwNoStoreEntry();
184: return null;
185: }
186:
187: public InputStream getStoreFileInputStream()
188: throws FileNotFoundException, IOException {
189: throwNoStoreEntry();
190: return null;
191: }
192:
193: private void throwNoStoreEntry() throws FileNotFoundException {
194: throw new FileNotFoundException(
195: "There is no store entry for file " + getFile()
196: + " and timestamp " + getTimestamp());
197: }
198:
199: }
200:
201: private static class FakeStoreEntry extends StoreEntry {
202:
203: public FakeStoreEntry(File file, long ts) {
204: super (file, file, ts, "");
205: }
206:
207: OutputStream getStoreFileOutputStream()
208: throws FileNotFoundException, IOException {
209: throw new FileNotFoundException(
210: "There is no OutputStream for this for file "
211: + getFile());
212: }
213:
214: public InputStream getStoreFileInputStream()
215: throws FileNotFoundException, IOException {
216: return new FileInputStream(getFile());
217: }
218: }
219: }
|