1 18 19 package org.apache.tools.ant.types; 20 21 import java.io.BufferedReader ; 22 import java.io.File ; 23 import java.io.FileReader ; 24 import java.io.IOException ; 25 import java.util.Enumeration ; 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.BuildException; 29 import org.apache.tools.ant.Project; 30 31 38 public class PatternSet extends DataType implements Cloneable { 39 private Vector includeList = new Vector (); 40 private Vector excludeList = new Vector (); 41 private Vector includesFileList = new Vector (); 42 private Vector excludesFileList = new Vector (); 43 44 49 public class NameEntry { 50 private String name; 51 private String ifCond; 52 private String unlessCond; 53 54 59 public void setName(String name) { 60 this.name = name; 61 } 62 63 71 public void setIf(String cond) { 72 ifCond = cond; 73 } 74 75 83 public void setUnless(String cond) { 84 unlessCond = cond; 85 } 86 87 90 public String getName() { 91 return name; 92 } 93 94 103 public String evalName(Project p) { 104 return valid(p) ? name : null; 105 } 106 107 private boolean valid(Project p) { 108 if (ifCond != null && p.getProperty(ifCond) == null) { 109 return false; 110 } else if (unlessCond != null && p.getProperty(unlessCond) != null) { 111 return false; 112 } 113 return true; 114 } 115 116 119 public String toString() { 120 StringBuffer buf = new StringBuffer (); 121 if (name == null) { 122 buf.append("noname"); 123 } else { 124 buf.append(name); 125 } 126 if ((ifCond != null) || (unlessCond != null)) { 127 buf.append(":"); 128 String connector = ""; 129 130 if (ifCond != null) { 131 buf.append("if->"); 132 buf.append(ifCond); 133 connector = ";"; 134 } 135 if (unlessCond != null) { 136 buf.append(connector); 137 buf.append("unless->"); 138 buf.append(unlessCond); 139 } 140 } 141 142 return buf.toString(); 143 } 144 } 145 146 149 public PatternSet() { 150 super(); 151 } 152 153 162 public void setRefid(Reference r) throws BuildException { 163 if (!includeList.isEmpty() || !excludeList.isEmpty()) { 164 throw tooManyAttributes(); 165 } 166 super.setRefid(r); 167 } 168 169 174 public void addConfiguredPatternset(PatternSet p) { 175 if (isReference()) { 176 throw noChildrenAllowed(); 177 } 178 179 String [] nestedIncludes = p.getIncludePatterns(getProject()); 180 String [] nestedExcludes = p.getExcludePatterns(getProject()); 181 182 if (nestedIncludes != null) { 183 for (int i = 0; i < nestedIncludes.length; i++) { 184 createInclude().setName(nestedIncludes[i]); 185 } 186 } 187 188 if (nestedExcludes != null) { 189 for (int i = 0; i < nestedExcludes.length; i++) { 190 createExclude().setName(nestedExcludes[i]); 191 } 192 } 193 } 194 195 199 public NameEntry createInclude() { 200 if (isReference()) { 201 throw noChildrenAllowed(); 202 } 203 return addPatternToList(includeList); 204 } 205 206 210 public NameEntry createIncludesFile() { 211 if (isReference()) { 212 throw noChildrenAllowed(); 213 } 214 return addPatternToList(includesFileList); 215 } 216 217 221 public NameEntry createExclude() { 222 if (isReference()) { 223 throw noChildrenAllowed(); 224 } 225 return addPatternToList(excludeList); 226 } 227 228 232 public NameEntry createExcludesFile() { 233 if (isReference()) { 234 throw noChildrenAllowed(); 235 } 236 return addPatternToList(excludesFileList); 237 } 238 239 245 public void setIncludes(String includes) { 246 if (isReference()) { 247 throw tooManyAttributes(); 248 } 249 if (includes != null && includes.length() > 0) { 250 StringTokenizer tok = new StringTokenizer (includes, ", ", false); 251 while (tok.hasMoreTokens()) { 252 createInclude().setName(tok.nextToken()); 253 } 254 } 255 } 256 257 263 public void setExcludes(String excludes) { 264 if (isReference()) { 265 throw tooManyAttributes(); 266 } 267 if (excludes != null && excludes.length() > 0) { 268 StringTokenizer tok = new StringTokenizer (excludes, ", ", false); 269 while (tok.hasMoreTokens()) { 270 createExclude().setName(tok.nextToken()); 271 } 272 } 273 } 274 275 278 private NameEntry addPatternToList(Vector list) { 279 NameEntry result = new NameEntry(); 280 list.addElement(result); 281 return result; 282 } 283 284 290 public void setIncludesfile(File includesFile) throws BuildException { 291 if (isReference()) { 292 throw tooManyAttributes(); 293 } 294 createIncludesFile().setName(includesFile.getAbsolutePath()); 295 } 296 297 303 public void setExcludesfile(File excludesFile) throws BuildException { 304 if (isReference()) { 305 throw tooManyAttributes(); 306 } 307 createExcludesFile().setName(excludesFile.getAbsolutePath()); 308 } 309 310 314 private void readPatterns(File patternfile, Vector patternlist, Project p) 315 throws BuildException { 316 317 BufferedReader patternReader = null; 318 try { 319 patternReader = 321 new BufferedReader (new FileReader (patternfile)); 322 323 String line = patternReader.readLine(); 326 while (line != null) { 327 if (line.length() > 0) { 328 line = p.replaceProperties(line); 329 addPatternToList(patternlist).setName(line); 330 } 331 line = patternReader.readLine(); 332 } 333 } catch (IOException ioe) { 334 String msg = "An error occurred while reading from pattern file: " 335 + patternfile; 336 throw new BuildException(msg, ioe); 337 } finally { 338 if (null != patternReader) { 339 try { 340 patternReader.close(); 341 } catch (IOException ioe) { 342 } 344 } 345 } 346 } 347 348 353 public void append(PatternSet other, Project p) { 354 if (isReference()) { 355 throw new BuildException("Cannot append to a reference"); 356 } 357 358 String [] incl = other.getIncludePatterns(p); 359 if (incl != null) { 360 for (int i = 0; i < incl.length; i++) { 361 createInclude().setName(incl[i]); 362 } 363 } 364 365 String [] excl = other.getExcludePatterns(p); 366 if (excl != null) { 367 for (int i = 0; i < excl.length; i++) { 368 createExclude().setName(excl[i]); 369 } 370 } 371 } 372 373 378 public String [] getIncludePatterns(Project p) { 379 if (isReference()) { 380 return getRef(p).getIncludePatterns(p); 381 } else { 382 readFiles(p); 383 return makeArray(includeList, p); 384 } 385 } 386 387 392 public String [] getExcludePatterns(Project p) { 393 if (isReference()) { 394 return getRef(p).getExcludePatterns(p); 395 } else { 396 readFiles(p); 397 return makeArray(excludeList, p); 398 } 399 } 400 401 407 public boolean hasPatterns(Project p) { 408 if (isReference()) { 409 return getRef(p).hasPatterns(p); 410 } else { 411 return includesFileList.size() > 0 || excludesFileList.size() > 0 412 || includeList.size() > 0 || excludeList.size() > 0; 413 } 414 } 415 416 420 private PatternSet getRef(Project p) { 421 return (PatternSet) getCheckedRef(p); 422 } 423 424 427 private String [] makeArray(Vector list, Project p) { 428 if (list.size() == 0) { 429 return null; 430 } 431 432 Vector tmpNames = new Vector (); 433 for (Enumeration e = list.elements(); e.hasMoreElements();) { 434 NameEntry ne = (NameEntry) e.nextElement(); 435 String pattern = ne.evalName(p); 436 if (pattern != null && pattern.length() > 0) { 437 tmpNames.addElement(pattern); 438 } 439 } 440 441 String [] result = new String [tmpNames.size()]; 442 tmpNames.copyInto(result); 443 return result; 444 } 445 446 449 private void readFiles(Project p) { 450 if (includesFileList.size() > 0) { 451 Enumeration e = includesFileList.elements(); 452 while (e.hasMoreElements()) { 453 NameEntry ne = (NameEntry) e.nextElement(); 454 String fileName = ne.evalName(p); 455 if (fileName != null) { 456 File inclFile = p.resolveFile(fileName); 457 if (!inclFile.exists()) { 458 throw new BuildException("Includesfile " 459 + inclFile.getAbsolutePath() 460 + " not found."); 461 } 462 readPatterns(inclFile, includeList, p); 463 } 464 } 465 includesFileList.removeAllElements(); 466 } 467 468 if (excludesFileList.size() > 0) { 469 Enumeration e = excludesFileList.elements(); 470 while (e.hasMoreElements()) { 471 NameEntry ne = (NameEntry) e.nextElement(); 472 String fileName = ne.evalName(p); 473 if (fileName != null) { 474 File exclFile = p.resolveFile(fileName); 475 if (!exclFile.exists()) { 476 throw new BuildException("Excludesfile " 477 + exclFile.getAbsolutePath() 478 + " not found."); 479 } 480 readPatterns(exclFile, excludeList, p); 481 } 482 } 483 excludesFileList.removeAllElements(); 484 } 485 } 486 487 490 public String toString() { 491 return "patternSet{ includes: " + includeList 492 + " excludes: " + excludeList + " }"; 493 } 494 495 499 public Object clone() { 500 try { 501 PatternSet ps = (PatternSet) super.clone(); 502 ps.includeList = (Vector ) includeList.clone(); 503 ps.excludeList = (Vector ) excludeList.clone(); 504 ps.includesFileList = (Vector ) includesFileList.clone(); 505 ps.excludesFileList = (Vector ) excludesFileList.clone(); 506 return ps; 507 } catch (CloneNotSupportedException e) { 508 throw new BuildException(e); 509 } 510 } 511 512 } 513 | Popular Tags |