Class Primitive

java.lang.Object
org.simpleframework.xml.core.Primitive
All Implemented Interfaces:
Converter

class Primitive extends Object implements Converter
The Primitive 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 the String.valueOf method. Enumerated types are converted using the Enum.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 the Filter 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:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final Context
    The context object is used to perform text value filtering.
    private final String
    This the value used to represent a null primitive value.
    private final Class
    This is the type that this primitive expects to represent.
    private final PrimitiveFactory
    This is used to convert the string values to primitives.
    private final Type
    This is the actual method or field that has been annotated.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Primitive(Context context, Type type)
    Constructor for the Primitive object.
    Primitive(Context context, Type type, String empty)
    Constructor for the Primitive object.
  • Method Summary

    Modifier and Type
    Method
    Description
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    read(InputNode node, Class type)
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    read(InputNode node, Object value)
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    private Object
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    private Object
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    private Object
    readTemplate(String value, Class type)
    This read method will extract the text value from the node and replace any template variables before converting it to a primitive value.
    boolean
    This validate method will validate the primitive by checking the node text.
    private boolean
    This validateElement method validates a primitive by checking the node text.
    void
    write(OutputNode node, Object source)
    This write method will serialize the contents of the provided object to the given XML element.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 String empty
      This the value used to represent a null primitive value.
    • expect

      private final 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 Details

    • Primitive

      public Primitive(Context context, Type type)
      Constructor for the Primitive 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 serialization
      type - this is the type of primitive this represents
    • Primitive

      public Primitive(Context context, Type type, String empty)
      Constructor for the Primitive 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 serialization
      type - this is the type of primitive this represents
      empty - this is the value used to represent a null value
  • Method Details

    • read

      public Object read(InputNode node) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context object used for this instance of serialization to replace all template variables with values from the context filter.
      Specified by:
      read in interface Converter
      Parameters:
      node - this is the node to be converted to a primitive
      Returns:
      this returns the primitive that has been deserialized
      Throws:
      Exception - if a deserialized type cannot be instantiated
    • read

      public Object read(InputNode node, Object value) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context object used for this instance of serialization to replace all template variables with values from the context filter.
      Specified by:
      read in interface Converter
      Parameters:
      node - this is the node to be converted to a primitive
      value - this is the original primitive value used
      Returns:
      this returns the primitive that has been deserialized
      Throws:
      Exception - if value is not null an exception is thrown
    • read

      public Object read(InputNode node, Class type) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context 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
      type - this is the type to read the primitive with
      Returns:
      this returns the primitive that has been deserialized
      Throws:
      Exception
    • readElement

      private Object readElement(InputNode node) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context 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:
      Exception
    • readElement

      private Object readElement(InputNode node, Instance value) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context 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
      value - this is the instance to set the result to
      Returns:
      this returns the primitive that has been deserialized
      Throws:
      Exception
    • readTemplate

      private Object readTemplate(String value, Class type) throws Exception
      This read method will extract the text value from the node and replace any template variables before converting it to a primitive value. This uses the Context 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 template
      type - this is the type that that the primitive is
      Returns:
      this returns the primitive that has been deserialized
      Throws:
      Exception
    • validate

      public boolean validate(InputNode node) throws Exception
      This validate 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.
      Specified by:
      validate in interface Converter
      Parameters:
      node - this is the node to be validated as a primitive
      Returns:
      this returns the primitive that has been validated
      Throws:
      Exception - if the class XML schema does not fully match
    • validateElement

      private boolean validateElement(InputNode node) throws Exception
      This validateElement 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:
      Exception
    • write

      public void write(OutputNode node, Object source) throws Exception
      This write method will serialize the contents of the provided object to the given XML element. This will use the String.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 using Enum.name.
      Specified by:
      write in interface Converter
      Parameters:
      node - this is the XML element to have its text set
      source - this is the object to be serialized
      Throws:
      Exception - throw if the object cannot be serialized