/*
 * Decompiled with CFR 0.152.
 */
package com.hst.framework.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HstEncodeUtil {
    private static String ISO_8859_1 = "ISO-8859-1";
    private static String GBK = "GBK";
    private static String UTF8 = "UTF-8";
    private static String CP850 = "Cp850";

    public static void conGBK2ISO(Object baseEntity) throws Exception {
        HstEncodeUtil.convertEncode(GBK, ISO_8859_1, baseEntity);
    }

    public static void conUTF82ISO(Object baseEntity) throws Exception {
        HstEncodeUtil.decodeURL(baseEntity);
        HstEncodeUtil.convertEncode(GBK, ISO_8859_1, baseEntity);
    }

    public static void conISO2GBK(Object baseEntity) throws Exception {
        HstEncodeUtil.convertEncode(ISO_8859_1, GBK, baseEntity);
    }

    public static void conUTF2GBK(Object baseEntity) throws Exception {
        HstEncodeUtil.decodeURL(baseEntity);
    }

    public static void conISO2GBK(Object[] baseObjects) throws Exception {
        if (baseObjects == null) {
            return;
        }
        int i = 0;
        while (i < baseObjects.length) {
            HstEncodeUtil.convertEncode(ISO_8859_1, GBK, baseObjects[i]);
            ++i;
        }
    }

    public static void conISO2GBK(List baseList) throws Exception {
        if (baseList == null) {
            return;
        }
        Iterator iter = baseList.iterator();
        while (iter.hasNext()) {
            HstEncodeUtil.convertEncode(ISO_8859_1, GBK, iter.next());
        }
    }

    public static void conGBK2ISO(List baseList) throws Exception {
        if (baseList == null) {
            return;
        }
        Iterator iter = baseList.iterator();
        while (iter.hasNext()) {
            HstEncodeUtil.convertEncode(GBK, ISO_8859_1, iter.next());
        }
    }

    public static void conISO2Cp850(Object baseEntity) throws Exception {
        HstEncodeUtil.convertEncode(ISO_8859_1, CP850, baseEntity);
    }

    public static void conCp8502GBK(Object baseEntity) throws Exception {
        HstEncodeUtil.convertEncode(CP850, GBK, baseEntity);
    }

    public static void convertEncode(String source, String to, Object object) throws Exception {
        if (object == null) {
            return;
        }
        Class<?> clazz = object.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        Method setMethod = null;
        int i = 0;
        while (i < methods.length) {
            if (methods[i].getReturnType().equals(String.class) && methods[i].getName().substring(0, 3).equals("get")) {
                try {
                    String value = (String)methods[i].invoke(object, new Object[0]);
                    setMethod = clazz.getDeclaredMethod("set" + methods[i].getName().substring(3), String.class);
                    if (value != null) {
                        setMethod.invoke(object, new String(value.getBytes(source), to));
                    }
                }
                catch (InvocationTargetException ite) {
                    Exception ue = new Exception("\u7f16\u7801\u8f6c\u6362\u65f6\u4ea7\u751f\u5f02\u5e38!", ite);
                    throw ue;
                }
            }
            ++i;
        }
    }

    public static void decodeURL(Object object) throws Exception {
        if (object == null) {
            return;
        }
        Class<?> clazz = object.getClass();
        Method[] methods = clazz.getDeclaredMethods();
        Method setMethod = null;
        int i = 0;
        while (i < methods.length) {
            if (methods[i].getReturnType().equals(String.class) && methods[i].getName().substring(0, 3).equals("get")) {
                try {
                    String value = (String)methods[i].invoke(object, new Object[0]);
                    setMethod = clazz.getDeclaredMethod("set" + methods[i].getName().substring(3), String.class);
                    if (value != null) {
                        setMethod.invoke(object, URLDecoder.decode(value, UTF8));
                    }
                }
                catch (InvocationTargetException ite) {
                    Exception ue = new Exception("\u89e3\u7801\u65f6\u4ea7\u751f\u5f02\u5e38!", ite);
                    throw ue;
                }
            }
            ++i;
        }
    }

    public static String ISO2GBK(String str) {
        if (str == null) {
            return null;
        }
        try {
            return new String(str.getBytes(ISO_8859_1), GBK);
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static String ISO2GBKWithTrim(String str) {
        if (str == null) {
            return null;
        }
        try {
            return new String(str.trim().getBytes(ISO_8859_1), GBK);
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static String GBK2ISO(String str) {
        if (str == null) {
            return null;
        }
        try {
            return new String(str.getBytes(GBK), ISO_8859_1);
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static String UTF82ISO(String str) {
        if (str == null) {
            return null;
        }
        try {
            return HstEncodeUtil.GBK2ISO(URLDecoder.decode(str, UTF8));
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static String UTF82GBK(String str) {
        if (str == null) {
            return null;
        }
        try {
            return URLDecoder.decode(str, UTF8);
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static void conMapISO2GBK(Map result) throws Exception {
        if (result == null) {
            return;
        }
        for (Object key : result.keySet()) {
            HstEncodeUtil.convertEncode(ISO_8859_1, GBK, result.get(key));
        }
    }

    public static void conMapGBK2ISO(Map result) throws Exception {
        if (result == null) {
            return;
        }
        for (Object key : result.keySet()) {
            HstEncodeUtil.convertEncode(GBK, ISO_8859_1, result.get(key));
        }
    }

    public static void conMapUTF82GBK(Map result) throws Exception {
        if (result == null) {
            return;
        }
        for (Object key : result.keySet()) {
            Object obj = result.get(key);
            result.put(key, HstEncodeUtil.UTF82GBK((String)obj));
        }
    }

    public static void conMapUTF82ISO(Map result) throws Exception {
        if (result == null) {
            return;
        }
        for (Object key : result.keySet()) {
            Object obj = result.get(key);
            result.put(key, HstEncodeUtil.UTF82ISO((String)obj));
        }
    }
}
