1 32 33 package com.jeantessier.dependency; 34 35 import java.util.*; 36 37 import org.apache.log4j.*; 38 import org.apache.oro.text.perl.*; 39 40 import com.jeantessier.text.*; 41 42 public class RegularExpressionSelectionCriteria implements SelectionCriteria { 43 private Perl5Util perl = new Perl5Util(new MaximumCapacityPatternCache()); 44 45 private List globalIncludes = new LinkedList(); 46 private List globalExcludes = new LinkedList(); 47 private boolean matchingPackages = true; 48 private List packageIncludes = new LinkedList(); 49 private List packageExcludes = new LinkedList(); 50 private boolean matchingClasses = true; 51 private List classIncludes = new LinkedList(); 52 private List classExcludes = new LinkedList(); 53 private boolean matchingFeatures = true; 54 private List featureIncludes = new LinkedList(); 55 private List featureExcludes = new LinkedList(); 56 57 public RegularExpressionSelectionCriteria() { 58 getGlobalIncludes().add("//"); 59 } 60 61 public List getGlobalIncludes() { 62 return globalIncludes; 63 } 64 65 public void setGlobalIncludes(String globalIncludes) { 66 setGlobalIncludes(parseRE(globalIncludes)); 67 } 68 69 public void setGlobalIncludes(List globalIncludes) { 70 this.globalIncludes = globalIncludes; 71 } 72 73 public List getGlobalExcludes() { 74 return globalExcludes; 75 } 76 77 public void setGlobalExcludes(String globalExcludes) { 78 setGlobalExcludes(parseRE(globalExcludes)); 79 } 80 81 public void setGlobalExcludes(List globalExcludes) { 82 this.globalExcludes = globalExcludes; 83 } 84 85 public boolean isMatchingPackages() { 86 return matchingPackages; 87 } 88 89 public void setMatchingPackages(boolean matchingPackages) { 90 this.matchingPackages = matchingPackages; 91 } 92 93 public List getPackageIncludes() { 94 return packageIncludes; 95 } 96 97 public void setPackageIncludes(String packageIncludes) { 98 setPackageIncludes(parseRE(packageIncludes)); 99 } 100 101 public void setPackageIncludes(List packageIncludes) { 102 this.packageIncludes = packageIncludes; 103 } 104 105 public List getPackageExcludes() { 106 return packageExcludes; 107 } 108 109 public void setPackageExcludes(String packageExcludes) { 110 setPackageExcludes(parseRE(packageExcludes)); 111 } 112 113 public void setPackageExcludes(List packageExcludes) { 114 this.packageExcludes = packageExcludes; 115 } 116 117 public boolean isMatchingClasses() { 118 return matchingClasses; 119 } 120 121 public void setMatchingClasses(boolean matchingClasses) { 122 this.matchingClasses = matchingClasses; 123 } 124 125 public List getClassIncludes() { 126 return classIncludes; 127 } 128 129 public void setClassIncludes(String classIncludes) { 130 setClassIncludes(parseRE(classIncludes)); 131 } 132 133 public void setClassIncludes(List classIncludes) { 134 this.classIncludes = classIncludes; 135 } 136 137 public List getClassExcludes() { 138 return classExcludes; 139 } 140 141 public void setClassExcludes(String classExcludes) { 142 setClassExcludes(parseRE(classExcludes)); 143 } 144 145 public void setClassExcludes(List classExcludes) { 146 this.classExcludes = classExcludes; 147 } 148 149 public boolean isMatchingFeatures() { 150 return matchingFeatures; 151 } 152 153 public void setMatchingFeatures(boolean matchingFeatures) { 154 this.matchingFeatures = matchingFeatures; 155 } 156 157 public List getFeatureIncludes() { 158 return featureIncludes; 159 } 160 161 public void setFeatureIncludes(String featureIncludes) { 162 setFeatureIncludes(parseRE(featureIncludes)); 163 } 164 165 public void setFeatureIncludes(List featureIncludes) { 166 this.featureIncludes = featureIncludes; 167 } 168 169 public List getFeatureExcludes() { 170 return featureExcludes; 171 } 172 173 public void setFeatureExcludes(String featureExcludes) { 174 setFeatureExcludes(parseRE(featureExcludes)); 175 } 176 177 public void setFeatureExcludes(List featureExcludes) { 178 this.featureExcludes = featureExcludes; 179 } 180 181 public boolean matches(PackageNode node) { 182 return isMatchingPackages() && matchesPackageName(node.getName()); 183 } 184 185 public boolean matches(ClassNode node) { 186 return isMatchingClasses() && matchesClassName(node.getName()); 187 } 188 189 public boolean matches(FeatureNode node) { 190 return isMatchingFeatures() && matchesFeatureName(node.getName()); 191 } 192 193 public boolean matchesPackageName(String name) { 194 return matches(getGlobalIncludes(), getPackageIncludes(), name) && 195 !matches(getGlobalExcludes(), getPackageExcludes(), name); 196 } 197 198 public boolean matchesClassName(String name) { 199 return matches(getGlobalIncludes(), getClassIncludes(), name) && 200 !matches(getGlobalExcludes(), getClassExcludes(), name); 201 } 202 203 public boolean matchesFeatureName(String name) { 204 return matches(getGlobalIncludes(), getFeatureIncludes(), name) && 205 !matches(getGlobalExcludes(), getFeatureExcludes(), name); 206 } 207 208 private boolean matches(List globalRegularExpressions, List regularExpressions, String name) { 209 boolean found = false; 210 Iterator i; 211 212 i = globalRegularExpressions.iterator(); 213 while (!found && i.hasNext()) { 214 String condition = (String ) i.next(); 215 found = perl.match(condition, name); 216 } 217 218 i = regularExpressions.iterator(); 219 while (!found && i.hasNext()) { 220 String condition = (String ) i.next(); 221 found = perl.match(condition, name); 222 } 223 224 return found; 225 } 226 227 static List parseRE(String re) { 229 List result = new LinkedList(); 230 231 Logger logger = Logger.getLogger(RegularExpressionSelectionCriteria.class); 232 logger.debug("ParseRE \"" + re + "\""); 233 234 int length = re.length(); 235 int start = 0; 236 int stop = -1; 237 238 while (start < length && stop < length) { 239 String separator = null; 240 241 while (start < length && stop < start) { 243 if (re.charAt(start) == 'm' && (start + 1) < length) { 244 separator = re.substring(start + 1, start + 2); 245 stop = start + 2; 246 } else if (re.charAt(start) == '/') { 247 separator = "/"; 248 stop = start + 1; 249 } else { 250 start++; 251 } 252 } 253 254 logger.debug("start is " + start); 255 logger.debug("separator is " + separator); 256 257 while (stop < length && start < stop) { 259 stop = re.indexOf(separator, stop); 260 logger.debug("indexOf() is " + stop); 261 262 if (stop == -1 || re.charAt(stop - 1) != '\\') { 263 264 if (stop == -1) { 265 stop = length; 266 } else { 267 stop++; 269 while (stop < length && (re.charAt(stop) == 'g' || 270 re.charAt(stop) == 'i' || 271 re.charAt(stop) == 'm' || 272 re.charAt(stop) == 'o' || 273 re.charAt(stop) == 's' || 274 re.charAt(stop) == 'x')) { 275 stop++; 276 } 277 } 278 279 logger.debug("stop is " + stop); 280 281 logger.debug("candidate is \"" + re.substring(start, stop) + "\""); 283 result.add(re.substring(start, stop)); 284 285 start = stop + 1; 287 } else { 288 stop++; 289 } 290 } 291 } 292 293 return result; 294 } 295 } 296 | Popular Tags |