Coverage Report - org.simpleframework.xml.strategy.Strategy
 
Classes in this File Line Coverage Branch Coverage Complexity
Strategy
N/A
N/A
1
 
 1  
 /*
 2  
  * Strategy.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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>Strategy</code> interface represents a strategy that can be 
 29  
  * used to resolve and load the <code>Class</code> objects that compose 
 30  
  * a serializable object. A strategy implementation will make use of the
 31  
  * provided attribute node map to extract details that can be used to
 32  
  * determine what type of object must be used. 
 33  
  * <pre>
 34  
  * 
 35  
  *    &lt;xml version="1.0"&gt;
 36  
  *    &lt;example class="some.example.Demo"&gt;
 37  
  *       &lt;integer&gt;2&lt;/integer&gt;
 38  
  *    &lt;/example&gt;
 39  
  *    
 40  
  * </pre> 
 41  
  * The above example shows how the default strategy augments elements
 42  
  * with "class" attributes that describe the type that should be used
 43  
  * to instantiate a field when an object is deserialized. So looking at
 44  
  * the above example the root element would be a "some.example.Demo".
 45  
  * <p>
 46  
  * Custom <code>Strategy</code> implementations give the persister a
 47  
  * chance to intercept the class loading and type resolution for XML
 48  
  * documents. It also opens up the possibility for class versioning.
 49  
  * To establish contextual information a <code>Map</code> object can be
 50  
  * used. The map object is a transient object that is created and used
 51  
  * for the duration of a single operation of the persister.
 52  
  * 
 53  
  * @author Niall Gallagher
 54  
  *
 55  
  * @see org.simpleframework.xml.core.Persister
 56  
  */
 57  
 public interface Strategy {
 58  
 
 59  
    /**
 60  
     * This is used to resolve and load a class for the given element.
 61  
     * The class should be of the same type or a subclass of the class
 62  
     * specified. It can be resolved using the details within the
 63  
     * provided XML node map, if the details used do not represent any
 64  
     * serializable values they should be removed so as not to disrupt
 65  
     * the deserialization process. For example the default strategy
 66  
     * removes all "class" attributes from the given node map.
 67  
     * 
 68  
     * @param type this is the type of the root element expected
 69  
     * @param node this is the node map used to resolve an override
 70  
     * @param map this is used to maintain contextual information
 71  
     * 
 72  
     * @return the value that should be used to describe the instance
 73  
     * 
 74  
     * @throws Exception thrown if the class cannot be resolved
 75  
     */
 76  
    Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception;
 77  
 
 78  
    /**
 79  
     * This is used to attach attribute values to the given node
 80  
     * map during the serialization process. This method allows
 81  
     * the strategy to augment the XML document so that it can be
 82  
     * deserialized using a similar strategy. For example the 
 83  
     * default strategy adds a "class" attribute to the node map.
 84  
     *  
 85  
     * @param type this is the declared class for the field used
 86  
     * @param value this is the instance variable being serialized
 87  
     * @param node this is the node map used to represent the value
 88  
     * @param map this is used to maintain contextual information
 89  
     * 
 90  
     * @return this returns true if serialization is complete    
 91  
     * 
 92  
     * @throws Exception thrown if the details cannot be set
 93  
     */
 94  
    boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception;
 95  
 
 96  
 }