svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / explorer / filesystem / FilesystemExplorer.java @ 24571
History | View | Annotate | Download (6.98 KB)
1 |
package org.gvsig.fmap.dal.explorer.filesystem; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileFilter; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
import java.util.Map; |
9 |
|
10 |
import org.gvsig.fmap.dal.DALLocator; |
11 |
import org.gvsig.fmap.dal.DataExplorerParameters; |
12 |
import org.gvsig.fmap.dal.DataStoreParameters; |
13 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
14 |
import org.gvsig.fmap.dal.exception.DataException; |
15 |
import org.gvsig.fmap.dal.exception.InitializeException; |
16 |
import org.gvsig.fmap.dal.exception.RemoveException; |
17 |
import org.gvsig.fmap.dal.spi.DataExplorerProvider; |
18 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
19 |
import org.gvsig.fmap.dal.spi.DataStoreProvider; |
20 |
import org.gvsig.tools.ToolsLocator; |
21 |
import org.gvsig.tools.extensionpoint.ExtensionBuilder; |
22 |
|
23 |
|
24 |
public class FilesystemExplorer implements DataExplorerProvider { |
25 |
// FIXME: , IPersistence{
|
26 |
|
27 |
public static final String NAME = "FilesystemExplorer"; |
28 |
|
29 |
final private static String FILE_FILTER_EPSNAME = "FilesystemExplorerFilters"; |
30 |
final private static String FILE_FILTER_EPSDESCRIPTION = "Registro de filtros asociados al explorador del sistema de ficheros."; |
31 |
|
32 |
FilesystemExplorerParameters parameters; |
33 |
private File root; |
34 |
|
35 |
private File current; |
36 |
|
37 |
|
38 |
public static class FilterWraper implements ExtensionBuilder { |
39 |
|
40 |
private FileFilter filter; |
41 |
|
42 |
public FilterWraper(FileFilter filter) { |
43 |
this.filter = filter;
|
44 |
} |
45 |
|
46 |
public Object create() { |
47 |
return filter;
|
48 |
} |
49 |
|
50 |
public Object create(Object[] args) { |
51 |
return filter;
|
52 |
} |
53 |
|
54 |
public Object create(Map args) { |
55 |
return filter;
|
56 |
} |
57 |
|
58 |
} |
59 |
|
60 |
public static class FilterWraperIterator implements Iterator { |
61 |
private Iterator it; |
62 |
|
63 |
public FilterWraperIterator(Iterator it) { |
64 |
this.it = it;
|
65 |
} |
66 |
|
67 |
public boolean hasNext() { |
68 |
return this.it.hasNext(); |
69 |
} |
70 |
|
71 |
public Object next() { |
72 |
return ((FilterWraper) this.it.next()).create(); |
73 |
} |
74 |
|
75 |
public void remove() { |
76 |
throw new UnsupportedOperationException(); |
77 |
} |
78 |
|
79 |
} |
80 |
|
81 |
public static void registerFilter(FilesystemFileFilter filter) { |
82 |
FilterWraper filterWraper = new FilterWraper(filter);
|
83 |
|
84 |
ToolsLocator.getExtensionPointManager().add(FILE_FILTER_EPSNAME, |
85 |
FILE_FILTER_EPSDESCRIPTION).append( |
86 |
filter.getDataStoreProviderName(), filter.getDescription(), |
87 |
filterWraper); |
88 |
} |
89 |
|
90 |
public static Iterator getFilters() { |
91 |
Iterator it = new FilterWraperIterator(ToolsLocator |
92 |
.getExtensionPointManager().get(FILE_FILTER_EPSNAME).iterator()); |
93 |
return it;
|
94 |
} |
95 |
|
96 |
public void init(DataExplorerParameters parameters) |
97 |
throws InitializeException {
|
98 |
this.parameters = (FilesystemExplorerParameters) parameters;
|
99 |
this.root = new File(this.parameters.getRoot()); |
100 |
this.current = new File(this.parameters.getInitialpath()); |
101 |
} |
102 |
|
103 |
public String getType() { |
104 |
return NAME;
|
105 |
} |
106 |
|
107 |
public DataExplorerParameters getParameters() {
|
108 |
return parameters;
|
109 |
} |
110 |
|
111 |
public void dispose() throws DataException { |
112 |
this.parameters = null; |
113 |
this.root = null; |
114 |
} |
115 |
|
116 |
public List list() throws DataException { |
117 |
if (!this.current.exists()) { |
118 |
//TODO crear excepcion de Data??
|
119 |
new org.gvsig.fmap.dal.exception.FileNotFoundException(this.current); |
120 |
} |
121 |
|
122 |
if (!this.current.isDirectory()) { |
123 |
new IllegalArgumentException(this.getName() |
124 |
+ ": Path not a directory '"
|
125 |
+ this.current + "'"); |
126 |
} |
127 |
// DataManager dsm=DataManager.getManager();
|
128 |
|
129 |
String files[] = this.current.list(); |
130 |
int i;
|
131 |
File theFile;
|
132 |
ArrayList list = new ArrayList(); |
133 |
DataStoreParameters dsp = null;
|
134 |
|
135 |
for (i = 0; i < files.length; i++) { |
136 |
theFile = new File(this.root, files[i]); |
137 |
dsp = this.getParametersFor(theFile);
|
138 |
if (dsp != null) { |
139 |
list.add(dsp); |
140 |
} |
141 |
} |
142 |
return list;
|
143 |
} |
144 |
|
145 |
public void setCurrentPath(File path) { |
146 |
// FIXME Comprobar si es un directorio existente
|
147 |
this.current = path;
|
148 |
} |
149 |
|
150 |
public File getCurrentPath() { |
151 |
return this.current; |
152 |
} |
153 |
|
154 |
public File getRoot() { |
155 |
return this.root; |
156 |
} |
157 |
|
158 |
public void remove(DataStoreParameters dsp) throws RemoveException { |
159 |
String providerName = dsp.getDataStoreName();
|
160 |
try {
|
161 |
this.checkIsMine(dsp);
|
162 |
DataStoreProvider provider = this.getManager().createStoreProvider(
|
163 |
providerName); |
164 |
|
165 |
provider.remove(dsp); |
166 |
} catch (DataException e) {
|
167 |
throw new RemoveException(this.getName(), e); |
168 |
} |
169 |
} |
170 |
|
171 |
public boolean add(NewDataStoreParameters ndsp, boolean overwrite) |
172 |
throws DataException {
|
173 |
String providerName = ndsp.getDataStoreName();
|
174 |
this.checkIsMine(ndsp);
|
175 |
try {
|
176 |
DataStoreProvider provider = this.getManager().createStoreProvider(
|
177 |
providerName); |
178 |
|
179 |
provider.create(ndsp, overwrite); |
180 |
return true; // TODO deber?a devolver un booleano el provider o esto ser un void |
181 |
} catch (DataException e) {
|
182 |
throw new RemoveException(this.getName(), e); |
183 |
} |
184 |
} |
185 |
|
186 |
public boolean canAdd() { |
187 |
return this.root.canWrite(); |
188 |
} |
189 |
|
190 |
public String getName() { |
191 |
return this.FILE_FILTER_EPSNAME; |
192 |
} |
193 |
|
194 |
public NewDataStoreParameters getAddParameters(String storeName) { |
195 |
// TODO Auto-generated method stub
|
196 |
return null; |
197 |
} |
198 |
|
199 |
public boolean canAdd(String storeName) |
200 |
throws DataException {
|
201 |
// String providerName = parameters.getDataStoreName();
|
202 |
// this.checkIsMine(parameters);
|
203 |
//
|
204 |
// DataStoreProvider provider = this.getManager().createStoreProvider(
|
205 |
// providerName);
|
206 |
//
|
207 |
// return provider.canCreate(parameters);
|
208 |
return false; |
209 |
|
210 |
} |
211 |
|
212 |
// ==========================================
|
213 |
|
214 |
private DataManagerProviderServices getManager() {
|
215 |
return (DataManagerProviderServices) DALLocator.getDataManager();
|
216 |
} |
217 |
|
218 |
private DataStoreParameters getParametersFor(File file) |
219 |
throws DataException {
|
220 |
if (!file.exists()) {
|
221 |
return null; |
222 |
} |
223 |
if (!file.isFile()) {
|
224 |
return null; |
225 |
} |
226 |
if (!file.canRead()) {
|
227 |
return null; |
228 |
} |
229 |
if (file.isHidden()) { // XXX ??? |
230 |
return null; |
231 |
} |
232 |
Iterator filters = getFilters();
|
233 |
while (filters.hasNext()) {
|
234 |
FilesystemFileFilter filter = (FilesystemFileFilter) filters.next(); |
235 |
if (filter.accept(file)) {
|
236 |
DataStoreParameters params = DALLocator.getDataManager() |
237 |
.createStoreParameters( |
238 |
filter.getDataStoreProviderName()); |
239 |
((FilesystemStoreParameters) params).setFile(file); |
240 |
return params;
|
241 |
} |
242 |
} |
243 |
return null; |
244 |
} |
245 |
|
246 |
private void checkIsMine(DataStoreParameters dsp) |
247 |
throws IllegalArgumentException, DataException { |
248 |
if (!(dsp instanceof FilesystemExplorer)) { |
249 |
new IllegalArgumentException("not instance of FilesystemExplorer"); |
250 |
} |
251 |
Iterator filters = getFilters();
|
252 |
File file = ((FilesystemStoreParameters) dsp).getFile();
|
253 |
if (!file.getAbsolutePath().startsWith(this.root.getAbsolutePath())) { |
254 |
throw new IllegalArgumentException("worng explorer"); |
255 |
} |
256 |
FilesystemFileFilter filter; |
257 |
while (filters.hasNext()) {
|
258 |
filter = (FilesystemFileFilter) filters.next(); |
259 |
if (dsp.getDataStoreName().equals(filter.getDataStoreProviderName())) {
|
260 |
return;
|
261 |
} |
262 |
} |
263 |
throw new IllegalArgumentException("worng explorer"); |
264 |
} |
265 |
|
266 |
|
267 |
} |