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

📄 writexmllog.java

📁 一个关于tlms的一个小程序 看看能否帮助到别人
💻 JAVA
字号:
package com.szmx.component.log.xml;

import com.szmx.component.log.BaseLogObject;
import com.szmx.component.log.WriteLogException;
import com.szmx.framework.util.convert.TimestampConverter;
import com.szmx.framework.util.DateUtil;
import com.szmx.framework.util.SysPropertiesUtil;
import com.szmx.framework.util.FileUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import org.dom4j.io.XMLWriter;
import org.dom4j.io.OutputFormat;

import java.util.List;
import java.util.Iterator;
import java.util.Date;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;

/**
 * User     : yHuang@szmx.com
 * Date     :  Oct 9, 20059:03:30 AM
 * Version  : 0.1
 * updateBy :
 * updateDt :
 */
public class WriteXMLLog {

    private static Document createDocument(List list) throws WriteLogException {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement(getClassName(list)+"s");
        if(list == null){
            root.addText("no log found!");
        }
        else{
            try{
                for(Iterator it = list.iterator();it.hasNext();)
                {
                    BaseLogObject o = (BaseLogObject)it.next();
                    String className = getClassName(o);
                    Element log = root.addElement(className);
                    log.addElement("date").addText(DateUtil.convertDateToString(TimestampConverter.TS_FORMAT,o.getLogDt()));
                    log.addElement("user").addText(o.getUser());
                    write(o , log);
                }
            }catch(Exception e)
            {
                throw new WriteLogException(e);
            }
        }
        return document;
    }

    private static Element write(BaseLogObject o , Element log) throws IllegalAccessException, InvocationTargetException, UnsupportedEncodingException {

        Method[] method = o.getClass().getDeclaredMethods();
        for(int i = 0 ; i < method.length ; i ++)
        {
            if(method[i].getName().indexOf("get")!=-1)
            {
                Object value = method[i].invoke(o, new Object[0]);
                String valueStr = "";
                if(value!=null){
                    valueStr = value.toString();
                    valueStr = new String( valueStr.getBytes("UTF-8"),"GBK"); 

                }
                log.addElement(getField(method[i].getName())).addText(valueStr);
            }
        }
        return log;
    }

    private static String getClassName(Object o)
    {
        String className = o.getClass().getName();
         return className.substring(className.lastIndexOf(".")+1,className.length()) ;
    }

    private static String getClassName(List list)
    {
        if(list!=null&&list.iterator().hasNext())
        {
            String className = list.iterator().next().getClass().getName();
            return className.substring(className.lastIndexOf(".")+1,className.length()) ;
        }else{
            return "root";
        }
    }

    private static String getField(String method)
    {
        return method.substring(3).toLowerCase();
    }

    private static void write(Document document , String file) throws WriteLogException {
         try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter(file) , format);
            writer.write(document);
            writer.close();
        } catch (IOException e) {
            throw new WriteLogException(e);
        }
    }

    public static String getFileName(String logType , Date dt)
    {
        String path = SysPropertiesUtil.getProperty("log.backup.path");
        FileUtil.createFile(path);
        return  path + logType + "_" + DateUtil.convertDateToString("yyyy-MM-dd" ,dt) + ".xml";
    }

    public static void write(List list,String logType , Date dt) throws WriteLogException {
        write(createDocument(list) , getFileName(logType , dt));
    }
}

⌨️ 快捷键说明

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