Removed
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.core.projects; |
15 |
|
16 |
import java.beans.*; |
17 |
import java.io.*; |
18 |
import java.util.*; |
19 |
|
20 |
|
21 |
import org.openide.filesystems.*; |
22 |
import org.openide.filesystems.AbstractFileSystem.*; |
23 |
|
24 |
|
25 |
/** |
26 |
* Simple implementation of memory file system. |
27 |
* @author Jaroslav Tulach |
28 |
*/ |
29 |
public final class MemoryFileSystem extends AbstractFileSystem implements Info, Change, AbstractFileSystem.List, Attr { |
30 |
/** time when the filesystem was created. It is supposed to be the default |
31 |
* time of modification for all resources that has not been modified yet |
32 |
*/ |
33 |
private java.util.Date created = new java.util.Date (); |
34 |
|
35 |
static final class Entry { |
36 |
/** String, Object */ |
37 |
public HashMap attrs = new HashMap (); |
38 |
public byte[] data; |
39 |
public java.util.Date last; |
40 |
} |
41 |
|
42 |
/** maps String to Entry */ |
43 |
private Hashtable entries = new Hashtable (); |
44 |
|
45 |
/** finds entry for given name */ |
46 |
private Entry e (String n) { |
47 |
if (n.length() > 0 && n.charAt(0) == '/') n = n.substring (1); |
48 |
|
49 |
Entry x = (Entry)entries.get (n); |
50 |
if (x == null) { |
51 |
x = new Entry (); |
52 |
entries.put (n, x); |
53 |
} |
54 |
return x; |
55 |
} |
56 |
|
57 |
/** finds whether there already is this name */ |
58 |
private boolean is (String n) { |
59 |
if (n.length() > 0 && n.charAt(0) == '/') n = n.substring (1); |
60 |
Entry x = (Entry)entries.get (n); |
61 |
return x != null; |
62 |
} |
63 |
|
64 |
/** Creates new MemoryFS */ |
65 |
public MemoryFileSystem() { |
66 |
attr = this; |
67 |
list = this; |
68 |
change = this; |
69 |
info = this; |
70 |
|
71 |
try { |
72 |
setSystemName("MemoryFileSystem"); |
73 |
} catch (PropertyVetoException ex) { |
74 |
ex.printStackTrace(); |
75 |
} |
76 |
} |
77 |
|
78 |
/** Creates MemoryFS with data */ |
79 |
public MemoryFileSystem(String[] resources) { |
80 |
this (); |
81 |
|
82 |
StringBuffer sb = new StringBuffer (); |
83 |
|
84 |
for (int i = 0; i < resources.length; i++) { |
85 |
sb.append (resources[i]); |
86 |
|
87 |
if (resources[i].endsWith("/")) { |
88 |
// folder |
89 |
e (resources[i]).data = null; |
90 |
} |
91 |
else { |
92 |
e (resources[i]).data = new byte[0]; |
93 |
} |
94 |
} |
95 |
} |
96 |
|
97 |
|
98 |
|
99 |
public String getDisplayName() { |
100 |
return "MemoryFileSystem"; |
101 |
} |
102 |
|
103 |
public boolean isReadOnly() { |
104 |
return false; |
105 |
} |
106 |
|
107 |
public Enumeration attributes(String name) { |
108 |
return Collections.enumeration (e (name).attrs.keySet ()); |
109 |
} |
110 |
|
111 |
public String[] children(String f) { |
112 |
if (f.length () > 0 && f.charAt(0) == '/') f = f.substring (1); |
113 |
if (f.length () > 0 && !f.endsWith ("/")) f = f + "/"; |
114 |
|
115 |
HashSet l = new HashSet (); |
116 |
//System.out.println("Folder: " + f); |
117 |
|
118 |
Iterator it = entries.keySet().iterator (); |
119 |
while (it.hasNext ()) { |
120 |
String name = (String)it.next(); |
121 |
|
122 |
if (name.startsWith(f) || f.trim().length() == 0) { |
123 |
int i = name.indexOf ('/', f.length ()); |
124 |
String child = null; |
125 |
if (i > 0) { |
126 |
child = name.substring (f.length (), i); |
127 |
} else { |
128 |
child = name.substring (f.length ()); |
129 |
} |
130 |
|
131 |
if (child.trim().length() > 0) { |
132 |
l.add (child); |
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
return (String[])l.toArray (new String[0]); |
138 |
} |
139 |
|
140 |
public void createData(String name) throws IOException { |
141 |
if (is(name)) throw new IOException("File already exists"); |
142 |
e (name).data = new byte[0]; |
143 |
} |
144 |
|
145 |
public void createFolder(String name) throws java.io.IOException { |
146 |
if (is(name)) throw new IOException("File already exists"); |
147 |
e (name).data = null; |
148 |
} |
149 |
|
150 |
public void delete(String name) throws IOException { |
151 |
entries.remove (name); |
152 |
} |
153 |
|
154 |
public void deleteAttributes(String name) { |
155 |
} |
156 |
|
157 |
public boolean folder(String name) { |
158 |
return e (name).data == null; |
159 |
} |
160 |
|
161 |
public InputStream inputStream(String name) throws java.io.FileNotFoundException { |
162 |
byte[] arr = e (name).data; |
163 |
if (arr == null) { |
164 |
arr = new byte[0]; |
165 |
} |
166 |
return new ByteArrayInputStream (arr); |
167 |
} |
168 |
|
169 |
public java.util.Date lastModified(String name) { |
170 |
java.util.Date d = e (name).last; |
171 |
return d == null ? created : d; |
172 |
} |
173 |
|
174 |
public void lock(String name) throws IOException { |
175 |
} |
176 |
|
177 |
public void markUnimportant(String name) { |
178 |
} |
179 |
|
180 |
public String mimeType(String name) { |
181 |
return (String)e (name).attrs.get ("mimeType"); |
182 |
} |
183 |
|
184 |
public OutputStream outputStream(final String name) throws java.io.IOException { |
185 |
class Out extends ByteArrayOutputStream { |
186 |
public void close () throws IOException { |
187 |
super.close (); |
188 |
|
189 |
e (name).data = toByteArray (); |
190 |
e (name).last = new Date (); |
191 |
} |
192 |
} |
193 |
|
194 |
return new Out (); |
195 |
} |
196 |
|
197 |
public Object readAttribute(String name, String attrName) { |
198 |
return e (name).attrs.get (attrName); |
199 |
} |
200 |
|
201 |
public boolean readOnly(String name) { |
202 |
return false; |
203 |
} |
204 |
|
205 |
public void rename(String oldName, String newName) throws IOException { |
206 |
if (! is(oldName)) throw new IOException("The file to rename does not exist."); |
207 |
if (is(newName)) throw new IOException("Cannot rename to existing file"); |
208 |
if (newName.length () > 0 && newName.charAt(0) == '/') newName = newName.substring (1); |
209 |
Entry e = e (oldName); |
210 |
entries.remove(e); |
211 |
entries.put (newName, e); |
212 |
} |
213 |
|
214 |
public void renameAttributes(String oldName, String newName) { |
215 |
} |
216 |
|
217 |
public long size(String name) { |
218 |
byte[] d = e (name).data; |
219 |
|
220 |
return d == null ? 0 : d.length; |
221 |
} |
222 |
|
223 |
public void unlock(String name) { |
224 |
} |
225 |
|
226 |
public void writeAttribute(String name, String attrName, Object value) throws IOException { |
227 |
e (name).attrs.put (attrName, value); |
228 |
} |
229 |
} |