1 5 package xdoclet; 6 7 import java.io.File ; 8 import java.util.ArrayList ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import org.apache.tools.ant.BuildException; 15 import org.apache.tools.ant.DynamicConfigurator; 16 import org.apache.tools.ant.Project; 17 import org.apache.tools.ant.types.FileSet; 18 19 import xjavadoc.ant.XJavadocTask; 20 21 import xdoclet.loader.ModuleFinder; 22 import xdoclet.loader.SubTaskDefinition; 23 import xdoclet.loader.XDocletModule; 24 25 import xdoclet.util.Translator; 26 27 40 public class DocletTask extends XJavadocTask implements DynamicConfigurator 41 { 42 public final static String XDOCLET_VERSION = "@VERSION@"; 44 45 48 private static Map subtaskNameMap; 49 52 private static Map subtaskMap; 53 54 private List packageSubstitutions = new ArrayList (); 55 56 private boolean isModulesRegistered = false; 57 58 private File destDir; 59 private File mergeDir; 60 private String excludedTags = null; 61 private boolean force = false; 62 private boolean verbose = false; 63 private String addedTags; 64 private List subTasks = new ArrayList (); 65 private List configParams = new ArrayList (); 66 67 public DocletTask() 68 { 69 ModuleFinder.initClasspath(getClass()); 70 } 71 72 public static String getSubTaskName(Class subTaskClass) 73 { 74 return (String ) getSubtaskNameMap().get(subTaskClass); 75 } 76 77 static Map getConfigParamsAsMap(List configParams) 78 { 79 HashMap map = new HashMap (); 80 81 for (Iterator i = configParams.iterator(); i.hasNext(); ) { 82 ConfigParameter cp = (ConfigParameter) i.next(); 83 84 map.put(cp.getName(), cp.getValue()); 85 } 86 return map; 87 } 88 89 static void registerSubTaskName(SubTask subTask, String name) 90 { 91 getSubtaskNameMap().put(subTask.getClass(), name); 92 } 93 94 private static Map getSubtaskMap() 95 { 96 if (subtaskMap == null) 97 subtaskMap = new HashMap (); 98 99 return subtaskMap; 100 } 101 102 private static Map getSubtaskNameMap() 103 { 104 if (subtaskNameMap == null) 105 subtaskNameMap = new HashMap (); 106 107 return subtaskNameMap; 108 } 109 110 115 public List getPackageSubstitutions() 116 { 117 return packageSubstitutions; 118 } 119 120 125 public List getConfigParams() 126 { 127 return configParams; 128 } 129 130 public Map getConfigParamsAsMap() 131 { 132 return getConfigParamsAsMap(getConfigParams()); 133 } 134 135 140 public File getMergeDir() 141 { 142 return mergeDir; 143 } 144 145 150 public String getExcludedTags() 151 { 152 return excludedTags; 153 } 154 155 160 public File getDestDir() 161 { 162 return destDir; 163 } 164 165 170 public boolean isForce() 171 { 172 return force; 173 } 174 175 180 public boolean isVerbose() 181 { 182 return verbose; 183 } 184 185 public String getAddedTags() 186 { 187 return addedTags; 188 } 189 190 196 public void setPackageSubstitutions(List packageSubstitutions) 197 { 198 this.packageSubstitutions = packageSubstitutions; 199 } 200 201 205 public void setDynamicAttribute(String name, String value) 206 { 207 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.ATTRIBUTE_NOT_SUPPORTED, new String []{getTaskName(), name})); 208 } 209 210 217 public void setPackageNames(String src) 218 { 219 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.OBSOLETE_TASK_ATTRIBUTE, new String []{"packageNames"})); 220 } 221 222 229 public void setExcludePackageNames(String src) 230 { 231 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.OBSOLETE_TASK_ATTRIBUTE, new String []{"excludePackageNames"})); 232 } 233 234 244 public void setExcludedTags(String tags) 245 { 246 excludedTags = tags; 247 } 248 249 255 public void setDestDir(File dir) 256 { 257 destDir = dir; 258 } 259 260 266 public void setMergeDir(File dir) 267 { 268 mergeDir = dir; 269 } 270 271 280 public void setForce(boolean force) 281 { 282 this.force = force; 283 } 284 285 290 public void setVerbose(boolean verbose) 291 { 292 this.verbose = verbose; 293 } 294 295 301 public void setAddedTags(String addedTags) 302 { 303 this.addedTags = addedTags; 304 } 305 306 311 public void addPackageSubstitution(xdoclet.tagshandler.PackageTagsHandler.PackageSubstitution ps) 312 { 313 packageSubstitutions.add(ps); 314 } 315 316 321 public void addFileset(FileSet set) 322 { 323 super.addFileset(set); 326 } 327 328 333 public Object createDynamicElement(String name) throws BuildException 334 { 335 if (!isModulesRegistered) { 336 registerModules(); 337 isModulesRegistered = true; 338 } 339 340 SubTask subTask = (SubTask) getSubtaskMap().get(name); 341 342 if (subTask == null) { 343 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.CREATE_TASK_ERROR, new String []{name, getTaskName()})); 344 } 345 subTasks.add(subTask); 346 return subTask; 347 } 348 349 355 public void addSubTask(SubTask subtask) 356 { 357 subTasks.add(subtask); 358 } 359 360 367 public void addTemplate(TemplateSubTask subtask) throws BuildException 368 { 369 if (subtask.getSubTaskClassName() == null) { 370 addSubTask(subtask); 371 } 372 else { 373 try { 374 Class subtaskClass = Class.forName(subtask.getSubTaskClassName()); 375 TemplateSubTask alias = (TemplateSubTask) subtaskClass.newInstance(); 376 377 alias.copyAttributesFrom(subtask); 379 380 addSubTask(alias); 381 } 382 catch (ClassNotFoundException e) { 383 throw new BuildException(Translator.getString(XDocletMessages.class, 384 XDocletMessages.CLASS_NOT_FOUND_EXCEPTION, 385 new String []{subtask.getSubTaskClassName(), e.getMessage()}), e, location); 386 } 387 catch (InstantiationException e) { 388 throw new BuildException(Translator.getString(XDocletMessages.class, 389 XDocletMessages.INSTANTIATION_EXCEPTION, 390 new String []{subtask.getSubTaskClassName(), e.getMessage()}), e, location); 391 } 392 catch (IllegalAccessException e) { 393 throw new BuildException(Translator.getString(XDocletMessages.class, 394 XDocletMessages.ILLEGAL_ACCESS_EXCEPTION, 395 new String []{subtask.getSubTaskClassName(), e.getMessage()}), e, location); 396 } 397 } 398 } 399 400 406 public void addXmlTemplate(XmlSubTask subtask) 407 { 408 addTemplate(subtask); 409 } 410 411 416 public void addConfigParam(ConfigParameter configParam) 417 { 418 configParams.add(configParam); 419 } 420 421 426 protected final List getSubTasks() 427 { 428 return subTasks; 429 } 430 431 437 protected HashMap getConfigParams(List subtasks) 438 { 439 HashMap configs = new HashMap (); 440 441 ConfigParamIntrospector.fillConfigParamsFor(this, configs); 443 444 for (int i = 0; i < subtasks.size(); i++) { 446 SubTask subtask = (SubTask) subtasks.get(i); 447 448 if (subtask != null) { 449 ConfigParamIntrospector.fillConfigParamsFor(subtask, configs); 450 451 fillWithUserDefinedConfigParams(configs, subtask.getConfigParams(), subtask.getSubTaskName() + '.'); 453 } 454 } 455 456 fillWithUserDefinedConfigParams(configs, getConfigParams(), ""); 458 459 return configs; 460 } 461 462 465 protected void start() throws BuildException 466 { 467 try { 468 new XDocletMain().start(getXJavaDoc()); 469 } 470 catch (XDocletException e) { 471 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.XDOCLET_FAILED), e, location); 472 } 473 finally { 474 DocletContext.setSingleInstance(null); 475 483 ModuleFinder.resetFoundModules(); 484 } 485 } 486 487 492 protected void validateOptions() throws BuildException 493 { 494 super.validateOptions(); 495 if (destDir == null) { 496 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.ATTRIBUTE_NOT_PRESENT_ERROR, new String []{"destDir"}), location); 497 } 498 validateSubTasks(); 499 } 500 501 507 protected void checkClass(String className) 508 { 509 try { 510 Class.forName(className); 511 } 512 catch (Exception e) { 513 throw new BuildException(Translator.getString(XDocletMessages.class, XDocletMessages.CHECK_CLASS_FAILED, new String []{className, getTaskName()})); 514 } 515 } 516 517 522 protected void validateSubTasks() throws BuildException 523 { 524 DocletContext context = createContext(); 525 SubTask[] subtasks = context.getSubTasks(); 526 527 for (int i = 0; i < subtasks.length; i++) { 528 SubTask subtask = subtasks[i]; 529 530 if (subtask != null) { 531 log("validating subTask: " + subtask.getSubTaskName() + " class: " + subtask.getClass(), Project.MSG_DEBUG); 532 try { 533 subtask.validateOptions(); 534 } 535 catch (XDocletException ex) { 536 throw new BuildException(subtask.getSubTaskName() + ": " + ex.getMessage(), location); 537 } 538 } 539 } 540 } 541 542 private void registerModules() 543 { 544 List modules = ModuleFinder.findModules(); 546 Iterator i = modules.iterator(); 547 548 while (i.hasNext()) { 549 XDocletModule module = (XDocletModule) i.next(); 550 List subTaskDefinitions = module.getSubTaskDefinitions(); 551 Iterator j = subTaskDefinitions.iterator(); 552 553 while (j.hasNext()) { 554 SubTaskDefinition subTaskDefinition = (SubTaskDefinition) j.next(); 555 556 try { 557 Class parentTaskClass = Class.forName(subTaskDefinition.parentTaskClass); 558 559 if (parentTaskClass.isAssignableFrom(getClass())) { 560 if (getSubtaskMap().containsKey(subTaskDefinition.name)) { 561 String conflictingSubTaskClassName = getSubtaskMap().get(subTaskDefinition.name).getClass().getName(); 562 563 if (!subTaskDefinition.implementationClass.equals(conflictingSubTaskClassName)) { 564 throw new BuildException(Translator.getString(XDocletMessages.class, 567 XDocletMessages.AMBIGUOUS_SUBTASK_DEFINITION, 568 new String []{subTaskDefinition.name, conflictingSubTaskClassName, 569 subTaskDefinition.implementationClass})); 570 } 571 572 continue; 575 } 576 577 Class subTaskClass = Class.forName(subTaskDefinition.implementationClass); 578 SubTask subTask = (SubTask) subTaskClass.newInstance(); 579 580 log("Registering SubTask " + subTaskDefinition.name + " (" + subTaskDefinition.implementationClass + ") to DocletTask " + getClass().getName(), Project.MSG_DEBUG); 581 getSubtaskMap().put(subTaskDefinition.name, subTask); 582 registerSubTaskName(subTask, subTaskDefinition.name); 583 } 584 } 585 catch (ClassNotFoundException e) { 586 throw new BuildException(Translator.getString(XDocletMessages.class, 587 XDocletMessages.DEPENDENT_CLASS_FOR_SUBTASK_NOT_FOUND, 588 new String []{subTaskDefinition.parentTaskClass, ModuleFinder.getClasspath()}), e); 589 } 590 catch (InstantiationException e) { 591 throw new BuildException(Translator.getString(XDocletMessages.class, 592 XDocletMessages.INSTANTIATION_EXCEPTION, 593 new String []{subTaskDefinition.implementationClass, e.getMessage()}), e); 594 } 595 catch (IllegalAccessException e) { 596 throw new BuildException(Translator.getString(XDocletMessages.class, 597 XDocletMessages.ILLEGAL_ACCESS_EXCEPTION, 598 new String []{subTaskDefinition.implementationClass, e.getMessage()}), e); 599 } 600 catch (ClassCastException e) { 601 throw new BuildException(Translator.getString(XDocletMessages.class, 602 XDocletMessages.CLASS_CAST_EXCEPTION, 603 new String []{subTaskDefinition.implementationClass, SubTask.class.getName()}), e); 604 } 605 } 606 } 607 } 608 609 615 private DocletContext createContext() 616 { 617 if (DocletContext.getInstance() != null) { 618 return DocletContext.getInstance(); 619 } 620 621 List subtasks = getSubTasks(); 622 HashMap configs = getConfigParams(subtasks); 623 624 DocletContext context = new DocletContext( 625 this.destDir.toString(), 626 this.mergeDir != null ? this.mergeDir.toString() : null, 627 this.excludedTags, 628 (SubTask[]) subtasks.toArray(new SubTask[0]), 629 project.getProperties(), 630 configs, 631 force, 632 verbose, 633 addedTags 634 ); 635 636 DocletContext.setSingleInstance(context); 638 639 return context; 640 } 641 642 649 private void fillWithUserDefinedConfigParams(HashMap configs, List configParams, String prefix) 650 { 651 for (int i = 0; i < configParams.size(); i++) { 653 ConfigParameter configParam = (ConfigParameter) configParams.get(i); 654 655 configs.put((prefix + configParam.getName()).toLowerCase(), configParam.getValue()); 656 } 657 } 658 659 } 660 | Popular Tags |