1 4 package com.openedit.page; 5 6 import java.io.File ; 7 import java.util.ArrayList ; 8 import java.util.HashSet ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Set ; 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 fieldRoot; 20 protected PageManager fieldPageManager; 21 protected boolean fieldUseMatches; 22 protected boolean fieldRecursive = true; 23 protected Set fieldSkipFiles; 24 25 protected boolean skip(String inFileName) 26 { 27 return getSkipFiles().contains(inFileName); 28 } 29 30 protected Set getSkipFiles() 31 { 32 if (fieldSkipFiles == null) 33 { 34 fieldSkipFiles = new HashSet (); 35 fieldSkipFiles.add("base"); 36 fieldSkipFiles.add(".versions"); 37 fieldSkipFiles.add("WEB-INF"); 38 } 39 return fieldSkipFiles; 40 } 41 public void addSkipFileName(String inName) 42 { 43 getSkipFiles().add(inName); 44 } 45 46 public List findPages(String inContains) throws OpenEditException 47 { 48 List paths = new ArrayList (); 49 findMatches(getRoot(), getRoot().getAbsolutePath().length(), inContains, paths); 50 51 List pages = new ArrayList (paths.size()); 52 53 for (Iterator iter = paths.iterator(); iter.hasNext();) 54 { 55 String path = (String ) iter.next(); 56 pages.add(getPageManager().getPage(path)); 57 } 58 return pages; 59 } 60 protected void findMatches(File inRoot, int from, String contains, List found) 61 { 62 if( inRoot.isDirectory()) 63 { 64 File [] children = inRoot.listFiles(); 65 if( children != null) 66 { 67 for (int i = 0; i < children.length; i++) 68 { 69 File 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 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 inPath) 118 { 119 for (Iterator iterator = getSkipFiles().iterator(); iterator.hasNext();) 120 { 121 String file = (String ) 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 getRoot() 140 { 141 return fieldRoot; 142 } 143 public void setRoot(File 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 |