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

📄 xmlutilconfig.java

📁 一个“对象--XML 映射”(Object-Xml Mapping) 的类库。 它的目的是帮助开发者方便、快速的从XML 文件构建出Java 对象
💻 JAVA
字号:
/**
 * @author 沈东良 Edward Shen<a href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 * 2007-3-19 下午04:19:51
 */
package net.sf.oxmled.use.example;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.oxmled.common.config.IConfig;
import net.sf.oxmled.common.util.ClassLoaderUtil;
import net.sf.oxmled.xml.util.dom4j.IXmlUtil;
import net.sf.oxmled.xml.util.dom4j.XmlUtil;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.tree.DefaultElement;


/**
 * @author 沈东良 Edward Shen<a
 *         href="mailto:shendl_s@hotmail.com">shendl_s@hotmail.com</a>
 *         2007-3-19 下午04:19:51 这个类,读取xml格式的配置文件
 */
public class XmlUtilConfig implements IConfig{
	//public final static String defaultPath="config/config.xml";
	/**
	 * 配置文件的路径
	 */
	private String configFile=IConfig.defaultPath;
	/**
	 * 
	 */
	private net.sf.oxmled.xml.util.dom4j.IXmlUtil xmlUtil=null;
	

	/**
	 * @return the xmlUtil
	 */
	public IXmlUtil getXmlUtil() {
		synchronized(this){
		if(this.xmlUtil==null){
			this.xmlUtil=new XmlUtil();
		}
		}
		return xmlUtil;
	}
	/**
	 * @param xmlUtil the xmlUtil to set
	 */
	public void setXmlUtil(IXmlUtil xmlUtil) {
		this.xmlUtil = xmlUtil;
	}
	/**
	 * 用于修改或添加配置文件的Map项目。
	 * 
	 * @param key  键
	 * @param value  单个的值
	 * @throws DocumentException 
	 * @throws IOException 
	 * @throws URISyntaxException 
	 */
	public void put(String key, String value) throws DocumentException, IOException, URISyntaxException {
		//throw new UnsupportedOperationException();
		/**
		 * 1,得到Document
		 * 2,查找map选项
		 * 3,修改
		 * 或者
		 * 增加
		 * 
		 * 
		 */
	
		if(this.getMap().containsKey(key)){
			this.getMap().put(key, value);
		   Iterator it=this.getDocument().selectNodes("//maps/map").iterator();
		   while(it.hasNext()){
			   Element tmp=(Element)it.next();
			   if(tmp.elementTextTrim("key").equals(key.trim())){
				   Element  valueElement=(Element)tmp.elements("value").get(0);
				   valueElement.setText(value);
				   
			   }
			   
			   
		   }
		   
		}else{
			this.getMap().put(key, value);
			Element maps=(Element)this.getDocument().selectSingleNode("//maps");
			Element mapElement=new DefaultElement("map");
			Element keyElement=new DefaultElement("key");
			Element valueElement=new DefaultElement("value");
			keyElement.setText(key);
			valueElement.setText(value);
			mapElement.add(keyElement);
			mapElement.add(valueElement);
			maps.add(mapElement);
			
			
		}
		//XmlUtil.writeToCompactFormat(instance.getDocument(), defaultPath);
		this.getXmlUtil().writeToPrettyPrint(this.getDocument(), ClassLoaderUtil
				.getResource(this.getConfigFile()).toURI());
	}
	/**
	 * 用于修改或添加配置文件的Map项目。
	 * @param key  键
	 * @param values  多个的值
	 * @throws URISyntaxException 
	 * @throws DocumentException 
	 * @throws IOException 
	 */
	public void put(String key,List<String> values) throws IOException, DocumentException, URISyntaxException{
		
		if(this.getMultiMap().containsKey(key)){
			this.getMultiMap().put(key, values);
		   Iterator it=this.getDocument().selectNodes("//maps/map").iterator();
		   while(it.hasNext()){
			   Element tmp=(Element)it.next();
			   if(tmp.elementTextTrim("key").equals(key.trim())){
				   /**
				    * 删除原来的values
				    */
				   List oldValues=tmp.elements("values");
				   for(int i=0;i<oldValues.size();i++){
					   tmp.remove((Element)oldValues.get(i));
				   }
				   
				   
				   /**
				    * 根据values生成Element
				    */
				   for(int i=0;i<values.size();i++){
					   Element valuesElement=new DefaultElement("values");
					   valuesElement.setText((String)values.get(i));
					   tmp.add(valuesElement);
				   }
				   
				   
			   }
			   
			   
		   }
		   
		}else{
			this.getMultiMap().put(key, values);
			Element maps=(Element)this.getDocument().selectSingleNode("//maps");
			Element mapElement=new DefaultElement("map");
			Element keyElement=new DefaultElement("key");
			keyElement.setText(key);
			mapElement.add(keyElement);
			 /**
			    * 根据values生成Element
			    */
			   for(int i=0;i<values.size();i++){
				   Element valuesElement=new DefaultElement("values");
				   valuesElement.setText((String)values.get(i));
				   mapElement.add(valuesElement);
			   }
					
			
			maps.add(mapElement);
			
			
		}
		//XmlUtil.writeToCompactFormat(instance.getDocument(), defaultPath);
		this.getXmlUtil().writeToPrettyPrint(this.getDocument(), ClassLoaderUtil
				.getResource(this.getConfigFile()).toURI());
		
	}

	/**
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		return (this.getMap().get(key)).trim();
	}
	/**
	 * 
	 * @param key
	 * @return
	 */
	public List<String> gets(String key) {
		return this.getMultiMap().get(key);
	}
	

	

