/** * Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding * any additional mappings it may find. Mappings lower in the hierarchy have priority over those higher in the * hierarcy. * * @param clazz the class to look for converter mappings for * @return the converter mappings */ protected Map<String, Object> buildConverterMapping(Class clazz) throws Exception { Map<String, Object> mapping = new HashMap<String, Object>(); // check for conversion mapping associated with super classes and any implemented interfaces Class curClazz = clazz; while (!curClazz.equals(Object.class)) { // add current class' mappings addConverterMapping(mapping, curClazz); // check interfaces' mappings Class[] interfaces = curClazz.getInterfaces(); for (Class anInterface : interfaces) { addConverterMapping(mapping, anInterface); } curClazz = curClazz.getSuperclass(); } if (mapping.size() > 0) { converterHolder.addMapping(clazz, mapping); } else { converterHolder.addNoMapping(clazz); } return mapping; }
private Map<String, Object> conditionalReload(Class clazz, Map<String, Object> oldValues) throws Exception { Map<String, Object> mapping = oldValues; if (reloadingConfigs) { URL fileUrl = ClassLoaderUtil.getResource(buildConverterFilename(clazz), clazz); if (fileManager.fileNeedsReloading(fileUrl)) { mapping = buildConverterMapping(clazz); } } return mapping; }
private Object getRealValue(Map context, Object value, Class convertToClass) { if (value == null || convertToClass == null) { return value; } return xworkConverter.convertValue(context, value, convertToClass); } }
classProp = getClassProperty(context); tc = (TypeConverter) getConverter(clazz, property); if (lastPropertyPath != null && clazz != null) { String path = lastPropertyPath + "." + property; tc = (TypeConverter) getConverter(clazz, path); if (toClass.equals(String.class) && (value != null) && !(value.getClass().equals(String.class) || value.getClass().equals(String[].class))) { tc = lookup(value.getClass()); } else { tc = lookup(toClass); if (LOG.isDebugEnabled()) LOG.debug("unable to convert value using type converter [#0]", e, tc.getClass().getName()); handleConversionException(context, property, value, target); if (LOG.isDebugEnabled()) LOG.debug("unable to convert value using type converter [#0]", e, defaultTypeConverter.getClass().getName()); handleConversionException(context, property, value, target); if (LOG.isDebugEnabled()) LOG.debug("unable to convert value using type converter [#0]", e, super.getClass().getName()); handleConversionException(context, property, value, target);
public static String getConversionErrorMessage(String propertyName, ValueStack stack) { String defaultMessage = LocalizedTextUtil.findDefaultText(XWorkMessages.DEFAULT_INVALID_FIELDVALUE, ActionContext.getContext().getLocale(), new Object[]{ propertyName }); List<String> indexValues = getIndexValues(propertyName); propertyName = removeAllIndexesInProperytName(propertyName); String getTextExpression = "getText('" + CONVERSION_ERROR_PROPERTY_PREFIX + propertyName + "','" + defaultMessage + "')"; String message = (String) stack.findValue(getTextExpression); if (message == null) { message = defaultMessage; } else { message = MessageFormat.format(message, indexValues.toArray()); } return message; }
mapping = buildConverterMapping(clazz); } else { mapping = conditionalReload(clazz, mapping);
/** * Determines the key property for a Collection by getting it from the @KeyProperty annotation. * * As fallback, it determines the String key property for a Collection by getting it from the conversion properties * file using the KeyProperty_ prefix. KeyProperty_${property}=somePropertyOfBeansInTheSet * * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for. * @param property the property of the Map or Collection for the given parent class * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getKeyProperty(Class, String) */ public String getKeyProperty(Class parentClass, String property) { KeyProperty annotation = getAnnotation(parentClass, property, KeyProperty.class); if (annotation != null) { return annotation.value(); } return (String) xworkConverter.getConverter(parentClass, KEY_PROPERTY_PREFIX + property); }
/** * The validation implementation must guarantee that setValidatorContext will * be called with a non-null ValidatorContext before validate is called. * * @param object * @throws ValidationException */ @Override public void doValidate(Object object) throws ValidationException { String fieldName = getFieldName(); String fullFieldName = getValidatorContext().getFullFieldName(fieldName); ActionContext context = ActionContext.getContext(); Map<String, Object> conversionErrors = context.getConversionErrors(); if (conversionErrors.containsKey(fullFieldName)) { if ((defaultMessage == null) || ("".equals(defaultMessage.trim()))) { defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, context.getValueStack()); } addFieldError(fieldName, object); } }
String converterFilename = buildConverterFilename(clazz); fileProcessor.process(mapping, clazz, converterFilename);
return annotation.value(); String configValue = (String) xworkConverter.getConverter(parentClass, CREATE_IF_NULL_PREFIX + property);
String message = XWorkConverter.getConversionErrorMessage(propertyName, stack);
private Object getRealValue(Map context, Object value, Class convertToClass) { if (value == null || convertToClass == null) { return value; } return xworkConverter.convertValue(context, value, convertToClass); } }
/** * Determines the element class by looking for the value of @Element annotation for the given * class. * If no annotation is found, the element class is determined by using the generic parametrics. * * As fallback, it determines the key class by looking for the value of Element_${property} in the properties * file for the given class. Also looks for the deprecated Collection_${property} * * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for. * @param property the property of the Map or Collection for the given parent class * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getElementClass(Class, String, Object) */ public Class getElementClass(Class parentClass, String property, Object key) { Element annotation = getAnnotation(parentClass, property, Element.class); if (annotation != null) { return annotation.value(); } Class clazz = getClass(parentClass, property, true); if (clazz != null) { return clazz; } clazz = (Class) xworkConverter.getConverter(parentClass, ELEMENT_PREFIX + property); if (clazz == null) { clazz = (Class) xworkConverter.getConverter(parentClass, DEPRECATED_ELEMENT_PREFIX + property); if (LOG.isInfoEnabled() && clazz != null) { LOG.info("The Collection_xxx pattern for collection type conversion is deprecated. Please use Element_xxx!"); } } return clazz; }
@Override public Object convertValue(Map<String, Object> map, Object o, Class aClass) { return convertValue(map, null, null, null, o, aClass); }
/** * Determines the key class by looking for the value of @Key annotation for the given class. * If no annotation is found, the key class is determined by using the generic parametrics. * * As fallback, it determines the key class by looking for the value of Key_${property} in the properties * file for the given class. * * @param parentClass the Class which contains as a property the Map or Collection we are finding the key for. * @param property the property of the Map or Collection for the given parent class * @see com.opensymphony.xwork2.conversion.ObjectTypeDeterminer#getKeyClass(Class, String) */ public Class getKeyClass(Class parentClass, String property) { Key annotation = getAnnotation(parentClass, property, Key.class); if (annotation != null) { return annotation.value(); } Class clazz = getClass(parentClass, property, false); if (clazz != null) { return clazz; } return (Class) xworkConverter.getConverter(parentClass, KEY_PREFIX + property); }
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 tryFindValue(String expr, Class asType) throws OgnlException { Object value = null; try { expr = lookupForOverrides(expr); value = getValue(expr, asType); if (value == null) { value = findInContext(expr); return converter.convertValue(getContext(), value, asType); } } finally { context.remove(THROW_EXCEPTION_ON_FAILURE); } return value; }
/** * @see com.opensymphony.xwork2.util.ValueStack#findValue(java.lang.String, java.lang.Class) */ public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) { try { setupExceptionOnFailure(throwExceptionOnFailure); return tryFindValueWhenExpressionIsNotNull(expr, asType); } catch (OgnlException e) { final Object value = handleOgnlException(expr, throwExceptionOnFailure, e); return converter.convertValue(getContext(), value, asType); } catch (Exception e) { final Object value = handleOtherException(expr, throwExceptionOnFailure, e); return converter.convertValue(getContext(), value, asType); } finally { ReflectionContextState.clear(context); } }
String stringElement = (String)conv.convertValue(context, element, String.class); if (shallBeIncluded(stringElement, excludeEmptyElements)) { if (evaluator != null) {