Use Digester to parse conf/server.xml in Tomcat source Catalina

Source: Internet
Author: User
Tags tomcat

Recently in the study of Tomcat source code, in Catalina. In the Createstartdigester method, the Tomcat developer uses the digester to read the Conf/server.xml file, which was previously read in XML files, typically using dom4j and sax. Because of Digester is unfamiliar, so today took time to study how Digester parse XML file. Let's start with a brief explanation of the dom4j and Sax parsing xml:

DOM4J is to read all of an XML file into memory and build it into a DOM tree to parse it, so dom4j is suitable for reading smaller XML files.

Sax parses an XML file based on a file stream, and when it reads the XML file stream, sax triggers the corresponding action through the node, or sax is a file-stream-based trigger mechanism to parse the XML file.

Digeter is an Apache common project that transforms XML into objects, and the user obtains the node information of the XML directly from the object. Digester is the wrapper for sax, which also parses the XML file based on the file stream, except that these parsing operations are transparent to the user. The Tomcat configuration file Conf/server.xml is read by Digester.

First, Download Digester of the Jar

(1) digester jar Download Address (version: 2.0)
http://commons.apache.org/digester/

(2) Digester-dependent logging jar Download address (version: 1.1.1)

http://commons.apache.org/logging/

(3) Digester-dependent beanutils jar Download address (version: 1.8.3)

http://commons.apache.org/beanutils/

Just follow the steps above to download the jar package and import it into Eclipse engineering. For everyone to save the trouble of downloading the jar package, I uploaded the "Digester_jar" resource in the blog resource, which contains all the required jar packages and source code.

The source of the corresponding object references the "Digester_object" Resource in my blog resource, which contains the Java object code required in the test code.

Second, Key Method Description

(1) serverdigester.addobjectcreate ("Server", "Com.test.server.digester.Server")

When parsing an XML file, a "Com.test.server.digester.Server" object is initialized when "Server" is encountered, and the object is pressed into the top of the stack

(2) serverdigester.addsetproperties ("Server", "Port", "Port")

Registers the port property with the server object and invokes the server's Setport method when resolving to the Port property of the server node

(3) Serverdigester.addsetnext ("Server/listener", "AddListener", "Com.test.server.digester.Listener")

When the listener node under the server node is parsed, the AddListener method of the server object is called, and the current listener object is written to the server object. The AddListener method is called regardless of the number of listener nodes under the server node

(4) Serverdigester.addcallmethod ("Server/service/engine", "Setengine", 0)

Service to add engine, call the current top object Setengine function, the number of arguments is 0

Addcallmethod and Addbeanpropertysetter method equivalence

Third, Precautions

The attributes defined in the XML are consistent with the set and get methods in the object, such as when a node attribute "Enginetype" is defined in the XML, then the set method of the Java object must be Setenginetype, except for the first letter, the other letters must be identical, Otherwise, the corresponding property value will not be resolved at parse time. I do not know whether there are other rules, not clear.

Four, XML Parsing Code

<?xml version= "1.0" encoding= "UTF-8"?>

<!--

&NBSP;&NBSP;&NBSP ;      Attribute must is lower case.

         For example:

                   attribute:enginetype</

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.