JAKARTA Commons Digester Introduction and Specific Usage

zhaozj2021-02-16  40

1.

Digester is the program that allows you to configure an XML to Java Object. Need. And Digester also includes the following advanced features:

* Can embed your original corresponding way, without affecting your needs. * Customized Namespace-Aware execution, so that you can define Rules properly

Special XML namespace. * Add all Rules into ruleSets, you can easily and easily reuse some other items that require the same type.

Among them. Three concepts that need to be understood: Pattern is a Rule (Pattern) example of XML: pattern // This is the rule Datasource '' DataSources / DataSource ' DataSource' 'Datasource' / DataSource / name '' datasources / datasource / driver ' ' datasources / datasource '' datasources / datasource / name '' datasources / datasource / driver ' When the handler finds Pattern, you will call and link the Rulerule (Processing Rule) when Pattern matches him, you will get the default rule to create Object by org.apache.commons.digester.rule STACK (Object Stack) Objects are only putted here will be executed 2. Before using Digester, you must first have some libraries in your classpath, commons-digester, commons-beanutils, commit-collections, commons-logging, and compliance SAX (Simple API for XML) 1.2.1. I recommend it to catch crimson and Xerces. The current version is: 1.53. Digester's processing (Rule, Pattern, Object Stack) Create a new object

Method of creating a response rule:

Void addObjectcreate (java.lang.string pattern, java.lang.string classname,

Java.lang.string AttributeName)

// Establish call Method Void AddCallMethod (java.lang.string pattern, java.lang.string methodname, int

Paramcount)

// Establish the parameter void addcallparam (java.lang.string pattern, int paramindex)

// XML parsing java.lang.Object Parse (java.lang.string URI) throws java.io ioException, org.xml.sax.saxException 4. Custom rule method When you open the source directory, ORG / Apache / COMMONS / Digester / there is one

DiGester-rules.dtd. This DTD file is how to define how Digester will resolve the XML file you passed, you can also

Definition related patterns, write in Digester-Rules.xml, let Digester can analyze according to your needs

Data. Do this can reduce procedures, you must write addObjectCreate, AddCallMethod, etc.

However, I haven't seen any Project using this practice.

5. Example used in Struts an ActionServlet () throws servletexception {// ...................................

// Prepare a Digester to Scan The Web Application Deployment Descriptor Digester Digester = New Digester (); Digester.push (this); digester.setnamespaceaware (true); Digester.SetValidating (False);

// ....... 省 省 ............... // register ur Local Copy of the dtds That We can Find for (int i = 0; i

// Configure the processing rules That We NEED / / Settings related Elements and Execute Rules ("Web-App / Servlet-Mapping", "AddserveRvletMapping",

2); Digester.AddcallParam ("Web-App / Servlet-Mapping / Servlet-Name", 0); Digester.addCallParam ("Web-App / Servlet-Mapping / Url-Pattern", 1);

InputStream INPUT = NULL; TRY {// Get / Web-INF / Under Web.xml to Result Input =

GetServletContext (). getResourceAsStream ("/ web-inf / web.xml"); Digester.Parse (INPUT);} catch (throwable e) {log.error ("configwebxml"), E);} finally {IF (Input! = Null) {Try {INPUT.CLOSE ();} catch (ooexception e) {;}}} // ....... 省略 .......... .....

/ ** * Remember a servlet mapping from our web application deployment * descriptor, if it is for this servlet. * * @Param servletName The name of the servlet being mapped * @param urlPattern The URL pattern to which this servlet is mapped * / // When Digester will execute AddservletMapping this Method Public Void AddservletMapping (String Servletname, String Urlpattern) when Parse

IF (log.isDebugenabled ()) {log.debug ("Process servletname =" servletname ", urlpattern =" urlpattern);} f (servletname == null) {return;} if (servletname.equals (THIS. ServletName)) {this.servletmapping = urlpattern;

}

Related Bibliography or Related Articles * Jakarta Commons: http://jakarta.apache.org/commons/index.html * Jakarta Commons Digester: http://jakarta.apache.org/commons/digester.html * Simplify XML File Processing with The Jakarta Commons Digester: http://jakarta.apache.org/commons/digester/api/index.html If your English is OK, you can check this article: http://www.javaworld.com/javaworld/ JW-10-2002 / JW-1025-OpenSourceProfile.html

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

New Post(0)