com.opensymphony.xwork2.conversion.impl
Class XWorkConverter

java.lang.Object
  extended by com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter
      extended by com.opensymphony.xwork2.conversion.impl.XWorkConverter
All Implemented Interfaces:
TypeConverter
Direct Known Subclasses:
AnnotationXWorkConverter

public class XWorkConverter
extends DefaultTypeConverter

XWorkConverter is a singleton used by many of the Struts 2's Ognl extention points, such as InstantiatingNullHandler, XWorkListPropertyAccessor etc to do object conversion.

Type conversion is great for situations where you need to turn a String in to a more complex object. Because the web is type-agnostic (everything is a string in HTTP), Struts 2's type conversion features are very useful. For instance, if you were prompting a user to enter in coordinates in the form of a string (such as "3, 22"), you could have Struts 2 do the conversion both from String to Point and from Point to String.

Using this "point" example, if your action (or another compound object in which you are setting properties on) has a corresponding ClassName-conversion.properties file, Struts 2 will use the configured type converters for conversion to and from strings. So turning "3, 22" in to new Point(3, 22) is done by merely adding the following entry to ClassName-conversion.properties (Note that the PointConverter should impl the TypeConverter interface):

point = com.acme.PointConverter

Your type converter should be sure to check what class type it is being requested to convert. Because it is used for both to and from strings, you will need to split the conversion method in to two parts: one that turns Strings in to Points, and one that turns Points in to Strings.

After this is done, you can now reference your point (using <s:property value="point"/> in JSP or ${point} in FreeMarker) and it will be printed as "3, 22" again. As such, if you submit this back to an action, it will be converted back to a Point once again.

In some situations you may wish to apply a type converter globally. This can be done by editing the file xwork-conversion.properties in the root of your class path (typically WEB-INF/classes) and providing a property in the form of the class name of the object you wish to convert on the left hand side and the class name of the type converter on the right hand side. For example, providing a type converter for all Point objects would mean adding the following entry:

com.acme.Point = com.acme.PointConverter

