xworkconverter.java

来自「在Struts2中的jar包xwork的源代码.版本为2.0.7」· Java 代码 · 共 840 行 · 第 1/3 页

JAVA
840
字号
        if (tc == null && context != null) {            // ok, let's see if we can look it up by path as requested in XW-297            Object lastPropertyPath = context.get(OgnlContextState.CURRENT_PROPERTY_PATH);            Class clazz = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED);            if (lastPropertyPath != null && clazz != null) {                String path = lastPropertyPath + "." + property;                tc = (TypeConverter) getConverter(clazz, path);            }        }        if (tc == null) {            if (toClass.equals(String.class) && (value != null) && !(value.getClass().equals(String.class) || value.getClass().equals(String[].class)))            {                // when converting to a string, use the source target's class's converter                tc = lookup(value.getClass());            } else {                // when converting from a string, use the toClass's converter                tc = lookup(toClass);            }                        if (LOG.isDebugEnabled())                LOG.debug("global-level type converter for property ["+property+"] = "+(tc==null?"none found":tc));        }                        if (tc != null) {            try {                return tc.convertValue(context, target, member, property, value, toClass);            } catch (Exception e) {                handleConversionException(context, property, value, target);                return OgnlRuntime.NoConversionPossible;            }        }        if (defaultTypeConverter != null) {            try {                if (LOG.isDebugEnabled())                    LOG.debug("falling back to default type converter ["+defaultTypeConverter+"]");                return defaultTypeConverter.convertValue(context, target, member, property, value, toClass);            } catch (Exception e) {                handleConversionException(context, property, value, target);                return OgnlRuntime.NoConversionPossible;            }        } else {            try {                if (LOG.isDebugEnabled())                    LOG.debug("falling back to Ognl's default type conversion");                return super.convertValue(context, target, member, property, value, toClass);            } catch (Exception e) {                handleConversionException(context, property, value, target);                return OgnlRuntime.NoConversionPossible;            }        }    }    /**     * Looks for a TypeConverter in the default mappings.     *     * @param className name of the class the TypeConverter must handle     * @return a TypeConverter to handle the specified class or null if none can be found     */    public TypeConverter lookup(String className) {        if (unknownMappings.contains(className)) {            return null;        }        TypeConverter result = (TypeConverter) defaultMappings.get(className);        //Looks for super classes        if (result == null) {            Class clazz = null;            try {                clazz = Thread.currentThread().getContextClassLoader().loadClass(className);            } catch (ClassNotFoundException cnfe) {            }            result = lookupSuper(clazz);            if (result != null) {                //Register now, the next lookup will be faster                registerConverter(className, result);            } else {                // if it isn't found, never look again (also faster)                registerConverterNotFound(className);            }        }        return result;    }    /**     * Looks for a TypeConverter in the default mappings.     *     * @param clazz the class the TypeConverter must handle     * @return a TypeConverter to handle the specified class or null if none can be found     */    public TypeConverter lookup(Class clazz) {        return lookup(clazz.getName());    }    protected Object getConverter(Class clazz, String property) {        if (LOG.isDebugEnabled()) {            LOG.debug("Property: " + property);            LOG.debug("Class: " + clazz.getName());        }        synchronized (clazz) {            if ((property != null) && !noMapping.contains(clazz)) {                try {                    Map<String, Object> mapping = mappings.get(clazz);                    if (mapping == null) {                        mapping = buildConverterMapping(clazz);                    } else {                        mapping = conditionalReload(clazz, mapping);                    }                    Object converter = mapping.get(property);                    if (LOG.isDebugEnabled() && converter == null) {                        LOG.debug("converter is null for property " + property + ". Mapping size: " + mapping.size());                        Iterator<String> iter = mapping.keySet().iterator();                        while (iter.hasNext()) {                            String next = iter.next();                            LOG.debug(next + ":" + mapping.get(next));                        }                    }                    return converter;                } catch (Throwable t) {                    noMapping.add(clazz);                }            }        }        return null;    }    protected void handleConversionException(Map context, String property, Object value, Object object) {        if ((Boolean.TRUE.equals(context.get(REPORT_CONVERSION_ERRORS)))) {            String realProperty = property;            String fullName = (String) context.get(CONVERSION_PROPERTY_FULLNAME);            if (fullName != null) {                realProperty = fullName;            }            Map conversionErrors = (Map) context.get(ActionContext.CONVERSION_ERRORS);            if (conversionErrors == null) {                conversionErrors = new HashMap();                context.put(ActionContext.CONVERSION_ERRORS, conversionErrors);            }            conversionErrors.put(realProperty, value);        }    }    public synchronized void registerConverter(String className, TypeConverter converter) {        defaultMappings.put(className, converter);    }    public synchronized void registerConverterNotFound(String className) {        unknownMappings.add(className);    }    private Object[] getClassProperty(Map context) {        return (Object[]) context.get("__link");    }    /**     * not used     */    private Object acceptableErrorValue(Class toClass) {        if (!toClass.isPrimitive()) {            return null;        }                if (toClass == int.class) {            return new Integer(0);        } else if (toClass == double.class) {            return new Double(0);        } else if (toClass == long.class) {            return new Long(0);        } else if (toClass == boolean.class) {            return Boolean.FALSE;        } else if (toClass == short.class) {            return new Short((short) 0);        } else if (toClass == float.class) {            return new Float(0);        } else if (toClass == byte.class) {            return new Byte((byte) 0);        } else if (toClass == char.class) {            return new Character((char) 0);        }                return null;    }    /**     * Looks for converter mappings for the specified class and adds it to an existing map.  Only new converters are     * added.  If a converter is defined on a key that already exists, the converter is ignored.     *     * @param mapping an existing map to add new converter mappings to     * @param clazz   class to look for converter mappings for     */    void addConverterMapping(Map<String, Object> mapping, Class clazz) {        try {            String converterFilename = buildConverterFilename(clazz);            InputStream is = FileManager.loadFile(converterFilename, clazz);            if (is != null) {                if (LOG.isDebugEnabled()) {                    LOG.debug("processing conversion file ["+converterFilename+"] [class="+clazz+"]");                }                                Properties prop = new Properties();                prop.load(is);                Iterator it = prop.entrySet().iterator();                while (it.hasNext()) {                    Map.Entry entry = (Map.Entry) it.next();                    String key = (String) entry.getKey();                    if (mapping.containsKey(key)) {                        break;                    }                    // for keyProperty of Set                    if (key.startsWith(DefaultObjectTypeDeterminer.KEY_PROPERTY_PREFIX)                            || key.startsWith(DefaultObjectTypeDeterminer.CREATE_IF_NULL_PREFIX)) {                        if (LOG.isDebugEnabled()) {                            LOG.debug("\t"+key + ":" + entry.getValue()+"[treated as String]");                        }                        mapping.put(key, entry.getValue());                    }                    //for properties of classes                    else if (!(key.startsWith(DefaultObjectTypeDeterminer.ELEMENT_PREFIX) ||                            key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX) ||                            key.startsWith(DefaultObjectTypeDeterminer.DEPRECATED_ELEMENT_PREFIX))                            ) {                        TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());                        if (LOG.isDebugEnabled()) {                            LOG.debug("\t"+key + ":" + entry.getValue()+"[treated as TypeConverter "+_typeConverter+"]");                        }                        mapping.put(key, _typeConverter);                    }                    //for keys of Maps                    else if (key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX)) {                        Class converterClass = Thread.currentThread().getContextClassLoader().loadClass((String) entry.getValue());                                                //check if the converter is a type converter if it is one                        //then just put it in the map as is. Otherwise                        //put a value in for the type converter of the class                        if (converterClass.isAssignableFrom(TypeConverter.class)) {                            TypeConverter _typeConverter = createTypeConverter((String) entry.getValue());                            if (LOG.isDebugEnabled()) {                                LOG.debug("\t"+key + ":" + entry.getValue()+"[treated as TypeConverter "+_typeConverter+"]");                            }                            mapping.put(key, _typeConverter);                        } else {                            if (LOG.isDebugEnabled()) {                                LOG.debug("\t"+key + ":" + entry.getValue()+"[treated as Class "+converterClass+"]");                            }                            mapping.put(key, converterClass);                        }                    }                    //elements(values) of maps / lists                    else {                        Class _c = Thread.currentThread().getContextClassLoader().loadClass((String) entry.getValue());                        if (LOG.isDebugEnabled()) {                            LOG.debug("\t"+key + ":" + entry.getValue()+"[treated as Class "+_c+"]");                        }                        mapping.put(key, _c);                    }                }            }

⌨️ 快捷键说明

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