⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xworkbasicconverter.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        Collection result;        Class memberType = String.class;        if (o != null) {            //memberType = (Class) XWorkConverter.getInstance().getConverter(o.getClass(), XWorkConverter.CONVERSION_COLLECTION_PREFIX + prop);            memberType = XWorkConverter.getInstance().getObjectTypeDeterminer().getElementClass(o.getClass(), prop, null);            if (memberType == null) {                memberType = String.class;            }        }        if (toType.isAssignableFrom(value.getClass())) {            // no need to do anything            result = (Collection) value;        } else if (value.getClass().isArray()) {            Object[] objArray = (Object[]) value;            TypeConverter converter = Ognl.getTypeConverter(context);            result = createCollection(o, prop, toType, memberType, objArray.length);            for (int i = 0; i < objArray.length; i++) {                result.add(converter.convertValue(context, o, member, prop, objArray[i], memberType));            }        } else if (Collection.class.isAssignableFrom(value.getClass())) {            Collection col = (Collection) value;            TypeConverter converter = Ognl.getTypeConverter(context);            result = createCollection(o, prop, toType, memberType, col.size());            for (Iterator it = col.iterator(); it.hasNext();) {                result.add(converter.convertValue(context, o, member, prop, it.next(), memberType));            }        } else {            result = createCollection(o, prop, toType, memberType, -1);            result.add(value);        }        return result;    }    private Object doConvertToDate(Map context, Object value, Class toType) {        Date result = null;        if (value instanceof String && value != null && ((String) value).length() > 0) {            String sa = (String) value;            Locale locale = getLocale(context);            DateFormat df = null;            if (java.sql.Time.class == toType) {                df = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);            } else if (java.sql.Timestamp.class == toType) {                Date check = null;                SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,                        DateFormat.MEDIUM,                        locale);                SimpleDateFormat fullfmt = new SimpleDateFormat(dtfmt.toPattern() + MILLISECOND_FORMAT,                        locale);                SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.SHORT,                        locale);                SimpleDateFormat[] fmts = {fullfmt, dtfmt, dfmt};                for (int i = 0; i < fmts.length; i++) {                    try {                        check = fmts[i].parse(sa);                        df = fmts[i];                        if (check != null) {                            break;                        }                    } catch (ParseException ignore) {                    }                }            } else if (java.util.Date.class == toType) {                Date check = null;                SimpleDateFormat d1 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);                SimpleDateFormat d2 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, locale);                SimpleDateFormat d3 = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);                SimpleDateFormat rfc3399 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");                SimpleDateFormat[] dfs = {d1, d2, d3, rfc3399}; //added RFC 3339 date format (XW-473)                for (int i = 0; i < dfs.length; i++) {                    try {                        check = dfs[i].parse(sa);                        df = dfs[i];                        if (check != null) {                            break;                        }                    }                    catch (ParseException ignore) {                    }                }            }            //final fallback for dates without time            if (df == null) {                df = DateFormat.getDateInstance(DateFormat.SHORT, locale);            }            try {                df.setLenient(false); // let's use strict parsing (XW-341)                result = df.parse(sa);                if (!(Date.class == toType)) {                    try {                        Constructor constructor = toType.getConstructor(new Class[]{long.class});                        return constructor.newInstance(new Object[]{new Long(result.getTime())});                    } catch (Exception e) {                        throw new XWorkException("Couldn't create class " + toType + " using default (long) constructor", e);                    }                }            } catch (ParseException e) {                throw new XWorkException("Could not parse date", e);            }        } else if (Date.class.isAssignableFrom(value.getClass())) {            result = (Date) value;        }        return result;    }    private Object doConvertToNumber(Map context, Object value, Class toType) {        if (value instanceof String) {            if (toType == BigDecimal.class) {                return new BigDecimal((String) value);            } else if (toType == BigInteger.class) {                return new BigInteger((String) value);            } else {                String stringValue = (String) value;                if (!toType.isPrimitive() && (stringValue == null || stringValue.length() == 0)) {                    return null;                }                NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));                ParsePosition parsePos = new ParsePosition(0);                if (isIntegerType(toType)) {                    numFormat.setParseIntegerOnly(true);                }                numFormat.setGroupingUsed(true);                Number number = numFormat.parse(stringValue, parsePos);                if (parsePos.getIndex() != stringValue.length()) {                    throw new XWorkException("Unparseable number: \"" + stringValue + "\" at position "                            + parsePos.getIndex());                } else {                    value = super.convertValue(context, number, toType);                }            }        } else if (value instanceof Object[]) {            Object[] objArray = (Object[]) value;            if (objArray.length == 1) {                return doConvertToNumber(context, objArray[0], toType);            }        }        // pass it through DefaultTypeConverter        return super.convertValue(context, value, toType);    }    protected boolean isIntegerType(Class type) {        if (double.class == type || float.class == type || Double.class == type || Float.class == type                || char.class == type || Character.class == type) {            return false;        }        return true;    }    /**     * Converts the input as a number using java's number formatter to a string output.     */    private String doConvertFromNumberToString(Map context, Object value, Class toType) {        // XW-409: If the input is a Number we should format it to a string using the choosen locale and use java's numberformatter        if (Number.class.isAssignableFrom(toType)) {            NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));            if (isIntegerType(toType)) {                numFormat.setParseIntegerOnly(true);            }            numFormat.setGroupingUsed(true);            numFormat.setMaximumFractionDigits(99); // to be sure we include all digits after decimal seperator, otherwise some of the fractions can be chopped            String number = numFormat.format(value);            if (number != null) {                return number;            }        }        return null; // no number    }    private String doConvertToString(Map context, Object value) {        String result = null;        if (value instanceof int[]) {            int[] x = (int[]) value;            List intArray = new ArrayList(x.length);            for (int i = 0; i < x.length; i++) {                intArray.add(new Integer(x[i]));            }            result = TextUtils.join(", ", intArray);        } else if (value instanceof long[]) {            long[] x = (long[]) value;            List intArray = new ArrayList(x.length);            for (int i = 0; i < x.length; i++) {                intArray.add(new Long(x[i]));            }            result = TextUtils.join(", ", intArray);        } else if (value instanceof double[]) {            double[] x = (double[]) value;            List intArray = new ArrayList(x.length);            for (int i = 0; i < x.length; i++) {                intArray.add(new Double(x[i]));            }            result = TextUtils.join(", ", intArray);        } else if (value instanceof boolean[]) {            boolean[] x = (boolean[]) value;            List intArray = new ArrayList(x.length);            for (int i = 0; i < x.length; i++) {                intArray.add(new Boolean(x[i]));            }            result = TextUtils.join(", ", intArray);        } else if (value instanceof Date) {            DateFormat df = null;            if (value instanceof java.sql.Time) {                df = DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale(context));            } else if (value instanceof java.sql.Timestamp) {                SimpleDateFormat dfmt = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.SHORT,                        DateFormat.MEDIUM,                        getLocale(context));                df = new SimpleDateFormat(dfmt.toPattern() + MILLISECOND_FORMAT);            } else {                df = DateFormat.getDateInstance(DateFormat.SHORT, getLocale(context));            }            result = df.format(value);        } else if (value instanceof String[]) {            result = TextUtils.join(", ", (String[]) value);        }        return result;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -