Java调用poi3.8编写的一个简单XmlCache缓存工具

使用poi3.8编写的一个XMLCache工具,通过将xml缓存到项目中提升访问性能.有不足之处还望大家提出来.

首先是data缓存类:

package com.scxn.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * XML缓存对象
 * @author LeiYong
 *
 */
public class XNModelXMLCache {
	private String noteName;
	private String text;
	private boolean isUpdate = false;
	private Map<String,String> attributeMap = new Hashtable<String, String>(); 
	private Map<String,List<XNModelXMLCache>> childrenMap = new Hashtable<String, List<XNModelXMLCache>>();


	/**
	 * 
	 * @param treeName
	 *            目录/文件名
	 */
	public XNModelXMLCache(String noteName) {
		this.noteName = noteName;
		isUpdate = false;
	}


	//get and set
	public String getNoteName() {
		return noteName;
	}

	public void setNoteName(String noteName) {
		if (this.noteName.equals(noteName)) {
			this.noteName = noteName;
			this.isUpdate = true;
		}
	}

	/**
	 * 添加属性
	 * @param name
	 * @param value
	 */
	public void putAttribute(String name,String value){
		attributeMap.put(name, value);
		this.isUpdate = true;
	}
	public String getAttribute(String name){
		return attributeMap.get(name);
	}

	/**
	 * 添加子节点
	 * @param name
	 * @param cache
	 */
	public void putChildren(String name,List<XNModelXMLCache> list) {
		childrenMap.put(name,list);
		isUpdate = true;
	}
	/**
	 * 遍历测试
	 */
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("noteName:"+noteName+",text:"+text+",Attribute(");
		Set<String> attrSet =  attributeMap.keySet();
		Iterator<String> attrIterator = attrSet.iterator();
		while (attrIterator.hasNext()) {
			String key = attrIterator.next();
			sb.append(key+":"+getAttribute(key));
		}
		sb.append(")----children(");
		Set<String> childSet =  childrenMap.keySet();
		Iterator<String> childIterator = childSet.iterator();
		while (childIterator.hasNext()) {
			String key = childIterator.next();
			List<XNModelXMLCache> list = getChildren(key);
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					sb.append(list.get(i).toString());
				}
			}
		}
		return sb.toString();
	}
	public List<XNModelXMLCache> getChildren(String name) {
		return childrenMap.get(name);
	}
	/**
	 * 检查更新
	 * @return 是否需要更新
	 */
	@SuppressWarnings("unchecked")
	public boolean getIsUpdate() {
		if (!isUpdate) {
			Object[] values = childrenMap.values().toArray();
			for (int i = 0; i < values.length; i++) {
				List<XNModelXMLCache> list = (List<XNModelXMLCache>)values[i];;
				for (int j = 0; j < list.size(); j++) {
					XNModelXMLCache c = list.get(j);
						if(c.getIsUpdate()){
							return true;
						}
				}
			}
		}
		return isUpdate;
	}
	/**
	 * 获取key集合
	 * @return list<String key>
	 */
	public List<String> getAttributeKeys(){
		Set<String> attrSet =  attributeMap.keySet();
		List<String> list = new ArrayList<String>(attrSet);
		return list;
	}
	/**
	 * 获取key集合
	 * @return list<String key>
	 */
	public List<String> getChildrenKeys(){
		Set<String> attrSet =  childrenMap.keySet();
		List<String> list = new ArrayList<String>(attrSet);
		return list;
	}
	public int getAttributeCount(){
		return attributeMap.size();
	}
	public int getChildrenCount(){
		return childrenMap.size();
	}
	public void setIsUpdate(boolean isUpdate){
		this.isUpdate = isUpdate;
	}
	public String getText() {
		return this.text;
	}
	public void setText(String text) {
		isUpdate = true;
		this.text = text;
	}
}
XML缓存管理工具类:

package com.scxn.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.scxn.model.XNModelXMLCache;

/**
 * XML缓存管理工具
 * @author ly
 *
 */
public class XMLCacheUtil {
	private static Map<String, XNModelXMLCache> documentMap = new Hashtable<String, XNModelXMLCache>();

