KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > store > StoreEntry


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.localhistory.store;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.BufferedOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.OutputStream JavaDoc;
30 import java.sql.Date JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipInputStream JavaDoc;
33 import java.util.zip.ZipOutputStream JavaDoc;
34 import org.openide.filesystems.FileObject;
35 import org.openide.filesystems.FileUtil;
36
37 /**
38  *
39  * @author Tomas Stupka
40  *
41  * // XXX override for folder and to be deleted
42  * // XXX status or isDeleted
43  *
44  */

45 public abstract class StoreEntry {
46             
47     private final File JavaDoc file;
48     private final File JavaDoc storeFile;
49     private final long ts;
50     private final String JavaDoc label;
51     private final Date JavaDoc date;
52     private String JavaDoc mimeType = null;
53       
54     public static StoreEntry createStoreEntry(File JavaDoc file, File JavaDoc storeFile, long ts, String JavaDoc label) {
55         return new DefaultStoreEntry(file, storeFile, ts, label);
56     }
57
58     public static StoreEntry createDeletedStoreEntry(File JavaDoc file, long ts) {
59         return new DeletedStoreEntry(file, ts);
60     }
61
62     public static StoreEntry createFakeStoreEntry(File JavaDoc file, long ts) {
63         return new FakeStoreEntry(file, ts);
64     }
65     
66     private StoreEntry(File JavaDoc file, File JavaDoc storeFile, long ts, String JavaDoc label) {
67         this.file = file;
68         this.storeFile = storeFile;
69         this.ts = ts;
70         this.label = label;
71         this.date = new Date JavaDoc(ts);
72     }
73     
74     public File JavaDoc getStoreFile() {
75         return storeFile;
76     }
77
78     public File JavaDoc getFile() {
79         return file;
80     }
81     
82     public long getTimestamp() {
83         return ts;
84     }
85     
86     public String JavaDoc getLabel() {
87         return label != null ? label : "";
88     }
89     
90     public Date JavaDoc getDate() {
91         return date;
92     }
93     
94     public boolean representsFile() {
95         return storeFile.isFile();
96     }
97         
98     public String JavaDoc getMIMEType() {
99         if(mimeType == null) {
100             FileObject fo = FileUtil.toFileObject(getFile());
101             if(fo != null) {
102                 mimeType = fo.getMIMEType();
103             } else {
104                 mimeType = "content/unknown";
105             }
106         }
107         return mimeType;
108     }
109     
110     // XXX compresion level etc.
111
static OutputStream JavaDoc createStoreFileOutputSteam(File JavaDoc storeFile) throws FileNotFoundException JavaDoc, IOException JavaDoc {
112         ZipOutputStream JavaDoc zos = new ZipOutputStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(storeFile)));
113         ZipEntry JavaDoc entry = new ZipEntry JavaDoc(storeFile.getName());
114         zos.putNextEntry(entry);
115         return zos;
116     }
117         
118     abstract OutputStream JavaDoc getStoreFileOutputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc;
119     public abstract InputStream JavaDoc getStoreFileInputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc;
120     
121     private static class DefaultStoreEntry extends StoreEntry {
122         
123         private DefaultStoreEntry(File JavaDoc file, File JavaDoc storeFile, long ts, String JavaDoc label) {
124             super(file, storeFile, ts, label);
125         }
126         
127         OutputStream JavaDoc getStoreFileOutputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
128             return createStoreFileOutputSteam(getStoreFile());
129         }
130
131         public InputStream JavaDoc getStoreFileInputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
132             ZipInputStream JavaDoc zis = new ZipInputStream JavaDoc(new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(getStoreFile())));
133             ZipEntry JavaDoc entry;
134             while ( (entry = zis.getNextEntry()) != null ) {
135                 if( entry.getName().equals(getStoreFile().getName()) ) {
136                     return zis;
137                 }
138             }
139             throw new FileNotFoundException JavaDoc();
140         }
141     }
142     
143     private static class DeletedStoreEntry extends StoreEntry {
144         public DeletedStoreEntry(File JavaDoc file, long ts) {
145             super(file, null, ts, "");
146         }
147         
148         OutputStream JavaDoc getStoreFileOutputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
149             throwNoStoreEntry();
150             return null;
151         }
152
153         public InputStream JavaDoc getStoreFileInputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
154             throwNoStoreEntry();
155             return null;
156         }
157         
158         private void throwNoStoreEntry() throws FileNotFoundException JavaDoc {
159             throw new FileNotFoundException JavaDoc("There is no store entry for file " + getFile() + " and timestamp " + getTimestamp());
160         }
161         
162     }
163
164     private static class FakeStoreEntry extends StoreEntry {
165         
166         public FakeStoreEntry(File JavaDoc file, long ts) {
167             super(file, file, ts, "");
168         }
169
170         OutputStream JavaDoc getStoreFileOutputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
171             throw new FileNotFoundException JavaDoc("There is no OutputStream for this for file " + getFile());
172         }
173
174         public InputStream JavaDoc getStoreFileInputStream() throws FileNotFoundException JavaDoc, IOException JavaDoc {
175             return new FileInputStream JavaDoc(getFile());
176         }
177     }
178 }
179
Popular Tags