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

📄 xworkbasicconverter.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2002-2007 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.util;import java.lang.reflect.Array;import java.lang.reflect.Constructor;import java.lang.reflect.Member;import java.math.BigDecimal;import java.math.BigInteger;import java.text.DateFormat;import java.text.NumberFormat;import java.text.ParseException;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collection;import java.util.Date;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.Set;import java.util.SortedSet;import java.util.TreeSet;import ognl.DefaultTypeConverter;import ognl.Ognl;import ognl.TypeConverter;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.XWorkException;/** * <!-- START SNIPPET: javadoc --> * XWork will automatically handle the most common type conversion for you. * <p/> * This includes support for converting to and from Strings for each of the following: * <ul> * <li>String</li> * <li>boolean / Boolean</li> * <li>char / Character</li> * <li>int / Integer, float / Float, long / Long, double / Double</li> * <li>dates - uses the SHORT or RFC3339 format (<code>yyyy-MM-dd'T'HH:mm:ss</code>) for the Locale associated with the current request</li> * <li>arrays - assuming the individual strings can be coverted to the individual items</li> * <li>collections - if not object type can be determined, it is assumed to be a String and a new ArrayList is * created</li> * </ul> * <p/> * <b>Note:</b> that with arrays the type conversion will defer to the type of the array elements and try to convert each * item individually. As with any other type conversion, if the conversion can't be performed the standard type * conversion error reporting is used to indicate a problem occured while processing the type conversion. * <!-- END SNIPPET: javadoc --> * * @author <a href="mailto:plightbo@gmail.com">Pat Lightbody</a> * @author Mike Mosiewicz * @author Rainer Hermanns * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a> */public class XWorkBasicConverter extends DefaultTypeConverter {    private static final String MILLISECOND_FORMAT = ".SSS";    public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {        Object result = null;        if (value == null || toType.isAssignableFrom(value.getClass())) {            // no need to convert at all, right?            return value;        }        if (toType == String.class) {            /* the code below has been disabled as it causes sideffects in Strtus2 (XW-512)            // if input (value) is a number then use special conversion method (XW-490)            Class inputType = value.getClass();            if (Number.class.isAssignableFrom(inputType)) {                result = doConvertFromNumberToString(context, value, inputType);                if (result != null) {                    return result;                }            }*/            // okay use default string conversion            result = doConvertToString(context, value);        } else if (toType == boolean.class) {            result = doConvertToBoolean(value);        } else if (toType == Boolean.class) {            result = doConvertToBoolean(value);        } else if (toType.isArray()) {            result = doConvertToArray(context, o, member, s, value, toType);        } else if (Date.class.isAssignableFrom(toType)) {            result = doConvertToDate(context, value, toType);        } else if (Collection.class.isAssignableFrom(toType)) {            result = doConvertToCollection(context, o, member, s, value, toType);        } else if (toType == Character.class) {            result = doConvertToCharacter(value);        } else if (toType == char.class) {            result = doConvertToCharacter(value);        } else if (Number.class.isAssignableFrom(toType)) {            result = doConvertToNumber(context, value, toType);        } else if (toType == Class.class) {            result = doConvertToClass(value);        }        if (result == null) {            if (value instanceof Object[]) {                Object[] array = (Object[]) value;                if (array.length >= 1) {                    value = array[0];                }                // let's try to convert the first element only                result = convertValue(context, o, member, s, value, toType);            } else if (!"".equals(value)) { // we've already tried the types we know                result = super.convertValue(context, value, toType);            }            if (result == null && value != null && !"".equals(value)) {                throw new XWorkException("Cannot create type " + toType + " from value " + value);            }        }        return result;    }    private Locale getLocale(Map context) {        if (context == null) {            return Locale.getDefault();        }        Locale locale = (Locale) context.get(ActionContext.LOCALE);        if (locale == null) {            locale = Locale.getDefault();        }        return locale;    }    /**     * Creates a Collection of the specified type.     *     * @param fromObject     * @param propertyName     * @param toType       the type of Collection to create     * @param memberType   the type of object elements in this collection must be     * @param size         the initial size of the collection (ignored if 0 or less)     * @return a Collection of the specified type     */    private Collection createCollection(Object fromObject, String propertyName, Class toType, Class memberType, int size) {//        try {//            Object original = Ognl.getValue(OgnlUtil.compile(propertyName),fromObject);//            if (original instanceof Collection) {//                Collection coll = (Collection) original;//                coll.clear();//                return coll;//            }//        } catch (Exception e) {//            // fail back to creating a new one//        }        Collection result;        if (toType == Set.class) {            if (size > 0) {                result = new HashSet(size);            } else {                result = new HashSet();            }        } else if (toType == SortedSet.class) {            result = new TreeSet();        } else {            if (size > 0) {                result = new XWorkList(memberType, size);            } else {                result = new XWorkList(memberType);            }        }        return result;    }    private Object doConvertToArray(Map context, Object o, Member member, String s, Object value, Class toType) {        Object result = null;        Class componentType = toType.getComponentType();        if (componentType != null) {            TypeConverter converter = Ognl.getTypeConverter(context);            if (value.getClass().isArray()) {                int length = Array.getLength(value);                result = Array.newInstance(componentType, length);                for (int i = 0; i < length; i++) {                    Object valueItem = Array.get(value, i);                    Array.set(result, i, converter.convertValue(context, o, member, s, valueItem, componentType));                }            } else {                result = Array.newInstance(componentType, 1);                Array.set(result, 0, converter.convertValue(context, o, member, s, value, componentType));            }        }        return result;    }    private Object doConvertToCharacter(Object value) {        if (value instanceof String) {            String cStr = (String) value;            return (cStr.length() > 0) ? new Character(cStr.charAt(0)) : null;        }        return null;    }    private Object doConvertToBoolean(Object value) {        if (value instanceof String) {            String bStr = (String) value;            return Boolean.valueOf(bStr);        }        return null;    }    private Class doConvertToClass(Object value) {        Class clazz = null;        if (value instanceof String && value != null && ((String) value).length() > 0) {            try {                clazz = Class.forName((String) value);            } catch (ClassNotFoundException e) {                throw new XWorkException(e.getLocalizedMessage(), e);            }        }        return clazz;    }    private Collection doConvertToCollection(Map context, Object o, Member member, String prop, Object value, Class toType) {

⌨️ 快捷键说明

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