Search in sources :

Example 1 with TypeConverter

use of com.opensymphony.xwork2.conversion.TypeConverter in project struts by apache.

the class CollectionConverter method convertValue.

public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
    Collection result;
    Class memberType = String.class;
    if (target != null) {
        memberType = objectTypeDeterminer.getElementClass(target.getClass(), propertyName, 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 = getTypeConverter(context);
        result = createCollection(toType, memberType, objArray.length);
        for (Object anObjArray : objArray) {
            Object convertedValue = converter.convertValue(context, target, member, propertyName, anObjArray, memberType);
            if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
                result.add(convertedValue);
            }
        }
    } else if (Collection.class.isAssignableFrom(value.getClass())) {
        Collection col = (Collection) value;
        TypeConverter converter = getTypeConverter(context);
        result = createCollection(toType, memberType, col.size());
        for (Object aCol : col) {
            Object convertedValue = converter.convertValue(context, target, member, propertyName, aCol, memberType);
            if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
                result.add(convertedValue);
            }
        }
    } else {
        result = createCollection(toType, memberType, -1);
        TypeConverter converter = getTypeConverter(context);
        Object convertedValue = converter.convertValue(context, target, member, propertyName, value, memberType);
        if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) {
            result.add(convertedValue);
        }
    }
    return result;
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter) Collection(java.util.Collection)

Example 2 with TypeConverter

use of com.opensymphony.xwork2.conversion.TypeConverter in project struts by apache.

the class XWorkList method convert.

private Object convert(Object element) {
    if ((element != null) && !clazz.isAssignableFrom(element.getClass())) {
        // convert to correct type
        LOG.debug("Converting from {} to {}", element.getClass().getName(), clazz.getName());
        TypeConverter converter = getTypeConverter();
        Map<String, Object> context = ActionContext.getContext().getContextMap();
        element = converter.convertValue(context, null, null, null, element, clazz);
    }
    return element;
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter)

Example 3 with TypeConverter

use of com.opensymphony.xwork2.conversion.TypeConverter in project struts by apache.

the class ArrayConverter method convertValue.

@Override
public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType) {
    Object result = null;
    Class componentType = toType.getComponentType();
    if (componentType != null) {
        TypeConverter converter = 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, target, member, propertyName, valueItem, componentType));
            }
        } else {
            result = Array.newInstance(componentType, 1);
            Array.set(result, 0, converter.convertValue(context, target, member, propertyName, value, componentType));
        }
    }
    return result;
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter)

Example 4 with TypeConverter

use of com.opensymphony.xwork2.conversion.TypeConverter in project struts by apache.

the class DefaultConversionFileProcessor method process.

public void process(Map<String, Object> mapping, Class clazz, String converterFilename) {
    try {
        InputStream is = fileManager.loadFile(ClassLoaderUtil.getResource(converterFilename, clazz));
        if (is != null) {
            LOG.debug("Processing conversion file [{}] for class [{}]", converterFilename, clazz);
            Properties prop = new Properties();
            prop.load(is);
            for (Map.Entry<Object, Object> entry : prop.entrySet()) {
                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)) {
                    LOG.debug("\t{}:{} [treated as String]", key, entry.getValue());
                    mapping.put(key, entry.getValue());
                } else // for properties of classes
                if (!(key.startsWith(DefaultObjectTypeDeterminer.ELEMENT_PREFIX) || key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX) || key.startsWith(DefaultObjectTypeDeterminer.DEPRECATED_ELEMENT_PREFIX))) {
                    TypeConverter _typeConverter = converterCreator.createTypeConverter((String) entry.getValue());
                    LOG.debug("\t{}:{} [treated as TypeConverter {}]", key, entry.getValue(), _typeConverter);
                    mapping.put(key, _typeConverter);
                } else // for keys of Maps
                if (key.startsWith(DefaultObjectTypeDeterminer.KEY_PREFIX)) {
                    Class converterClass = ClassLoaderUtil.loadClass((String) entry.getValue(), this.getClass());
                    // put a value in for the type converter of the class
                    if (converterClass.isAssignableFrom(TypeConverter.class)) {
                        TypeConverter _typeConverter = converterCreator.createTypeConverter((String) entry.getValue());
                        LOG.debug("\t{}:{} [treated as TypeConverter {}]", key, entry.getValue(), _typeConverter);
                        mapping.put(key, _typeConverter);
                    } else {
                        LOG.debug("\t{}:{} [treated as Class {}]", key, entry.getValue(), converterClass);
                        mapping.put(key, converterClass);
                    }
                } else // elements(values) of maps / lists
                {
                    Class _c = ClassLoaderUtil.loadClass((String) entry.getValue(), this.getClass());
                    LOG.debug("\t{}:{} [treated as Class {}]", key, entry.getValue(), _c);
                    mapping.put(key, _c);
                }
            }
        }
    } catch (Exception ex) {
        LOG.error("Problem loading properties for {}", clazz.getName(), ex);
    }
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter) InputStream(java.io.InputStream) Properties(java.util.Properties) Map(java.util.Map)

Example 5 with TypeConverter

use of com.opensymphony.xwork2.conversion.TypeConverter in project struts by apache.

the class StrutsConversionPropertiesProcessor method loadConversionProperties.

public void loadConversionProperties(String propsName, boolean require) {
    try {
        Iterator<URL> resources = ClassLoaderUtil.getResources(propsName, getClass(), true);
        while (resources.hasNext()) {
            if (XWORK_CONVERSION_PROPERTIES.equals(propsName)) {
                LOG.warn("Instead of using deprecated {} please use the new file name {}", XWORK_CONVERSION_PROPERTIES, STRUTS_CONVERSION_PROPERTIES);
            }
            URL url = resources.next();
            Properties props = new Properties();
            props.load(url.openStream());
            LOG.debug("Processing conversion file [{}]", propsName);
            for (Object o : props.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                String key = (String) entry.getKey();
                try {
                    TypeConverter typeConverter = converterCreator.createTypeConverter((String) entry.getValue());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("\t{}:{} [treated as TypeConverter {}]", key, entry.getValue(), typeConverter);
                    }
                    converterHolder.addDefaultMapping(key, typeConverter);
                } catch (Exception e) {
                    LOG.error("Conversion registration error", e);
                }
            }
        }
    } catch (IOException ex) {
        if (require) {
            throw new StrutsException("Cannot load conversion properties file: " + propsName, ex);
        } else {
            LOG.debug("Cannot load conversion properties file: {}", propsName, ex);
        }
    }
}
Also used : TypeConverter(com.opensymphony.xwork2.conversion.TypeConverter) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) Properties(java.util.Properties) Map(java.util.Map) URL(java.net.URL) IOException(java.io.IOException) StrutsException(org.apache.struts2.StrutsException)

Aggregations

TypeConverter (com.opensymphony.xwork2.conversion.TypeConverter)5 Map (java.util.Map)2 Properties (java.util.Properties)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Collection (java.util.Collection)1 StrutsException (org.apache.struts2.StrutsException)1