package cn.com.tiptop.outsyscheck.util;
import IceInternal.Ex;
/*import cn.com.tiptop.common.util.ExceptionUtil;
import cn.com.tiptop.gap.sqlite.synctaskconfig.entity.DBXml;
import cn.com.tiptop.gap.sqlite.synctaskconfig.entity.Tasks;*/
import cn.com.tiptop.outsyscheck.entity.DBXml;
import cn.com.tiptop.outsyscheck.entity.Tasks;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/**
* xml生成工具
* @author admin
*
*/
public class JaxbUtil {
private static final Logger logger = LoggerFactory.getLogger(JaxbUtil.class);
/**
* 生成xml
* @param datas 数据
* @param filePath 文件路径
* @return
*/
public static <T> boolean createXml(T datas,String filePath) {
try {
File f = new File(filePath);
if(!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
if(!f.exists()) {
f.createNewFile();
}
}
FileWriter fw = new FileWriter(f);
JAXBContext context = JAXBContext.newInstance(datas.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // 防止文件中文乱码
m.marshal(datas, fw);
fw.flush();
fw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 生成xml
* @param datas 数据
* @param filePath 文件路径
* @param coding 文件编码
* @return
*/
public static <T> boolean createXml(T datas,String filePath,String coding) {
try {
File f = new File(filePath);
if(!f.getParentFile().exists()) {
f.getParentFile().mkdirs();
}
FileWriter fw = new FileWriter(f);
JAXBContext context = JAXBContext.newInstance(datas.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(Marshaller.JAXB_ENCODING, coding); // 防止文件中文乱码
m.marshal(datas, fw);
fw.flush();
fw.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 读取xml
* @param filePath 文件路径
* @param type 接收容器
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T readXml(String filePath,Class<T> type) {
T value = null;
InputStream in = null;
InputStreamReader read = null;
try {
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller u = context.createUnmarshaller();
in = new FileInputStream(filePath);
read = new InputStreamReader(in,"UTF-8");
value = (T) u.unmarshal(read);
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(in!=null) {
in.close();
}
if(read!=null) {
read.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
return value;
}
/**
* 读取xml
* @param filePath 文件路径
* @param coding 文件编码
* @param type 接收容器
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T readXml(String filePath,String coding,Class<T> type) {
T value = null;
InputStream in = null;
InputStreamReader read = null;
try {
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller u = context.createUnmarshaller();
in = new FileInputStream(filePath);
read = new InputStreamReader(in,coding);
value = (T) u.unmarshal(read);
}catch(Exception e) {
e.printStackTrace();
}finally {
try {
if(in!=null) {
in.close();
}
if(read!=null) {
read.close();
}
}catch(Exception e) {
e.printStackTrace();
}
}
return value;
}
/**
* 为配置任务流量追加数据
*
* @param filePath
* @param dbXml
*/
public static void appendTask(String filePath, DBXml dbXml){
Tasks readXml = getTasks(filePath);
List<DBXml> list = new ArrayList<>();
if(readXml == null){
readXml = new Tasks();
list.add(dbXml);
readXml.setTask(list);
createXml(readXml,filePath,"utf-8");
}else {
if(readXml.getTask()==null){
list.add(dbXml);
}else{
List<DBXml> taskList = readXml.getTask();
for (DBXml tfi:taskList) {//只保存同一个TaskUniqueID
if(!tfi.getDbUniqueId().equals(dbXml.getDbUniqueId())){
list.add(tfi);
}
}
list.add(dbXml);
}
readXml.setTask(list);
createXml(readXml,filePath,"utf-8");
}
}
private static Tasks getTasks(String filePath) {
Tasks readXml = null;
try{
if(new File(filePath).isFile()){
readXml = readXml(filePath, Tasks.class);
}
}catch (Exception e){
logger.error("appendTask"+ ExceptionUtil.getException(e));
e.printStackTrace();
}
return readXml;
}
/**
*
*/
public static void deleteTask(String filePath, String dbUniqueId){
Tasks readXml = getTasks(filePath);
if(readXml!=null&&readXml.getTask()!=null){
List<DBXml> list = new ArrayList<>();
List<DBXml> taskList = readXml.getTask();
for (DBXml dbXml:taskList) {
if(!dbXml.getDbUniqueId().equals(dbUniqueId)){
list.add(dbXml);
}
}
readXml.setTask(list);
createXml(readXml,filePath,"utf-8");
}
}
}
xml的生成,读取,加数据模板
最新推荐文章于 2024-08-11 08:54:43 发布