Package org.simpleframework.xml.core
Class Primitive
- java.lang.Object
-
- org.simpleframework.xml.core.Primitive
-
- All Implemented Interfaces:
Converter
class Primitive extends java.lang.Object implements Converter
ThePrimitive
object is used to provide serialization for primitive objects. This can serialize and deserialize any primitive object and enumerations. Primitive values are converted to text using theString.valueOf
method. Enumerated types are converted using theEnum.valueOf
method.Text within attributes and elements can contain template variables similar to those found in Apache Ant. This allows values such as system properties, environment variables, and user specified mappings to be inserted into the text in place of the template reference variables.
<example attribute="${value}> <text>Text with a ${variable}</text> </example>
In the above XML element the template variable references will be checked against theFilter
object used by the context serialization object. If they corrospond to a filtered value then they are replaced, if not the text remains unchanged.- See Also:
Filter
-
-
Field Summary
Fields Modifier and Type Field Description private Context
context
The context object is used to perform text value filtering.private java.lang.String
empty
This the value used to represent a null primitive value.private java.lang.Class
expect
This is the type that this primitive expects to represent.private PrimitiveFactory
factory
This is used to convert the string values to primitives.private Type
type
This is the actual method or field that has been annotated.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
read(InputNode node)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.java.lang.Object
read(InputNode node, java.lang.Class type)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.java.lang.Object
read(InputNode node, java.lang.Object value)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.private java.lang.Object
readElement(InputNode node)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.private java.lang.Object
readElement(InputNode node, Instance value)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.private java.lang.Object
readTemplate(java.lang.String value, java.lang.Class type)
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value.boolean
validate(InputNode node)
Thisvalidate
method will validate the primitive by checking the node text.private boolean
validateElement(InputNode node)
ThisvalidateElement
method validates a primitive by checking the node text.void
write(OutputNode node, java.lang.Object source)
Thiswrite
method will serialize the contents of the provided object to the given XML element.
-
-
-
Field Detail
-
factory
private final PrimitiveFactory factory
This is used to convert the string values to primitives.
-
context
private final Context context
The context object is used to perform text value filtering.
-
empty
private final java.lang.String empty
This the value used to represent a null primitive value.
-
expect
private final java.lang.Class expect
This is the type that this primitive expects to represent.
-
type
private final Type type
This is the actual method or field that has been annotated.
-
-
Constructor Detail
-
Primitive
public Primitive(Context context, Type type)
Constructor for thePrimitive
object. This is used to convert an XML node to a primitive object and vice versa. To perform deserialization the primitive object requires the context object used for the instance of serialization to performed.- Parameters:
context
- the context object used for the serializationtype
- this is the type of primitive this represents
-
Primitive
public Primitive(Context context, Type type, java.lang.String empty)
Constructor for thePrimitive
object. This is used to convert an XML node to a primitive object and vice versa. To perform deserialization the primitive object requires the context object used for the instance of serialization to performed.- Parameters:
context
- the context object used for the serializationtype
- this is the type of primitive this representsempty
- this is the value used to represent a null value
-
-
Method Detail
-
read
public java.lang.Object read(InputNode node) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.
-
read
public java.lang.Object read(InputNode node, java.lang.Object value) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.
-
read
public java.lang.Object read(InputNode node, java.lang.Class type) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.- Parameters:
node
- this is the node to be converted to a primitivetype
- this is the type to read the primitive with- Returns:
- this returns the primitive that has been deserialized
- Throws:
java.lang.Exception
-
readElement
private java.lang.Object readElement(InputNode node) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.- Parameters:
node
- this is the node to be converted to a primitive- Returns:
- this returns the primitive that has been deserialized
- Throws:
java.lang.Exception
-
readElement
private java.lang.Object readElement(InputNode node, Instance value) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.- Parameters:
node
- this is the node to be converted to a primitivevalue
- this is the instance to set the result to- Returns:
- this returns the primitive that has been deserialized
- Throws:
java.lang.Exception
-
readTemplate
private java.lang.Object readTemplate(java.lang.String value, java.lang.Class type) throws java.lang.Exception
Thisread
method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses theContext
object used for this instance of serialization to replace all template variables with values from the context filter.- Parameters:
value
- this is the value to be processed as a templatetype
- this is the type that that the primitive is- Returns:
- this returns the primitive that has been deserialized
- Throws:
java.lang.Exception
-
validate
public boolean validate(InputNode node) throws java.lang.Exception
Thisvalidate
method will validate the primitive by checking the node text. If the value is a reference then this will not extract any value from the node. Transformation of the extracted value is not done as it can not account for template variables. Thus any text extracted is valid.
-
validateElement
private boolean validateElement(InputNode node) throws java.lang.Exception
ThisvalidateElement
method validates a primitive by checking the node text. If the value is a reference then this will not extract any value from the node. Transformation of the extracted value is not done as it can not account for template variables. Thus any text extracted is valid.- Parameters:
node
- this is the node to be validated as a primitive- Returns:
- this returns the primitive that has been validated
- Throws:
java.lang.Exception
-
write
public void write(OutputNode node, java.lang.Object source) throws java.lang.Exception
Thiswrite
method will serialize the contents of the provided object to the given XML element. This will use theString.valueOf
method to convert the object to a string if the object represents a primitive, if however the object represents an enumerated type then the text value is created usingEnum.name
.
-
-