Customize your xDoclet tag

xiaoxiao2021-03-06  120

Customize your xDoclet tag

XDoclets are tools that use specific tags in the Java source file and then generate a specified file. The xdoclet tag itself has provided some commonly used labels, such as @ ejb, @ hibernate, @ web, etc., but still can't meet our needs.

For example, our latest project references a JavaScript verification framework, you can complete the client table verification by configuring a specific XML configuration file, but don't want the developer to learn a set of frames, so I want the developer to write in the source code. @javascript Label and then generate its configuration file.

JavaScript client verification has always been a problem with a more headache in Web development. It is often full of many similar or even the same verification code. How to unify these codes, and how to do the code page. Good solution. Using this verification framework then the client development becomes configured Validation-config.xml This file is so easy. This article does not intend to tell how to use the JSValidation framework, interested friends can go to http://www.cosoft.org The official website of .CN / Projects / JSValidation Jsvalidation goes to learn.

Although using this framework has been well completed by the client verification code, and provide the DTD file to perform XML files, but manual writing XML files is still very easy to errors, the efficiency is relatively low, and new developers must master a A new framework is not suitable for developers.

Most of the framework like Struts or JSF needs to write a similar formbean thing, as an example of JSF, if there is a text box in the form, then the corresponding page class or the 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 xdoclets, then you can automatically generate a JSVALIDATION profile, and also beneficial to train new developers, and then add Ant Task and form a daily construction. Imagined code should be the following look:

/ **

* @ javascript.field

* name = "frmzbaddglobalpage: txtteaname"

* Display-name = "User Name"

*

* @ javascript.depend

* Name = "required"

*

* @Return username

* /

Public HTMLINPUTTEXT GETTXTUSERNAME ()

{

Return txtusername;

}

By analyzing some tag packs comes with the xDoclet, you can only provide a custom xdoclet tag that you only need to provide three files: a class inherited in Xmlsubtask, a class inherited to Doclettask (for ant), a template language file for an XDT I.e.

In the XMLsubtask class, first, define the template file name:

Private static string default_template_file = "resources / validation-config.xdt"; Define 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 just combine three files can be combined, the detailed code is as follows:

Public JavaScriptsubtask ()

{

SetTemplateURL (GetClass (). getResource (default_template_file);

SetDestinationFile (generated_file_name);

}

Public void execute () THROWS XDOCLETEXCEPTION

{

Setddurl (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:

/ *

* Create a date 2004-4-26

* /

Package paradise.xdoclet.modules.javaScript;

Import xdoclet.doclettask;

/ **

* @Author Breeze

* /

Public Class JavaScriptDoclettask Extends Doclettask

{

Public JavaScriptdoclettask ()

{

Adsubtask (New JavaScriptsubtask ());

}

}

Definitions in Ant as follows:

Description = "Generate JavaScript Validation-Config"> Next, the core part is related to the XDT template language, the XDT file can be said to be one of the most important files for custom xdoclet tags, as an example of JavaScriptxDoclet, simple Let's introduce the XDT template language: Traversing all classes containing tags (specified in Ant) Traversing all methods of the current class If traversed The method contains the specified label Traveling all tags in the current method More template language, refer to XDoclet's .xdt document, is a very well understood template language. Next, start to customize your own label, create a new Xtags.xml file, plus the beginning

转载请注明原文地址:https://www.9cbs.com/read-100753.html

New Post(0)