/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.thoughtworks.xstream.XStream
 *  com.thoughtworks.xstream.io.HierarchicalStreamDriver
 *  com.thoughtworks.xstream.io.naming.NameCoder
 *  com.thoughtworks.xstream.io.xml.Xpp3Driver
 *  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.common.util.MyXmlFriednlyReplacer;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamDriver;
import com.thoughtworks.xstream.io.naming.NameCoder;
import com.thoughtworks.xstream.io.xml.Xpp3Driver;
import com.thoughtworks.xstream.mapper.CannotResolveClassException;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class XmlParse {
    private static Log logger = LogFactory.getLog(XmlParse.class);
    private static final int SPECIAL_TYPE_NUM = 6;
    public static Map<Class, Object> primitiveClass = new HashMap<Class, Object>(6);
    private static Class[] primitive = new Class[6];
    private static Object[] primitiveDefaultValue = new Class[6];

    static {
        primitive = new Class[]{String.class, BigDecimal.class, BigInteger.class, Integer.class, Double.class, Float.class};
        primitiveDefaultValue = new Object[]{"", new BigDecimal("0.00"), new BigInteger("0"), new Integer(0), new Double(0.0), new Float(0.0)};
        int i = 0;
        while (i < 6) {
            primitiveClass.put(primitive[i], primitiveDefaultValue[i]);
            ++i;
        }
    }

    public static XStream createXStream(String rootKey, Map<String, Class> map) {
        XStream stream = new XStream((HierarchicalStreamDriver)new Xpp3Driver((NameCoder)new MyXmlFriednlyReplacer())){

            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return XmlParse.createSkipOverElementMapperWrapper((Mapper)next);
            }
        };
        Set<String> set = map.keySet();
        for (String element : set) {
            Class keyClass = map.get(element);
            stream.alias(element, keyClass);
            XmlParse.setAliasField(rootKey, stream, keyClass, map);
        }
        return stream;
    }

    private static void setAliasField(String rootKey, XStream stream, Class clazz, Map<String, Class> map) {
        Field[] fields = clazz.getDeclaredFields();
        int i = 0;
        while (i < fields.length) {
            String fieldName = fields[i].getName();
            Class<?> fieldType = fields[i].getType();
            if (primitiveClass.containsKey(fieldType) || fieldType.isPrimitive()) {
                stream.aliasField(fieldName.toUpperCase(), clazz, fieldName);
            } else if (Collection.class.isAssignableFrom(fieldType)) {
                stream.addImplicitCollection(clazz, fieldName);
            } else {
                String value = XmlParse.getValue(fieldType, map);
                if (value != null && !rootKey.equals(value)) {
                    stream.aliasField(value, map.get(rootKey), fieldName);
                }
            }
            ++i;
        }
    }

    private static String getValue(Class value, Map<String, Class> map) {
        if (map.containsValue(value)) {
            Set<String> set = map.keySet();
            for (String element : set) {
                Class clazz = map.get(element);
                if (!clazz.equals(value)) continue;
                return element;
            }
        }
        return null;
    }

    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;
    }
}
