KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > page > FileFinder


1 /*
2  * Created on Jun 3, 2006
3  */

4 package com.openedit.page;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.page.manage.PageManager;
15 import com.openedit.util.PathUtilities;
16
17 public class FileFinder
18 {
19     protected File JavaDoc fieldRoot;
20     protected PageManager fieldPageManager;
21     protected boolean fieldUseMatches;
22     protected boolean fieldRecursive = true;
23     protected Set JavaDoc fieldSkipFiles;
24     
25     protected boolean skip(String JavaDoc inFileName)
26     {
27         return getSkipFiles().contains(inFileName);
28     }
29     
30     protected Set JavaDoc getSkipFiles()
31     {
32         if (fieldSkipFiles == null)
33         {
34             fieldSkipFiles = new HashSet JavaDoc();
35             fieldSkipFiles.add("base");
36             fieldSkipFiles.add(".versions");
37             fieldSkipFiles.add("WEB-INF");
38         }
39         return fieldSkipFiles;
40     }
41     public void addSkipFileName(String JavaDoc inName)
42     {
43         getSkipFiles().add(inName);
44     }
45     
46     public List JavaDoc findPages(String JavaDoc inContains) throws OpenEditException
47     {
48         List JavaDoc paths = new ArrayList JavaDoc();
49         findMatches(getRoot(), getRoot().getAbsolutePath().length(), inContains, paths);
50         
51         List JavaDoc pages = new ArrayList JavaDoc(paths.size());
52         
53         for (Iterator JavaDoc iter = paths.iterator(); iter.hasNext();)
54         {
55             String JavaDoc path = (String JavaDoc) iter.next();
56             pages.add(getPageManager().getPage(path));
57         }
58         return pages;
59     }
60     protected void findMatches(File JavaDoc inRoot, int from, String JavaDoc contains, List JavaDoc found)
61     {
62         if( inRoot.isDirectory())
63         {
64             File JavaDoc[] children = inRoot.listFiles();
65             if( children != null)
66             {
67                 for (int i = 0; i < children.length; i++)
68                 {
69                     File JavaDoc child = children[i];
70                     if ( child.isDirectory())
71                     {
72                         if( isUseMatches() )
73                         {
74                             if( skipMatches( child.getName()) )
75                             {
76                                 continue;
77                             }
78                         }
79                         else if ( skip(child.getName()))
80                         {
81                             continue;
82                         }
83                         if( !isRecursive() )
84                         {
85                             continue;
86                         }
87                     }
88                     findMatches(children[i], from, contains, found);
89                 }
90             }
91         }
92         else
93         {
94             String JavaDoc path = inRoot.getAbsolutePath().substring(from);
95             if( isUseMatches() )
96             {
97                 if( PathUtilities.match(path, contains))
98                 {
99                     if( !skipMatches( path ))
100                     {
101                         found.add(path);
102                     }
103                 }
104             }
105             else
106             {
107                 if( path.indexOf(contains) > -1 )
108                 {
109                     if( !skip(path))
110                     {
111                         found.add(path);
112                     }
113                 }
114             }
115         }
116     }
117     protected boolean skipMatches(String JavaDoc inPath)
118     {
119         for (Iterator JavaDoc iterator = getSkipFiles().iterator(); iterator.hasNext();)
120         {
121             String JavaDoc file = (String JavaDoc) iterator.next();
122             if( PathUtilities.match(inPath, file))
123             {
124                 return true;
125             }
126         }
127
128         return false;
129     }
130
131     public PageManager getPageManager()
132     {
133         return fieldPageManager;
134     }
135     public void setPageManager(PageManager inPageManager)
136     {
137         fieldPageManager = inPageManager;
138     }
139     public File JavaDoc getRoot()
140     {
141         return fieldRoot;
142     }
143     public void setRoot(File JavaDoc inRoot)
144     {
145         fieldRoot = inRoot;
146     }
147     public boolean isUseMatches()
148     {
149         return fieldUseMatches;
150     }
151     public void setUseMatches(boolean inUseMatches)
152     {
153         fieldUseMatches = inUseMatches;
154     }
155     public boolean isRecursive()
156     {
157         return fieldRecursive;
158     }
159     public void setRecursive(boolean inRecursive)
160     {
161         fieldRecursive = inRecursive;
162     }
163
164     
165 }
166
Popular Tags