Class VisitorStrategy

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

public class VisitorStrategy extends Object implements Strategy
The VisitorStrategy object is a simplification of a strategy, which allows manipulation of the serialization process. Typically implementing a Strategy is impractical as it requires the implementation to determine the type a node represents. Instead it is often easier to visit each node that is being serialized or deserialized and manipulate it so that the resulting XML can be customized.

To perform customization in this way a Visitor can be implemented. This can be passed to this strategy which will ensure the visitor is given each XML element as it is either being serialized or deserialized. Such an inversion of control allows the nodes to be manipulated with little effort. By default this used TreeStrategy object as a default strategy to delegate to. However, any strategy can be used.

See Also:
  • Field Details

    • strategy

      private final Strategy strategy
      This is the strategy that is delegated to by this strategy.
    • visitor

      private final Visitor visitor
      This is the visitor that is used to intercept serialization.
  • Constructor Details

    • VisitorStrategy

      public VisitorStrategy(Visitor visitor)
      Constructor for the VisitorStrategy object. This strategy requires a visitor implementation that can be used to intercept the serialization and deserialization process.
      Parameters:
      visitor - this is the visitor used for interception
    • VisitorStrategy

      public VisitorStrategy(Visitor visitor, Strategy strategy)
      Constructor for the VisitorStrategy object. This strategy requires a visitor implementation that can be used to intercept the serialization and deserialization process.
      Parameters:
      visitor - this is the visitor used for interception
      strategy - this is the strategy to be delegated to
  • Method Details

    • read

      public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception
      This method will read with an internal strategy after it has been intercepted by the visitor. Interception of the XML node before it is delegated to the internal strategy allows the visitor to change some attributes or details before the node is interpreted by the strategy.
      Specified by:
      read in interface Strategy
      Parameters:
      type - this is the type of the root element expected
      node - this is the node map used to resolve an override
      map - this is used to maintain contextual information
      Returns:
      the value that should be used to describe the instance
      Throws:
      Exception - thrown if the class cannot be resolved
    • write

      public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception
      This method will write with an internal strategy before it has been intercepted by the visitor. Interception of the XML node before it is delegated to the internal strategy allows the visitor to change some attributes or details before the node is interpreted by the strategy.
      Specified by:
      write in interface Strategy
      Parameters:
      type - this is the type of the root element expected
      value - this is the instance variable being serialized
      node - this is the node map used to resolve an override
      map - this is used to maintain contextual information
      Returns:
      the value that should be used to describe the instance
      Throws:
      Exception - thrown if the details cannot be set