xml 와 대상 상호 전환 도구 클래스

3379 단어 자바

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.DocumentException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class XmlBeanServer {
    /**
     *    Map
     */
    private static final ConcurrentHashMap, XStream> converterMap = new ConcurrentHashMap, XStream>();

    /**
     *    
     */
    private static final Object lock = new Object();

    /**
     *  XML     Bean  
     *
     * @param xml
     * @return
     * @see [ 、 #  、 #  ]
     */
      public static  T xmlToBean(Class clazz, String xml) {
        return (T) getConverter(clazz).fromXML(xml);
    }

    /**
     *  Bean     XML  
     *
     * @param bean
     * @return
     * @see [ 、 #  、 #  ]
     */
    public static String beanToXml(Object bean) {
        return getConverter(bean.getClass()).toXML(bean).replaceAll("__", "_");
    }

    /**
     *   Bean class        
     *
     * @param clazz
     * @return
     * @see [ 、 #  、 #  ]
     */
    private static XStream getConverter(Class> clazz) {
        XStream con = converterMap.get(clazz);
        if (null == con) {
            synchronized (lock) {
                con = converterMap.get(clazz);
                if (null == con) {
                    con = new XStream(new DomDriver());
                    con.processAnnotations(clazz);
                    converterMap.put(clazz, con);
                }
            }
        }
        con.alias(clazz.getSimpleName(), clazz);
        con.alias("GoodsInfo", GoodsInfo.class);
        con.alias("GoodsResInfo", GoodsResInfo.class);
        return con;
    }

     public static String regexLabel(String xml, String label) {
        String context = "";
        //     
        String regex = "(.*?)" + label + ">";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(xml);
        List list = new ArrayList();
        while (m.find()) {
            int i = 1;
            list.add(m.group(i));
            i++;
        }
        if (list.size() > 0) {
            context = list.get(0);
        }
        return "" + context + "" + label + ">";
    }

    
	/**
     *  XML       Bean  
     *
     * @param xml
     * @return
     * @see [ 、 #  、 #  ]
     */
    public static  T convertXmlToBean(Class clazz, String xml) {
        XStream con = converterMap.get(clazz);
        if (null == con) {
            synchronized (lock) {
                con = converterMap.get(clazz);
                if (null == con) {
                    con = new XStream(new DomDriver());
                    con.processAnnotations(clazz);
                    converterMap.put(clazz, con);
                }
            }
        }
		
        con.alias(clazz.getSimpleName(), clazz);
        con.alias("WbandInfoByTelNewModelBean", WbandInfoByTelNewModelBean.class);
        con.alias("joinacctInfo", JoinacctInfoBean.class);
        con.alias("AcctsubsListBean", AcctsubsListBean.class);
        return (T) con.fromXML(xml);
    }
}

좋은 웹페이지 즐겨찾기