1 32 33 package com.jeantessier.dependencyfinder.ant; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.apache.tools.ant.*; 39 import org.apache.tools.ant.types.*; 40 41 import org.xml.sax.*; 42 43 import com.jeantessier.classreader.*; 44 import com.jeantessier.metrics.*; 45 46 public class OOMetrics extends Task { 47 public static final String DEFAULT_PROJECT_NAME = "Project"; 48 public static final String DEFAULT_SORT = "name"; 49 50 private String projectName = DEFAULT_PROJECT_NAME; 51 private File configuration; 52 private boolean csv = false; 53 private boolean txt = false; 54 private boolean xml = false; 55 private boolean validate = false; 56 private String encoding = com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING; 57 private String dtdPrefix = com.jeantessier.metrics.XMLPrinter.DEFAULT_DTD_PREFIX; 58 private String indentText; 59 private boolean projectMetrics = false; 60 private boolean groupMetrics = false; 61 private boolean classMetrics = false; 62 private boolean methodMetrics = false; 63 private Path scopeIncludesList; 64 private Path scopeExcludesList; 65 private Path filterIncludesList; 66 private Path filterExcludesList; 67 private boolean showAllMetrics = false; 68 private boolean showEmptyMetrics = false; 69 private boolean showHiddenMeasurements = false; 70 private String sort = DEFAULT_SORT; 71 private boolean expand = false; 72 private boolean reverse = false; 73 private File destprefix; 74 private Path path; 75 76 public String getProjectname() { 77 return projectName; 78 } 79 80 public void setProjectname(String projectName) { 81 this.projectName = projectName; 82 } 83 84 public File getConfiguration() { 85 return configuration; 86 } 87 88 public void setConfiguration(File configuration) { 89 this.configuration = configuration; 90 } 91 92 public boolean getCsv() { 93 return csv; 94 } 95 96 public void setCsv(boolean csv) { 97 this.csv = csv; 98 } 99 100 public boolean getTxt() { 101 return txt; 102 } 103 104 public void setTxt(boolean txt) { 105 this.txt = txt; 106 } 107 108 public boolean getXml() { 109 return xml; 110 } 111 112 public void setXml(boolean xml) { 113 this.xml = xml; 114 } 115 116 public boolean getValidate() { 117 return validate; 118 } 119 120 public void setValidate(boolean validate) { 121 this.validate = validate; 122 } 123 124 public String getEncoding() { 125 return encoding; 126 } 127 128 public void setEncoding(String encoding) { 129 this.encoding = encoding; 130 } 131 132 public String getDtdprefix() { 133 return dtdPrefix; 134 } 135 136 public void setDtdprefix(String dtdPrefix) { 137 this.dtdPrefix = dtdPrefix; 138 } 139 140 public String getIndenttext() { 141 return indentText; 142 } 143 144 public void setIntenttext(String indentText) { 145 this.indentText = indentText; 146 } 147 148 public boolean getProjectmetrics() { 149 return projectMetrics; 150 } 151 152 public void setProjectmetrics(boolean projectMetrics) { 153 this.projectMetrics = projectMetrics; 154 } 155 156 public boolean getGroupmetrics() { 157 return groupMetrics; 158 } 159 160 public void setGroupmetrics(boolean groupMetrics) { 161 this.groupMetrics = groupMetrics; 162 } 163 164 public boolean getClassmetrics() { 165 return classMetrics; 166 } 167 168 public void setClassmetrics(boolean classMetrics) { 169 this.classMetrics = classMetrics; 170 } 171 172 public boolean getMethodmetrics() { 173 return methodMetrics; 174 } 175 176 public void setMethodmetrics(boolean methodMetrics) { 177 this.methodMetrics = methodMetrics; 178 } 179 180 public void setAllmetrics(boolean allMetrics) { 181 setProjectmetrics(allMetrics); 182 setGroupmetrics(allMetrics); 183 setClassmetrics(allMetrics); 184 setMethodmetrics(allMetrics); 185 } 186 187 public Path createScopeincludeslist() { 188 if (scopeIncludesList == null) { 189 scopeIncludesList = new Path(getProject()); 190 } 191 192 return scopeIncludesList; 193 } 194 195 public Path getScopeincludeslist() { 196 return scopeIncludesList; 197 } 198 199 public Path createScopeexcludeslist() { 200 if (scopeExcludesList == null) { 201 scopeExcludesList = new Path(getProject()); 202 } 203 204 return scopeExcludesList; 205 } 206 207 public Path getScopeexcludeslist() { 208 return scopeExcludesList; 209 } 210 211 public Path createFilterincludeslist() { 212 if (filterIncludesList == null) { 213 filterIncludesList = new Path(getProject()); 214 } 215 216 return filterIncludesList; 217 } 218 219 public Path getFilterincludeslist() { 220 return filterIncludesList; 221 } 222 223 public Path createFilterexcludeslist() { 224 if (filterExcludesList == null) { 225 filterExcludesList = new Path(getProject()); 226 } 227 228 return filterExcludesList; 229 } 230 231 public Path getFilterexcludeslist() { 232 return filterExcludesList; 233 } 234 235 public boolean getShowallmetrics() { 236 return showAllMetrics; 237 } 238 239 public void setShowallmetrics(boolean showAllMetrics) { 240 this.showAllMetrics = showAllMetrics; 241 } 242 243 public boolean getShowemptymetrics() { 244 return showEmptyMetrics; 245 } 246 247 public void setShowemptymetrics(boolean showEmptyMetrics) { 248 this.showEmptyMetrics = showEmptyMetrics; 249 } 250 251 public boolean getShowhiddenmeasurements() { 252 return showHiddenMeasurements; 253 } 254 255 public void setShowhiddenmeasurements(boolean showHiddenMeasurements) { 256 this.showHiddenMeasurements = showHiddenMeasurements; 257 } 258 259 public String getSort() { 260 return sort; 261 } 262 263 public void setSort(String sort) { 264 this.sort = sort; 265 } 266 267 public boolean getExpand() { 268 return expand; 269 } 270 271 public void setExpand(boolean expand) { 272 this.expand = expand; 273 } 274 275 public boolean getReverse() { 276 return reverse; 277 } 278 279 public void setReverse(boolean reverse) { 280 this.reverse = reverse; 281 } 282 283 public File getDestprefix() { 284 return destprefix; 285 } 286 287 public void setDestprefix(File destprefix) { 288 this.destprefix = destprefix; 289 } 290 291 public Path createPath() { 292 if (path == null) { 293 path = new Path(getProject()); 294 } 295 296 return path; 297 } 298 299 public Path getPath() { 300 return path; 301 } 302 303 public void execute() throws BuildException { 304 306 if (getConfiguration() == null) { 307 throw new BuildException("configuration must be set!"); 308 } 309 310 if (!getConfiguration().exists()) { 311 throw new BuildException("configuration does not exist!"); 312 } 313 314 if (!getConfiguration().isFile()) { 315 throw new BuildException("configuration is not a file!"); 316 } 317 318 if (getPath() == null) { 319 throw new BuildException("path must be set!"); 320 } 321 322 if (getDestprefix() == null) { 323 throw new BuildException("destprefix must be set!"); 324 } 325 326 try { 327 log("Reading classes from path " + getPath()); 328 329 VerboseListener verboseListener = new VerboseListener(this); 330 331 MetricsFactory factory = new MetricsFactory(getProjectname(), new MetricsConfigurationLoader(getValidate()).load(getConfiguration().getAbsolutePath())); 332 333 ClassfileLoader loader = new AggregatingClassfileLoader(); 334 loader.addLoadListener(verboseListener); 335 loader.load(Arrays.asList(getPath().list())); 336 337 com.jeantessier.metrics.MetricsGatherer gatherer = new com.jeantessier.metrics.MetricsGatherer(projectName, factory); 338 gatherer.addMetricsListener(verboseListener); 339 if (getScopeincludeslist() != null || getScopeexcludeslist() != null) { 340 gatherer.setScopeIncludes(createCollection(getScopeincludeslist(), getScopeexcludeslist())); 341 } 342 if (getFilterincludeslist() != null || getFilterexcludeslist() != null) { 343 gatherer.setFilterIncludes(createCollection(getFilterincludeslist(), getFilterexcludeslist())); 344 } 345 gatherer.visitClassfiles(loader.getAllClassfiles()); 346 347 if (getShowallmetrics()) { 348 Iterator i; 349 350 i = gatherer.getMetricsFactory().getAllClassMetrics().iterator(); 351 while (i.hasNext()) { 352 gatherer.getMetricsFactory().includeClassMetrics((Metrics) i.next()); 353 } 354 355 i = gatherer.getMetricsFactory().getAllMethodMetrics().iterator(); 356 while (i.hasNext()) { 357 gatherer.getMetricsFactory().includeMethodMetrics((Metrics) i.next()); 358 } 359 } 360 361 if (getCsv()) { 362 printCSVFiles(gatherer.getMetricsFactory()); 363 } else if (getTxt()) { 364 printTextFile(gatherer.getMetricsFactory()); 365 } else if (getXml()) { 366 printXMLFile(gatherer.getMetricsFactory()); 367 } 368 } catch (SAXException ex) { 369 throw new BuildException(ex); 370 } catch (IOException ex) { 371 throw new BuildException(ex); 372 } 373 } 374 375 private Collection createCollection(Path includes, Path excludes) throws IOException { 376 Collection result = new HashSet(); 377 378 if (includes != null) { 379 String [] filenames = includes.list(); 380 for (int i=0; i<filenames.length; i++) { 381 BufferedReader reader = new BufferedReader(new FileReader(filenames[i])); 382 String line; 383 while ((line = reader.readLine()) != null) { 384 result.add(line); 385 } 386 reader.close(); 387 } 388 } 389 390 if (excludes != null) { 391 String [] filenames = excludes.list(); 392 for (int i=0; i<filenames.length; i++) { 393 BufferedReader reader = new BufferedReader(new FileReader(filenames[i])); 394 String line; 395 while ((line = reader.readLine()) != null) { 396 result.remove(line); 397 } 398 reader.close(); 399 } 400 } 401 402 return result; 403 } 404 405 private void printCSVFiles(MetricsFactory factory) throws IOException { 406 MetricsComparator comparator = new MetricsComparator(getSort()); 407 if (getReverse()) { 408 comparator.reverse(); 409 } 410 411 List metrics; 412 Iterator i; 413 com.jeantessier.metrics.Printer printer; 414 415 if (getProjectmetrics()) { 416 String filename = getDestprefix().getAbsolutePath() + "_project.csv"; 417 log("Saving metrics to " + filename); 418 419 PrintWriter out = new PrintWriter(new FileWriter(filename)); 420 421 metrics = new ArrayList(factory.getProjectMetrics()); 422 Collections.sort(metrics, comparator); 423 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getProjectMeasurements()); 424 printer.setShowEmptyMetrics(getShowemptymetrics()); 425 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 426 if (getIndenttext() != null) { 427 printer.setIndentText(getIndenttext()); 428 } 429 430 printer.visitMetrics(metrics); 431 432 out.close(); 433 } 434 435 if (getGroupmetrics()) { 436 String filename = getDestprefix().getAbsolutePath() + "_groups.csv"; 437 log("Saving metrics to " + filename); 438 439 PrintWriter out = new PrintWriter(new FileWriter(filename)); 440 441 metrics = new ArrayList(factory.getGroupMetrics()); 442 Collections.sort(metrics, comparator); 443 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getGroupMeasurements()); 444 printer.setShowEmptyMetrics(getShowemptymetrics()); 445 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 446 if (getIndenttext() != null) { 447 printer.setIndentText(getIndenttext()); 448 } 449 450 printer.visitMetrics(metrics); 451 452 out.close(); 453 } 454 455 if (getClassmetrics()) { 456 String filename = getDestprefix().getAbsolutePath() + "_classes.csv"; 457 log("Saving metrics to " + filename); 458 459 PrintWriter out = new PrintWriter(new FileWriter(filename)); 460 461 metrics = new ArrayList(factory.getClassMetrics()); 462 Collections.sort(metrics, comparator); 463 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getClassMeasurements()); 464 printer.setShowEmptyMetrics(getShowemptymetrics()); 465 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 466 if (getIndenttext() != null) { 467 printer.setIndentText(getIndenttext()); 468 } 469 470 printer.visitMetrics(metrics); 471 472 out.close(); 473 } 474 475 if (getMethodmetrics()) { 476 String filename = getDestprefix().getAbsolutePath() + "_methods.csv"; 477 log("Saving metrics to " + filename); 478 479 PrintWriter out = new PrintWriter(new FileWriter(filename)); 480 481 metrics = new ArrayList(factory.getMethodMetrics()); 482 Collections.sort(metrics, comparator); 483 printer = new com.jeantessier.metrics.CSVPrinter(out, factory.getConfiguration().getMethodMeasurements()); 484 printer.setShowEmptyMetrics(getShowemptymetrics()); 485 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 486 if (getIndenttext() != null) { 487 printer.setIndentText(getIndenttext()); 488 } 489 490 printer.visitMetrics(metrics); 491 492 out.close(); 493 } 494 } 495 496 private void printTextFile(MetricsFactory factory) throws IOException { 497 MetricsComparator comparator = new MetricsComparator(getSort()); 498 if (getReverse()) { 499 comparator.reverse(); 500 } 501 502 String filename = getDestprefix().getAbsolutePath() + ".txt"; 503 log("Saving metrics to " + filename); 504 505 PrintWriter out = new PrintWriter(new FileWriter(filename)); 506 507 List metrics; 508 Iterator i; 509 510 if (getProjectmetrics()) { 511 out.println("Project metrics"); 512 out.println("---------------"); 513 metrics = new ArrayList(factory.getProjectMetrics()); 514 Collections.sort(metrics, comparator); 515 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getProjectMeasurements()); 516 printer.setShowEmptyMetrics(getShowemptymetrics()); 517 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 518 printer.setExpandCollectionMeasurements(getExpand()); 519 if (getIndenttext() != null) { 520 printer.setIndentText(getIndenttext()); 521 } 522 523 printer.visitMetrics(metrics); 524 525 out.println(); 526 } 527 528 if (getGroupmetrics()) { 529 out.println("Group metrics"); 530 out.println("-------------"); 531 metrics = new ArrayList(factory.getGroupMetrics()); 532 Collections.sort(metrics, comparator); 533 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getGroupMeasurements()); 534 printer.setShowEmptyMetrics(getShowemptymetrics()); 535 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 536 printer.setExpandCollectionMeasurements(getExpand()); 537 if (getIndenttext() != null) { 538 printer.setIndentText(getIndenttext()); 539 } 540 541 printer.visitMetrics(metrics); 542 543 out.println(); 544 } 545 546 if (getClassmetrics()) { 547 out.println("Class metrics"); 548 out.println("-------------"); 549 metrics = new ArrayList(factory.getClassMetrics()); 550 Collections.sort(metrics, comparator); 551 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getClassMeasurements()); 552 printer.setShowEmptyMetrics(getShowemptymetrics()); 553 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 554 printer.setExpandCollectionMeasurements(getExpand()); 555 if (getIndenttext() != null) { 556 printer.setIndentText(getIndenttext()); 557 } 558 559 printer.visitMetrics(metrics); 560 561 out.println(); 562 } 563 564 if (getMethodmetrics()) { 565 out.println("Method metrics"); 566 out.println("--------------"); 567 metrics = new ArrayList(factory.getMethodMetrics()); 568 Collections.sort(metrics, comparator); 569 com.jeantessier.metrics.TextPrinter printer = new com.jeantessier.metrics.TextPrinter(out, factory.getConfiguration().getMethodMeasurements()); 570 printer.setShowEmptyMetrics(getShowemptymetrics()); 571 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 572 printer.setExpandCollectionMeasurements(getExpand()); 573 if (getIndenttext() != null) { 574 printer.setIndentText(getIndenttext()); 575 } 576 577 printer.visitMetrics(metrics); 578 579 out.println(); 580 } 581 582 out.close(); 583 } 584 585 private void printXMLFile(MetricsFactory factory) throws IOException { 586 MetricsComparator comparator = new MetricsComparator(getSort()); 587 if (getReverse()) { 588 comparator.reverse(); 589 } 590 591 String filename = getDestprefix().getAbsolutePath() + ".xml"; 592 log("Saving metrics to " + filename); 593 594 PrintWriter out = new PrintWriter(new FileWriter(filename)); 595 596 List metrics = new ArrayList(factory.getProjectMetrics()); 597 Collections.sort(metrics, comparator); 598 com.jeantessier.metrics.Printer printer = new com.jeantessier.metrics.XMLPrinter(out, factory.getConfiguration(), getEncoding(), getDtdprefix()); 599 printer.setShowEmptyMetrics(getShowemptymetrics()); 600 printer.setShowHiddenMeasurements(getShowhiddenmeasurements()); 601 if (getIndenttext() != null) { 602 printer.setIndentText(getIndenttext()); 603 } 604 605 printer.visitMetrics(metrics); 606 607 out.close(); 608 } 609 } 610 | Popular Tags |