	/**
	 * 加载配置文件并返回
	 * 
	 * @param xmlFilePath
	 *            地址不存在xml则返回null
	 * @return XNModelProperties
	 */
	public static XNModelXMLCache loadXml(String xmlFilePath) {
		File file = new File(xmlFilePath);
		if (!file.isFile() || !file.getName().endsWith(".xml")) {
			return null;
		}
		XNModelXMLCache xmlCache = documentMap.get(xmlFilePath);
		try {
			if (xmlCache == null) {
				Document document = Dom4jUtil.loadXML(xmlFilePath);
				Element root = document.getRootElement();// 根节点
				xmlCache = new XNModelXMLCache(root.getName());
				loadXMLToObject(root, xmlCache);
			}
		} catch (IOException e) {
			PrintUtil.printError(ExcelReportUtil.class, e.getMessage());
		} catch (DocumentException e) {
			PrintUtil.printError(ExcelReportUtil.class, e.getMessage());
		}
		return xmlCache;
	}

	/**
	 * 将XNModelXMLCache对象写入xml文件
	 * 
	 * @param xmlCache
	 *            XNModelXMLCache对象
	 * @param xmlFilePath
	 *            xml文件位置
	 * @param checkUpdate
	 *            是否检查对象属性变动
	 * @return 是否成功
	 */
	public static boolean writeXml(XNModelXMLCache xmlCache,
			String xmlFilePath, boolean checkUpdate) {
		//创建文件目录,验证文件路径是否合法
		if (FileUtil.checkFileAndCreateDirectory(xmlFilePath, ".xml")) {
			try {
				if (!checkUpdate || xmlCache.getIsUpdate()) {
					Document document = DocumentHelper.createDocument();
					document.setXMLEncoding("utf-8");
					Element root = document.addElement(xmlCache.getNoteName());
					loadObjectToXML(root, xmlCache);
					OutputFormat format = OutputFormat.createPrettyPrint();
					XMLWriter writer = new XMLWriter(
							new FileWriter(xmlFilePath), format);
					writer.write(document);
					writer.close();
					return true;
				}
			} catch (IOException e) {
				PrintUtil.printError(ExcelReportUtil.class, e.getMessage());
			}
		}
		return false;
	}

	/**
	 * XNModelXMLCache转节点
	 * 
	 * @param root
	 * @param xmlCache
	 */
	private static void loadObjectToXML(Element root, XNModelXMLCache xmlCache) {
		for (int i = 0; i < xmlCache.getAttributeCount(); i++) {
			List<String> keys = xmlCache.getAttributeKeys();
			String key = keys.get(i);
			root.addAttribute(key, xmlCache.getAttribute(key));
		}
		if (xmlCache.getText() != null) {
			root.setText(xmlCache.getText());
		}
		for (int i = 0; i < xmlCache.getChildrenCount(); i++) {
			List<String> keys = xmlCache.getChildrenKeys();
			String key = keys.get(i);
			List<XNModelXMLCache> cacheList = xmlCache.getChildren(key);
			if (cacheList == null) {
				continue;
			}
			for (int j = 0; j < cacheList.size(); j++) {
				Element e = root.addElement(key);
				loadObjectToXML(e, cacheList.get(j));
			}
		}
	}

	/**
	 * 节点转XNModelXMLCache
	 * 
	 * @param root
	 *            Element
	 * @param xmlCache
	 */
	@SuppressWarnings("unchecked")
	private static void loadXMLToObject(Element root, XNModelXMLCache xmlCache) {
		for (int i = 0; i < root.attributeCount(); i++) {
			xmlCache.putAttribute(root.attribute(i).getName(), root
					.attribute(i).getStringValue());
		}
		xmlCache.setText(root.getText());
		Iterator it = root.elementIterator();
		while (it.hasNext()) {
			Element e = (Element) it.next();
			XNModelXMLCache cache = new XNModelXMLCache(e.getName());
			loadXMLToObject(e, cache);
			List<XNModelXMLCache> cacheList = xmlCache.getChildren(e.getName());
			if (cacheList == null) {
				cacheList = new ArrayList<XNModelXMLCache>();
				xmlCache.putChildren(e.getName(), cacheList);
			}
			cacheList.add(cache);
		}
		xmlCache.setIsUpdate(false);
	}
<span style="white-space:pre">	/**
<span style="white-space:pre">	</span> * 测试
<span style="white-space:pre">	</span> * @param args
<span style="white-space:pre">	</span> */</span>
	public static void main(String[] args) {
		XNModelXMLCache xmlCache = loadXml("e:/reportType.xml");
		System.out.println(xmlCache.toString());
		System.out.println(xmlCache.getIsUpdate());
		System.out.println(writeXml(xmlCache, "e:/config2.xml", false));
		// System.out.println(JSonUtil.objectToJSONArray(xmlCache));
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值