View Javadoc

1   package org.molwind.chemical.io;
2   
3   /*
4    * This file is part of Molwind.
5    *
6    * Molwind is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Molwind is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Molwind. If not, see <http://www.gnu.org/licenses/>.
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   * SDFileLocator implements a locator to find worlds based on SDF chemical file
38   * format.
39   *
40   * @author <a href="mailto:oliver.karch@molwind.org">Oliver Karch</a>
41   * @version 1.0
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       * The relativ adress for looking up the worlds
52       *
53       */
54      public static String pathNames;
55  
56      /**
57       * The default file extensions recognized by this locator.
58       */
59      public static final String[] DEFAULT_SDF_SUFFIX = {
60          ".sdf", ".SDF", ".mol", ".MOL"
61      };
62  
63      /**
64       * The default name of the attribute denoting the parent.
65       */
66      public static final String DEFAULT_PARENT_ATTRIBUTE = "parent";
67  
68      /**
69       * The default name of the attribute denoting the id.
70       */
71      public static final String DEFAULT_ID_ATTRIBUTE = "compoundid";
72      /**
73       *
74       * The default value for seperating a path
75       */
76      public static final String  DEFAULT_PATH_SEPARATOR = ":"; 
77  
78      /**
79       * Creates an SDFilelocator instance.
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       * Adds a path to the base paths 
90       * @param path
91       *        the path to be add
92       *
93       */
94      public void addBasePath(final String path){
95  	basePaths.add(path);
96  
97      }
98      /**
99       * Removes the given path from the base paths
100      *
101      * @param path
102      *       the path to be removed
103      */
104     public void removeBasePath(final String path){
105 
106 	basePaths.remove(path);
107     }
108 
109       /**
110      * Locates the world by the given name by iteraring all world names
111      *
112      * @param 
113      *      the filename
114      * @return 
115      *      a molwind world descriptor
116      * @throws java.io.IOException
117      *      is thrown upon i/o error
118      */
119     public MolwindWorld locateByFileName(String fileName)
120     throws IOException{
121 	//File file
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      * Locates a world with the given name.
160      *
161      * @param worldName
162      *      the name of the world
163      * @return
164      *      a molwind world descriptor
165      * @throws java.io.IOException
166      *      is thrown upon i/o error
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      * private String[] getWorldPaths( String[] suffixes(relative namen) ) {
246      *ArrayList<String> wPaths = new ArrayList<String>() 
247      *    for(String s:suffixes) {
248      *	if( File(s).isAbsolute() )
249      *	    wPaths.add( s );
250      *	else {
251      *	    
252      *
253      *    }
254      * hPaths = new String[ wPaths.size() ];
255      * return (String[])wPaths.toArray( hPaths );
256     */
257 
258     /**
259      *
260      *@return 
261      *    returns an array of available worlds
262      *
263      */
264 
265     public String[] getWorldNames(){
266 	//	String[] paths = getWorldPaths(pathNames.split(DEFAULT_PATH_SEPARATOR));
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      * Get the <code>parentAttributeName</code> value.
359      *
360      * @return
361      *      the <code>parentAttributeName</code> value
362      */
363     public String getParentAttributeName() {
364         return parentAttributeName;
365     }
366 
367     /**
368      * Set the <code>ParentAttributeName</code> value.
369      *
370      * @param newParentAttributeName
371      *      the new <code>ParentAttributeName</code> value
372      */
373     public void setParentAttributeName(final String newParentAttributeName) {
374         this.parentAttributeName = newParentAttributeName;
375     }
376 
377     /**
378      * Get the <code>idAttributeName</code> value.
379      *
380      * @return
381      *      the <code>idAttributeName</code> value
382      */
383     public String getIdAttributeName() {
384         return idAttributeName;
385     }
386 
387     /**
388      * Set the <code>idAttributeName</code> value.
389      *
390      * @param newIdAttributeName
391      *      the new <code>idAttributeName</code> value
392      */
393     public void setIdAttributeName(final String newIdAttributeName) {
394         this.idAttributeName = newIdAttributeName;
395     }
396     /**
397      * Set the <code>pathNames</code> 
398      * @param paths
399      *        the new <code>pathNames</code> value
400      */
401 
402     public void setPathNames(final String paths) {
403         this.pathNames = paths;
404     }
405      /**
406      * Get the worldpaths
407      *
408      *@return 
409      *      worldpaths
410      *
411      */
412     public String getPathNames(){
413 	return this.pathNames;
414 
415     }
416 
417 
418     
419 
420 }
421