Class TreeStrategy

java.lang.Object
org.simpleframework.xml.strategy.TreeStrategy
All Implemented Interfaces:
Strategy

public class TreeStrategy extends Object implements Strategy
The TreeStrategy object is used to provide a simple strategy for handling object graphs in a tree structure. This does not resolve cycles in the object graph. This will make use of the specified class attribute to resolve the class to use for a given element during the deserialization process. For the serialization process the "class" attribute will be added to the element specified. If there is a need to use an attribute name other than "class" then the name of the attribute to use can be specified.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final String
    This is the attribute that is used to determine the real type.
    private final String
    This is the attribute that is used to determine an array size.
    private final Loader
    This is the loader that is used to load the specified class.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the TreeStrategy object.
    TreeStrategy(String label, String length)
    Constructor for the TreeStrategy object.
  • Method Summary

    Modifier and Type
    Method
    Description
    read(Type type, NodeMap node, Map map)
    This is used to resolve and load a class for the given element.
    private Value
    readArray(Class type, NodeMap node)
    This is used to resolve and load a class for the given element.
    private Class
    readValue(Type type, NodeMap node)
    This is used to resolve and load a class for the given element.
    boolean
    write(Type type, Object value, NodeMap node, Map map)
    This is used to attach a attribute to the provided element that is used to identify the class.
    private Class
    writeArray(Class field, Object value, NodeMap node)
    This is used to add a length attribute to the element due to the fact that the serialized value is an array.

    Methods inherited from class java.lang.Object

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

    • loader

      private final Loader loader
      This is the loader that is used to load the specified class.
    • length

      private final String length
      This is the attribute that is used to determine an array size.
    • label

      private final String label
      This is the attribute that is used to determine the real type.
  • Constructor Details

    • TreeStrategy

      public TreeStrategy()
      Constructor for the TreeStrategy object. This is used to create a strategy that can resolve and load class objects for deserialization using a "class" attribute. Also for serialization this will add the appropriate "class" value.
    • TreeStrategy

      public TreeStrategy(String label, String length)
      Constructor for the TreeStrategy object. This is used to create a strategy that can resolve and load class objects for deserialization using the specified attribute. The attribute value can be any legal XML attribute name.
      Parameters:
      label - this is the name of the attribute to use
      length - this is used to determine the array length
  • Method Details

    • read

      public Value read(Type type, NodeMap node, Map map) throws Exception
      This is used to resolve and load a class for the given element. Resolution of the class to used is done by inspecting the XML element provided. If there is a "class" attribute on the element then its value is used to resolve the class to use. If no such attribute exists on the element this returns null.
      Specified by:
      read in interface Strategy
      Parameters:
      type - this is the type of the XML element expected
      node - this is the element used to resolve an override
      map - this is used to maintain contextual information
      Returns:
      returns the class that should be used for the object
      Throws:
      Exception - thrown if the class cannot be resolved
    • readArray

      private Value readArray(Class type, NodeMap node) throws Exception
      This is used to resolve and load a class for the given element. Resolution of the class to used is done by inspecting the XML element provided. If there is a "class" attribute on the element then its value is used to resolve the class to use. This also expects a "length" attribute for the array length.
      Parameters:
      type - this is the type of the XML element expected
      node - this is the element used to resolve an override
      Returns:
      returns the class that should be used for the object
      Throws:
      Exception - thrown if the class cannot be resolved
    • readValue

      private Class readValue(Type type, NodeMap node) throws Exception
      This is used to resolve and load a class for the given element. Resolution of the class to used is done by inspecting the XML element provided. If there is a "class" attribute on the element then its value is used to resolve the class to use. If no such attribute exists the specified field is returned, or if the field type is an array then the component type.
      Parameters:
      type - this is the type of the XML element expected
      node - this is the element used to resolve an override
      Returns:
      returns the class that should be used for the object
      Throws:
      Exception - thrown if the class cannot be resolved
    • write

      public boolean write(Type type, Object value, NodeMap node, Map map)
      This is used to attach a attribute to the provided element that is used to identify the class. The attribute name is "class" and has the value of the fully qualified class name for the object provided. This will only be invoked if the object class is different from the field class.
      Specified by:
      write in interface Strategy
      Parameters:
      type - this is the declared class for the field used
      value - this is the instance variable being serialized
      node - this is the element used to represent the value
      map - this is used to maintain contextual information
      Returns:
      this returns true if serialization is complete
    • writeArray

      private Class writeArray(Class field, Object value, NodeMap node)
      This is used to add a length attribute to the element due to the fact that the serialized value is an array. The length of the array is acquired and inserted in to the attributes.
      Parameters:
      field - this is the field type for the array to set
      value - this is the actual value for the array to set
      node - this is the map of attributes for the element
      Returns:
      returns the array component type that is set