Class PrimitiveKey

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

class PrimitiveKey extends Object implements Converter
The PrimitiveKey is used to serialize a primitive key to and from a node. If a key name is provided in the annotation then this will serialize and deserialize that key with the given name, if the key is an attribute, then it is written using the provided name.
 
    <entry key="one">example one</entry>
    <entry key="two">example two</entry>
    <entry key="three">example three</entry>    
 
 
Allowing the key to be written as either an XML attribute or an element enables a more flexible means for representing the key. Composite elements can not be used as attribute values as they do not serialize to a string. Primitive keys as elements can be maintained as references using the cycle strategy.
See Also:
  • Field Details

    • factory

      private final PrimitiveFactory factory
      The primitive factory used to resolve the primitive to a string.
    • context

      private final Context context
      This is the context used to support the serialization process.
    • root

      private final Primitive root
      The primitive converter used to read the key from the node.
    • style

      private final Style style
      This is the style used to style the XML elements for the key.
    • entry

      private final Entry entry
      The entry object contains the details on how to write the key.
    • type

      private final Type type
      Represents the primitive type the key is serialized to and from.
  • Constructor Details

    • PrimitiveKey

      public PrimitiveKey(Context context, Entry entry, Type type)
      Constructor for the PrimitiveKey object. This is used to create the key object which converts the map key to an instance of the key type. This can also resolve references.
      Parameters:
      context - this is the context object used for serialization
      entry - this is the entry object that describes entries
      type - this is the type that this converter deals with
  • Method Details

    • read

      public Object read(InputNode node) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then an exception is thrown.
      Specified by:
      read in interface Converter
      Parameters:
      node - this is the node to read the key value from
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception - if a deserialized type cannot be instantiated
    • read

      public Object read(InputNode node, Object value) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then an exception is thrown.
      Specified by:
      read in interface Converter
      Parameters:
      node - this is the node to read the key value from
      value - this is the value to deserialize in to
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception - if value is not null an exception is thrown
    • readAttribute

      private Object readAttribute(InputNode node, String key) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then an null is assumed and returned.
      Parameters:
      node - this is the node to read the key value from
      key - this is the name of the attribute used by the key
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception
    • readElement

      private Object readElement(InputNode node, String key) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then null is assumed and returned.
      Parameters:
      node - this is the node to read the key value from
      key - this is the name of the element used by the key
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception
    • validate

      public boolean validate(InputNode node) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then the node is considered as null and is valid.
      Specified by:
      validate in interface Converter
      Parameters:
      node - this is the node to read the key value from
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception - if the class XML schema does not fully match
    • validateAttribute

      private boolean validateAttribute(InputNode node, String key) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then the node is considered as null and is valid.
      Parameters:
      node - this is the node to read the key value from
      key - this is the name of the attribute used by the key
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception
    • validateElement

      private boolean validateElement(InputNode node, String key) throws Exception
      This method is used to read the key value from the node. The value read from the node is resolved using the template filter. If the key value can not be found according to the annotation attributes then the node is considered as null and is valid.
      Parameters:
      node - this is the node to read the key value from
      key - this is the name of the element used by the key
      Returns:
      this returns the value deserialized from the node
      Throws:
      Exception
    • write

      public void write(OutputNode node, Object item) throws Exception
      This method is used to write the value to the specified node. The value written to the node can be an attribute or an element depending on the annotation attribute values. This method will maintain references for serialized elements.
      Specified by:
      write in interface Converter
      Parameters:
      node - this is the node that the value is written to
      item - this is the item that is to be written
      Throws:
      Exception - throw if the object cannot be serialized
    • writeElement

      private void writeElement(OutputNode node, Object item) throws Exception
      This method is used to write the value to the specified node. This will write the item as an element to the provided node, also this enables references to be used during serialization.
      Parameters:
      node - this is the node that the value is written to
      item - this is the item that is to be written
      Throws:
      Exception
    • writeAttribute

      private void writeAttribute(OutputNode node, Object item) throws Exception
      This method is used to write the value to the specified node. This will write the item as an attribute to the provided node, the name of the attribute is taken from the annotation.
      Parameters:
      node - this is the node that the value is written to
      item - this is the item that is to be written
      Throws:
      Exception
    • isOverridden

      private boolean isOverridden(OutputNode node, Object value) throws Exception
      This is used to determine whether the specified value has been overridden by the strategy. If the item has been overridden then no more serialization is require for that value, this is effectively telling the serialization process to stop writing.
      Parameters:
      node - the node that a potential override is written to
      value - this is the object instance to be serialized
      Returns:
      returns true if the strategy overrides the object
      Throws:
      Exception