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());
	}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值