Develop your own xdoclet tag

xiaoxiao2021-03-06  60

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.

I have seen JSValidation online recently:

Interested friends can go

Http://www.cosoft.org.cn/projects/jsvalidation JSValidation official website 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. Take JSF as an example. If there is a text box in the form , then the corresponding Page class or Called 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:

Next, it is also the core part, that is, 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, briefly introduce the XDT template language:

Traversing all classes containing tags (specified in Ant)

All methods of traversing the current class If the method traveled over the method included in the specified label

All tags of 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

http://xdoclet.sourceforge.net/dtds/xtags_1_1.dtd ">>

Then write all custom labels, for example:

javascript

Method

javascript.form

Form

ID

form ID

True

show-error

form error display

True

onfail

Form Error Run Custom JavaScript Function

false

Pay attention to a few places:

" Method " represents the label on the method instead of the class. E.g

/ **

*@javascript.form

* Name = "test"

* /

Public string getxxx ()

{

}

Finally, these files are pamped into JAR, and their placement directory is: root directory

|

| --META-INF / XTAGS.XML

|

| - Source code

| ---- |

| ---- | --Resources / *. xdt, *. dtd

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

New Post(0)