Class Transformer
- java.lang.Object
-
- org.simpleframework.xml.transform.Transformer
-
public class Transformer extends java.lang.Object
TheTransformer
object is used to convert strings to and from object instances. This is used during the serialization and deserialization process to transform types from the Java class libraries, as well as other types which do not contain XML schema annotations. Typically this will be used to transform primitive types to and from strings, such asint
values.@Element private String[] value;
For example taking the above value the array of strings needs to be converted in to a single string value that can be inserted in to the element in such a way that in can be read later. In this case the serialized value of the string array would be as follows.<value>one, two, three</value>
Here each non-null string is inserted in to a comma separated list of values, which can later be deserialized. Just to note the above array could be annotated withElementList
just as easily, in which case each entry would have its own element. The choice of which annotation to use is up to the developer. A more obvious benefit to transformations like this can be seen for values annotated with theAttribute
annotation.
-
-
Field Summary
Fields Modifier and Type Field Description private Cache<Transform>
cache
This is used to cache all transforms matched to a given type.private Cache<java.lang.Object>
error
This is used to cache the types that to not have a transform.private Matcher
matcher
This is used to perform the matching of types to transforms.
-
Constructor Summary
Constructors Constructor Description Transformer(Matcher matcher)
Constructor for theTransformer
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private Transform
lookup(java.lang.Class type)
This method is used to acquire aTransform
for the the specified type.private Transform
match(java.lang.Class type)
This method is used to acquire aTransform
for the the specified type.java.lang.Object
read(java.lang.String value, java.lang.Class type)
This method is used to convert the string value given to an appropriate representation.boolean
valid(java.lang.Class type)
This method is used to determine if the type specified can be transformed.java.lang.String
write(java.lang.Object value, java.lang.Class type)
This method is used to convert the provided value into an XML usable format.
-
-
-
Field Detail
-
cache
private final Cache<Transform> cache
This is used to cache all transforms matched to a given type.
-
error
private final Cache<java.lang.Object> error
This is used to cache the types that to not have a transform.
-
matcher
private final Matcher matcher
This is used to perform the matching of types to transforms.
-
-
Constructor Detail
-
Transformer
public Transformer(Matcher matcher)
Constructor for theTransformer
object. This is used to create a transformer which will transform specified types using transforms loaded from the class path. Transforms are matched to types using the specified matcher object.- Parameters:
matcher
- this is used to match types to transforms
-
-
Method Detail
-
read
public java.lang.Object read(java.lang.String value, java.lang.Class type) throws java.lang.Exception
This method is used to convert the string value given to an appropriate representation. This is used when an object is being deserialized from the XML document and the value for the string representation is required.- Parameters:
value
- this is the string representation of the valuetype
- this is the type to convert the string value to- Returns:
- this returns an appropriate instanced to be used
- Throws:
java.lang.Exception
-
write
public java.lang.String write(java.lang.Object value, java.lang.Class type) throws java.lang.Exception
This method is used to convert the provided value into an XML usable format. This is used in the serialization process when there is a need to convert a field value in to a string so that that value can be written as a valid XML entity.- Parameters:
value
- this is the value to be converted to a stringtype
- this is the type to convert to a string value- Returns:
- this is the string representation of the given value
- Throws:
java.lang.Exception
-
valid
public boolean valid(java.lang.Class type) throws java.lang.Exception
This method is used to determine if the type specified can be transformed. This will use theMatcher
to find a suitable transform, if one exists then this returns true, if not then this returns false. This is used during serialization to determine how to convert a field or method parameter.- Parameters:
type
- the type to determine whether its transformable- Returns:
- true if the type specified can be transformed by this
- Throws:
java.lang.Exception
-
lookup
private Transform lookup(java.lang.Class type) throws java.lang.Exception
This method is used to acquire aTransform
for the the specified type. If there is no transform for the type then this will return null. Once acquired once the transform is cached so that subsequent lookups will be performed faster.- Parameters:
type
- the type to determine whether its transformable- Returns:
- this will return a transform for the specified type
- Throws:
java.lang.Exception
-
match
private Transform match(java.lang.Class type) throws java.lang.Exception
This method is used to acquire aTransform
for the the specified type. If there is no transform for the type then this will return null. Once acquired once the transform is cached so that subsequent lookups will be performed faster.- Parameters:
type
- the type to determine whether its transformable- Returns:
- this will return a transform for the specified type
- Throws:
java.lang.Exception
-
-