O'Reilly logo

Java Extreme Programming Cookbook by Brian M. Coyner, Eric M. Burke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating and Executing a Custom Template

Problem

You want to write and execute a custom XDoclet template file (.xdt).

Solution

Create an .xdt file that contains the necessary XDoclet template tags to generate the desired output. Finally, update your Ant buildfile to execute the template subtask via the xdoclet.DocletTask task.

Discussion

Using XDoclet to generate EJB files is fairly straightforward because the templates are already written. The challenge occurs when you want to create a custom template to generate a specific file. Knowing where to look up information on custom templates and deciphering that information can be a difficult task.

XDoclet templates are at the core of XDoclet’s extensibility, and XDoclet provides a plethora of built-in template tags for us to use. Template tags control how and what information is generated. Template tags are broken down into two categories, block and content.

Block

Block tags are used for iterating and performing logic, which is synonymous with for loops and if statements. The snippet below shows how to iterate through all classes using the built-in Class template tag:

<XDtClass:forAllClasses>    
</XDtClass:forAllClasses>

The next snippet shows how to check if a method contains a specific @ tag using the built-in Method template tag. In this example we are checking for “deprecated”:

<XDtMethod:ifHasMethodTag tagName="deprecated">    
</XDtMethod:ifHasMethodTag>

Content

Content tags are used for outputting information. These tags are synonymous with getter ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required