1 5 package xdoclet.tagshandler; 6 7 import java.text.DateFormat ; 8 import java.util.Calendar ; 9 10 import java.util.Collection ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 import java.util.Set ; 15 import java.util.StringTokenizer ; 16 17 import org.apache.commons.logging.Log; 18 19 import xjavadoc.*; 20 21 import xdoclet.*; 22 import xdoclet.template.*; 23 import xdoclet.util.LogUtil; 24 import xdoclet.util.Translator; 25 import xdoclet.util.TypeConversionUtil; 26 27 35 public class ClassTagsHandler extends AbstractProgramElementTagsHandler 36 { 37 40 protected final static DateFormat dateFormatter = DateFormat.getDateTimeInstance(); 41 42 protected final static Calendar now = Calendar.getInstance(); 43 44 52 public static String getClassNameFor(XClass clazz) 53 { 54 return clazz.getName(); 55 } 56 57 64 public static String getFullClassNameFor(XClass clazz) 65 { 66 if (clazz.getContainingClass() != null) { 67 return clazz.getContainingClass().getQualifiedName(); 68 } 69 return clazz.getQualifiedName(); 70 } 71 72 84 public static void forAllDistinctClassTags(TemplateEngine engine, String template, String tagName, String paramName) throws XDocletException 85 { 86 Set tagKeys = new HashSet (); 88 Log log = LogUtil.getLog(ClassTagsHandler.class, "forAllDistinctClassTags"); 89 90 try { 91 92 for (Iterator i = getXJavaDoc().getSourceClasses().iterator(); i.hasNext(); ) { 93 XClass clazz = (XClass) i.next(); 94 95 setCurrentClass(clazz); 96 97 Collection tags = clazz.getDoc().getTags(tagName); 98 99 if (tags == null) 100 continue; 101 102 for (Iterator j = tags.iterator(); j.hasNext(); ) { 103 XTag tag = (XTag) j.next(); 104 105 setCurrentClassTag(tag); 106 if (!tagKeys.contains(tag.getAttributeValue(paramName))) { 107 tagKeys.add(tag.getAttributeValue(paramName)); 108 engine.generate(template); 109 } 110 else { 111 log.warn(Translator.getString(XDocletMessages.class, XDocletMessages.DUPLICATED_TAG, 112 new String []{tagName, paramName, tag.getAttributeValue(paramName)})); 113 } 114 } 115 } 116 } 117 catch (TemplateException e) { 118 if (e instanceof XDocletException) { 119 throw (XDocletException) e; 120 } 121 else { 122 throw new XDocletException(e, Translator.getString(XDocletMessages.class, XDocletMessages.RUNNING_FAILED)); 123 } 124 } 125 } 126 127 141 public void forAllDistinctClassTags(String template, Properties attributes) throws XDocletException 142 { 143 String tagName = attributes.getProperty("tagName"); 144 String paramName = attributes.getProperty("paramName"); 145 146 forAllDistinctClassTags(getEngine(), template, tagName, paramName); 147 } 148 149 156 public String className() throws XDocletException 157 { 158 return getCurrentClass().getName(); 159 } 160 161 168 public String fullClassName() throws XDocletException 169 { 170 return getCurrentClass().getQualifiedName(); 171 } 172 173 180 public String transformedClassName() throws XDocletException 181 { 182 return getCurrentClass().getTransformedName(); 183 } 184 185 192 public String fullTransformedClassName() throws XDocletException 193 { 194 return getCurrentClass().getTransformedQualifiedName(); 195 } 196 197 204 public String fullSuperclassName() throws XDocletException 205 { 206 return getFullSuperclassNameFor(getCurrentClass()); 207 } 208 209 216 public void classOf(String template) throws XDocletException 217 { 218 try { 219 String fullClassName = getEngine().outputOf(template); 220 221 String result = fullClassName; 222 int pos = fullClassName.lastIndexOf('.'); 223 224 if (pos >= 0) { 225 result = fullClassName.substring(pos + 1); 226 } 227 228 getEngine().print(result); 229 } 231 catch (TemplateException ex) { 232 throw new XDocletException(ex, Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_FAILED, new String []{"ClassTagsHandler.classOf"})); 233 } 234 } 235 236 244 public void ifIsClassAbstract(String template) throws XDocletException 245 { 246 if (getCurrentClass().isAbstract()) { 247 generate(template); 248 } 249 } 250 251 259 public void ifIsClassNotAbstract(String template) throws XDocletException 260 { 261 if (!getCurrentClass().isAbstract()) { 262 generate(template); 263 } 264 } 265 266 277 public void pushClass(String template, Properties attributes) throws XDocletException 278 { 279 String value = attributes.getProperty("value", null); 280 XClass currentClass = null; 281 282 if (value == null) { 283 try { 284 value = getEngine().outputOf(template); 285 } 286 catch (TemplateException ex) { 287 throw new XDocletException(ex, Translator.getString(XDocletMessages.class, XDocletMessages.METHOD_FAILED, new String []{"pushClass"})); 288 } 289 } 290 291 currentClass = getXJavaDoc().getXClass(value.replace('$', '.')); 292 293 if (currentClass == null) { 294 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.JAVADOC_COULDNT_LOAD_CLASS, new String []{value})); 295 } 296 297 XMethod oldMethod = getCurrentMethod(); 298 299 pushCurrentClass(currentClass); 300 setCurrentMethod(null); 301 generate(template); 302 popCurrentClass(); 303 setCurrentMethod(oldMethod); 304 } 305 306 322 public void forAllClasses(String template, Properties attributes) throws XDocletException 323 { 324 Log log = LogUtil.getLog(ClassTagsHandler.class, "forAllClasses"); 325 326 String abstractStr = attributes.getProperty("abstract"); 327 boolean acceptAbstractClasses = TypeConversionUtil.stringToBoolean(abstractStr, true); 328 String typeName = attributes.getProperty("type"); 329 String extentStr = attributes.getProperty("extent"); 330 int extent = TypeTagsHandler.extractExtentType(extentStr); 331 332 if (log.isDebugEnabled()) { 333 log.debug("acceptAbstractClasses=" + acceptAbstractClasses); 334 log.debug("typeName=" + typeName); 335 log.debug("extentStr=" + extentStr); 336 log.debug("extent=" + extent); 337 } 338 339 for (Iterator i = getAllClasses().iterator(); i.hasNext(); ) { 340 XClass currentClass = (XClass) i.next(); 341 342 setCurrentClass(currentClass); 343 log.debug("currentClass=" + currentClass); 344 345 if (DocletSupport.isDocletGenerated(getCurrentClass()) || (getCurrentClass().isAbstract() && acceptAbstractClasses == false)) { 346 log.debug("isDocletGenerated or isAbstract"); 347 continue; 348 } 349 350 if (typeName != null) { 351 if (TypeTagsHandler.isOfType(currentClass, typeName, extent)) { 352 log.debug("isOfType true, generate()."); 353 generate(template); 354 } 355 else { 356 log.debug("isOfType false"); 357 } 358 } 359 else { 360 log.debug("typeName=null, generate()."); 361 generate(template); 362 } 363 } 364 } 365 366 373 public String modifiers() throws XDocletException 374 { 375 return modifiers(FOR_CLASS); 376 } 377 378 385 public String symbolicClassName() throws XDocletException 386 { 387 return getCurrentClass().getName(); 389 } 390 391 407 public void ifDoesntHaveClassTag(String template, Properties attributes) throws XDocletException 408 { 409 if (!hasTag(attributes, FOR_CLASS)) { 410 generate(template); 411 } 412 else { 413 String error = attributes.getProperty("error"); 414 415 if (error != null) { 416 getEngine().print(error); 417 } 418 } 419 } 420 421 437 public void ifHasClassTag(String template, Properties attributes) throws XDocletException 438 { 439 if (hasTag(attributes, FOR_CLASS)) { 440 generate(template); 441 } 442 else { 443 String error = attributes.getProperty("error"); 444 445 if (error != null) { 446 getEngine().print(error); 447 } 448 } 449 } 450 451 467 public void ifClassTagValueMatches(String template, Properties attributes) throws XDocletException 468 { 469 String wantedTagValue = getTagValue(attributes, FOR_CLASS); 470 471 if (wantedTagValue.equals(matchPattern)) { 472 generate(template); 473 } 474 } 475 476 490 public void ifClassTagValueEquals(String template, Properties attributes) throws XDocletException 491 { 492 if (isTagValueEqual(attributes, FOR_CLASS)) { 493 generate(template); 494 } 495 } 496 497 511 public void ifClassTagValueNotEquals(String template, Properties attributes) throws XDocletException 512 { 513 if (!isTagValueEqual(attributes, FOR_CLASS)) { 514 generate(template); 515 } 516 } 517 518 539 public String classTagValue(Properties attributes) throws XDocletException 540 { 541 return getExpandedDelimitedTagValue(attributes, FOR_CLASS); 542 } 543 544 559 public String classTagValueMatch(Properties attributes) throws XDocletException 560 { 561 matchPattern = getTagValue(attributes, FOR_CLASS); 562 return ""; 563 } 564 565 578 public void forAllClassTags(String template, Properties attributes) throws XDocletException 579 { 580 boolean superclasses = TypeConversionUtil.stringToBoolean(attributes.getProperty("superclasses"), true); 581 Collection tags = getCurrentClass().getDoc().getTags(attributes.getProperty("tagName"), superclasses); 582 Set done = new HashSet (); 583 584 matchPattern = null; 585 586 String tagKey = attributes.getProperty("tagKey"); 587 588 for (Iterator i = tags.iterator(); i.hasNext(); ) { 589 XTag tag = (XTag) i.next(); 590 591 if (tagKey != null) { 592 String key = tag.getAttributeValue(tagKey); 593 594 if (!done.add(key)) { 595 continue; 597 } 598 } 599 600 setCurrentClassTag(tag); 601 602 generate(template); 603 } 604 setCurrentClassTag(null); 605 matchPattern = null; 606 } 607 608 624 public void forAllClassTagTokens(String template, Properties attributes) throws XDocletException 625 { 626 forAllMemberTagTokens(template, attributes, FOR_CLASS); 627 } 628 629 644 public String classComment(Properties attributes) throws XDocletException 645 { 646 boolean noCommentSigns = TypeConversionUtil.stringToBoolean(attributes.getProperty("no-comment-signs"), false); 649 650 if (noCommentSigns) { 651 return getCurrentClass().getDoc().getCommentText(); 652 } 653 654 char[] spaces = getIndentChars(attributes); 655 StringBuffer sbuf = new StringBuffer (); 656 657 sbuf.append(spaces).append("/**").append(PrettyPrintWriter.LINE_SEPARATOR); 658 sbuf.append(classCommentText(attributes)); 659 sbuf.append(classCommentTags(attributes)); 660 sbuf.append(spaces).append(" */"); 661 662 return sbuf.toString(); 663 } 664 665 680 public String classCommentText(Properties attributes) throws XDocletException 681 { 682 boolean noCommentSigns = TypeConversionUtil.stringToBoolean(attributes.getProperty("no-comment-signs"), false); 683 684 if (noCommentSigns) { 685 return getCurrentClass().getDoc().getCommentText(); 686 } 687 688 char[] spaces = getIndentChars(attributes); 689 StringBuffer sbuf = new StringBuffer (); 690 691 sbuf.append(spaces).append(" * ").append(getCurrentClass().getDoc().getCommentText()).append(PrettyPrintWriter.LINE_SEPARATOR); 692 693 return sbuf.toString(); 694 } 695 696 708 public String classCommentTags(Properties attributes) throws XDocletException 709 { 710 char[] spaces = getIndentChars(attributes); 711 StringBuffer sbuf = new StringBuffer (); 712 Collection classTags = getCurrentClass().getDoc().getTags(); 713 714 for (Iterator i = classTags.iterator(); i.hasNext(); ) { 715 XTag tag = (XTag) i.next(); 716 717 String classTag = tag.getName(); 718 719 if (classTag.indexOf(':') == -1 && classTag.indexOf('.') == -1) { 721 if (getDocletContext().getExcludedTags().indexOf(classTag) == -1) { 723 sbuf.append(spaces).append(" * @").append(classTag).append(' '); 724 sbuf.append(tag.getValue()).append(PrettyPrintWriter.LINE_SEPARATOR); 725 } 726 } 727 } 728 729 if (getDocletContext().getAddedTags() != null) { 730 StringTokenizer st = new StringTokenizer (getDocletContext().getAddedTags(), ","); 731 732 while (st.hasMoreTokens()) { 733 sbuf.append(spaces).append(" * ").append(st.nextToken()).append(PrettyPrintWriter.LINE_SEPARATOR); 734 } 735 } 736 737 return sbuf.toString(); 738 } 739 740 750 public String firstSentenceDescription(Properties attributes) throws XDocletException 751 { 752 String desc = getCurrentClass().getDoc().getFirstSentence(); 753 754 if (desc == null) { 755 String noDescriptionIfLackingStr = attributes.getProperty("no-description-if-lacking"); 756 boolean noDescriptionIfLacking = TypeConversionUtil.stringToBoolean(noDescriptionIfLackingStr, true); 757 758 if (noDescriptionIfLacking) { 759 desc = Translator.getString(XDocletMessages.class, XDocletMessages.NO_DESCRIPTION); 760 } 761 else { 762 desc = ""; 763 } 764 } 765 766 return checkForWrap(desc.trim()); 768 } 769 770 781 public String importedList(Properties attributes) throws XDocletException 782 { 783 String currentClass = attributes.getProperty("currentClass"); 784 785 if (currentClass == null) { 786 throw new XDocletException(Translator.getString(XDocletMessages.class, XDocletMessages.TAG_MUST_INCLUDE_A_PROPERTY, new String []{"importList", "currentClass"})); 787 } 788 789 String currentPackage = PackageTagsHandler.getPackageNameFor(currentClass); 790 791 StringBuffer sbuf = new StringBuffer (); 792 793 Collection packages = getCurrentClass().getImportedPackages(); 794 795 for (Iterator i = packages.iterator(); i.hasNext(); ) { 796 XPackage pakkage = (XPackage) i.next(); 797 798 if (!pakkage.getName().equals(currentPackage)) { 799 sbuf.append("import ").append(pakkage.getName()).append(".*;").append(PrettyPrintWriter.LINE_SEPARATOR); 800 } 801 } 802 803 for (Iterator j = getCurrentClass().getImportedClasses().iterator(); j.hasNext(); ) { 804 XClass clazz = (XClass) j.next(); 805 806 if (!PackageTagsHandler.getPackageNameFor(clazz.toString()).equals(currentPackage)) { 807 sbuf.append("import ").append(clazz.toString()).append(';').append(PrettyPrintWriter.LINE_SEPARATOR); 808 } 809 } 810 811 return sbuf.toString(); 812 } 813 } 814 | Popular Tags |