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

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

 

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

 

Java代码   收藏代码
  1. public   void  load() {  
  2.   
  3.         long  t1 = System.nanoTime();  
  4.         initDirs();  
  5.         initNaming();  
  6.         Digester digester = createStartDigester();  
  7.         ........  
  8.        digester.push(this );  
  9.        digester.parse(inputSource);  
  10.         ........  
  11.         getServer().init();  
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对象。形同:

 

Java代码   收藏代码
  1. // Configure the actions we will be using   
  2. digester.addObjectCreate("Server" ,  
  3.                          "org.apache.catalina.core.StandardServer" ,  
  4.                          "className" );  
  5. digester.addSetProperties("Server" );  
  6. digester.addSetNext("Server" ,  
  7.                     "setServer" ,  
  8.                     "org.apache.catalina.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代码   收藏代码
  1. <? xml   version = "1.0" ?>   
  2. < catalog   library = "somewhere" >   
  3.    < book >   
  4.       < author > Author 1 </ author >   
  5.       < title > Title 1 </ title >   
  6.    </ book >   
  7.    < book >   
  8.       < author > Author 2 </ author >   
  9.       < title > His One Book </ title >   
  10.    </ book >   
  11.    < book >   
  12.       < author > Author 3 </ author >   
  13.       < title > His Other Book </ title >   
  14.    </ book >   
  15. </ catalog >   
<?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

 

Java代码   收藏代码
  1. package  com.xiao;  
  2.   
  3. public   class  Book {  
  4.        private  String author;  
  5.        private  String title;  
  6.   
  7.        public  Book() {}  
  8.   
  9.        public   void  setAuthor( String rhs ) { author = rhs; }  
  10.        public   void  setTitle(  String rhs ) { title  = rhs; }  
  11.          
  12.        public  String getAuthor(  ) {  return  author; }  
  13.        public  String getTitle(  ) {  return  title; }  
  14.          
  15.        public  String toString() {  
  16.           return   "Book: Author='"  + author +  "' Title='"  + title +  "'" ;  
  17.        }  
  18.     }  
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

 

Java代码   收藏代码
  1. package  com.xiao;  
  2. import  java.util.Vector;  
  3. public   class  Catalog {  
  4.    private  Vector<Book> books;  
  5.   
  6.    public  Catalog() {  
  7.       books = new  Vector<Book>();  
  8.    }  
  9.    public   void  addBook( Book rhs ) {  
  10.       books.addElement( rhs );  
  11.    }  
  12.    public  String toString() {  
  13.       String newline = System.getProperty( "line.separator"  );  
  14.       StringBuffer buf = new  StringBuffer();  
  15.       buf.append( "--- Books ---"  ).append( newline );  
  16.       for int  i= 0 ; i<books.size(); i++ ){  
  17.           Book book  = books.elementAt(i);  
  18.          buf.append( book.toString()).append( newline );  
  19.       }  
  20.       return  buf.toString();  
  21.    }  
  22. }  
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

 

Java代码   收藏代码
  1. package  com.xiao;  
  2. import  java.io.IOException;  
  3. import  org.apache.tomcat.util.digester.Digester;  
  4. import  org.xml.sax.SAXException;  
  5.   
  6. public   class  CreateCatalog {  
  7.     protected   Catalog ct;  
  8.       
  9.     public   void  SetCatalog(Catalog ol){  
  10.           
  11.         ct = ol;  
  12.     }  
  13.       
  14.     public  Catalog GetCatalog(){  
  15.           
  16.         return   this .ct;  
  17.     }  
  18.     public  String toString() {  
  19.           
  20.         return  ct.toString();  
  21.         }  
  22.       
  23.     public  Digester createStartDigester() throws  IOException, SAXException  
  24.     {  
  25.         Digester digester = new  Digester();  
  26.         digester.setValidating( false  );  
  27.         //解析XML时,遇到catalog,就实例化一个com.xiao.Catalog对象,并且压栈   
  28.         digester.addObjectCreate( "catalog" "com.xiao.Catalog" );  
  29.         //对catalog,调用栈的次top对象(现在还没有压入,父对象)的SetCatalog函数。   
  30.         //passing the element that is on the top of the stack, which must be of type com.xiao.Catalog   
  31.         //This is the rule that causes the parent/child relationship to be created.   
  32.         digester.addSetNext("catalog" "SetCatalog" "com.xiao.Catalog" );  
  33.           
  34.         digester.addObjectCreate( "catalog/book" , "com.xiao.Book" );  
  35.           
  36.         //对rule,调用当前top object的setAuthor函数,参数个数为1   
  37.         digester.addCallMethod("catalog/book/author" "setAuthor" , 1 );  
  38.         //对rule,添加第一个参数值   
  39.         digester.addCallParam("catalog/book/author" 0 );  
  40.         digester.addCallMethod("catalog/book/title" "setTitle" , 1 );  
  41.         digester.addCallParam("catalog/book/title" 0 );  
  42.         //此时次top的object就是com.xiao.Catalog,调用它的addBook函数,将com.xiao.Book传入   
  43.         digester.addSetNext("catalog/book" "addBook" "com.xiao.Book" );  
  44.           
  45.         return  (digester);  
  46.     }  
  47. }  
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

 

Java代码   收藏代码
  1. package  com.xiao;  
  2.   
  3. import  org.apache.tomcat.util.digester.Digester;  
  4. import  org.xml.sax.InputSource;  
  5. import  org.xml.sax.SAXException;  
  6.   
  7. import  java.io.*;  
  8. public   class  TestDigester {  
  9.       
  10.     public   static   void  main(String[] args)  throws  IOException, SAXException  {  
  11.         // TODO Auto-generated method stub   
  12.         CreateCatalog cc = new  CreateCatalog();  
  13.         Digester digester = cc.createStartDigester();     
  14.         String configFile = "xiapingtest/ts.xml" ;     
  15.         InputSource inputSource = null ;  
  16.         InputStream inputStream = null ;  
  17.         File file = new  File(System.getProperty( "user.dir" ),configFile);  
  18.         inputSource = new  InputSource( "file://"  + file.getAbsolutePath());  
  19.         inputStream = new  FileInputStream(file);  
  20.         inputSource.setByteStream(inputStream);  
  21.         //在加入CreateCatalog对象,这个是第一个压入的对象   
  22.         digester.push(cc);  
  23.         //处理xml文件,逐个加入对象   
  24.         digester.parse(inputSource);          
  25.         System.out.println(cc.toString());  
  26.     }  
  27.   
  28. }  
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)