使用Digester解析XML文件和JAVA对象的映射

 
package  cn.hxex.exam.config;

import  java.util.Hashtable;

import  cn.hxex.exam.exception.ExamException;

public   class  ExamConfig  {
   
private Hashtable daos;
   
public ExamConfig(){
       daos
=new Hashtable();
   }

   
public void addDao(DAOConfig bean){
       daos.put(bean.getId(), bean);
   }

   
   
public DAOConfig getDAOConfig(String name){
       DAOConfig config
=(DAOConfig)daos.get(name);
       
if(config==null){
           
throw new ExamException("couldn't find the bean:"+name);
       }

       
return config;
   }

}


package  cn.hxex.exam.config;

import  java.io.InputStream;

import  org.apache.commons.digester.Digester;

public   class  ExamConfigUtil  {
   
public static final String DEFAULT_CONFIG_FILE="ExamConfig.xml";
   
public static final ExamConfig MESSAGE_CONFIG;
   
   
static{
       Digester digester
=new Digester();
       digester.setValidating(
false);
       
       
//初始化根配置对象
       digester.addObjectCreate("config""cn.hxex.exam.config.Examconfig");
       digester.addSetProperties(
"config");
       
       
//初始化DAOConfig对象
       digester.addObjectCreate("config/daos/dao""cn.hxex.exam.config.DAOConfig");
       digester.addSetProperties(
"config/daos/dao");
       digester.addSetNext(
"config/dao/dao""addDao","cn.hxex.exam.config.DAOConfig");
       
       
//读取配置文件
       ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
       InputStream in
=classLoader.getResourceAsStream(DEFAULT_CONFIG_FILE);
       ExamConfig config
=null;
       
try {
          
if(in!=null){
              config
=(ExamConfig)digester.parse(in);
          }

        }
 catch (Exception e) {
          e.printStackTrace();
       }

        MESSAGE_CONFIG
=config;    
   }

   
   
public static ExamConfig getConfig(){
       
return ExamConfigUtil.MESSAGE_CONFIG;
   }

   
   
public static DAOConfig getDAOConfig(String name){
       
return MESSAGE_CONFIG.getDAOConfig(name);
   }

   
}


< config >
    
< daos >
        
< dao  id ="UserDAO"  type ="cn.hxex.exam.dao.hibernate.UserDAOHibernate"   />
        
< dao  id ="TeacherDAO"  type ="cn.hxex.exam.dao.hibernate.TeacherDAOHibernate"   />
        
< dao  id ="TestPaperDAO"  type ="cn.hxex.exam.dao.hibernate.TestPaperDAOHibernate"   />
        
< dao  id ="RoleDAO"  type ="cn.hxex.exam.dao.hibernate.RoleDAOHibernate"   />
        
< dao  id ="ClassesDAO"  type ="cn.hxex.exam.dao.hibernate.ClassesDAOHibernate"   />
        
< dao  id ="StudentDAO"  type ="cn.hxex.exam.dao.hibernate.StudentDAOHibernate"   />
        
< dao  id ="SelectQuestionDAO"  type ="cn.hxex.exam.dao.hibernate.SelectQuestionDAOHibernate"   />
        
< dao  id ="YesNoQuestionDAO"  type ="cn.hxex.exam.dao.hibernate.YesNoQuestionDAOHibernate"   />
    
</ daos >
    
< cache  type ="cn.hxex.exam.cache.ehcache.EHCache" >
        
< property  name ="cachename"  value ="cache"   />
    
</ cache >
    
< sysconfig >
        
< property  name ="superuser"  value ="super,test"   />
    
</ sysconfig >
</ config >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值