tomcat 7 源码分析-3 使用Digester读取xml文件实例化server

接下来tomcat要load了,看下面一些程序片段

public void load() {

        long t1 = System.nanoTime();
        initDirs();
        initNaming();
        Digester digester = createStartDigester();
        ........
       digester.push(this);
       digester.parse(inputSource);
        ........
        getServer().init();

 令人费解的如何实例化server的?digester有何作用?原来digester是apache的common项目,作用是讲XML转成Object。tomcat读取配置文件conf\server.xml,实例化server对象。形同:

        // Configure the actions we will be using
        digester.addObjectCreate("Server",
                                 "org.apache.catalina.core.StandardServer",
                                 "className");
        digester.addSetProperties("Server");
        digester.addSetNext("Server",
                            "setServer",
                            "org.apache.catalina.Server");

 要读懂这些还真费解,这里写了个小例子。

首先xml文件为

<?xml version="1.0"?>
<catalog library="somewhere">
   <book>
      <author>Author 1</author>
      <title>Title 1</title>
   </book>
   <book>
      <author>Author 2</author>
      <title>His One Book</title>
   </book>
   <book>
      <author>Author 3</author>
      <title>His Other Book</title>
   </book>
</catalog>

 Book.java

package com.xiao;

public class Book {
	   private String author;
	   private String title;

	   public Book() {}

	   public void setAuthor( String rhs ) { author = rhs; }
	   public void setTitle(  String rhs ) { title  = rhs; }
	   
	   public String getAuthor(  ) { return author; }
	   public String getTitle(  ) { return title; }
	   
	   public String toString() {
	      return "Book: Author='" + author + "' Title='" + title + "'";
	   }
	}

Catalog.java

package com.xiao;
import java.util.Vector;
public class Catalog {
   private Vector<Book> books;

   public Catalog() {
      books = new Vector<Book>();
   }
   public void addBook( Book rhs ) {
      books.addElement( rhs );
   }
   public String toString() {
      String newline = System.getProperty( "line.separator" );
      StringBuffer buf = new StringBuffer();
      buf.append( "--- Books ---" ).append( newline );
      for( int i=0; i<books.size(); i++ ){
    	  Book book  = books.elementAt(i);
         buf.append( book.toString()).append( newline );
      }
      return buf.toString();
   }
}

 CreateCatalog.java

package com.xiao;
import java.io.IOException;
import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.SAXException;

public class CreateCatalog {
	protected  Catalog ct;
	
	public void SetCatalog(Catalog ol){
		
		ct = ol;
	}
	
	public Catalog GetCatalog(){
		
		return this.ct;
	}
	public String toString() {
		
		return ct.toString();
		}
	
	public Digester createStartDigester()throws IOException, SAXException
	{
		Digester digester = new Digester();
		digester.setValidating( false );
		//解析XML时,遇到catalog,就实例化一个com.xiao.Catalog对象,并且压栈
		digester.addObjectCreate( "catalog", "com.xiao.Catalog");
		//对catalog,调用栈的次top对象(现在还没有压入,父对象)的SetCatalog函数。
		//passing the element that is on the top of the stack, which must be of type com.xiao.Catalog
		//This is the rule that causes the parent/child relationship to be created.
		digester.addSetNext("catalog", "SetCatalog", "com.xiao.Catalog");
		
		digester.addObjectCreate( "catalog/book","com.xiao.Book");
		
		//对rule,调用当前top object的setAuthor函数,参数个数为1
		digester.addCallMethod("catalog/book/author", "setAuthor",1);
		//对rule,添加第一个参数值
		digester.addCallParam("catalog/book/author", 0);
		digester.addCallMethod("catalog/book/title", "setTitle",1);
		digester.addCallParam("catalog/book/title", 0);
		//此时次top的object就是com.xiao.Catalog,调用它的addBook函数,将com.xiao.Book传入
		digester.addSetNext("catalog/book", "addBook", "com.xiao.Book");
		
		return (digester);
	}
}

 TestDigester.java

package com.xiao;

import org.apache.tomcat.util.digester.Digester;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.*;
public class TestDigester {
	
	public static void main(String[] args) throws IOException, SAXException  {
		// TODO Auto-generated method stub
		CreateCatalog cc = new CreateCatalog();
		Digester digester = cc.createStartDigester();	
		String configFile = "xiapingtest/ts.xml";	
		InputSource inputSource = null;
		InputStream inputStream = null;
		File file = new File(System.getProperty("user.dir"),configFile);
		inputSource = new InputSource("file://" + file.getAbsolutePath());
		inputStream = new FileInputStream(file);
		inputSource.setByteStream(inputStream);
		//在加入CreateCatalog对象,这个是第一个压入的对象
		digester.push(cc);
		//处理xml文件,逐个加入对象
		digester.parse(inputSource);		
		System.out.println(cc.toString());
	}

}

 部分解释以及在代码注释里。

你可能感兴趣的:(apache,tomcat,xml)