How tomcat works Reading Notes 15th Digester Library

Source: Internet
Author: User

How tomcat works Reading Notes 15th Digester Library
Digester Library

In the previous sections, We have configured all the components in tomcat in the form of hard coding.
For example

Context context = new StandardContext();Loader loader = new WebappLoader();context.setLoader(loader);
Add WepappLoader to the context container.
The problem with this is that the Bootstrap class must be reloaded once I want to change the configuration.
Fortunately, tomcat designers use a flexible configuration method, that is, using xml to record the configuration of each component in tomcat. In addition, Digester is used to convert the elements in xml into java objects.

Digester is an open-source project under the Jakarta project of the Apache Software Foundation. For more information, please refer to Baidu.
Here we will mainly introduce what Digester can do.
The description of this project on the official apaceh website is as follows:
XML-to-Java-object mapping utility.
As you can see, the elements in xml are converted into java classes.

The Digester class first looks at an xml file as follows:
 
   
        
  
 
In this xml, the root element is employee, which contains an office element and an address element.
Before discussing how Digester parses xml, let's talk about two concepts, patterns and rules.
Mode: I cannot give a written definition of the mode. Roughly speaking, in the preceding xml, the mode of the employee element is employee, the mode of the office element is employee/office, and the mode of the address element is employee/office/address. You should probably understand it. To put it bluntly, the mode is the path.
Rule: it is an instance of the org. apache. commons. digester. Rule class. The rule specifies the action that a rule should be executed when parsing xml. That's clear enough. In addition, Rule has the begin and end methods. When parsing the start tag that matches an element of a certain pattern, it will point to the begin method. Is end still used?
For example, when parsing the above xml:
1. First, the employee element is parsed. At this time, check whether there are corresponding rules matching this pattern, that is, the employee matches. If so, execute the begin method of the Rule object.
2. If an office element is encountered, check whether there is a corresponding Rule in this mode, that is, the employee/office matches. If yes, execute the begin method of the Rule object.
3. If the start label of the address element is met, check whether there is a corresponding Rule. In this mode, the employee/office/address matches. If yes, run the begin method of the Rule object.
4. When the end label of the address element is met, call the end method of the object to match the rule.
I will not write it.


Predefined Digester rules
The predefined rules of Digester mainly include the following.
There are four methods to create an object
The two main examples are as follows:
Digester. addObjectCreate ("employee", ex15.pyrmont. digestertest. Employee. class );
Or
Digester. addObjectCreate ("employee", "ex15.pyrmont. digestertest. Employee ");
The first parameter is the mode name, and the second parameter can be a Class name (String type) or a Class.
There are two other ways to specify the class to be loaded in xml.
If the xml file is as follows:
ClassName = "ex15.pyrmont. digestertest. Employee">
The Code is as follows:
Digester. addObjectCreate ("employee", "ex15.pyrmont. digestertest. Employee", "className ");

The employee is the class name. The program will find the corresponding property value in the employee element according to the third parameter of the above method as the class to be loaded. Of course, if the property cannot be found, the second parameter of the above method is used for loading. Similarly, the second parameter can enable the Class Name of the String type or the Class.

(Employee, office, and address are all at the end of the article)


Set the property digester. addSetProperties ("employee ");
The above Code does the following: After the system finds an element that matches the employee pattern, if the element has an attribute, the corresponding attribute value is injected into the class.

If an xml file is above, there must be at least two setFirstName and setLastName methods in the class it matches.

Call method digester. addCallMethod ("employee", "printName ");
The function of the code above is to call the printName method of the class that was first created after finding an element that matches the employee pattern (precisely after an end tag is encountered.

Create contact digester. addSetNext ("employee/office", "addOffice") between objects ");
The mode of the first parameter should be in this form
Firstobject/secondobjcet
The above code calls the addOffice method of firstobject and takes secondobject as the parameter. Of course, both objects have been created.

The following example shows how to use Digester.
Sample Code 1
package ex15.pyrmont.digestertest;import java.io.File;import java.util.ArrayList;import java.util.Iterator;import org.apache.commons.digester.Digester;public class Test02 {  public static void main(String[] args) {    String path = System.getProperty("user.dir") + File.separator +            "src"+File.separator  + "etc";    File file = new File(path, "employee2.xml");    Digester digester = new Digester();    // add rules    digester.addObjectCreate("employee", "ex15.pyrmont.digestertest.Employee");    digester.addSetProperties("employee");        digester.addCallMethod("employee", "printName");        digester.addObjectCreate("employee/office", "ex15.pyrmont.digestertest.Office");    digester.addSetProperties("employee/office");     digester.addSetNext("employee/office", "addOffice");     digester.addObjectCreate("employee/office/address", "ex15.pyrmont.digestertest.Address");    digester.addSetProperties("employee/office/address");    digester.addSetNext("employee/office/address", "setAddress");    try {      Employee employee = (Employee) digester.parse(file);      ArrayList
    offices = employee.getOffices();      Iterator
    iterator = offices.iterator();      System.out.println("-------------------------------------------------");      while (iterator.hasNext()) {        Office office = (Office) iterator.next();        Address address = office.getAddress();        System.out.println(office.getDescription());        System.out.println("Address : " +          address.getStreetNumber() + " " + address.getStreetName());        System.out.println("--------------------------------");      }          }    catch(Exception e) {      e.printStackTrace();    }  }  }


