Context context = new StandardContext(); Loader loader = new WebappLoader(); context.setLoader(loader);就完成了向context容器里添加WepappLoader的功能。
<?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Freddie" lastName="Mercury"> <office description="Headquarters"> <address streetName="Wellington Avenue" streetNumber="223"/> </office> </employee>在这个xml里面,根元素是employee,里面包含一个元素office,office里面又包含一个元素address。
employee是类名,程序会按照上面方法的第三个参数去employee元素里找对应的属性值作为要加载的类,当然如果找不到那个属性的话,就是有上面方法的第二个参数来加载。同样第二个参数可以使String类型的类名也可以使Class。
(employee,office,address都在文章的最后)
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(); } } }
<?xml version="1.0" encoding="ISO-8859-1"?> <employee firstName="Freddie" lastName="Mercury"> <office description="Headquarters"> <address streetName="Wellington Avenue" streetNumber="223"/> </office> <office description="Client site"> <address streetName="Downing Street" streetNumber="10"/> </office> </employee>
至于最后的运行结果大家自己猜一下,然后运行一下看看。
Digester.java protected Rules rules = null; public void addObjectCreate(String pattern, Class clazz) { addRule(pattern,new ObjectCreateRule(clazz)); } public void addRule(String pattern, Rule rule) { rule.setDigester(this); getRules().add(pattern, rule); //getRules返回的就是Digester中的那个rules } ObjectCreateRule.java public void begin(Attributes attributes) throws Exception { .... // Instantiate the new object and push it on the context stack Class clazz = digester.getClassLoader().loadClass(realClassName); Object instance = clazz.newInstance(); digester.push(instance); }Digester里面存储了一个rules对象
protected ArrayList rules = new ArrayList();另外ObjectCreateRule也是rule的子类。
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"); } }
package ex15.pyrmont.digestertest; import java.util.ArrayList; public class Employee { private String firstName; private String lastName; private ArrayList<Office> offices = new ArrayList<Office>(); 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<Office> 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; } }