1 32 33 package com.jeantessier.dependencyfinder.ant; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.log4j.*; 39 import org.apache.tools.ant.*; 40 import org.apache.tools.ant.types.*; 41 42 import org.xml.sax.*; 43 44 import com.jeantessier.dependency.*; 45 46 public class DependencyReporter extends GraphTask { 47 private String scopeIncludes = "//"; 48 private String scopeExcludes = ""; 49 private boolean packageScope; 50 private String packageScopeIncludes = ""; 51 private String packageScopeExcludes = ""; 52 private boolean classScope; 53 private String classScopeIncludes = ""; 54 private String classScopeExcludes = ""; 55 private boolean featureScope; 56 private String featureScopeIncludes = ""; 57 private String featureScopeExcludes = ""; 58 private String filterIncludes = "//"; 59 private String filterExcludes = ""; 60 private boolean packageFilter; 61 private String packageFilterIncludes = ""; 62 private String packageFilterExcludes = ""; 63 private boolean classFilter; 64 private String classFilterIncludes = ""; 65 private String classFilterExcludes = ""; 66 private boolean featureFilter; 67 private String featureFilterIncludes = ""; 68 private String featureFilterExcludes = ""; 69 70 private Path scopeIncludesList; 71 private Path scopeExcludesList; 72 private Path filterIncludesList; 73 private Path filterExcludesList; 74 75 private boolean showInbounds = false; 76 private boolean showOutbounds = false; 77 private boolean showEmptyNodes = false; 78 private boolean showAll = false; 79 80 private boolean minimize = false; 81 private boolean maximize = false; 82 private boolean copyOnly = false; 83 private boolean xml = false; 84 private String encoding = XMLPrinter.DEFAULT_ENCODING; 85 private String dtdPrefix = XMLPrinter.DEFAULT_DTD_PREFIX; 86 private String indentText; 87 88 public String getScopeincludes() { 89 return scopeIncludes; 90 } 91 92 public void setScopeincludes(String scopeIncludes) { 93 this.scopeIncludes = scopeIncludes; 94 } 95 96 public String getScopeexcludes() { 97 return scopeExcludes; 98 } 99 100 public void setScopeexcludes(String scopeExcludes) { 101 this.scopeExcludes = scopeExcludes; 102 } 103 104 public boolean getPackagescope() { 105 return packageScope; 106 } 107 108 public void setPackagescope(boolean packageScope) { 109 this.packageScope = packageScope; 110 } 111 112 public String getPackagescopeincludes() { 113 return packageScopeIncludes; 114 } 115 116 public void setPackagescopeincludes(String packageScopeIncludes) { 117 this.packageScopeIncludes = packageScopeIncludes; 118 } 119 120 public String getPackagescopeexcludes() { 121 return packageScopeExcludes; 122 } 123 124 public void setPackagescopeexcludes(String packageScopeExcludes) { 125 this.packageScopeExcludes = packageScopeExcludes; 126 } 127 128 public boolean getClassscope() { 129 return classScope; 130 } 131 132 public void setClassscope(boolean classScope) { 133 this.classScope = classScope; 134 } 135 136 public String getClassscopeincludes() { 137 return classScopeIncludes; 138 } 139 140 public void setClassscopeincludes(String classScopeIncludes) { 141 this.classScopeIncludes = classScopeIncludes; 142 } 143 144 public String getClassscopeexcludes() { 145 return classScopeExcludes; 146 } 147 148 public void setClassscopeexcludes(String classScopeExcludes) { 149 this.classScopeExcludes = classScopeExcludes; 150 } 151 152 public boolean getFeaturescope() { 153 return featureScope; 154 } 155 156 public void setFeaturescope(boolean featureScope) { 157 this.featureScope = featureScope; 158 } 159 160 public String getFeaturescopeincludes() { 161 return featureScopeIncludes; 162 } 163 164 public void setFeaturescopeincludes(String featureScopeIncludes) { 165 this.featureScopeIncludes = featureScopeIncludes; 166 } 167 168 public String getFeaturescopeexcludes() { 169 return featureScopeExcludes; 170 } 171 172 public void setFeaturescopeexcludes(String featureScopeExcludes) { 173 this.featureScopeExcludes = featureScopeExcludes; 174 } 175 176 public String getFilterincludes() { 177 return filterIncludes; 178 } 179 180 public void setFilterincludes(String filterIncludes) { 181 this.filterIncludes = filterIncludes; 182 } 183 184 public String getFilterexcludes() { 185 return filterExcludes; 186 } 187 188 public void setFilterexcludes(String filterExcludes) { 189 this.filterExcludes = filterExcludes; 190 } 191 192 public boolean getPackagefilter() { 193 return packageFilter; 194 } 195 196 public void setPackagefilter(boolean packageFilter) { 197 this.packageFilter = packageFilter; 198 } 199 200 public String getPackagefilterincludes() { 201 return packageFilterIncludes; 202 } 203 204 public void setPackagefilterincludes(String packageFilterIncludes) { 205 this.packageFilterIncludes = packageFilterIncludes; 206 } 207 208 public String getPackagefilterexcludes() { 209 return packageFilterExcludes; 210 } 211 212 public void setPackagefilterexcludes(String packageFilterExcludes) { 213 this.packageFilterExcludes = packageFilterExcludes; 214 } 215 216 public boolean getClassfilter() { 217 return classFilter; 218 } 219 220 public void setClassfilter(boolean classFilter) { 221 this.classFilter = classFilter; 222 } 223 224 public String getClassfilterincludes() { 225 return classFilterIncludes; 226 } 227 228 public void setClassfilterincludes(String classFilterIncludes) { 229 this.classFilterIncludes = classFilterIncludes; 230 } 231 232 public String getClassfilterexcludes() { 233 return classFilterExcludes; 234 } 235 236 public void setClassfilterexcludes(String classFilterExcludes) { 237 this.classFilterExcludes = classFilterExcludes; 238 } 239 240 public boolean getFeaturefilter() { 241 return featureFilter; 242 } 243 244 public void setFeaturefilter(boolean featureFilter) { 245 this.featureFilter = featureFilter; 246 } 247 248 public String getFeaturefilterincludes() { 249 return featureFilterIncludes; 250 } 251 252 public void setFeaturefilterincludes(String featureFilterIncludes) { 253 this.featureFilterIncludes = featureFilterIncludes; 254 } 255 256 public String getFeaturefilterexcludes() { 257 return featureFilterExcludes; 258 } 259 260 public void setFeaturefilterexcludes(String featureFilterExcludes) { 261 this.featureFilterExcludes = featureFilterExcludes; 262 } 263 264 public void setAll(boolean value) { 265 setPackagescope(value); 266 setClassscope(value); 267 setFeaturescope(value); 268 setPackagefilter(value); 269 setClassfilter(value); 270 setFeaturefilter(value); 271 } 272 273 public void setP2p(boolean value) { 274 setPackagescope(value); 275 setPackagefilter(value); 276 } 277 278 public void setC2p(boolean value) { 279 setClassscope(value); 280 setPackagefilter(value); 281 } 282 283 public void setC2c(boolean value) { 284 setClassscope(value); 285 setClassfilter(value); 286 } 287 288 public void setF2f(boolean value) { 289 setFeaturescope(value); 290 setFeaturefilter(value); 291 } 292 293 public void setIncludes(String value) { 294 setScopeincludes(value); 295 setFilterincludes(value); 296 } 297 298 public void setExcludes(String value) { 299 setScopeexcludes(value); 300 setFilterexcludes(value); 301 } 302 303 public Path createScopeincludeslist() { 304 if (scopeIncludesList == null) { 305 scopeIncludesList = new Path(getProject()); 306 } 307 308 return scopeIncludesList; 309 } 310 311 public Path getScopeincludeslist() { 312 return scopeIncludesList; 313 } 314 315 public Path createScopeexcludeslist() { 316 if (scopeExcludesList == null) { 317 scopeExcludesList = new Path(getProject()); 318 } 319 320 return scopeExcludesList; 321 } 322 323 public Path getScopeexcludeslist() { 324 return scopeExcludesList; 325 } 326 327 public Path createFilterincludeslist() { 328 if (filterIncludesList == null) { 329 filterIncludesList = new Path(getProject()); 330 } 331 332 return filterIncludesList; 333 } 334 335 public Path getFilterincludeslist() { 336 return filterIncludesList; 337 } 338 339 public Path createFilterexcludeslist() { 340 if (filterExcludesList == null) { 341 filterExcludesList = new Path(getProject()); 342 } 343 344 return filterExcludesList; 345 } 346 347 public Path getFilterexcludeslist() { 348 return filterExcludesList; 349 } 350 351 public boolean getShowinbounds() { 352 return showInbounds; 353 } 354 355 public void setShowinbounds(boolean showInbounds) { 356 this.showInbounds = showInbounds; 357 } 358 359 public boolean getShowoutbounds() { 360 return showOutbounds; 361 } 362 363 public void setShowoutbounds(boolean showOutbounds) { 364 this.showOutbounds = showOutbounds; 365 } 366 367 public boolean getShowemptynodes() { 368 return showEmptyNodes; 369 } 370 371 public void setShowemptynodes(boolean showEmptyNodes) { 372 this.showEmptyNodes = showEmptyNodes; 373 } 374 375 public void setShowAll(boolean showAll) { 376 setShowinbounds(showAll); 377 setShowoutbounds(showAll); 378 setShowemptynodes(showAll); 379 } 380 381 public boolean getMinimize() { 382 return minimize; 383 } 384 385 public void setMinimize(boolean minimize) { 386 this.minimize = minimize; 387 } 388 389 public boolean getMaximize() { 390 return maximize; 391 } 392 393 public void setMaximize(boolean maximize) { 394 this.maximize = maximize; 395 } 396 397 public boolean getCopyOnly() { 398 return copyOnly; 399 } 400 401 public void setCopyOnly(boolean copyOnly) { 402 this.copyOnly = copyOnly; 403 } 404 405 public boolean getXml() { 406 return xml; 407 } 408 409 public void setXml(boolean xml) { 410 this.xml = xml; 411 } 412 413 public String getEncoding() { 414 return encoding; 415 } 416 417 public void setEncoding(String encoding) { 418 this.encoding = encoding; 419 } 420 421 public String getDtdprefix() { 422 return dtdPrefix; 423 } 424 425 public void setDtdprefix(String dtdPrefix) { 426 this.dtdPrefix = dtdPrefix; 427 } 428 429 public String getIndenttext() { 430 return indentText; 431 } 432 433 public void setIntenttext(String indentText) { 434 this.indentText = indentText; 435 } 436 437 protected void validateParameters() throws BuildException { 438 super.validateParameters(); 439 440 if (hasScopeRegularExpressionSwitches() && hasScopeListSwitches()) { 441 throw new BuildException("Cannot have scope attributes for regular expressions and lists at the same time!"); 442 } 443 444 if (hasFilterRegularExpressionSwitches() && hasFilterListSwitches()) { 445 throw new BuildException("Cannot have filter attributes for regular expressions and lists at the same time!"); 446 } 447 } 448 449 public void execute() throws BuildException { 450 validateParameters(); 452 453 VerboseListener verboseListener = new VerboseListener(this); 454 455 try { 456 GraphCopier copier; 457 if (getCopyOnly() || getMaximize()) { 458 copier = new GraphCopier(getStrategy()); 459 } else { 460 copier = new GraphSummarizer(getScopeCriteria(), getFilterCriteria()); 461 } 462 463 String [] filenames = getSrc().list(); 464 for (int i=0; i<filenames.length; i++) { 465 log("Reading graph from " + filenames[i]); 466 467 Collection packages = Collections.EMPTY_LIST; 468 469 if (filenames[i].endsWith(".xml")) { 470 NodeLoader loader = new NodeLoader(getValidate()); 471 loader.addDependencyListener(verboseListener); 472 packages = loader.load(filenames[i]).getPackages().values(); 473 } 474 475 if (getMaximize()) { 476 log("Maximizing ..."); 477 new LinkMaximizer().traverseNodes(packages); 478 } else if (getMinimize()) { 479 log("Minimizing ..."); 480 new LinkMinimizer().traverseNodes(packages); 481 } 482 483 copier.traverseNodes(packages); 484 } 485 486 log("Saving dependency graph to " + getDestfile().getAbsolutePath()); 487 488 PrintWriter out = new PrintWriter(new FileWriter(getDestfile())); 489 490 Printer printer; 491 if (getXml()) { 492 printer = new XMLPrinter(out, getEncoding(), getDtdprefix()); 493 } else { 494 printer = new TextPrinter(out); 495 } 496 497 if (getIndenttext() != null) { 498 printer.setIndentText(getIndenttext()); 499 } 500 501 printer.setShowInbounds(getShowinbounds()); 502 printer.setShowOutbounds(getShowoutbounds()); 503 printer.setShowEmptyNodes(getShowemptynodes()); 504 505 printer.traverseNodes(copier.getScopeFactory().getPackages().values()); 506 507 out.close(); 508 } catch (SAXException ex) { 509 throw new BuildException(ex); 510 } catch (IOException ex) { 511 throw new BuildException(ex); 512 } 513 } 514 515 protected SelectionCriteria getScopeCriteria() throws BuildException { 516 SelectionCriteria result = new ComprehensiveSelectionCriteria(); 517 518 try { 519 if (hasScopeRegularExpressionSwitches()) { 520 result = createRegularExpressionScopeCriteria(); 521 } else if (hasScopeListSwitches()) { 522 result = createCollectionSelectionCriteria(getScopeincludeslist(), getScopeexcludeslist()); 523 } 524 } catch (IOException ex) { 525 throw new BuildException(ex); 526 } 527 528 return result; 529 } 530 531 protected SelectionCriteria getFilterCriteria() throws BuildException { 532 SelectionCriteria result = new ComprehensiveSelectionCriteria(); 533 534 try { 535 if (hasFilterRegularExpressionSwitches()) { 536 result = createRegularExpressionFilterCriteria(); 537 } else if (hasFilterListSwitches()) { 538 result = createCollectionSelectionCriteria(getFilterincludeslist(), getFilterexcludeslist()); 539 } 540 } catch (IOException ex) { 541 throw new BuildException(ex); 542 } 543 544 return result; 545 } 546 547 protected RegularExpressionSelectionCriteria createRegularExpressionScopeCriteria() throws BuildException { 548 RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria(); 549 550 result.setMatchingPackages(getPackagescope()); 551 result.setMatchingClasses(getClassscope()); 552 result.setMatchingFeatures(getFeaturescope()); 553 554 result.setGlobalIncludes(getScopeincludes()); 555 result.setGlobalExcludes(getScopeexcludes()); 556 result.setPackageIncludes(getPackagescopeincludes()); 557 result.setPackageExcludes(getPackagescopeexcludes()); 558 result.setClassIncludes(getClassscopeincludes()); 559 result.setClassExcludes(getClassscopeexcludes()); 560 result.setFeatureIncludes(getFeaturescopeincludes()); 561 result.setFeatureExcludes(getFeaturescopeexcludes()); 562 563 return result; 564 } 565 566 protected RegularExpressionSelectionCriteria createRegularExpressionFilterCriteria() throws BuildException { 567 RegularExpressionSelectionCriteria result = new RegularExpressionSelectionCriteria(); 568 569 result.setMatchingPackages(getPackagefilter()); 570 result.setMatchingClasses(getClassfilter()); 571 result.setMatchingFeatures(getFeaturefilter()); 572 573 result.setGlobalIncludes(getFilterincludes()); 574 result.setGlobalExcludes(getFilterexcludes()); 575 result.setPackageIncludes(getPackagefilterincludes()); 576 result.setPackageExcludes(getPackagefilterexcludes()); 577 result.setClassIncludes(getClassfilterincludes()); 578 result.setClassExcludes(getClassfilterexcludes()); 579 result.setFeatureIncludes(getFeaturefilterincludes()); 580 result.setFeatureExcludes(getFeaturefilterexcludes()); 581 582 return result; 583 } 584 585 private boolean hasScopeRegularExpressionSwitches() { 586 return 587 !getScopeincludes().equals("//") || 588 !getScopeexcludes().equals("") || 589 getPackagescope() || 590 !getPackagescopeincludes().equals("") || 591 !getPackagescopeexcludes().equals("") || 592 getClassscope() || 593 !getClassscopeincludes().equals("") || 594 !getClassscopeexcludes().equals("") || 595 getFeaturescope() || 596 !getFeaturescopeincludes().equals("") || 597 !getFeaturescopeexcludes().equals(""); 598 } 599 600 private boolean hasScopeListSwitches() { 601 return 602 getScopeincludeslist() != null || 603 getScopeexcludeslist() != null; 604 } 605 606 private boolean hasFilterRegularExpressionSwitches() { 607 return 608 !getFilterincludes().equals("//") || 609 !getFilterexcludes().equals("") || 610 getPackagefilter() || 611 !getPackagefilterincludes().equals("") || 612 !getPackagefilterexcludes().equals("") || 613 getClassfilter() || 614 !getClassfilterincludes().equals("") || 615 !getClassfilterexcludes().equals("") || 616 getFeaturefilter() || 617 !getFeaturefilterincludes().equals("") || 618 !getFeaturefilterexcludes().equals(""); 619 } 620 621 private boolean hasFilterListSwitches() { 622 return 623 getFilterincludeslist() != null || 624 getFilterexcludeslist() != null; 625 } 626 627 private CollectionSelectionCriteria createCollectionSelectionCriteria(Path includes, Path excludes) throws IOException { 628 return new CollectionSelectionCriteria(loadCollection(includes), loadCollection(excludes)); 629 } 630 631 private TraversalStrategy getStrategy() throws BuildException { 632 return new SelectiveTraversalStrategy(getScopeCriteria(), getFilterCriteria()); 633 } 634 635 private Collection loadCollection(Path path) { 636 Collection result = null; 637 638 if (path != null) { 639 result = new HashSet(); 640 641 String [] filenames = path.list(); 642 for (int i=0; i<filenames.length; i++) { 643 BufferedReader reader = null; 644 String line; 645 646 try { 647 reader = new BufferedReader(new FileReader(filenames[i])); 648 while ((line = reader.readLine()) != null) { 649 result.add(line); 650 } 651 } catch (IOException ex) { 652 Logger.getLogger(getClass()).error("Couldn't read file " + filenames[i], ex); 653 } finally { 654 try { 655 if (reader != null) { 656 reader.close(); 657 } 658 } catch (IOException ex) { 659 Logger.getLogger(getClass()).error("Couldn't close file " + filenames[i], ex); 660 } 661 } 662 } 663 } 664 665 return result; 666 } 667 } 668 | Popular Tags |