Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/inigoserrano/isdirvalidator/Directory.java


1   package com.inigoserrano.isdirvalidator;
2   
3   //the standard java imports
4   import java.util.Vector;
5   import java.util.Hashtable;
6   import java.util.Enumeration;
7   import java.io.File;
8   //import the regExp classes
9   import org.apache.regexp.RE;
10  import org.apache.regexp.RESyntaxException;
11  
12  /**
13   * This class check the structure of the disk. It check that the fisical disk
14   * hasnīt files that dosenīt fit any conditions (file and filePatterm
15   * conditions). It also check that the files that are required exisit in the
16   * ficial disk. And also check... that the directory exisits :-)
17   * If no file and filePatterm conditions are provided then it only check that
18   * the directory exisit.
19   * <br><br>
20   * <Spanish>
21   * Esta utilidad comprueba que en el disco no exista más ficheros que los que
22   * se declaran como parametros a la clase, pero puede que en los parámetros
23   * se indiquen más ficheros que los que existen en el disco y esto no se
24   * consiera un error. Se puede indicar que un determinado fichero es
25   * obligatorio y si ese fichero no existe se considerara como un error.
26   * </Spanish>
27   * <br>
28   * <i>This program is license under the terms of the GNU GPL v 2.0 License</i>
29   * @author Iņigo Serrano
30   * @version 2.0.1
31   */
32  public class Directory {
33      /**
34       * The directory to check
35       */
36      private String directory = null;
37  
38      /**
39       * Vector to store all the patterms that are permited the diferents files
40       */
41      private Vector pattermStore = null;
42  
43      /**
44       * Vector to store all the patterms that are permited the diferents 
45       * subDirectories
46       */
47      private Vector subDirectoryPattermStore = null;
48  
49      /**
50       * Hashtable to store all the names of file that are permited
51       */
52      private Hashtable filesStore = null; //To store the files that must exist
53  
54      /**
55       * The name of the file in the directory that dosenīt fit the conditions
56       */
57      private String invalidFile = null;
58  
59      /**
60       * The name of the subDirectory in the directory that dosnīt fit the 
61       * conditions
62       */
63      private String invalidSubDirectory = null;
64  
65      /**
66       * Boolean to indicate that the error is because in the fisical directory
67       * exist one subdirectory that dosnīt match any condition
68       */
69      private boolean extraSubDirectoryError = false;
70  
71      /**
72       * Boolean to indicate that the error is because in the fisical directory
73       * exist one file that dosenīt match any condition
74       */
75      private boolean extraFileError = false;
76  
77      /**
78       * Boolean to indicate that the error is because a file that is indicated to
79       * be required dosenīt exisit in the fisical directory
80       */
81      private boolean obligatoryFileError = false;
82  
83      /**
84       * Boolean to indicate that the error is because a directory
85       * dosnīt exist in the fisical directory
86       */
87      private boolean directoryError = false;
88  
89      /**
90       * Check if the directory is valid or not. The conditions to be valid is:
91       * <ul>
92       * <li>The directory must exist in the fisical disk</li>
93       * <li>Each file in the fisical disk must mach at least one condition
94       * indicated in the file or filePatterm</li>
95       * <li>The files indicated to be required must exist in the fisical 
96       * disk</li>
97       * <li>Each subDirectory in the fisical disk must mach at least 
98       * one condition indicated in the subDirectoryPatterm</li>
99       * @return true if valid or not conditions, else false
100      */
101     public boolean isValid() {
102         return (
103             this.existDirectory()
104                 && this.checkAllTheFileConditions()
105                 && this.checkAllTheSubDirectoriesConditions());
106     }
107 
108     /**
109      * To indicate that the error is because in the fisical directory
110      * exist one file that dosenīt match any condition
111      * @return true if that is the cause of the error, else false
112      */
113     public boolean isExtraFileError() {
114         return this.extraFileError;
115     }
116 
117     /**
118       * To indicate that the error is because in the fisical directory
119       * exist one subDirectory that dosnīt match any condition
120       * @return true if that is the cause of the error, else false
121       */
122     public boolean isExtraSubDirectoryError() {
123         return this.extraSubDirectoryError;
124     }
125 
126     /**
127      * To indicate that the error is because in the fisical directory
128      * dosnīt exist the directory
129      * @return true if that is the cause of the error, else false
130      */
131     public boolean isDirectoryError() {
132         return this.directoryError;
133     }
134 
135     /**
136      * To indicate that the error is because in the fisical directory
137      * dosnīt exist one file that is required
138      * @return true if that is the cause of the error, else false
139      */
140     public boolean isObligatoryFileError() {
141         return this.obligatoryFileError;
142     }
143 
144     /**
145      * To indicate what is the name of the file that dosenīt match any condition
146      * @return The name of the file that dosenīt fit any condition
147      */
148     public String getInvalidFile() {
149         return this.invalidFile;
150     }
151 
152     /**
153      * To indicate what is the name of the subDirectory that dosenīt match 
154      * any condition
155      * @return The name of the subDirectory that dosenīt fit any condition
156      */
157     public String getInvalidSubDirectory() {
158         return this.invalidSubDirectory;
159     }
160 
161     /**
162      * Check all the files in the directory. If no file conditions or 
163      * filePatterm
164      * conditions is provided then return true, because it asumes that the user
165      * only want to check that the directory exist.
166      * <br><br>
167      * <Spanish>
168      * Compruebo todos los ficheros del directorio, tanto que encajen en un
169      * fichero o que encajen en un patron.
170      * Si no se expecifica ni ficheros ni patrones se devuelve true, porque se
171      * considera que solo se quiere comprobar que exista el directorio.
172      * </Spanish>
173      * @return true if all the files match or no conditions, else false
174      */
175     private boolean checkAllTheFileConditions() {
176         File theDirectory = null;
177         File theFile = null;
178         String[] allTheFiles = null;
179         Enumeration iterator = null;
180         //code
181         //If no conditions then return true
182         if ((this.filesStore == null) && (this.pattermStore == null)) {
183             return true;
184         }
185         //Check all the files
186         theDirectory = new File(this.directory);
187         allTheFiles = theDirectory.list();
188         for (int counter = 0; counter < allTheFiles.length; counter++) {
189             theFile =
190                 new File(theDirectory + File.separator + allTheFiles[counter]);
191             if (theFile.isFile()) {
192                 this.invalidFile =
193                     theDirectory + File.separator + allTheFiles[counter];
194                 if (!this.checkOneFile(allTheFiles[counter])) {
195                     this.extraFileError = true;
196                     return false;
197                 }
198             }
199         }
200         //Check that all the required files exists
201         if (this.filesStore != null) {
202             iterator = this.filesStore.keys();
203             while (iterator.hasMoreElements()) {
204                 this.invalidFile = (String) iterator.nextElement();
205                 if (!((Boolean) this.filesStore.get(this.invalidFile))
206                     .booleanValue()) {
207                     this.invalidFile =
208                         theDirectory + File.separator + this.invalidFile;
209                     this.obligatoryFileError = true;
210                     return false;
211                 }
212             }
213         }
214         return true;
215     }
216 
217     /**
218      * Check all the subdirectories in the directory. If no subDirectoryPatterm
219      * conditions is provided then return true, because it asumes that the user
220      * dosnīt want to use this function.
221      * <br><br>
222      * <Spanish>
223      * Compruebo todos los subdirectorios del directorio, que encajen en un 
224      * patron.
225      * Si no se expecifican patrones se devuelve true, porque se
226      * considera que no se quiere utilizar esta caracteristica.
227      * </Spanish>
228      * @return true if all the files match or no conditions, else false
229      */
230     private boolean checkAllTheSubDirectoriesConditions() {
231         File theDirectory = null;
232         File theFile = null;
233         String[] allTheSubDirectories = null;
234         //code
235         //If no conditions the return true
236         if (this.subDirectoryPattermStore == null) {
237             return true;
238         }
239         //Check all the subdirectories
240         theDirectory = new File(this.directory);
241         allTheSubDirectories = theDirectory.list();
242         for (int counter = 0;
243             counter < allTheSubDirectories.length;
244             counter++) {
245             theFile =
246                 new File(
247                     theDirectory
248                         + File.separator
249                         + allTheSubDirectories[counter]);
250             if (theFile.isDirectory()) {
251                 this.invalidSubDirectory =
252                     theDirectory
253                         + File.separator
254                         + allTheSubDirectories[counter];
255                 if (!this
256                     .checkOneSubDirectory(allTheSubDirectories[counter])) {
257                     this.extraSubDirectoryError = true;
258                     return false;
259                 }
260             }
261         }
262         return true;
263     }
264 
265     /**
266      * Check that the file passed as argument exist in the fisical disk and
267      * match at least one condition.
268      * <br><br>
269      * <Spanish>
270      * Compruebo un fichero del directorio. Si ese fichero no está en la lista
271      * de ficheros a comprobar ni encaja con ningun patron entonces es un error
272      * </Spanish>
273      * @return true if the file exisit and match at least one condition, else
274      * return false
275      * @param newFile The name of the file to check, without the path
276      */
277     private boolean checkOneFile(String newFile) {
278         if (this.filesStore != null) {
279             if (this.existFile(newFile)) {
280                 return true;
281             }
282         }
283         //The file dosenīt fit the file conditions, or not file conditions
284         //so check the patterms conditions
285         if (this.pattermStore != null) {
286             return this.checkOneFileByPatterm(newFile);
287         } else {
288             //no patterm and invalid file
289             return false;
290         }
291     }
292 
293     /**
294      * It check that the file match at least one file condition (addFile())
295      * @param oneFile the name of the file in the fisical disk, without the path
296      * @return true if match some conditions, else false
297      */
298     private boolean existFile(String oneFile) {
299         if (this.filesStore.containsKey(oneFile)) {
300             this.filesStore.remove(oneFile);
301             this.filesStore.put(oneFile, new Boolean(true));
302             return true;
303         } else {
304             return false;
305         }
306     }
307 
308     /**
309      * Check one file with all the patterm conditions. If the file match at 
310      * least one then return true, else return false
311      * <br><br>
312      * <Spanish>
313      * Compruebo un fichero con todos los patrones, si no encaja en ninguno
314      * entonces es un fallo y devuelvo false
315      * </Spanish>
316      * @param oneFile the name of the file to check
317      * @return True if the file match, else false
318      */
319     private boolean checkOneFileByPatterm(String oneFile) {
320         Enumeration iterator = pattermStore.elements();
321         while (iterator.hasMoreElements()) {
322             if (((RE) iterator.nextElement()).match(oneFile)) {
323                 return true;
324             }
325         }
326         return false;
327     }
328 
329     /**
330      * Check one subDirectory with all the patterm conditions. If the 
331      * subDirectory match at least one then return true, else return false
332      * <br><br>
333      * <Spanish>
334      * Compruebo un subDirectorio con todos los patrones, si no encaja en 
335      * ninguno entonces es un fallo y devuelvo false
336      * </Spanish>
337      * @param oneSubDirectory the name of the subDirectory to check
338      * @return True if the file match, else false
339      */
340     private boolean checkOneSubDirectory(String oneSubDirectory) {
341         Enumeration iterator = subDirectoryPattermStore.elements();
342         while (iterator.hasMoreElements()) {
343             if (((RE) iterator.nextElement()).match(oneSubDirectory)) {
344                 return true;
345             }
346         }
347         return false;
348     }
349 
350     /**
351      * Check that the directory exist in the fisical disk (and is a directory).
352      * <br><br>
353      * <Spanish>
354      * Comprueba si existe el directorio (existe y es directorio)
355      * </Spanish>
356      * @return True if the directory exisit in the fisical disk
357      */
358     private boolean existDirectory() {
359         File theDirectory = new File(this.directory);
360         if (theDirectory.exists() && theDirectory.isDirectory()) {
361             return true;
362         } else {
363             this.directoryError = true;
364             return false;
365         }
366     }
367 
368     /**
369      * Add a new file name in the conditions
370      * @param newFile the name of the file, without the path
371      * @param mustExist true if this file is required, false if no
372      */
373     public void addFile(String newFile, boolean mustExist) {
374         if (this.filesStore == null) {
375             this.filesStore = new Hashtable();
376         }
377         this.filesStore.put(newFile, new Boolean(!mustExist));
378     }
379 
380     /**
381      * Add a new file name in the conditions
382      * <br><br>
383      * <Spanish>
384      * Aņade un nuevo nombre de fichero al almacen
385      * </Spanish>
386      * @param  newFile the name of the file, without the path
387      */
388     public void addFile(String newFile) {
389         this.addFile(newFile, false);
390     }
391 
392     /**
393      * Add a new file patterm in the conditions
394      * <br><br>
395      * <Spanish>
396      * Aqui le voy aņadiendo los patrones a cumplir
397      * </Spanish>
398      * @param newPatterm the patterm
399      * @throws RESyntaxException if the patterm isnīt valid
400      */
401     public void addFilePatterm(String newPatterm) throws RESyntaxException {
402         if (this.pattermStore == null) {
403             this.pattermStore = new Vector();
404         }
405         this.pattermStore.addElement(new RE(newPatterm));
406     }
407 
408     /**
409      * Add a new subDirectory patterm in the conditions
410      * <br><br>
411      * <Spanish>
412      * Aqui le voy aņadiendo los patrones a cumplir
413      * </Spanish>
414      * @param newPatterm the patterm
415      * @throws RESyntaxException if the patterm isnīt valid
416      */
417     public void addSubDirectoryPatterm(String newPatterm)
418         throws RESyntaxException {
419         if (this.subDirectoryPattermStore == null) {
420             this.subDirectoryPattermStore = new Vector();
421         }
422         this.subDirectoryPattermStore.addElement(new RE(newPatterm));
423     }
424 
425     /**
426      * Gets the fisical directory, with its path
427      * @return the directory
428      */
429     public String getDirectory() {
430         return this.directory;
431     }
432 
433     /**
434      * Set the directory , with its path
435      * @param newDirectory the directory
436      */
437     public void setDirectory(String newDirectory) {
438         this.directory = newDirectory;
439     }
440 }