Creating Date: 2003.5.9 Basic Parts: 2003.5.15 Applications: 2003.5.18
I. Overview
Digester components are used for mapping processing of XML documents to Java objects. This skill is particularly important for domestic comparison to build a configurable software system and build framework software platform. This skill is currently not valued. Therefore, this component is particularly important for domestic. The basic processing flow of Digester is as follows:
XML Document Read -> System Processing Module (Call Digest Components) Generate -> Objects in Run
First create an XML configuration document, and then the system reads the XML document at runtime, and triggers the rules defined in the module to generate the required objects. The processing module is equivalent to a advanced object generator, and during the generation of the object, the properties of the object and the relationship between objects can be set.
Second, Digester Basic Description
1. Steps to use Digester
1. Instantiate an object of org.apache.commons.digester.digester class, which is assumed to be DIG. 2. Take some attribute settings for the object DIG (such as what ClassLoader for this object, if you need to generate a log, etc.). 3. Pre-Push some objects in the object stack of the object DIG. 4. Register all element matching mode, these matching mode is the next processing rule. 5. Call DIG.PARSE () to process the XML file in the rules above to generate the object we need.
2. Digester is a Java component, relying on many other components, if a one is loaded, too much trouble, install a tomcat4.1.24, all, then copy them to the current record C: / Digester, the list is as follows:
1.c: /tomcat/server/lib/commons-beanutils.jar2.c: /tomcat/server/lib/commons-collections.jar3.c: /tomcat/server/lib/commons-digester.jar4.c: / Tomcat / server / lib / commons-logging.jar5.c: /tomcat/common/lib/commons-logging-api.jar 6.c: /tomcat/common/endorsed/xmlparserapis.jar7.c: / tomcat / common / Endorsed / XercesImpl.jar
Set the test environment: set classpath =% classpath%;.; C: /jdk/lib/dt.jar; c: /jdk/lib/tools.jar; c: /digester/commons-beanutils.jar; C: / Digester /commons-collections.jar;c:/digester/commons-digester.jar;c:/digester/logging.jar;c :/digester/xerceptimpl.jar;c/digester/digester/xmlparsrapis.jar;c :/digester /commons-logging-api.jar test environment program (TEST1.JAVA) Import org.apache.commons.digester.digester;
Public class test1 {public static void main (string [] args) {digester diagram) {Digester Dig = new digester (); system.out.println ("Hello MyDigester");}}
If Javac Test1.java is compiled, Java Test1 is printed
Hello MyDigesterDigester's operating environment is OK.
Third, the application example
1. Simple application: read configuration files
Digester provides an XML document -> The mapping of the Java object, the mapping situation can be used as follows:
Time b> - match mode "A / B"
The middle "->" is a matching method. The matching method has a rule of the unified interface. It provides 9 preset rules in the Digester version, which is the most common rules, respectively. Yes:
ObjectCreateRuleFactoryCreateruleSetPropertiesRuleSetPropertyRuleSetNextruleSettopruleSetRootRulecallMethodRulecallParamrule
These preset rules include basic operations (creation, setting attributes, calling methods) of the objects.
These operations map through an XML file, so that the system can change the system by modifying the XML file, as long as it is not a change in the interface (the change in the interface is generally a large change), no modification is required. If you do well, the system's business process can be easily transformed without changing the code.
We describe the basic operations of Digester to make a foundation for the next step in the next step.
Document 1: config.xml (only one line)
File 2: Abstract class logger.java
File 3: A realization of this abstract class MYLOGGER.JAVA
File 4: Application MyApp.javaMain Method Pseudo Code: 1. Stateing Variables Logger MyLog; 2. Read and resolve config.xml: getLogger (); 3. Execute some operations 4. MYLOG.LOG ("Hello Test"); 5. Execute other operations
getLogger Method: Digester digester = new Digester (); digester.addObjectCreate ( "logger", "mylogger", "className"); digester.addSetProperties ( "logger); digester.push (this); digester.parse (" config. XML ");
2: Advanced Applications: Building a Frame Software Platform
Tomcat is a software framework platform built with Digester, you can put your own processing module in, of course, because the author does not write a good document, it will have a certain difficulty, but we can see from the following description:
Code pieces 1. Program org.apache.catalina.Startup / Catalina.java
Digester.addObjectCreate ("Server / Service / Connector", "org.apache.catalina.connector", "classname"); where "Server / Service / Connector" is the mode to be matched, "Org.apache. Catalina.connector.http.httpconnector "is the default connection processing class of the system," classname "is the properties that can be replaced in the matching mode. Code segment 2. Corresponding Conf / Server.xml
Port = "8080" MINPROCESSORS = "5" maxprocessors = "75" Enablelookups = "True" redirectport = "8443" AcceptCount = "100" debug = "0" connectionTIMEOUT = "20000" Useurivalidationhack = "false" disableuploadtimeout = "true" /> As can be seen, the actual running system uses the COYOTECONNECTOR CONNECTOR, which means that you can also write your own Connector instead of it, of course because the internal structure is not very clear, we have to do this may have certain difficulties, others Many processing modules can be modified, including Server, Service, Listener, Logger, and more. In the process of building a software platform with a Digester, the interface is one of the most important technologies. When designing the platform, you should first design the interface between each module in the platform, and Java's Interface provides a good technical means. in conclusion: This paper mainly discusses the basic principle of a component Digester in Jakarta project in the Apache Foundation and its reuse in building a framework software platform. It is considered to use Digester as a solution to build a better software. Platform problem.