Employee2.xml is as follows:
   
     
          
      
          
    
   
As for the final running result, you can guess and run it.


RuleSet what is this?
You can understand the code.
Package ex15.pyrmont. digestertest; import java. io. file; import java. util. arrayList; import java. util. iterator; import org. apache. commons. digester. digester; public class Test03 {public static void main (String [] args) {String path = System. getProperty ("user. dir ") + File. separator + "src" + File. separator + "etc"; File file = new File (path, "employee2.xml"); Digester digester = new Digester (); digester. add RuleSet (new EmployeeRuleSet (); // What is EmployeeRuleSet? Try {Employee employee = (Employee) digester. parse (file);... the same as Test2} catch (Exception e) {e. printStackTrace ();}}}



Package ex15.pyrmont. digestertest; import org. apache. commons. digester. digester; import org. apache. commons. digester. ruleSetBase; // be sure to inherit RuleSetBasepublic class EmployeeRuleSet extends RuleSetBase {// rewrite addRuleInstances public void addRuleInstances (Digester digester) {// add rules digester. addObjectCreate ("employee", "ex15.pyrmont. digestertest. employee "); digester. addSetProperties ("employee"); digester. addObjectCreate ("employee/office", "ex15.pyrmont. digestertest. office "); digester. addSetProperties ("employee/office"); digester. addSetNext ("employee/office", "addOffice"); digester. addObjectCreate ("employee/office/address", "ex15.pyrmont. digestertest. address "); digester. addSetProperties ("employee/office/address"); digester. addSetNext ("employee/office/address", "setAddress ");}}


RuleSetBase is the set of rules.
Test03 has the same results as Test02, but test3 seems to have fewer code, because we have hidden all rules in EmployeeRuleSet.


Related code
package ex15.pyrmont.digestertest;import java.util.ArrayList;public class Employee {  private String firstName;  private String lastName;  private ArrayList
   
     offices = new ArrayList
    
     ();      public Employee() {    System.out.println("Creating Employee");  }  public String getFirstName() {    return firstName;  }  public void setFirstName(String firstName) {    System.out.println("Setting firstName : " + firstName);    this.firstName = firstName;  }  public String getLastName() {    return lastName;  }  public void setLastName(String lastName) {    System.out.println("Setting lastName : " + lastName);    this.lastName = lastName;  }  public void addOffice(Office office) {    System.out.println("Adding Office to this employee");    offices.add(office);  }  public ArrayList
     
       getOffices() {    return offices;  }  public void printName() {    System.out.println("My name is " + firstName + " " + lastName+"sssssssss");  }}package ex15.pyrmont.digestertest;public class Office {  private Address address;  private String description;  public Office() {    System.out.println("..Creating Office");  }  public String getDescription() {    return description;  }  public void setDescription(String description) {    System.out.println("..Setting office description : " + description);    this.description = description;  }  public Address getAddress() {    return address;  }  public void setAddress(Address address) {    System.out.println("..Setting office address : " + address);    this.address = address;  }}package ex15.pyrmont.digestertest;public class Address {  private String streetName;  private String streetNumber;  public Address() {    System.out.println("....Creating Address");  }  public String getStreetName() {    return streetName;  }  public void setStreetName(String streetName) {    System.out.println("....Setting streetName : " + streetName);    this.streetName = streetName;  }  public String getStreetNumber() {    return streetNumber;  }  public void setStreetNumber(String streetNumber) {    System.out.println("....Setting streetNumber : " + streetNumber);    this.streetNumber = streetNumber;  }  public String toString() {    return "...." + streetNumber + " " + streetName;  }}
     
    
   


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.