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

Quick Search    Search Deep

Source code: mrsoft/util/DirectoryLoader.java


1   package mrsoft.util;
2   
3   import mrsoft.util.*;
4   import java.io.*;
5   import java.util.Vector;
6   
7   
8   public class DirectoryLoader
9   {
10      Vector files; //holds the paths as strings
11      int sumlength, countfiles; //aggregates
12      boolean force_stop = false; //signal to stop loading
13  
14      String extension = "all"; //include all files
15      boolean branch=true; //include subincl_dir
16      boolean verbose = false; //print to console
17      boolean incl_dir=false; //include incl_dir if true
18      boolean incl_files=true; //include regular files or not
19  
20    Vector listeners = new Vector();
21      
22      public void setExtension(String value) { extension = value; }
23      public void setVerbose(boolean state) { verbose = state; }
24      public void setBranch(boolean state) { branch = state; }
25      public void includeDirectories(boolean state) { incl_dir = state; }
26      public void includeFiles(boolean state) { incl_files = state; }
27  
28    /** Returns the number of files currently found */    
29      public int getCount() { return countfiles; }
30  
31    /** Returns the sum of the lengths of the files currently found. */
32      public int getLength() { return sumlength; }
33  
34    /** Returns the vector with files that was loaded */
35      public Vector getFiles() { return files; }
36  
37    /** Forces the search for files to stop */
38      public void forceStop() { force_stop=true; }
39          
40    /** Adds a listener that gets notified of progress on traversing the directory and file structure */
41    public void addDirectoryLoaderListener(DirectoryLoaderListener l) { listeners.add(l); }
42    
43    /** Removes the listener */
44    public void removeDirectoryLoaderListener(DirectoryLoaderListener l) { listeners.remove(l); }
45  
46    /** Starts the procedure. start_dir is the directory where the search starts.<BR>
47    Returns the vector with files that belongs to this object, and might contain results from previous searches.
48    */
49      public Vector load(String start_dir)
50      {
51          force_stop=false;
52          
53          String names[]; //contains all entries in a directory
54          String temp = "";
55          String filename; 
56          
57          if(files==null) { 
58              clean();
59          }
60          
61          File path, fpath;
62  
63          path = new File(start_dir);
64          names = path.list();
65          for(int i=0;i<names.length;i++) {
66              if(force_stop) break;
67              filename = start_dir + File.separator + names[i]; //get full path
68              fpath = new File(filename);
69              if(fpath.isDirectory() & branch) { // is a directory
70          newDirectory(fpath);
71                  temp = fpath.getPath() + File.separator;
72                  if(verbose) System.out.println("directory " + temp);
73                // recursively call upon itself to get subdirectory
74                  load(temp);
75              }
76              else if(test(filename)) { // type of file is requested
77                  newFile(fpath);
78          files.addElement(fpath);
79                  sumlength += fpath.length();
80                  countfiles++;
81              }
82          }
83          return files;
84      }
85      
86    /** Cleans up this object. */
87      public void clean()
88      {
89          sumlength=0; countfiles=0;
90          files = new Vector();
91      }
92  
93    protected void newDirectory(File file)
94    {
95      for(int i=0; i<listeners.size(); i++) {
96        DirectoryLoaderListener l = (DirectoryLoaderListener) listeners.get(i);
97        l.directoryEncountered(file);
98      }
99    }
100   
101   protected void newFile(File file)
102   {
103     for(int i=0; i<listeners.size(); i++) {
104       DirectoryLoaderListener l = (DirectoryLoaderListener) listeners.get(i);
105       l.fileEncountered(file);
106     }
107   }
108   
109   
110   /** Tests if the file should be included in the list. */
111     public boolean test(String file)
112     {
113         
114         if(extension.equals("all"))
115             return true;
116         
117         String ext;
118         int length = file.length();
119 
120 //search for extension name
121         for(int i=length-1;i>=0;i--) {
122 //Test if filename is a direcory (with no name), in which case the last character is a separator.            
123             if(file.charAt(i) == System.getProperty("path.separator").charAt(0)) {
124 //Include if direcories are requested
125                 if(incl_dir==true) return true;
126             }        
127             
128             if(file.charAt(i) == '.') { //remainder is extension
129                 ext = file.substring(i+1,length);
130                 return ext.equalsIgnoreCase(extension);
131             }
132         }
133         return false;
134     }//EM
135 
136     
137 
138 }//EC
139 
140 
141 
142         
143         
144         
145         
146             
147