How tomcat works 读书笔记十五 Digester库(二)

2015-01-25 11:43:17 · 作者: · 浏览: 25
reate("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如下
    

    
  
      
      
至于最后的运行结果大家自己猜一下,然后运行一下看看。


RuleSet

这是什么东西?
大家看代码就懂了。
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.addRuleSet(new EmployeeRuleSet());  //EmployeeRuleSet是什么东西?
    try {
      Employee employee = (Employee) digester.parse(file);
    
      ...... 与Test2一样
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}



package ex15.pyrmont.digestertest;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.RuleSetBase;

//一定要继承RuleSetBase
public class EmployeeRuleSet extends RuleSetBase  {

  //复写    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就是规则的集合嘛。
Test03与Test02的结果一样,但是test3的代码看起来更少一些,就是因为我们把规则都隐藏在了EmployeeRuleSet里面了。


相关代码

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 +