Coverage Report - org.simpleframework.xml.strategy.VisitorStrategy
 
Classes in this File Line Coverage Branch Coverage Complexity
VisitorStrategy
100%
13/13
50%
2/4
1.5
 
 1  
 /*
 2  
  * VisitorStrategy.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.strategy;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 import org.simpleframework.xml.stream.InputNode;
 24  
 import org.simpleframework.xml.stream.NodeMap;
 25  
 import org.simpleframework.xml.stream.OutputNode;
 26  
 
 27  
 /**
 28  
  * The <code>VisitorStrategy</code> object is a simplification of a
 29  
  * strategy, which allows manipulation of the serialization process.
 30  
  * Typically implementing a <code>Strategy</code> is impractical as
 31  
  * it requires the implementation to determine the type a node
 32  
  * represents. Instead it is often easier to visit each node that
 33  
  * is being serialized or deserialized and manipulate it so that 
 34  
  * the resulting XML can be customized. 
 35  
  * <p>
 36  
  * To perform customization in this way a <code>Visitor</code> can
 37  
  * be implemented. This can be passed to this strategy which will 
 38  
  * ensure the visitor is given each XML element as it is either 
 39  
  * being serialized or deserialized. Such an inversion of control
 40  
  * allows the nodes to be manipulated with little effort. By 
 41  
  * default this used <code>TreeStrategy</code> object as a default
 42  
  * strategy to delegate to. However, any strategy can be used.
 43  
  * 
 44  
  * @author Niall Gallagher
 45  
  * 
 46  
  * @see org.simpleframework.xml.strategy.Visitor
 47  
  */
 48  
 public class VisitorStrategy implements Strategy {
 49  
    
 50  
    /**
 51  
     * This is the strategy that is delegated to by this strategy.
 52  
     */
 53  
    private final Strategy strategy;
 54  
    
 55  
    /**
 56  
     * This is the visitor that is used to intercept serialization.
 57  
     */
 58  
    private final Visitor visitor;
 59  
    
 60  
    /**
 61  
     * Constructor for the <code>VisitorStrategy</code> object. This
 62  
     * strategy requires a visitor implementation that can be used
 63  
     * to intercept the serialization and deserialization process.
 64  
     * 
 65  
     * @param visitor this is the visitor used for interception
 66  
     */
 67  
    public VisitorStrategy(Visitor visitor) {
 68  10
       this(visitor, new TreeStrategy());
 69  10
    }
 70  
    
 71  
    /**
 72  
     * Constructor for the <code>VisitorStrategy</code> object. This
 73  
     * strategy requires a visitor implementation that can be used
 74  
     * to intercept the serialization and deserialization process.
 75  
     * 
 76  
     * @param visitor this is the visitor used for interception
 77  
     * @param strategy this is the strategy to be delegated to
 78  
     */
 79  235
    public VisitorStrategy(Visitor visitor, Strategy strategy) {
 80  235
       this.strategy = strategy;
 81  235
       this.visitor = visitor;
 82  235
    }
 83  
 
 84  
    /**
 85  
     * This method will read with  an internal strategy after it has
 86  
     * been intercepted by the visitor. Interception of the XML node
 87  
     * before it is delegated to the internal strategy allows the 
 88  
     * visitor to change some attributes or details before the node
 89  
     * is interpreted by the strategy.
 90  
     * 
 91  
     * @param type this is the type of the root element expected
 92  
     * @param node this is the node map used to resolve an override
 93  
     * @param map this is used to maintain contextual information
 94  
     * 
 95  
     * @return the value that should be used to describe the instance
 96  
     */
 97  
    public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception {
 98  13513
       if(visitor != null) {
 99  13513
          visitor.read(type, node);
 100  
       }
 101  13513
       return strategy.read(type, node, map);
 102  
    }
 103  
 
 104  
    /**
 105  
     * This method will write with an internal strategy before it has
 106  
     * been intercepted by the visitor. Interception of the XML node
 107  
     * before it is delegated to the internal strategy allows the 
 108  
     * visitor to change some attributes or details before the node
 109  
     * is interpreted by the strategy.
 110  
     * 
 111  
     * @param type this is the type of the root element expected
 112  
     * @param node this is the node map used to resolve an override
 113  
     * @param map this is used to maintain contextual information
 114  
     * 
 115  
     * @return the value that should be used to describe the instance
 116  
     */
 117  
    public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception {
 118  26472
       boolean result = strategy.write(type, value, node, map); 
 119  
       
 120  26472
       if(visitor != null) {
 121  26472
          visitor.write(type, node);
 122  
       }
 123  26472
       return result;
 124  
    }
 125  
 }