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 ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.