Type conversion should not be used as a substitute for i18n. It is not recommended to use this feature to print out properly formatted dates. Rather, you should use the i18n features of Struts 2 (and consult the JavaDocs for JDK's MessageFormat object) to see how a properly formatted date should be displayed.

Any error that occurs during type conversion may or may not wish to be reported. For example, reporting that the input "abc" could not be converted to a number might be important. On the other hand, reporting that an empty string, "", cannot be converted to a number might not be important - especially in a web environment where it is hard to distinguish between a user not entering a value vs. entering a blank value.

By default, all conversion errors are reported using the generic i18n key xwork.default.invalid.fieldvalue, which you can override (the default text is Invalid field value for field "xxx", where xxx is the field name) in your global i18n resource bundle.

However, sometimes you may wish to override this message on a per-field basis. You can do this by adding an i18n key associated with just your action (Action.properties) using the pattern invalid.fieldvalue.xxx, where xxx is the field name.

It is important to know that none of these errors are actually reported directly. Rather, they are added to a map called conversionErrors in the ActionContext. There are several ways this map can then be accessed and the errors can be reported accordingly.

Version:
$Date: 2009-12-27 19:18:29 +0100 (Sun, 27 Dec 2009) $ $Id: XWorkConverter.java 894090 2009-12-27 18:18:29Z martinc $
Author:
Pat Lightbody, Rainer Hermanns, Alexandru Popescu, tm_jee
See Also:
XWorkBasicConverter

Field Summary
static java.lang.String CONVERSION_COLLECTION_PREFIX
           
static java.lang.String CONVERSION_ERROR_PROPERTY_PREFIX
           
static java.lang.String CONVERSION_PROPERTY_FULLNAME
           
protected  java.util.HashMap<java.lang.String,TypeConverter> defaultMappings
          Record class and its type converter mapping.
static java.lang.String LAST_BEAN_CLASS_ACCESSED
           
static java.lang.String LAST_BEAN_PROPERTY_ACCESSED
           
protected static Logger LOG
           
protected  java.util.HashMap<java.lang.Class,java.util.Map<java.lang.String,java.lang.Object>> mappings
          Target class conversion Mappings.
static java.lang.String MESSAGE_INDEX_BRACKET_PATTERN
           
static java.lang.String MESSAGE_INDEX_PATTERN
           
static java.util.regex.Pattern messageIndexPattern
           
protected  java.util.HashSet<java.lang.Class> noMapping
          Unavailable target class conversion mappings, serves as a simple cache.
static java.lang.String PERIOD
           
static java.lang.String REPORT_CONVERSION_ERRORS
           
protected  java.util.HashSet<java.lang.String> unknownMappings
          Record classes that doesn't have conversion mapping defined.
 
Fields inherited from interface com.opensymphony.xwork2.conversion.TypeConverter
NO_CONVERSION_POSSIBLE, TYPE_CONVERTER_CONTEXT_KEY
 
Constructor Summary
protected XWorkConverter()
           
 
Method Summary
protected  void addConverterMapping(java.util.Map<java.lang.String,java.lang.Object> mapping, java.lang.Class clazz)
          Looks for converter mappings for the specified class and adds it to an existing map.
static java.lang.String buildConverterFilename(java.lang.Class clazz)
           
protected  java.util.Map<java.lang.String,java.lang.Object> buildConverterMapping(java.lang.Class clazz)
          Looks for converter mappings for the specified class, traversing up its class hierarchy and interfaces and adding any additional mappings it may find.
 java.lang.Object convertValue(java.util.Map<java.lang.String,java.lang.Object> map, java.lang.Object o, java.lang.Class aClass)
           
 java.lang.Object convertValue(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.Object target, java.lang.reflect.Member member, java.lang.String property, java.lang.Object value, java.lang.Class toClass)
          Convert value from one form to another.
static java.lang.String getConversionErrorMessage(java.lang.String propertyName, ValueStack stack)
           
protected  java.lang.Object getConverter(java.lang.Class clazz, java.lang.String property)
           
protected  void handleConversionException(java.util.Map<java.lang.String,java.lang.Object> context, java.lang.String property, java.lang.Object value, java.lang.Object object)
           
 void loadConversionProperties(java.lang.String propsName)
           
 void loadConversionProperties(java.lang.String propsName, boolean require)
           
 TypeConverter lookup(java.lang.Class clazz)
          Looks for a TypeConverter in the default mappings.
 TypeConverter lookup(java.lang.String className)
          Looks for a TypeConverter in the default mappings.
 void registerConverter(java.lang.String className, TypeConverter converter)
           
 void registerConverterNotFound(java.lang.String className)
           
 void setDefaultTypeConverter(XWorkBasicConverter conv)
           
 void setObjectFactory(ObjectFactory factory)
           
 
Methods inherited from class com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter
bigDecValue, bigIntValue, booleanValue, convertValue, doubleValue, enumValue, getTypeConverter, longValue, stringValue, stringValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static final Logger LOG

REPORT_CONVERSION_ERRORS

public static final java.lang.String REPORT_CONVERSION_ERRORS
See Also:
Constant Field Values

CONVERSION_PROPERTY_FULLNAME

public static final java.lang.String CONVERSION_PROPERTY_FULLNAME
See Also:
Constant Field Values

CONVERSION_ERROR_PROPERTY_PREFIX

public static final java.lang.String CONVERSION_ERROR_PROPERTY_PREFIX
See Also:
Constant Field Values

CONVERSION_COLLECTION_PREFIX

public static final java.lang.String CONVERSION_COLLECTION_PREFIX
See Also:
Constant Field Values

LAST_BEAN_CLASS_ACCESSED

public static final java.lang.String LAST_BEAN_CLASS_ACCESSED
See Also:
Constant Field Values

LAST_BEAN_PROPERTY_ACCESSED

public static final java.lang.String LAST_BEAN_PROPERTY_ACCESSED
See Also:
Constant Field Values

MESSAGE_INDEX_PATTERN

public static final java.lang.String MESSAGE_INDEX_PATTERN
See Also:
Constant Field Values

MESSAGE_INDEX_BRACKET_PATTERN

public static final java.lang.String MESSAGE_INDEX_BRACKET_PATTERN
See Also:
Constant Field Values

PERIOD

public static final java.lang.String PERIOD
See Also:
Constant Field Values

messageIndexPattern

public static final java.util.regex.Pattern messageIndexPattern

mappings

protected java.util.HashMap<java.lang.Class,java.util.Map<java.lang.String,java.lang.Object>> mappings
Target class conversion Mappings.
 Map>
  - Class -> convert to class
  - Map
    - String -> property name
                eg. Element_property, property etc.
    - Object -> String to represent properties
                eg. value part of
                    KeyProperty_property=id
             -> TypeConverter to represent an Ognl TypeConverter
                eg. value part of
                    property=foo.bar.MyConverter
             -> Class to represent a class
                eg. value part of
                    Element_property=foo.bar.MyObject
 


noMapping

protected java.util.HashSet<java.lang.Class> noMapping
Unavailable target class conversion mappings, serves as a simple cache.


defaultMappings

protected java.util.HashMap<java.lang.String,TypeConverter> defaultMappings
Record class and its type converter mapping.
 - String - classname as String
 - TypeConverter - instance of TypeConverter
 


unknownMappings

protected java.util.HashSet<java.lang.String> unknownMappings
Record classes that doesn't have conversion mapping defined.
 - String -> classname as String
 

Constructor Detail

XWorkConverter

protected XWorkConverter()
Method Detail

setObjectFactory

public void setObjectFactory(ObjectFactory factory)

setDefaultTypeConverter

public void setDefaultTypeConverter(XWorkBasicConverter conv)

getConversionErrorMessage

public static java.lang.String getConversionErrorMessage(java.lang.String propertyName,
                                                         ValueStack stack)

buildConverterFilename

public static java.lang.String buildConverterFilename(java.lang.Class clazz)

convertValue

public java.lang.Object convertValue(java.util.Map<java.lang.String,java.lang.Object> map,
                                     java.lang.Object o,
                                     java.lang.Class aClass)
Overrides:
convertValue in class DefaultTypeConverter

convertValue

public java.lang.Object convertValue(java.util.Map<java.lang.String,java.lang.Object> context,
                                     java.lang.Object target,
                                     java.lang.reflect.Member member,
                                     java.lang.String property,
                                     java.lang.Object value,
                                     java.lang.Class toClass)
Convert value from one form to another. Minimum requirement of arguments:

Specified by:
convertValue in interface TypeConverter
Overrides:
convertValue in class DefaultTypeConverter
Parameters:
context - context under which the conversion is being done
target - target object in which the property is being set
member - member (Constructor, Method or Field) being set
property - property name being set
value - value to be converted
toClass - type to which value is converted
Returns:
Converted value of type toType or TypeConverter.NoConversionPossible to indicate that the conversion was not possible.
See Also:
TypeConverter.convertValue(java.util.Map, java.lang.Object, java.lang.reflect.Member, java.lang.String, java.lang.Object, java.lang.Class)

lookup

public TypeConverter lookup(java.lang.String className)
Looks for a TypeConverter in the default mappings.

Parameters:
className - name of the class the TypeConverter must handle
Returns:
a TypeConverter to handle the specified class or null if none can be found

lookup

public TypeConverter lookup(java.lang.Class clazz)
Looks for a TypeConverter in the default mappings.

Parameters:
clazz - the class the TypeConverter must handle
Returns:
a TypeConverter to handle the specified class or null if none can be found

getConverter

protected java.lang.Object getConverter(java.lang.Class clazz,
                                        java.lang.String property)

handleConversionException

protected void handleConversionException(java.util.Map<java.lang.String,java.lang.Object> context,
                                         java.lang.String property,
                                         java.lang.Object value,
                                         java.lang.Object object)

registerConverter

public void registerConverter(java.lang.String className,
                              TypeConverter converter)

registerConverterNotFound

public void registerConverterNotFound(java.lang.String className)

addConverterMapping

protected void addConverterMapping(java.util.Map<java.lang.String,java.lang.Object> mapping,
                                   java.lang.Class clazz)
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.

Parameters:
mapping - an existing map to add new converter mappings to
clazz - class to look for converter mappings for

buildConverterMapping

protected java.util.Map<java.lang.String,java.lang.Object> buildConverterMapping(java.lang.Class clazz)
                                                                          throws java.lang.Exception
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.

Parameters:
clazz - the class to look for converter mappings for
Returns:
the converter mappings
Throws:
java.lang.Exception

loadConversionProperties

public void loadConversionProperties(java.lang.String propsName)

loadConversionProperties

public void loadConversionProperties(java.lang.String propsName,
                                     boolean require)


Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.