/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.servlet.http.HttpServletRequest
 *  org.apache.commons.fileupload.FileItem
 */
package com.hst.framework.util;

import com.hst.framework.constants.WebConstants;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;

public class HstToolsUtil {
    public static String convertException(Exception e) {
        return "caught exception " + e.getClass().getName() + ", " + e.getMessage();
    }

    public static String combine(String[] list, String separator) {
        if (list == null) {
            return null;
        }
        StringBuffer result = new StringBuffer();
        int i = 0;
        int listSize = list.length;
        i = 0;
        while (i < listSize - 1) {
            result.append("'" + list[i] + "'");
            result.append(separator);
            ++i;
        }
        result.append("'" + list[i] + "'");
        return result.toString();
    }

    public static String convertEncode(String a_str, String a_orig, String a_dest) throws UnsupportedEncodingException {
        byte[] temp = a_str.getBytes(a_orig);
        return new String(temp, a_dest);
    }

    public static String getYesNo(String a_value) {
        if (a_value != null && (a_value.trim().equals("Y") || a_value.trim().equals("1"))) {
            return "Y";
        }
        return "N";
    }

    public static Date getDate(Connection conn) {
        block7: {
            String sql = null;
            String sProductName = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                sProductName = conn.getMetaData().getDatabaseProductName().toLowerCase();
                sql = sProductName.indexOf("oracle") != -1 ? "select systimestamp from dual" : (sProductName.indexOf("sql server") != -1 ? "select getdate()" : (sProductName.indexOf("adaptive server") != -1 ? "select getdate()" : (sProductName.indexOf("db2") != -1 ? "SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1" : (sProductName.indexOf("informix") != -1 ? "select today" : (sProductName.indexOf("mysql") != -1 ? "select now()" : "select getdate()")))));
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
                if (rs.next()) {
                    Timestamp timestamp = rs.getTimestamp(1);
                    HstToolsUtil.close(stmt);
                    return timestamp;
                }
            }
            catch (SQLException e) {
                e.printStackTrace();
                break block7;
            }
            finally {
                HstToolsUtil.close(stmt);
            }
            HstToolsUtil.close(stmt);
        }
        return null;
    }

    public static double formatDecimal(double value, int dec) {
        BigDecimal bd = new BigDecimal(String.valueOf(value));
        bd = bd.setScale(dec, 4);
        return bd.doubleValue();
    }

    public static BigDecimal decValue(double d, int scale) {
        BigDecimal rs = new BigDecimal(String.valueOf(d));
        return rs.setScale(scale, 4);
    }

    public static BigDecimal double2BigDecimal(double d) {
        return HstToolsUtil.decValue(d, 2);
    }

    public static String htmlEncoder(String str) {
        if (str == null) {
            return "";
        }
        if (str.equals("")) {
            return "";
        }
        StringBuffer buf = new StringBuffer();
        char ch1 = '\n';
        int ch2 = 10;
        int i = 0;
        while (i < str.length()) {
            ch1 = str.charAt(i);
            if (ch1 == ' ' && i + 1 < str.length()) {
                ch2 = str.charAt(i + 1);
                if (ch2 == 32) {
                    buf.append("\u3000");
                    ++i;
                } else {
                    buf.append(ch1);
                }
            } else if (ch1 == '\n') {
                buf.append("<br>");
            } else if (ch1 == '\t') {
                buf.append("\u3000\u3000");
            } else {
                buf.append(ch1);
            }
            ++i;
        }
        return buf.toString();
    }

    public static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getStackTrace(Throwable exception) {
        StackTraceElement[] ste = exception.getStackTrace();
        StringBuffer sbResult = new StringBuffer();
        int i = 0;
        while (i < ste.length) {
            if (i > 0) {
                sbResult.append("\n    at ");
            }
            sbResult.append(ste[i].toString());
            ++i;
        }
        return sbResult.toString();
    }

    public static String getParameter(HttpServletRequest request, String name, String defaultVal) {
        String value = request.getParameter(name);
        if (value == null || value.trim().length() < 1) {
            value = defaultVal;
        }
        return value;
    }

    public static String getParameter(HttpServletRequest request, String name) {
        String value = request.getParameter(name);
        if (value == null || value.trim().length() < 1) {
            return "";
        }
        return value;
    }

    public static String[] getParameterValues(HttpServletRequest request, String name) {
        String[] value = request.getParameterValues(name);
        return value;
    }

    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().length() < 1;
    }

    public static String getNullStr(String str, String str1) {
        if (HstToolsUtil.isNullOrEmpty(str)) {
            return str1;
        }
        return str;
    }

    public static String getCollectionStr(String collection) {
        String[] coll = collection.split(",");
        collection = "";
        int i = 0;
        while (i < coll.length) {
            collection = i == 0 ? String.valueOf(collection) + "'" + coll[i] + "'" : String.valueOf(collection) + ",'" + coll[i] + "'";
            ++i;
        }
        return collection;
    }

    public static String getGBKStr(String str) {
        try {
            String temp_p = str;
            temp_p = HstToolsUtil.getNullStr(temp_p, "");
            byte[] temp_t = temp_p.getBytes("ISO8859_1");
            String temp = new String(temp_t, "GBK");
            return temp;
        }
        catch (Exception exception) {
            return "";
        }
    }

    public static String getISOStr(String str) {
        try {
            String temp_p = str;
            temp_p = HstToolsUtil.getNullStr(temp_p, "");
            byte[] temp_t = temp_p.getBytes("ISO8859_1");
            String temp = new String(temp_t);
            return temp;
        }
        catch (Exception exception) {
            return "null";
        }
    }

    public static String ISO2GBK(String str) {
        if (str == null) {
            return null;
        }
        try {
            return new String(str.getBytes("iso8859-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("iso8859-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"), "iso8859-1");
        }
        catch (Exception exception) {
            return "\u7f16\u7801\u8f6c\u6362\u9519\u8bef!";
        }
    }

    public static String getSuffixName(String s) {
        if (s.length() == 0) {
            return null;
        }
        int i = s.lastIndexOf(".");
        if (i < 0 || i >= s.length() - 1) {
            return null;
        }
        return s.substring(i + 1);
    }

    public static String getFileName(String fileName) {
        if (fileName.length() == 0) {
            return null;
        }
        int i = fileName.lastIndexOf("\\");
        if (!(i >= 0 && i < fileName.length() - 1 || (i = fileName.lastIndexOf("/")) >= 0 && i < fileName.length() - 1)) {
            return fileName;
        }
        return fileName.substring(i + 1);
    }

    public static String date2string(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        String s = null;
        s = date != null ? df.format(date) : "";
        return s;
    }

    public static Date string2date(String s, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date d = new Date();
        try {
            d = df.parse(s);
        }
        catch (Exception exception) {
            System.out.println("\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u9519\u8bef");
            return null;
        }
        return d;
    }

    public static String date2string(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String s = null;
        s = date != null ? df.format(date) : "";
        return s;
    }

    public static Date string2date(String s) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date d = new Date();
        try {
            d = df.parse(s);
        }
        catch (Exception exception) {
            System.out.println("\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u9519\u8bef");
            return null;
        }
        return d;
    }

    public static String date2stringDetail(Date date) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String s = null;
        s = date != null ? df.format(date) : "";
        return s;
    }

    public static Date string2dateDetail(String s) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = new Date();
        try {
            d = df.parse(s);
        }
        catch (Exception exception) {
            System.out.println("\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\u9519\u8bef");
            return null;
        }
        return d;
    }

    public static String addDate(String preDate, int field, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(HstToolsUtil.string2date(preDate));
        if (field == 2) {
            c.add(5, 1);
            c.add(2, amount);
            c.add(5, -1);
        } else {
            c.add(field, amount);
        }
        return HstToolsUtil.date2string(c.getTime());
    }

    public static String notNull(String str) {
        if (str == null) {
            return "";
        }
        return str;
    }

    public static Boolean notNull(Boolean bool) {
        if (bool == null) {
            return Boolean.FALSE;
        }
        return bool;
    }

    public static Double notNull(Double dbl) {
        if (dbl == null) {
            return new Double(0.0);
        }
        return dbl;
    }

    public static Float notNull(Float flt) {
        if (flt == null) {
            return new Float(0.0f);
        }
        return flt;
    }

    public static Integer notNull(Integer integer) {
        if (integer == null) {
            return new Integer(0);
        }
        return integer;
    }

    public static Long notNull(Long lng) {
        if (lng == null) {
            return new Long(0L);
        }
        return lng;
    }

    public static Short notNull(Short sht) {
        if (sht == null) {
            return new Short("0");
        }
        return sht;
    }

    public static File upload(FileItem fileItem, String filePath) throws IOException {
        return HstToolsUtil.upload(fileItem, filePath, WebConstants.UPLOAD_MAX_SIZE);
    }

    public static File upload(FileItem fileItem, String filePath, long maxSize) throws IOException {
        if (fileItem == null) {
            throw new NullPointerException();
        }
        if (filePath == null || filePath.trim().length() < 1) {
            throw new IllegalArgumentException("\u5728\u670d\u52a1\u5668\u4fdd\u5b58\u7684\u8def\u5f84\u4e3a\u7a7a\u3002");
        }
        if (fileItem.getSize() > maxSize) {
            throw new IOException("\u6587\u4ef6\u5927\u5c0f\u8d85\u51fa\u9650\u5236\uff01");
        }
        char chLastChar = filePath.charAt(filePath.length() - 1);
        File file = null;
        if (chLastChar == '/' || chLastChar == '\\') {
            String sUploadFileName = fileItem.getName();
            if (sUploadFileName.lastIndexOf("\\") != -1) {
                sUploadFileName = sUploadFileName.substring(sUploadFileName.lastIndexOf("\\") + 1);
            }
            if (sUploadFileName.lastIndexOf(":") != -1) {
                sUploadFileName = sUploadFileName.substring(sUploadFileName.lastIndexOf(":") + 1);
            }
            file = new File(String.valueOf(filePath) + sUploadFileName);
        } else {
            file = new File(filePath);
        }
        File folder = file.getParentFile();
        if (!folder.exists() && !folder.mkdirs()) {
            throw new IOException("\u521b\u5efa\u6587\u4ef6\u5939\u5931\u8d25\u3002");
        }
        try {
            fileItem.write(file);
        }
        catch (Exception e) {
            throw new IOException("\u4fdd\u5b58\u6587\u4ef6\u9519\u8bef\uff1a" + e.toString());
        }
        return file;
    }

    public static void copyToFile(InputStream is, File destFile, int bufferSize) throws IOException {
        if (is == null) {
            throw new IllegalArgumentException("\u6e90\u8f93\u5165\u6d41\u4e3a\u7a7a\u3002");
        }
        if (destFile == null) {
            throw new IllegalArgumentException("\u76ee\u6807\u6587\u4ef6\u4e3a\u7a7a\u3002");
        }
        if (bufferSize < 1) {
            throw new IllegalArgumentException("\u7f13\u5b58\u5927\u5c0f\u975e\u6cd5\u3002");
        }
        File folder = destFile.getParentFile();
        if (!folder.exists() && !folder.mkdirs()) {
            throw new IOException("\u521b\u5efa\u6587\u4ef6\u5939\u5931\u8d25\u3002");
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(destFile);
            byte[] abyte = new byte[bufferSize];
            int s = 0;
            while ((s = is.read(abyte)) != -1) {
                fos.write(abyte, 0, s);
            }
        }
        catch (Throwable throwable) {
            HstToolsUtil.close(fos);
            throw throwable;
        }
        HstToolsUtil.close(fos);
    }

    public static String getString(File srcFile) throws IOException {
        String string;
        if (srcFile == null || !srcFile.exists()) {
            throw new IllegalArgumentException("\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728");
        }
        FileReader frd = null;
        StringWriter sw = null;
        try {
            frd = new FileReader(srcFile);
            sw = new StringWriter();
            char[] ach = new char[WebConstants.DEFAULT_BUFFER_SIZE];
            int s = 0;
            while ((s = frd.read(ach)) != -1) {
                sw.write(ach, 0, s);
            }
            string = sw.toString();
        }
        catch (IOException ioe) {
            try {
                throw ioe;
            }
            catch (Throwable throwable) {
                HstToolsUtil.close(frd);
                HstToolsUtil.close(sw);
                throw throwable;
            }
        }
        HstToolsUtil.close(frd);
        HstToolsUtil.close(sw);
        return string;
    }

    public static void writeToFile(String data, File file) throws IOException {
        if (data == null) {
            throw new IllegalArgumentException("\u5b57\u7b26\u4e32\u4e3a\u7a7a");
        }
        if (file == null) {
            throw new IllegalArgumentException("\u6587\u4ef6\u5bf9\u8c61\u4e3a\u7a7a");
        }
        File folder = file.getParentFile();
        if (!folder.exists() && !folder.mkdirs()) {
            throw new IOException("\u521b\u5efa\u6587\u4ef6\u5939\u5931\u8d25\u3002");
        }
        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            fw.write(data);
        }
        catch (Throwable throwable) {
            HstToolsUtil.close(fw);
            throw throwable;
        }
        HstToolsUtil.close(fw);
    }

    public static void appendToFile(String data, File file) throws IOException {
        if (data == null) {
            throw new IllegalArgumentException("\u5b57\u7b26\u4e32\u4e3a\u7a7a");
        }
        if (file == null) {
            throw new IllegalArgumentException("\u6587\u4ef6\u5bf9\u8c61\u4e3a\u7a7a");
        }
        if (!file.exists()) {
            HstToolsUtil.writeToFile(data, file);
        } else {
            FileWriter fw = null;
            try {
                fw = new FileWriter(file, true);
                fw.write(data);
            }
            catch (Throwable throwable) {
                HstToolsUtil.close(fw);
                throw throwable;
            }
            HstToolsUtil.close(fw);
        }
    }

    public static void copyFile(File srcFile, File destFile) throws IOException {
        HstToolsUtil.copyFile(srcFile, destFile, WebConstants.DEFAULT_BUFFER_SIZE);
    }

    public static void copyFile(File srcFile, File destFile, int bufferSize) throws IOException {
        if (srcFile == null || !srcFile.exists()) {
            throw new IllegalArgumentException("\u6e90\u6587\u4ef6\u4e0d\u5b58\u5728");
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(srcFile);
            HstToolsUtil.copyToFile(fis, destFile, bufferSize);
        }
        catch (Throwable throwable) {
            HstToolsUtil.close(fis);
            throw throwable;
        }
        HstToolsUtil.close(fis);
    }

    public static void close(InputStream is) {
        try {
            if (is != null) {
                is.close();
            }
        }
        catch (IOException iOException) {}
    }

    public static void close(OutputStream os) {
        try {
            if (os != null) {
                os.close();
            }
        }
        catch (IOException iOException) {}
    }

    public static void close(Writer wt) {
        try {
            if (wt != null) {
                wt.close();
            }
        }
        catch (IOException iOException) {}
    }

    public static void close(Reader rd) {
        try {
            if (rd != null) {
                rd.close();
            }
        }
        catch (IOException iOException) {}
    }

    public static Object[] filterObjects(Object[] objects1, Object[] objects2) {
        if (objects1 == null || objects2 == null) {
            throw new NullPointerException();
        }
        Object[] newObjects = new Object[objects1.length];
        System.arraycopy(objects1, 0, newObjects, 0, newObjects.length);
        List<Object> lst1 = Arrays.asList(newObjects);
        List<Object> lst2 = Arrays.asList(objects2);
        lst1.retainAll(lst2);
        return lst1.toArray();
    }

    public static String date2Chinese(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int year = cal.get(1);
        int month = cal.get(2) + 1;
        int day = cal.get(5);
        String sYear = "\u5e74";
        String sMonth = "\u6708";
        String sDay = "\u65e5";
        String[] number = new String[]{"\u039f", "\u4e00", "\u4e8c", "\u4e09", "\u56db", "\u4e94", "\u516d", "\u4e03", "\u516b", "\u4e5d"};
        while (year > 0) {
            int k = year % 10;
            year /= 10;
            sYear = String.valueOf(number[k]) + sYear;
        }
        sMonth = month <= 9 ? String.valueOf(number[month]) + sMonth : (month == 10 ? "\u5341" + sMonth : "\u5341" + number[month - 10] + sMonth);
        sDay = day <= 9 ? String.valueOf(number[day]) + sDay : String.valueOf(day >= 20 ? number[day / 10] : "") + "\u5341" + (day % 10 == 0 ? "" : number[day % 10]) + sDay;
        return String.valueOf(sYear) + sMonth + sDay;
    }

    public static String[] distinctStr(String[] targetStr) {
        if (targetStr == null) {
            return null;
        }
        TreeSet<String> set = new TreeSet<String>();
        int i = 0;
        while (i < targetStr.length) {
            set.add(targetStr[i]);
            ++i;
        }
        Iterator it = set.iterator();
        String[] result = new String[set.size()];
        int j = 0;
        while (it.hasNext()) {
            result[j] = (String)it.next();
            ++j;
        }
        return result;
    }

    public static void sort(List list, String keyFieldName, boolean isDesc) throws Exception {
        ArrayList<Object[]> temp = new ArrayList<Object[]>();
        String getterName = "get" + String.valueOf(keyFieldName.charAt(0)).toUpperCase() + keyFieldName.substring(1);
        int index = 0;
        while (index < list.size()) {
            Object element = list.get(index);
            Object key = element.getClass().getMethod(getterName, new Class[0]).invoke(element, new Object[0]);
            if (!(key instanceof String || key instanceof Number || key instanceof Date)) {
                throw new Exception("\u6392\u5217\u5b57\u6bb5\u7684\u7c7b\u578b\u53ea\u80fd\u662fString\u6216\u8005Date\u6216\u8005Number\u3002");
            }
            Object[] one = new Object[]{key, element};
            temp.add(one);
            ++index;
        }
        int i = 0;
        while (i < temp.size()) {
            Object[] one = (Object[])temp.get(i);
            Object keyOne = one[0];
            int rate = 0;
            int j = 0;
            while (j < temp.size()) {
                if (j != i) {
                    Object[] other = (Object[])temp.get(j);
                    Object keyOther = other[0];
                    if (HstToolsUtil.compare(keyOne, keyOther) > 0) {
                        ++rate;
                    }
                    if (HstToolsUtil.compare(keyOne, keyOther) == 0 && j < i) {
                        ++rate;
                    }
                }
                ++j;
            }
            if (isDesc) {
                rate = temp.size() - rate - 1;
            }
            list.remove(rate);
            list.add(rate, one[1]);
            ++i;
        }
    }

    public static int compare(Object one, Object other) throws Exception {
        if (one instanceof Comparable && other instanceof Comparable) {
            return ((Comparable)one).compareTo(other);
        }
        throw new Exception("\u4e24\u4e2a\u5bf9\u8c61\u4e0d\u80fd\u6bd4\u8f83\u3002");
    }

    public static String replaceFirstCharToBig(String source) {
        if (source == null || source.equals("")) {
            return null;
        }
        String first = source.substring(0, 1).toUpperCase();
        return source.length() == 1 ? first : first.concat(source.substring(1));
    }

    public static String replaceFirstCharToSmall(String source) {
        if (source == null || source.equals("")) {
            return null;
        }
        String first = source.substring(0, 1).toLowerCase();
        return source.length() == 1 ? first : first.concat(source.substring(1));
    }

    public static String getSequence(Connection conn, String sequenceName) throws Exception {
        StringBuffer SQL = new StringBuffer("SELECT " + sequenceName + ".NEXTVAL FROM DUAL");
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sequence = null;
        ps = conn.prepareStatement(SQL.toString());
        rs = ps.executeQuery();
        if (rs.next()) {
            sequence = rs.getString(1);
        }
        HstToolsUtil.close(ps);
        return sequence;
    }

    public static String replaceSpecialChar(String str) {
        if (HstToolsUtil.isNullOrEmpty(str)) {
            return "";
        }
        str = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
        str = str.replaceAll("\\+", "%2B");
        return str;
    }

    public static boolean isNumeric(String str) {
        int i = 0;
        while (i < str.length()) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
            ++i;
        }
        return true;
    }

    public static void main(String[] args) {
        String a = "A002";
        System.out.println(HstToolsUtil.isNumeric(a));
    }
}
