Geertjan's Blog

  • May 3, 2005

Generate TLDs in NetBeans IDE 4.1 with XDoclet

Geertjan Wielenga
Product Manager
Follow the Java trail in the NetBeans IDE 4.1 Tag Library Tutorial. At the end of the Java trail, you should have two taghandlers (i.e., written in Java, not in JSP syntax) called DisplayLogoTag.java and FormatTextTag.java, together with a tag library descriptor (TLD) file called SimpleTagLib.tld. That's how the tutorial ends, more or less, but in real life your tag handlers often change and evolve so that it can be time-consuming and boring to manually re-synchronize them with the TLD. Hence, to automate the process, we can use XDoclet from within the IDE to re-generate the TLD after changes have been made to the tag handlers. We can also add a menu item to our IDE project (but only if our IDE project is a free-form project) so that we can easily invoke the re-generation process.

Fast Track: Too lazy to create the application yourself? Or maybe you just want to see how TLD re-generation works, without creating the application. Either way, you can download the application here. And then open it in the IDE and take all the steps below to quickly set up the re-generation functionality.

  1. Download and extract XDoclet

  2. Add XDoclet tags to the DisplayLogoTag.java tag handler.

    • Add this in the comment block directly above the class declaration (i.e., NOT above the package declaration):

      \* @jsp.tag name="DisplayLogoTag" bodycontent="empty"
    • Add this in the comment block directly above the setSize method:

      \* @jsp.attribute name="size" required="true" rtexprvalue="true" type="java.lang.String"
  3. Add XDoclet tags to the FormatTextTag.java tag handler.

    • Add this in the comment block directly above the class declaration (i.e., NOT above the package declaration):

      \* @jsp.tag name="FormatTextTag" bodycontent="scriptless"
    • Add this in the comment block directly above the setStyle method:

      \* @jsp.attribute name="style" required="true" rtexprvalue="true" type="java.lang.String"
  4. Create the webdoclet-based target in your project's build.xml file.

     <target name="webdoclet-tld" depends="init, init-xdoclet" 
    description="Regenerate TLD">
    <echo>| |</echo>
    <echo>| C R E A T I N G T L D |</echo>
    <echo>| |</echo>
    <webdoclet destdir="${web.docbase.dir}/WEB-INF"
    <fileset dir="${src.dir}">
    <include name="\*\*/\*Tag.java"/>
    <jsptaglib Jspversion="2.0" taglibversion="1.0" shortName="simpletaglib"
    filename="SimpleTagLib.tld" validateXML="true"/>

    If you add this target to a different Ant file, you can import it into your project's own build.xml file by including this line at the top of your build.xml file:

    <import file="path-to-external-Ant-file/name-of-Ant-file.xml"/>
  5. Optionally, you can set up a contextual menu for the project to invoke this target. But this is possible for free-form projects only. Alternatively, maybe you want to create a target that re-generates all the TLDs in all the projects in the IDE. In that case, you can add a menu item or toolbar button to the IDE itself (not to an individual project) to invoke the target.
  6. Now, when you invoke the webdoclet-tld target, Ant picks up all the information from the XDoclet tags that you've defined in the tag handlers. If, for example, you change a tag handler so that its body content is "scriptless" instead of "empty", you need only change the bodycontent value of the @jsp.tag XDoclet tag, run the webdoclet-tld target, and the TLD is re-generated with the updated information.

Note that XDoclet currently does not support JSP 2.0. Therefore, if your tag handlers extend SimpleTagSupport, XDoclet will not generate the TLD file correctly. Therefore, just before you invoke the target, change the SimpleTagSupport to TagSupport, and then change it back after you've finished re-generating the TLD.

Join the discussion

Comments ( 2 )
  • Anatoly Saturday, May 14, 2005
    Dear Geertjan,
    Thank you very much for this article.
    However, I got the following question.
    To what specific directory within NetBeans I need to extract XDoclet?
    Thank you very much.
  • guest Tuesday, May 17, 2005
    Anywhere you like! Once you've extracted it, create a property like this in your project.properties file:
    Check out Brian's article, because this is where I learnt everything I know on XDoclet:
    Hope this helps, if not please leave another comment!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.