1 29 30 package com.caucho.config.types; 31 32 import com.caucho.config.ConfigException; 33 import com.caucho.log.Log; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.Path; 36 import com.caucho.vfs.Vfs; 37 38 import javax.annotation.PostConstruct; 39 import java.io.IOException ; 40 import java.util.ArrayList ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 47 public class FileSetType { 48 static final L10N L = new L10N(PathPatternType.class); 49 static final Logger log = Log.open(PathPatternType.class); 50 51 private Path _dir; 52 private String _userPathPrefix = ""; 53 54 private ArrayList <PathPatternType> _includeList; 55 56 private ArrayList <PathPatternType> _excludeList = 57 new ArrayList <PathPatternType>(); 58 59 62 public void setDir(Path dir) 63 { 64 _dir = dir; 65 } 66 67 70 public Path getDir() 71 { 72 return _dir; 73 } 74 75 78 public void addInclude(PathPatternType pattern) 79 { 80 if (_includeList == null) 81 _includeList = new ArrayList <PathPatternType>(); 82 83 _includeList.add(pattern); 84 } 85 86 89 public void addExclude(PathPatternType pattern) 90 { 91 _excludeList.add(pattern); 92 } 93 94 97 public void setUserPathPrefix(String prefix) 98 { 99 if (prefix != null && ! prefix.equals("") && ! prefix.endsWith("/")) 100 _userPathPrefix = prefix + "/"; 101 else 102 _userPathPrefix = prefix; 103 } 104 105 108 @PostConstruct 109 public void init() 110 throws ConfigException 111 { 112 if (_dir == null) 113 _dir = Vfs.lookup(); 114 } 115 116 119 public ArrayList <Path> getPaths() 120 { 121 return getPaths(new ArrayList <Path>()); 122 } 123 124 127 public ArrayList <Path> getPaths(ArrayList <Path> paths) 128 { 129 String dirPath = _dir.getPath(); 130 131 if (! dirPath.endsWith("/")) 132 dirPath += "/"; 133 134 getPaths(paths, _dir, dirPath); 135 136 return paths; 137 } 138 139 142 public void getPaths(ArrayList <Path> paths, Path path, String prefix) 143 { 144 if (path.isDirectory()) { 145 try { 146 String []list = path.list(); 147 148 for (int i = 0; i < list.length; i++) 149 getPaths(paths, path.lookup(list[i]), prefix); 150 } catch (IOException e) { 151 log.log(Level.WARNING, e.toString(), e); 152 } 153 } 154 else if (path.canRead()) { 155 if (isMatch(path, prefix)) { 156 String suffix = ""; 157 String fullPath = path.getPath(); 158 159 if (prefix.length() < fullPath.length()) 160 suffix = path.getPath().substring(prefix.length()); 161 162 path.setUserPath(_userPathPrefix + suffix); 163 164 paths.add(path); 165 } 166 } 167 } 168 169 172 public boolean isMatch(Path path, String prefix) 173 { 174 String suffix = ""; 175 String fullPath = path.getPath(); 176 177 if (prefix.length() < fullPath.length()) 178 suffix = path.getPath().substring(prefix.length()); 179 180 for (int i = 0; i < _excludeList.size(); i++) { 181 PathPatternType pattern = _excludeList.get(i); 182 183 if (pattern.isMatch(suffix)) 184 return false; 185 } 186 187 if (_includeList == null) 188 return true; 189 190 for (int i = 0; i < _includeList.size(); i++) { 191 PathPatternType pattern = _includeList.get(i); 192 193 if (pattern.isMatch(suffix)) 194 return true; 195 } 196 197 return false; 198 } 199 } 200 | Popular Tags |