Custom XDoclet labels automatically generate framework configurations

Source: Internet
Author: User
Tags manual writing

XDoclet is a tool that generates a specified file by reading specific labels in the Java source file. The XDoclet tag itself provides some common labels, such as @ EJB, @ hibernate, and @ Web, but it still cannot meet our needs.
  
For example, a javascript verification framework is referenced in our latest project. By configuring a specific xml configuration file, you can complete form verification on the client side, but developers do not want to learn a framework any more, so I want developers to write tags such as @ Javascript in the source code and generate their configuration files.
  
Javascript client verification has always been a headache in Web development. It is often because every page is filled with similar or even identical verification code. How can we manage these codes in a unified and effective manner, and how to achieve code page separation has not been a good solution.
  
Using this verification framework, client development becomes the file of configuring validation-config.xml so easily, this article does not intend to detail how to use the jsvalidation framework, interested friends can go to the http://www.cosoft.org.cn/projects/jsvalidation jsvalidation official website to learn.
  
Although this framework can be used to write client-side Verification Code and provide a DTD file for XML files, manual Writing of XML files is still very error-prone and inefficient, in addition, new developers must master a new framework, which is not suitable for beginners.
  
Most frameworks such as Struts and JSF need to write something similar to formbean for forms. Take JSF as an example, if there is a text box <input type = "text" name = "txtusername"/> in the form, the corresponding page class or formbean class should have the following code:
Private htmlinputtext txtusername = new htmlinputtext ();
/**
* @ Return Username
*/
Public htmlinputtext gettxtusername ()
{
Return txtusername;
}
/**
* @ Param text
*/
Public void settxtteaname (htmlinputtext text)
{
Txtteaname = text;
}
If you can use XDoclet, You can automatically generate the jsvalidation configuration file, which is also conducive to training new developers and then adding ant tasks to form daily building. The possible code is as follows:
/**
* @ Javascript. Field
* Name = "frmzbaddglobalpage: txtteaname"
* Display-name = "User Name"
*
* @ Javascript. depend
* Name = "required"
*
* @ Return Username
*/
Public htmlinputtext gettxtusername ()
{
Return txtusername;
}
By analyzing some of the tag packages that XDoclet comes with, it is found that you only need to provide three files to implement custom XDoclet labels: A class inherited from xmlsubtask, A class inherited from doclettask (used for ant) and an xdt template language file.
  
In the xmlsubtask class, first define the Template File Name:
Private Static string default_template_file =
"Resources/validation-config.xdt ";
Define the DTD file name:
Private Final Static string dtd_file_name_20 =
"Resources/validation-config.dtd ";
Define the configuration file name to be generated:
Private Static string generated_file_name = "validation-config.xml ";
  
Then, you only need to combine the three files. The detailed code is as follows:
Public writable criptsubtask ()
{
Settemplateurl (getclass (). getresource (default_template_file ));
Setdestinationfile (generated_file_name );
}
  
Public void execute () throws xdocletexception
{
Setdtdurl (getclass (). getresource (dtd_file_name_20 ));
Startprocess ();
}
  
Protected void enginestarted () throws xdocletexception
{
System. Out. println (
Translator. getstring (
Xdocletmessages. Class,
Xdocletmessages. generating_something,
New String [] {getdestinationfile ()}));
}
To use ant, you only need the following simple code:
/*
* Creation date:
*/
Package paradise. XDoclet. modules. Javascript;
  
Import XDoclet. doclettask;
  
/**
* @ Author breeze
*/
Public class javascriptdoclettask extends doclettask
{
Public javascriptdoclettask ()
{
Addsubtask (New javascriptsubtask ());
}
}
Define in ant as follows:
<Target name = "JavaScript" depends = "jxdoc_init" Description = "generate JavaScript Validation-config">
<Javascriptdoclet destdir = "$ {JSP}/JavaScript">
<Fileset dir = "$ {SRC}">
<Include name = "**/zaibian/*. Java"/>
</Fileset>
</Javascriptdoclet>
</Target>
  
Next, the core part is the xdt template language. The xdt file is one of the most important files for customizing XDoclet labels. Take javascriptxdoclet as an example to briefly introduce the xdt template language:
<Xdtclass: forallclasses> traverse all classes containing tags (specified in ant)
<Xdtmethod: forallmethods> traverses all methods of the current class.
<Xdtmethod: ifhasmethodtag tagname = "javascript. Form"> If the Traversal method contains the specified tag
<Xdtmethod: forallmethodtags tagname = "javascript. Depend"> traverse all labels of the current method
For more template languages, refer to the XDoclet. xdt document, which is a well-understood template language.
  
Next, start to customize your own tag, create a new xtags. xml file, and add the start
<? XML version = "1.0" encoding = "UTF-8"?>
  
<! Doctype XDoclet public "-// XDoclet team // DTD XDoclet tags 1.1 // en" http://xdoclet.sourceforge.net/dtds/xtags_1_1.dtd ">
Then write down all the custom labels, such:
<XDoclet>
<Namespace>
<Name> JavaScript </Name>
<Tags>
<Tag>
<Level> method </level>
<Name> Javascript. Form </Name>
<Usage-description> form </usage-description>
<Condition type = "method"/>
<Parameter type = "text">
<Name> id </Name>
<Usage-description> Form ID </usage-description>
<Mandatory> true </mandatory>
</Parameter>
<Parameter type = "text">
<Name> show-error </Name>
<Usage-description> form error display </usage-description>
<Mandatory> true </mandatory>
</Parameter>
<Parameter type = "text">
<Name> onfail </Name>
<Usage-description> form error Run M JavaScript function </usage-description>
<Mandatory> false </mandatory>
</Parameter>
</Tag>
</Tags>
</Namespace>
</XDoclet>
Note the following:
"<Level> method </level>" indicates that the label appears on the method rather than the class. For example
/**
* @ Javascript. Form
* Name = "test"
*/
Public String getxxx ()
{
}
A
The last step is to compress these files into jar files and put them in the following directories:
Root directory
|
| -- META-INF/xtags. xml
|
| -- Source code
| ---- |
| ---- | -- Resources/*. xdt, *. DTD

Recommended for beginners a programming technology learning site, 96 stack Software Programming Network, http://www.96dz.com, which has c ++ video tutorial, C # video tutorial, Java video tutorial download, c \ c ++, Java, and C #. net and other programming technology abstracts, including the current mainstream Linux programming and web programming learning materials video tutorial download.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.