org.simpleframework.xml.strategy
Class VisitorStrategy

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

public class VisitorStrategy
extends java.lang.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.

Author:
Niall Gallagher
See Also:
Visitor

Constructor Summary
VisitorStrategy(Visitor visitor)
          Constructor for the VisitorStrategy object.
VisitorStrategy(Visitor visitor, Strategy strategy)
          Constructor for the VisitorStrategy object.
 
Method Summary
 Value read(Type type, NodeMap<InputNode> node, java.util.Map map)
          This method will read with an internal strategy after it has been intercepted by the visitor.
 boolean write(Type type, java.lang.Object value, NodeMap<OutputNode> node, java.util.Map map)
          This method will write with an internal strategy before it has been intercepted by the visitor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

read

public Value read(Type type,
                  NodeMap<InputNode> node,
                  java.util.Map map)
           throws java.lang.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:
java.lang.Exception - thrown if the class cannot be resolved

write

public boolean write(Type type,
                     java.lang.Object value,
                     NodeMap<OutputNode> node,
                     java.util.Map map)
              throws java.lang.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
node - this is the node map used to resolve an override
map - this is used to maintain contextual information
value - this is the instance variable being serialized
Returns:
the value that should be used to describe the instance
Throws:
java.lang.Exception - thrown if the details cannot be set