	/**
	 * 读取map的东西
	 */
	private Map<String, String> map = new Hashtable<String, String>();

	/**
	 * 这个Map中的值是集合,也就是说里面有可重复的数据
	 */
	private Map<String, List<String>> multiMap = new Hashtable<String, List<String>>();

	/**
	 * @return multiMap
	 */
	public Map<String, List<String>> getMultiMap() {
		return multiMap;
	}

	/**
	 * @param multiMap
	 *            要设置的 multiMap
	 */
	public void setMultiMap(Map<String, List<String>> multiMap) {
		this.multiMap = multiMap;
	}

	/**
	 * @return map
	 */
	public Map<String, String> getMap() {
		return map;
	}

	/**
	 * @param map
	 *            要设置的 map
	 */
	public void setMap(Map<String, String> map) {
		this.map = map;
	}

	

	


	/**
	 * 禁止在类外部使用构造器
	 */
	private XmlUtilConfig() {
		/*
		 * 
		 */
	}

	/**
	 * 使用单例
	 */
	private static IConfig instance = null;
//OXmlEdConfig.instance = new OXmlEdConfig();
	/**
	 * 返回单例的对象----在多线程中可能出错,请使用getNewInstance
	 * 
	 * @return
	 * @throws Exception 
	 */
	public static IConfig getInstance(String configFile) throws Exception {
		/**
		 * 如果没有实现单例
		 * 
		 */
		synchronized(XmlUtilConfig.class){
		  if (XmlUtilConfig.instance == null) {
			XmlUtilConfig.instance = new XmlUtilConfig();
			XmlUtilConfig.instance.setConfigFile(configFile);
			XmlUtilConfig.instance.parseConfigXml();
		  }
		}
		return XmlUtilConfig.instance;
	}
	/**
	 * 返回单例的对象----在多线程中可能出错,请使用getNewInstance
	 * 
	 * @return
	 * @throws Exception 
	 */
	public static IConfig getInstance() throws Exception {
		/**
		 * 如果没有实现单例
		 * 
		 */
		synchronized(XmlUtilConfig.class){
		  if (XmlUtilConfig.instance == null) {
			XmlUtilConfig.instance = new XmlUtilConfig();
			XmlUtilConfig.instance.parseConfigXml();
		  }
		}
		return XmlUtilConfig.instance;
	}
	/**
	 * 得到多例
	 * 
	 * @return
	 * @throws Exception 
	 */
	public static IConfig getNewInstance(String configFile) throws Exception {
		IConfig instance = new XmlUtilConfig();
		instance.setConfigFile(configFile);
		instance.parseConfigXml();
		return instance;
	}
	/**
	 * 得到多例
	 * 
	 * @return
	 * @throws Exception 
	 */
	public static IConfig getNewInstance() throws Exception {
		IConfig instance = new XmlUtilConfig();
		instance.parseConfigXml();
		return instance;
	}

	/**
	 * 解析xml配置文件
	 * 
	 * @throws DocumentException
	 * 
	 */
	public void parseConfigXml() throws DocumentException {
		
		/**
		 * 读取Map-----煎不能重复
		 * 
		 */
		List maps = this.getDocument().selectNodes("//maps/map");
		Iterator it = maps.iterator();
		while (it.hasNext()) {
			Object tmp = it.next();
			Element map = null;
			if (tmp instanceof Element) {
				map = (Element) tmp;
				String key = map.elementTextTrim("key");

				if (!key.equals("")) {
					//一对一
					
					String value = map.elementTextTrim("value");
					if(value!=null){
					   this.getMap().put(key, value);
					}
					//一对多
					List values=map.elements("values");
					if(values.size()>0){
					List valuesList=new ArrayList();
					for(int i=0;i<values.size();i++){
						Element valuesTmp=(Element)values.get(i);
						valuesList.add(valuesTmp.getTextTrim());
					}
					this.getMultiMap().put(key, valuesList);
					}
				}
				
				
		
				
				
			}

		}

		
		

		

	}

	/**
	 * 解析xml配置文件产生的对象
	 */
	private Document document = null;

	/**
	 * 一个对象只解析一次
	 * 
	 * @return document
	 * @throws DocumentException
	 */
	public Document getDocument() throws DocumentException {
		synchronized(this){
		if (this.document == null) {
			this.document = this.getXmlUtil().parse(ClassLoaderUtil
					.getResource(this.getConfigFile()));

		}
		}
		return document;
	}

	/**
	 * @param document
	 *            要设置的 document
	 */
	public void setDocument(Document document) {
		this.document = document;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*
		 * 
		 */
		try {
			IConfig instance=XmlUtilConfig.getInstance();
			try {
				try {
					instance.put("a", "他aaa");
					List<String> values=new ArrayList<String>();
					values.add("我们");
					values.add("dsdf你sfsdfsfsss");
					instance.put("b", values);
					Element e=new DefaultElement("a");
					e.elements();
				} catch (URISyntaxException e) {
					/*
					*
					*/
					e.printStackTrace();
				}
			} catch (IOException e) {
				/*
				*
				*/
				e.printStackTrace();
			}
		} catch (DocumentException e) {
			/*
			 * 
			 */
			e.printStackTrace();
		} catch (Exception e) {
			/* TODO 自动生成 catch 块
			 *
			 */
			e.printStackTrace();
		}

	}
	/**
	 * @return the configFile
	 */
	public String getConfigFile() {
		return configFile;
	}
	/**
	 * @param configFile the configFile to set
	 */
	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}

	

}

⌨️ 快捷键说明

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