TOMCAT底层解析server.xml使用。
Dom4j是把一个xml文件全部读取到内存中,构建成一个DOM树来解析,所以Dom4j适合读取比较小的xml文件。
SAX是基于文件流来解析xml文件的,在读取xml文件流时,SAX会通过节点来触发相应的操作,也可以说SAX是基于文件流的事情触发机制来解析xml文件的。
Digeter是apache的common项目,作用是将XML转化成对象,使用者直接从对象中获取xml的节点信息。Digester是对SAX的包装,它也是基于文件流来解析xml文件,只不过这些解析操作对用户是透明的。Tomcat的配置文件conf/server.xml就是用Digester来读取的。
一般用来读取xml文件的工具包有DOM、SAX和JDOM等,但用过的人都知道,它们属于比较底层的API,写起来代码量很大,而且如果修改了xml文件的格式,代码也要做大幅度的改动。而使用Apache Jakarta的Digester,解析XML文件非常方便且不需要过多的关心底层的具体解析过程。Digester本来仅仅是Jakarta Struts中的一个工具,用于处理struts-config.xml配置文件。显然,将XML文件转换成相应的Java对象是一项很通用的功能,这个工具理应具有更广泛的用途,所以很快它就在Jakarta Commons项目(用于提供可重用的Java组件库)中有了一席之地。Digester由"事件"驱动,通过调用预定义的规则操作对象栈,将XML文件转换为Java对象。
工作原理如下:
Digester底层采用SAX(Simple API for XML)析XML文件,所以很自然的,对象转换由"事件"驱动,在遍历每个节点时,检查是否有匹配模式,如果有,则执行规则定义的操作,比如创建特定的Java对象,或调用特定对象的方法等。此处的XML元素根据匹配模式(matching pattern)识别,而相关操作由规则(rule)定义。
如下xml代码,右边是左边元素对应的匹配模式:
'datasources' 'datasources/datasource' 'datasources/datasource/name' 'datasources/datasource/driver' 'datasources/datasource' 'datasources/datasource/name' 'datasources/datasource/driver'
Tomcat与Digester的关系(注意该博客,很多资源)
Tomcat源码的catalina中利用Digester解析conf/server.xml
三个常见Digester例子
深入剖析Tomcat-Digester
理解Tomcat(一) 利用Digester解析xml文件
Digester处理多层的XML(仔细分析)
使用Digester解析XML配置文件
解析XML常用技术
==============================================================================
1.addObjectCreate(String rule,Class class)
设置节点与Java对象的映射规则,rule指定节点的筛选规则,class设置映射对象。SAX解析时,遇到rule指定的节节点,会创建一个class实例放入堆栈中。
比如:digester.addObectCreate("database/user","com.model.UserBean").解析遇到user节点时,会创建一个UserBean实例并放入堆栈中。
2.addSetProperties(String rule)
设置节点的属性设置规则。当解析遇到符合rule的节点时,根据属性列表中的属性值对,使用Java反射机制使用标准的JavaBean方法设置栈顶对象实例;
比如:digester.addSetProperties("database/user"),解析遇到user节点时,会获取键值对 userName=guest,password=guest,获得栈顶的UserBean对象,设置实例的userName、password属性;
解析元素上的属性
3.addBeanPropertySetter(String rule)
该方法的作用及使用方法类似于addSetProperties,只不过它是用rule所指定的标签来调用对象的setter。
该方法在tomcat6中貌似没有。
tomcat6中Digester类路径为:import org.apache.tomcat.util.digester.Digester;
不为:import org.apache.commons.digester.Digester;
4.addSetNext(String rule,String methodName)
设置当前rule节点与父节点的调用规则,当遇到rule节点时,调用堆栈中的次栈顶元素调用methodName方法。将栈顶元素作为次顶元素指定方法的输入参数。
比如:digester.addSetNext("database/user","addUser"),调用database实例的addUser,user为参数
5.addCallMethod(String rule,String methodName,int paraNumber)
该方法同样设置对象的属性,但更加灵活,不需要对象具有setter
根据rule规则指定的属性,调用对象的methodName方法,paraNumber参数是表示方法需要的参数个数,当paraNumber=0时,可以单独使用,不然需要配合addCallParam方法
比如:digester.addCallMethod("database/user/uerName","setUserName",0);
6.addCallParam(String rule,int paraIndex,String attributeName)
该方法与addCallMethod配合使用,根据rule指定的标签属性来调用方法
paraIndex表明需要填充的方法形参序号,从0开始,方法由addCallMethdo指定,attributeName指定标签属性名;
使用注意事项:
1.Digester类调用的顺序,必须与XML数据文件绝对一致;
2.Digester类依赖于JavaBean规范,类必须符合规范;
3.XML文件中标签/属性的名称必须与Bean中的一致(包括大小写);
==================================================================
关键方法说明
(1) serverDigester.addObjectCreate("Server","com.test.server.digester.Server")
当解析xml文件时,遇到“Server”就初始化一个“com.test.server.digester.Server”对象,并且把该对象压入栈顶
(2) serverDigester.addSetProperties("Server", "port", "port")
给Server对象注册port属性,当解析到Server节点的port属性时调用Server的setPort方法
(3) serverDigester.addSetNext("Server/Listener", "addListener","com.test.server.digester.Listener")
当解析Server节点下的Listener节点的时候,调用Server对象的addListener方法,把当前Listener对象写入到Server对象中。无论Server节点下有多少个Listener节点,都会调用addListener方法
(4) serverDigester.addCallMethod("Server/Service/Engine", "setEngine", 0)
Service中添加Engine,调用当前top object的setEngine函数,参数个数为0
addCallMethod与addBeanPropertySetter方法等价
Example1:
package digester; import java.util.ArrayList; public class Student { private String name; private int age; private int grade; private ArrayListlist = new ArrayList(); public Student (){ System.out.println("A student go to school"); } public void add ( Subject subjects ){ System.out.println("add Subjects " + subjects.getName() + " to the Student: "+ this.getName()); list.add( subjects ); } ///////////////////////////////////////////////// public int getAge() { return age; } public void setAge(int age) { System.out.println( " set age of Student:" + name); this.age = age; } public int getGrade() { return grade; } public void setGrade(int grade) { System.out.println( " set score of Student:" + name ); this.grade = grade; } public String getName() { return name; } public void setName(String name) { System.out.println(" set name pf Student" ); this.name = name; } public ArrayList getList (){ return this.list; } }
package digester; public class Subject { private String name; private String teacher; public Subject() { System.out.println("One subjects waiting scheduling!"); } public void print() { System.out.println("Subjects :" + name + "of " + teacher + "in schedule"); } ///////////////////////////////////////////////////// public String getName() { return name; } public void setName(String name) { System.out.println(" set name of Subjects:" + name); this.name = name; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { System.out.println(" set teacher of Subjects :" + teacher); this.teacher = teacher; } }
package digester; import java.io.File; import java.io.IOException; import java.util.Iterator; import org.apache.tomcat.util.digester.Digester; import org.xml.sax.SAXException; public class StudentDigesterTest { public static void main ( String [] args ) throws IOException{ unUseRule(); //useRule(); } public static void unUseRule(){ File xml = new File( "../apache-tomcat-6.0.41-src/test/student.xml"); Digester digester = new Digester(); digester.addObjectCreate( "student" , "digester.Student" ); digester.addSetProperties("student"); digester.addObjectCreate("student/subject" , "digester.Subject"); digester.addSetProperties("student/subject"); //上面该句可用这句代替digester.addSetProperties("student/subject","subject","subject"); /** * public void addSetProperties(String pattern,String attributeName,String propertyName) */ digester.addSetNext("student/subject" , "add" ); Student student = null; try { student = (Student)digester.parse(xml); }catch ( SAXException e ){ System.out.println("parse with mistakes!"); } catch (IOException e) { e.printStackTrace(); } Iterator it = student.getList().iterator(); while ( it.hasNext()) { } } public static void useRule(){ File xml = new File( "../apache-tomcat-6.0.41-src/test/student.xml"); Digester digester = new Digester(); digester.addRuleSet(new MyRuleSet()); Student student = null; try { student = (Student)digester.parse(xml); }catch ( SAXException e ){ System.out.println("parse with mistakes!"); } catch (IOException e) { e.printStackTrace(); } Iterator it = student.getList().iterator(); while ( it.hasNext()) { } } }
package digester; import org.apache.tomcat.util.digester.Digester; import org.apache.tomcat.util.digester.RuleSetBase; public class MyRuleSet extends RuleSetBase{ @Override public void addRuleInstances(Digester digester) { digester.addObjectCreate( "student" , "digester.Student" ); digester.addSetProperties("student"); digester.addObjectCreate("student/subject" , "digester.Subject"); digester.addSetProperties("student/subject"); digester.addSetNext("student/subject" , "add" ); } }
两种方法结果都为:
Example2:
viewcache2.xml:
package digester; import java.util.ArrayList; import java.util.List; public class ViewCache { private ArrayList areaList = new ArrayList(); public ArrayList getAreaList() { return areaList; } public void setAreaList(ArrayList areaList) { this.areaList = areaList; } // 供Digester调用的方法 public void addArea(Area area) { System.out.println("addArea.."); this.areaList.add(area); } }
package digester; public class Area { private int id; private String name; private String areaType; private int parentId; private int ordering; // private String zip; private String phoneArea; public Area(){ System.out.println("Area constructor.."); } public int getId() { return id; } public void setId(int id) { System.out.println( " set id of Area:" + id); this.id = id; } public String getName() { return name; } public void setName(String name) { System.out.println( " set name of Area:" + name); this.name = name; } public String getAreaType() { return areaType; } public void setAreaType(String areaType) { System.out.println( " set areaType of Area:" + areaType); this.areaType = areaType; } public int getParentId() { return parentId; } public void setParentId(int parentId) { System.out.println( " set parentId of Area:" + parentId); this.parentId = parentId; } public int getOrdering() { return ordering; } public void setOrdering(int ordering) { System.out.println( " set ordering of Area:" + ordering); this.ordering = ordering; } /*public String getZip() { return zip; } public void setZip(String zip) { this.zip = zip; }*/ public String getPhoneArea() { return phoneArea; } public void setPhoneArea(String phoneArea) { System.out.println( " set phoneArea of Area:" + phoneArea); this.phoneArea = phoneArea; } }
package digester; import java.io.IOException; import org.apache.tomcat.util.digester.Digester; import org.xml.sax.SAXException; public class AreaDigester { public ViewCache digester() throws Exception { Digester digester = new Digester(); digester.setValidating(false); digester.addObjectCreate("viewcache/areas", ViewCache.class); // 指明匹配模式和要创建的类 digester.addObjectCreate("viewcache/areas/area", Area.class); //使用这句可以打印出Area字段set值时的syso语句,下面的打印不出,没搞清楚。。。 digester.addSetProperties("viewcache/areas/area"); // digester.addSetProperties("viewcache/areas/area/id"); // digester.addSetProperties("viewcache/areas/area/parentId", "parentId", "parentId"); // digester.addSetProperties("viewcache/areas/area/name", "name", "name"); // digester.addSetProperties("viewcache/areas/area/areaType", "areaType", "areaType"); // digester.addSetProperties("viewcache/areas/area/ordering", "ordering", "ordering"); // //digester.addSetProperties("viewcache/areas/area/zip", "zip", "zip"); // digester.addSetProperties("viewcache/areas/area/phoneArea", "phoneArea", "phoneArea"); // 当移动到下一个标签中时的动作 digester.addSetNext("viewcache/areas/area", "addArea"); ViewCache vc = null; try { vc = (ViewCache) digester.parse("../apache-tomcat-6.0.41-src/test/viewcache2.xml"); } catch (IOException e) { throw new Exception(e); } catch (SAXException e) { throw new Exception(e); } return vc; } public static void main(String[] args) { AreaDigester m = new AreaDigester(); ViewCache vc = null; try { vc = m.digester(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
结果: