⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configutil.java

📁 mvc框架
💻 JAVA
字号:
package com.strutslet.util;

import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.strutslet.model.ActionForward;
import com.strutslet.model.ActionModel;

/**
 * xml解析工具类
 * 
 * @author 庄晓丹
 * 
 */
public class ConfigUtil {
	
	private static final Log log=LogFactory.getLog(ConfigUtil.class);
	
	private String file_path;
	
	protected static final ConfigUtil single=new ConfigUtil();
	
	private ConfigUtil(){}
	
	public static ConfigUtil newInstance(){
		return single;
	}
	public String getFile_path() {
		return file_path;
	}
	public void setFile_path(String file_path) {
		this.file_path = file_path;
	}
    public Map<String, ActionModel> parse(String file_path,ServletContext context)throws Exception{
    	this.file_path=file_path;
    	InputStream is=context.getResourceAsStream(file_path);
    	log.info("正在读取配置文件:"+file_path);
    	return parseXML(is);
    }
    
    private Map<String, ActionModel> parseXML(InputStream is)throws Exception{
    	  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
         
          Document doc = builder.parse(is);
          NodeList actions=doc.getElementsByTagName("action");
          int size=actions.getLength();
          
          Map<String, ActionModel> result=new HashMap<String, ActionModel>();
          ActionModel actionModel=null;
          Map<String, ActionForward> forwards=null;
          log.info("正在解析配置文件:"+file_path);
          for(int i=0;i<size;i++){
        	  Element xml_action=(Element)actions.item(i);
        	  actionModel=new ActionModel();  
        	  actionModel.setClassName(xml_action.getAttribute("class"));
        	  actionModel.setPath(xml_action.getAttribute("path"));
        	  
        	  NodeList xml_forwards=xml_action.getElementsByTagName("forward");
        	  forwards=new HashMap<String, ActionForward>();
        	  for(int j=0;j<xml_forwards.getLength();j++){
        		  Element forward=(Element)xml_forwards.item(j);
        		  forwards.put(forward.getAttribute("name"), new ActionForward(forward.getAttribute("name"),forward.getAttribute("url")));        		  
        	  }
        	  actionModel.setForwards(forwards);
        	  result.put(actionModel.getPath(), actionModel);
          }
          return result;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -