/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.thoughtworks.xstream.XStream
 *  com.thoughtworks.xstream.io.HierarchicalStreamDriver
 *  com.thoughtworks.xstream.io.HierarchicalStreamWriter
 *  com.thoughtworks.xstream.io.naming.NameCoder
 *  com.thoughtworks.xstream.io.xml.PrettyPrintWriter
 *  com.thoughtworks.xstream.io.xml.Xpp3DomDriver
 *  com.thoughtworks.xstream.mapper.CannotResolveClassException
 *  com.thoughtworks.xstream.mapper.Mapper
 *  com.thoughtworks.xstream.mapper.MapperWrapper
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 */
package com.hst.common.util;

import com.hst.framework.domain.BaseBusinessDomain;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.Xpp3DomDriver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.io.Writer;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class XstreamUtils {
    private static Log logger = LogFactory.getLog(XstreamUtils.class);
    private static Map<Class<?>, XStream> xstreamMap = new WeakHashMap();
    private static NameCoder nameCoder = new NameCoder(){

        public String encodeNode(String arg0) {
            return arg0;
        }

        public String encodeAttribute(String arg0) {
            return arg0;
        }

        public String decodeNode(String arg0) {
            return arg0;
        }

        public String decodeAttribute(String arg0) {
            return arg0;
        }
    };

    private static MapperWrapper createSkipOverElementMapperWrapper(Mapper mapper) {
        MapperWrapper resMapper = new MapperWrapper(mapper){

            public Class realClass(String elementName) {
                Class res = null;
                try {
                    res = super.realClass(elementName);
                }
                catch (CannotResolveClassException cannotResolveClassException) {
                    logger.warn((Object)("xstream change xml to object. filed (" + elementName + ") not exsit. "));
                }
                return res;
            }
        };
        return resMapper;
    }

    public static XStream getXstream(Class<?> classType) {
        return XstreamUtils.getXstream(classType, true);
    }

    public static XStream getXstream(Class<?> classType, boolean isSkipOverElement) {
        if (xstreamMap.containsKey(classType)) {
            return xstreamMap.get(classType);
        }
        XStream res = null;
        res = isSkipOverElement ? new XStream((HierarchicalStreamDriver)new Xpp3DomDriver(nameCoder)){

            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return XstreamUtils.createSkipOverElementMapperWrapper((Mapper)next);
            }
        } : new XStream((HierarchicalStreamDriver)new Xpp3DomDriver(nameCoder));
        logger.info((Object)("create xstream by {" + classType.getName() + "} , parameter {" + isSkipOverElement + "}"));
        res.processAnnotations(classType);
        xstreamMap.put(classType, res);
        return res;
    }

    public static XStream getXstream(Class<?> classType, boolean isSkipOverElement, boolean isNewLine) {
        if (xstreamMap.containsKey(classType)) {
            return xstreamMap.get(classType);
        }
        Xpp3DomDriver domDriver = null;
        domDriver = isNewLine ? new Xpp3DomDriver(nameCoder) : new Xpp3DomDriver(nameCoder){

            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out, this.getNameCoder()){

                    protected String getNewLine() {
                        return "";
                    }
                };
            }
        };
        XStream res = null;
        res = isSkipOverElement ? new XStream((HierarchicalStreamDriver)domDriver){

            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return XstreamUtils.createSkipOverElementMapperWrapper((Mapper)next);
            }
        } : new XStream((HierarchicalStreamDriver)domDriver);
        logger.info((Object)("create xstream by {" + classType.getName() + "} , parameter {" + isSkipOverElement + "}"));
        res.processAnnotations(classType);
        xstreamMap.put(classType, res);
        return res;
    }

    public static void main(String[] args) throws Exception {
        XStream xs = XstreamUtils.getXstream(BaseBusinessDomain.class);
        String xml = "<test><ywId>1233rr</ywId><parmJson>134ffgghhh</parmJson><retJson>ojjkdkdkjj</retJson><retJson2>ojjkdkdkjj</retJson2></test>";
        BaseBusinessDomain domian = (BaseBusinessDomain)xs.fromXML(xml);
        System.out.println(domian.getYwId());
    }
}
