223 lines
5.7 KiB
Java
223 lines
5.7 KiB
Java
package com.amarsoft.aims.util;
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.io.Writer;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Comparator;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import com.thoughtworks.xstream.XStream;
|
|
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
|
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
|
|
import com.thoughtworks.xstream.io.json.JsonWriter;
|
|
import com.thoughtworks.xstream.io.xml.DomDriver;
|
|
|
|
/**
|
|
* xml文件操作类
|
|
* @author yangsong
|
|
* @date 2013/11/07
|
|
*/
|
|
public class XMLHelper {
|
|
private static final Log log = LogFactory.getLog(XMLHelper.class);
|
|
|
|
/**
|
|
* 保存对象至XML文件
|
|
* @param file 文件
|
|
* @param obj 要存储的对象
|
|
* @param alias xml节点别名映射
|
|
*/
|
|
public static void saveToXMLFile(File file,Object obj,Map<String,Class<?>> alias){
|
|
//保存
|
|
ObjectOutputStream out = null;
|
|
try {
|
|
if(!file.exists())file.createNewFile();
|
|
XStream xstream = new XStream(new DomDriver());
|
|
alias(xstream,alias);
|
|
out = xstream.createObjectOutputStream(new FileWriter(file));
|
|
out.writeObject(obj);
|
|
} catch (IOException e) {
|
|
log.error("保存对象至XML出错",e);
|
|
} finally{
|
|
if(out != null){
|
|
try { out.close(); } catch (IOException e) { }
|
|
}
|
|
}
|
|
}
|
|
/**
|
|
* 从文件中读取对象
|
|
* @param file
|
|
* @param alias
|
|
* @return
|
|
*/
|
|
public static Object readFromXMLFile(File file,Map<String,Class<?>> alias){
|
|
XStream xstream = new XStream(new DomDriver());
|
|
alias(xstream,alias);
|
|
ObjectInputStream ois = null;
|
|
Object obj = null;
|
|
try {
|
|
if(!file.exists())file.createNewFile();
|
|
ois = xstream.createObjectInputStream(new FileReader(file));
|
|
obj = ois.readObject();
|
|
} catch (FileNotFoundException e) {
|
|
log.warn("Task记录文件不存在",e);
|
|
} catch (IOException e) {
|
|
log.error("Task记录文件读取出错",e);
|
|
} catch (ClassNotFoundException e) {
|
|
log.error("从文件中读取的java类不存在",e);
|
|
} catch(Exception e){
|
|
log.warn("从文件中没有读取到任何对象",e);
|
|
}finally{
|
|
if(ois!=null){
|
|
try { ois.close(); } catch (IOException e) { }
|
|
}
|
|
}
|
|
return obj;
|
|
}
|
|
/**
|
|
* 别名
|
|
* @param xstream
|
|
* @param alias
|
|
*/
|
|
private static void alias(XStream xstream,Map<String,Class<?>> alias){
|
|
if(alias==null||alias.size()==0)return;
|
|
Iterator<Entry<String, Class<?>>> iterator = alias.entrySet().iterator();
|
|
while(iterator.hasNext()){
|
|
Entry<String, Class<?>> entry = iterator.next();
|
|
xstream.alias(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 把对象转换为JSON字符串
|
|
* @param obj 需要转换的对象
|
|
* @param dropRootNode 是否删除根节点
|
|
* @param alias 别名设置
|
|
* @return
|
|
*/
|
|
public static String getAsJsonString(Object obj,boolean dropRootNode,Map<String,Class<?>> alias){
|
|
String strJson = null;
|
|
JsonHierarchicalStreamDriver streamDriver = null;
|
|
if(dropRootNode){ //如果需要删除根节点,则重写方法
|
|
streamDriver = new JsonHierarchicalStreamDriver() {
|
|
public HierarchicalStreamWriter createWriter(Writer out) {
|
|
return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
|
|
}
|
|
};
|
|
}else{
|
|
streamDriver = new JsonHierarchicalStreamDriver();
|
|
}
|
|
XStream xstream = new XStream(streamDriver);
|
|
xstream.setMode(XStream.NO_REFERENCES);
|
|
// xstream.setMode(XStream.ID_REFERENCES);
|
|
alias(xstream,alias);
|
|
strJson = xstream.toXML(obj);
|
|
return strJson;
|
|
}
|
|
/**
|
|
* 把对象转为XML字串
|
|
* @param obj
|
|
* @param alias
|
|
* @return
|
|
*/
|
|
public static String getAsXMLString(Object obj,Map<String,Class<?>> alias){
|
|
String strJson = null;
|
|
XStream xstream = new XStream(new DomDriver());
|
|
xstream.setMode(XStream.NO_REFERENCES);
|
|
// xstream.setMode(XStream.ID_REFERENCES);
|
|
alias(xstream,alias);
|
|
strJson = xstream.toXML(obj);
|
|
return strJson;
|
|
}
|
|
/**
|
|
* 把对象转换为JSON字符串
|
|
* @param obj
|
|
* @return
|
|
*/
|
|
public static String getAsJsonString(Object obj){
|
|
return getAsJsonString(obj,true,null);
|
|
}
|
|
/**
|
|
* 创建一个XPath的XMLFile对象
|
|
* @param file
|
|
* @param xPathExpr
|
|
* @return
|
|
*/
|
|
public static XMLParse createXMLFile(File file){
|
|
return new XMLParse(file);
|
|
}
|
|
/**
|
|
* 使用xml字串构建一个xmlParse对象
|
|
* @param xmlString
|
|
* @return
|
|
*/
|
|
public static XMLParse createXMLFile(String xmlString){
|
|
return new XMLParse(xmlString);
|
|
}
|
|
/**
|
|
* 从XML文件中创建出列表对象列表
|
|
* @param clazz 要创建的类名
|
|
* @param fileName 文件名
|
|
* @param create 如果文件不存在,是否创建文件
|
|
* @param alias 读取别名
|
|
* @return
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public static <T> List<T> createObjectsFromFile(Class<T> clazz,String fileName,boolean create,Map<String,Class<?>> alias){
|
|
File file = new File(fileName);
|
|
if(!file.exists()&&create){
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
log.error("创建新文件失败",e);
|
|
}
|
|
}
|
|
|
|
List<T> objectList = (List<T>)XMLHelper.readFromXMLFile(file, alias);
|
|
if(objectList==null) objectList = new ArrayList<T>();
|
|
return objectList;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* 保存对象列表
|
|
* @param clazz 类名
|
|
* @param fileName 文件名
|
|
* @param objectList 对象列表
|
|
* @param alias 写入时的别名
|
|
* @param comparator 排序接口
|
|
*/
|
|
public static <T> void saveObjectsToXMLFile(Class<T> clazz,String fileName,List<T> objectList,Map<String,Class<?>> alias,Comparator<T> comparator){
|
|
//如果传入排序接口,则进行一次排序操作
|
|
if(comparator!=null){
|
|
Collections.sort(objectList,comparator);
|
|
}
|
|
//保存
|
|
File file = new File(fileName);
|
|
if(!file.exists()){
|
|
try {
|
|
file.createNewFile();
|
|
} catch (IOException e) {
|
|
log.error("创建新文件失败",e);
|
|
}
|
|
}
|
|
XMLHelper.saveToXMLFile(file, objectList, alias);
|
|
}
|
|
|
|
}
|
|
|