public synchronized void registerConverter(String className, TypeConverter converter) { converterHolder.addDefaultMapping(className, converter); }
public synchronized void registerConverterNotFound(String className) { converterHolder.addUnknownMapping(className); }
public Object convertValue(Map context, Object target, Member member, String propertyName, Object value, Class toType) { return typeConverter.convertValue(context, target, member, propertyName, value, toType); }
if ((property != null) && !converterHolder.containsNoMapping(clazz)) { try { Map<String, Object> mapping = converterHolder.getMapping(clazz); mapping = buildConverterMapping(clazz); } else { mapping = conditionalReload(clazz, mapping); LOG.debug("Got exception trying to resolve convert for class [#0] and property [#1]", t, clazz, property); converterHolder.addNoMapping(clazz);
package util; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Map; import com.opensymphony.xwork2.conversion.TypeConversionException; import ognl.DefaultTypeConverter; public class MyDateConverter extends DefaultTypeConverter { @Override public Object convertValue(Map context, Object object, Class type) { if (type == Date.class) { String datePattern = "yyyy.MM.dd"; DateFormat format = new SimpleDateFormat(datePattern); format.setLenient(false); try { String[] dateString = (String[]) object; return format.parse(dateString[0]); } catch (Exception e) { throw new TypeConversionException("Given Date is Invalid"); } } return null; } }
memberType = objectTypeDeterminer.getElementClass(target.getClass(), propertyName, null); } else if (value.getClass().isArray()) { Object[] objArray = (Object[]) value; TypeConverter converter = getTypeConverter(context); result = createCollection(toType, memberType, objArray.length); Object convertedValue = converter.convertValue(context, target, member, propertyName, anObjArray, memberType); if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) { result.add(convertedValue); TypeConverter converter = getTypeConverter(context); result = createCollection(toType, memberType, col.size()); Object convertedValue = converter.convertValue(context, target, member, propertyName, aCol, memberType); if (!TypeConverter.NO_CONVERSION_POSSIBLE.equals(convertedValue)) { result.add(convertedValue); result = createCollection(toType, memberType, -1); result.add(value);
/** * Recurses through a class' interfaces and class hierarchy looking for a TypeConverter in the default mapping that * can handle the specified class. * * @param clazz the class the TypeConverter must handle * @return a TypeConverter to handle the specified class or null if none can be found */ TypeConverter lookupSuper(Class clazz) { TypeConverter result = null; if (clazz != null) { result = converterHolder.getDefaultMapping(clazz.getName()); if (result == null) { // Looks for direct interfaces (depth = 1 ) Class[] interfaces = clazz.getInterfaces(); for (Class anInterface : interfaces) { if (converterHolder.containsDefaultMapping(anInterface.getName())) { result = converterHolder.getDefaultMapping(anInterface.getName()); break; } } if (result == null) { // Looks for the superclass // If 'clazz' is the Object class, an interface, a primitive type or void then clazz.getSuperClass() returns null result = lookupSuper(clazz.getSuperclass()); } } } return result; }
private Object getValue(Map context, Object value) { Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED); String lastProperty = (String) context.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED); if (lastClass == null || lastProperty == null) { return value; } Class elementClass = objectTypeDeterminer.getElementClass(lastClass, lastProperty, null); if (elementClass == null) { return value; // nothing is specified, we assume it will be the value passed in. } return xworkConverter.convertValue(context, value, elementClass); }
private Object getKey(Map context, Object name) { Class lastClass = (Class) context.get(XWorkConverter.LAST_BEAN_CLASS_ACCESSED); String lastProperty = (String) context.get(XWorkConverter.LAST_BEAN_PROPERTY_ACCESSED); if (lastClass == null || lastProperty == null) { // return java.lang.String.class; // commented out the above -- it makes absolutely no sense for when setting basic maps! return name; } Class keyClass = objectTypeDeterminer.getKeyClass(lastClass, lastProperty); if (keyClass == null) { keyClass = java.lang.String.class; } return xworkConverter.convertValue(context, name, keyClass); } }
private Object convert(Object element) { if ((element != null) && !clazz.isAssignableFrom(element.getClass())) { // convert to correct type if (LOG.isDebugEnabled()) { LOG.debug("Converting from " + element.getClass().getName() + " to " + clazz.getName()); } TypeConverter conv = getTypeConverter(); Map<String, Object> context = ActionContext.getContext().getContextMap(); element = conv.convertValue(context, null, null, null, element, clazz); } return element; }
public Object nullMethodResult(Map context, Object target, String methodName, Object[] args) { return wrapped.nullMethodResult(context, target, methodName, args); }
public Object nullPropertyValue(Map context, Object target, Object property) { return wrapped.nullPropertyValue(context, target, property); }
public class StringToDateTimeConverter extends StrutsTypeConverter { // WARNING not safe in multi-threaded environments private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss"); public Object convertFromString(Map context, String[] strings, Class toClass) { if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) { return null; } try { return DATETIME_FORMAT.parse(strings[0]); } catch (ParseException e) { throw new TypeConversionException("Unable to convert given object to date: " + strings[0]); } } public String convertToString(Map context, Object date) { if (date != null && date instanceof Date) { return DATETIME_FORMAT.format(date); } else { return null; } } }
private Object doConvertToCollection(Map<String, Object> context, Object o, Member member, String prop, Object value, Class toType) { TypeConverter converter = container.getInstance(CollectionConverter.class); if (converter == null) { throw new XWorkException("TypeConverter with name [#0] must be registered first!", XWorkConstants.COLLECTION_CONVERTER); } return converter.convertValue(context, o, member, prop, value, toType); }
@SuppressWarnings("rawtypes") public Object convertFromString(Map context, String[] values, Class toClass) { if (values != null && values.length > 0 && values[0] != null && values[0].length() > 0) { String value = values[0]; if (value.matches("^(\\d+)$")) return new Date(Long.parseLong(value)); try { return _format.parse(value); } catch (ParseException e) { e.printStackTrace(); throw new TypeConversionException(e); } } return null; }
private Object doConvertToArray(Map<String, Object> context, Object o, Member member, String prop, Object value, Class toType) { TypeConverter converter = container.getInstance(ArrayConverter.class); if (converter == null) { throw new XWorkException("TypeConverter with name [#0] must be registered first!", XWorkConstants.ARRAY_CONVERTER); } return converter.convertValue(context, o, member, prop, value, toType); }
private Object doConvertToDate(Map<String, Object> context, Object value, Class toType) { TypeConverter converter = container.getInstance(DateConverter.class); if (converter == null) { throw new XWorkException("TypeConverter with name [#0] must be registered first!", XWorkConstants.DATE_CONVERTER); } return converter.convertValue(context, null, null, null, value, toType); }
private Object doConvertToString(Map<String, Object> context, Object value) { TypeConverter converter = container.getInstance(StringConverter.class); if (converter == null) { throw new XWorkException("TypeConverter with name [#0] must be registered first!", XWorkConstants.STRING_CONVERTER); } return converter.convertValue(context, null, null, null, value, null); }
private Object doConvertToNumber(Map<String, Object> context, Object value, Class toType) { TypeConverter converter = container.getInstance(NumberConverter.class); if (converter == null) { throw new XWorkException("TypeConverter with name [#0] must be registered first!", XWorkConstants.NUMBER_CONVERTER); } return converter.convertValue(context, null, null, null, value, toType); }