1 package org.molwind.chemical.io;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.File;
21 import java.io.FilenameFilter;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Iterator;
26
27 import org.apache.commons.io.filefilter.SuffixFileFilter;
28
29 import org.molwind.chemical.model.ChemicalWorld;
30 import org.molwind.chemical.model.SubstructureResolver;
31 import org.molwind.io.AbstractWorldLocator;
32 import org.molwind.io.BasePathSupport;
33 import org.molwind.model.MolwindWorld;
34 import org.molwind.util.Stringx;
35
36
37
38
39
40
41
42
43 public class SDFileLocator extends AbstractWorldLocator implements BasePathSupport {
44
45 private ArrayList suffixes;
46 private String parentAttributeName;
47 private String idAttributeName;
48 private ArrayList<String> basePaths;
49
50
51
52
53
54 public static String pathNames;
55
56
57
58
59 public static final String[] DEFAULT_SDF_SUFFIX = {
60 ".sdf", ".SDF", ".mol", ".MOL"
61 };
62
63
64
65
66 public static final String DEFAULT_PARENT_ATTRIBUTE = "parent";
67
68
69
70
71 public static final String DEFAULT_ID_ATTRIBUTE = "compoundid";
72
73
74
75
76 public static final String DEFAULT_PATH_SEPARATOR = ":";
77
78
79
80
81 public SDFileLocator() {
82 suffixes = new ArrayList( Arrays.asList(DEFAULT_SDF_SUFFIX));
83 setParentAttributeName(DEFAULT_PARENT_ATTRIBUTE);
84 setIdAttributeName(DEFAULT_ID_ATTRIBUTE);
85 basePaths = new ArrayList<String>();
86 }
87
88
89
90
91
92
93
94 public void addBasePath(final String path){
95 basePaths.add(path);
96
97 }
98
99
100
101
102
103
104 public void removeBasePath(final String path){
105
106 basePaths.remove(path);
107 }
108
109
110
111
112
113
114
115
116
117
118
119 public MolwindWorld locateByFileName(String fileName)
120 throws IOException{
121
122 String[] worldNames = getWorldNames();
123 for(String s:worldNames){
124 if(s.contains(fileName) && isChemicalFile(s)){
125 ChemicalWorld chemicalWorld = new ChemicalWorld(fileName,new File(s));
126 chemicalWorld.setRelationshipResolver(new SubstructureResolver(getParentAttributeName(),getIdAttributeName()));
127 return (MolwindWorld)chemicalWorld;
128
129 }
130
131 }
132
133 return null;
134
135 }
136
137
138 private boolean isChemicalFile(String path){
139 File file = new File(path);
140 if(!file.exists())
141 return false;
142 Iterator it = suffixes.iterator();
143 while(it.hasNext()){
144 String suffix = (String)it.next();
145
146 String[] suffixArray = new String[suffixes.size()];
147 SuffixFileFilter suffixFileFilter = new SuffixFileFilter((String[])suffixes.toArray(suffixArray));
148
149 if(suffixFileFilter.accept(file))
150 return true;
151
152 }
153
154 return false;
155
156 }
157
158
159
160
161
162
163
164
165
166
167
168 public MolwindWorld locateWorld(final String worldName)
169 throws IOException {
170 File file = new File(worldName);
171 if (file.exists() && file.isFile()) {
172
173 ChemicalWorld chemicalWorld = new ChemicalWorld(
174 toWorldName(file), file);
175 chemicalWorld.setRelationshipResolver(
176 new SubstructureResolver(
177 getParentAttributeName(), getIdAttributeName()));
178 return (MolwindWorld) chemicalWorld;
179 }
180
181 if (file.exists() && file.isDirectory()) {
182 ChemicalWorld chemicalWorld = new ChemicalWorld(file.getName());
183 chemicalWorld.setRelationshipResolver(
184 new SubstructureResolver(
185 getParentAttributeName(), getIdAttributeName()));
186
187 Iterator it = suffixes.iterator();
188 while (it.hasNext()) {
189 String suffix = (String) it.next();
190 File[] files = file.listFiles(
191 (FilenameFilter) new SuffixFileFilter(suffix));
192
193 for (int i = 0; i < files.length; i++) {
194 chemicalWorld.addFile(files[i]);
195 }
196 }
197 return (MolwindWorld) chemicalWorld;
198 }
199
200 Iterator it = suffixes.iterator();
201 while (it.hasNext()) {
202 String suffix = (String) it.next();
203 file = new File(Stringx.strapp(worldName, suffix));
204 if (file.exists()) {
205 ChemicalWorld chemicalWorld = new ChemicalWorld(
206 toWorldName(file), file);
207 chemicalWorld.setRelationshipResolver(
208 new SubstructureResolver(getParentAttributeName(),
209 getIdAttributeName()));
210 return (MolwindWorld) chemicalWorld;
211 }
212 }
213
214 return null;
215 }
216
217
218 private String[] getWorldPath(String[] suffixes){
219 ArrayList<String> wPaths = new ArrayList<String>();
220 for(String s:suffixes){
221 File file = new File(s);
222 if(file.isAbsolute()){
223 wPaths.add(s);
224
225 }else{
226 for(String bp:basePaths){
227
228 String pathName=bp+s;
229 wPaths.add(bp);
230 }
231
232 }
233
234 }
235
236
237
238 String[] hPaths = new String[ wPaths.size() ];
239 return (String[])wPaths.toArray( hPaths );
240
241 }
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public String[] getWorldNames(){
266
267 String[] paths = pathNames.split(DEFAULT_PATH_SEPARATOR);
268
269 ArrayList<String> result = new ArrayList<String>();
270
271 for(String s:paths){
272 File file = new File(s);
273 if(file.exists() && file.isFile()){
274
275 result.add(s);
276
277 }else if(file.exists() && file.isDirectory()){
278 try{
279 result.add(s);
280
281 iterateDirectory(s,result);
282 }catch(Exception e){
283 e.printStackTrace();
284 }
285
286 }
287 Iterator it = suffixes.iterator();
288 while (it.hasNext()) {
289 String suffix = (String) it.next();
290 file = new File(Stringx.strapp(s, suffix));
291 if (file.exists()) {
292
293 result.add(toWorldName(file));
294 }
295 }
296
297
298
299 }
300
301
302 String[] resultArray = new String[result.size()];
303
304
305 return (String[])result.toArray(resultArray) ;
306 }
307
308
309
310
311
312
313
314 private void iterateDirectory(String dPath,ArrayList<String> result)throws Exception
315 {
316
317 File file = new File(dPath);
318
319
320
321 File[] files = file.listFiles();
322
323 for(File f:files){
324 if(f.exists() && f.isFile()){
325
326
327
328
329 result.add(dPath+"/"+f.getName());
330
331 }else if(f.exists() && f.isDirectory()){
332
333 result.add(dPath+"/"+f.getName());
334 iterateDirectory(dPath+"/"+f.getName(),result);
335
336 }
337
338
339 }
340
341
342
343 }
344
345
346 private String toWorldName(final File file) {
347
348 return file.getName();
349 }
350
351
352
353
354
355
356
357
358
359
360
361
362
363 public String getParentAttributeName() {
364 return parentAttributeName;
365 }
366
367
368
369
370
371
372
373 public void setParentAttributeName(final String newParentAttributeName) {
374 this.parentAttributeName = newParentAttributeName;
375 }
376
377
378
379
380
381
382
383 public String getIdAttributeName() {
384 return idAttributeName;
385 }
386
387
388
389
390
391
392
393 public void setIdAttributeName(final String newIdAttributeName) {
394 this.idAttributeName = newIdAttributeName;
395 }
396
397
398
399
400
401
402 public void setPathNames(final String paths) {
403 this.pathNames = paths;
404 }
405
406
407
408
409
410
411
412 public String getPathNames(){
413 return this.pathNames;
414
415 }
416
417
418
419
420 }
421