Coverage Report - org.simpleframework.xml.core.CompositeMap
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeMap
88%
45/51
66%
12/18
3.571
 
 1  
 /*
 2  
  * CompositeMap.java July 2007
 3  
  *
 4  
  * Copyright (C) 2007, 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.core;
 20  
 
 21  
 import java.util.Map;
 22  
 
 23  
 import org.simpleframework.xml.strategy.Type;
 24  
 import org.simpleframework.xml.stream.InputNode;
 25  
 import org.simpleframework.xml.stream.OutputNode;
 26  
 import org.simpleframework.xml.stream.Style;
 27  
 
 28  
 /**
 29  
  * The <code>CompositeMap</code> is used to serialize and deserialize
 30  
  * maps to and from a source XML document. The structure of the map in
 31  
  * the XML format is determined by the annotation. Keys can be either
 32  
  * attributes or elements, and values can be inline. This can perform
 33  
  * serialization and deserialization of the key and value objects 
 34  
  * whether the object types are primitive or composite.
 35  
  * <pre>
 36  
  * 
 37  
  *    &lt;map&gt;
 38  
  *       &lt;entry key='1'&gt;           
 39  
  *          &lt;value&gt;one&lt;/value&gt;
 40  
  *       &lt;/entry&gt;
 41  
  *       &lt;entry key='2'&gt;
 42  
  *          &lt;value&gt;two&lt;/value&gt;
 43  
  *       &lt;/entry&gt;      
 44  
  *    &lt;/map&gt;
 45  
  *    
 46  
  * </pre>
 47  
  * For the above XML element map the element <code>entry</code> is 
 48  
  * used to wrap the key and value such that they can be grouped. This
 49  
  * element does not represent any real object. The names of each of
 50  
  * the XML elements serialized and deserialized can be configured.
 51  
  *  
 52  
  * @author Niall Gallagher
 53  
  * 
 54  
  * @see org.simpleframework.xml.core.Entry
 55  
  */
 56  
 class CompositeMap implements Converter {
 57  
       
 58  
    /**
 59  
     * The factory used to create suitable map object instances.
 60  
     */
 61  
    private final MapFactory factory;
 62  
    
 63  
    /**
 64  
     * This is the type that the value objects are instances of. 
 65  
     */
 66  
    private final Converter value;
 67  
    
 68  
    /**
 69  
     * This is the name of the entry wrapping the key and value.
 70  
     */
 71  
    private final Converter key;  
 72  
    
 73  
    /**
 74  
     * This is the style used to style the names used for the XML.
 75  
     */
 76  
    private final Style style;
 77  
    
 78  
    /**
 79  
     * The entry object contains the details on how to write the map.
 80  
     */
 81  
    private final Entry entry;
 82  
    
 83  
    /**
 84  
     * Constructor for the <code>CompositeMap</code> object. This will
 85  
     * create a converter that is capable of writing map objects to 
 86  
     * and from XML. The resulting XML is configured by an annotation
 87  
     * such that key values can attributes and values can be inline. 
 88  
     * 
 89  
     * @param context this is the root context for the serialization
 90  
     * @param entry this provides configuration for the resulting XML
 91  
     * @param type this is the map type that is to be converted
 92  
     */
 93  403
    public CompositeMap(Context context, Entry entry, Type type) throws Exception {
 94  403
       this.factory = new MapFactory(context, type);
 95  403
       this.value = entry.getValue(context);
 96  403
       this.key = entry.getKey(context);
 97  403
       this.style = context.getStyle();
 98  403
       this.entry = entry;
 99  403
    }
 100  
 
 101  
    /**
 102  
     * This <code>read</code> method will read the XML element map from
 103  
     * the provided node and deserialize its children as entry types.
 104  
     * Each entry type must contain a key and value so that the entry 
 105  
     * can be inserted in to the map as a pair. If either the key or 
 106  
     * value is composite it is read as a root object, which means its
 107  
     * <code>Root</code> annotation must be present and the name of the
 108  
     * object element must match that root element name.
 109  
     * 
 110  
     * @param node this is the XML element that is to be deserialized
 111  
     * 
 112  
     * @return this returns the item to attach to the object contact
 113  
     */
 114  
    public Object read(InputNode node) throws Exception{
 115  36
       Instance type = factory.getInstance(node);
 116  36
       Object map = type.getInstance();
 117  
       
 118  36
       if(!type.isReference()) {
 119  36
          return populate(node, map);
 120  
       }
 121  0
       return map;
 122  
    }
 123  
    
 124  
    /**
 125  
     * This <code>read</code> method will read the XML element map from
 126  
     * the provided node and deserialize its children as entry types.
 127  
     * Each entry type must contain a key and value so that the entry 
 128  
     * can be inserted in to the map as a pair. If either the key or 
 129  
     * value is composite it is read as a root object, which means its
 130  
     * <code>Root</code> annotation must be present and the name of the
 131  
     * object element must match that root element name.
 132  
     * 
 133  
     * @param node this is the XML element that is to be deserialized
 134  
     * @param result this is the map object that is to be populated
 135  
     * 
 136  
     * @return this returns the item to attach to the object contact
 137  
     */
 138  
    public Object read(InputNode node, Object result) throws Exception {
 139  81
       Instance type = factory.getInstance(node);
 140  
       
 141  81
       if(type.isReference()) {
 142  0
          return type.getInstance();
 143  
       }
 144  81
       type.setInstance(result);
 145  
       
 146  81
       if(result != null) {
 147  81
          return populate(node, result);
 148  
       }
 149  0
       return result;
 150  
    }
 151  
    
 152  
    /**
 153  
     * This <code>populate</code> method will read the XML element map 
 154  
     * from the provided node and deserialize its children as entry types.
 155  
     * Each entry type must contain a key and value so that the entry 
 156  
     * can be inserted in to the map as a pair. If either the key or 
 157  
     * value is composite it is read as a root object, which means its
 158  
     * <code>Root</code> annotation must be present and the name of the
 159  
     * object element must match that root element name.
 160  
     * 
 161  
     * @param node this is the XML element that is to be deserialized
 162  
     * @param result this is the map object that is to be populated
 163  
     * 
 164  
     * @return this returns the item to attach to the object contact
 165  
     */
 166  
    private Object populate(InputNode node, Object result) throws Exception {
 167  117
       Map map = (Map) result;                 
 168  
       
 169  
       while(true) {
 170  463
          InputNode next = node.getNext();
 171  
         
 172  463
          if(next == null) {
 173  117
             return map;
 174  
          }
 175  346
          Object index = key.read(next);
 176  346
          Object item = value.read(next);
 177  
             
 178  346
          map.put(index, item); 
 179  346
       }
 180  
    }
 181  
    
 182  
    /**
 183  
     * This <code>validate</code> method will validate the XML element 
 184  
     * map from the provided node and validate its children as entry 
 185  
     * types. Each entry type must contain a key and value so that the 
 186  
     * entry can be inserted in to the map as a pair. If either the key 
 187  
     * or value is composite it is read as a root object, which means its
 188  
     * <code>Root</code> annotation must be present and the name of the
 189  
     * object element must match that root element name.
 190  
     * 
 191  
     * @param node this is the XML element that is to be validate
 192  
     * 
 193  
     * @return true if the element matches the XML schema class given 
 194  
     */
 195  
    public boolean validate(InputNode node) throws Exception{
 196  49
       Instance value = factory.getInstance(node);
 197  
       
 198  49
       if(!value.isReference()) {
 199  49
          Object result = value.setInstance(null);
 200  49
          Class type = value.getType();
 201  
             
 202  49
          return validate(node, type);
 203  
       }
 204  0
       return true; 
 205  
    }
 206  
    
 207  
    /**
 208  
     * This <code>validate</code> method will validate the XML element 
 209  
     * map from the provided node and validate its children as entry 
 210  
     * types. Each entry type must contain a key and value so that the 
 211  
     * entry can be inserted in to the map as a pair. If either the key 
 212  
     * or value is composite it is read as a root object, which means its
 213  
     * <code>Root</code> annotation must be present and the name of the
 214  
     * object element must match that root element name.
 215  
     * 
 216  
     * @param node this is the XML element that is to be validate
 217  
     * @param type this is the type to validate the input node against
 218  
     * 
 219  
     * @return true if the element matches the XML schema class given 
 220  
     */
 221  
    private boolean validate(InputNode node, Class type) throws Exception {
 222  
       while(true) {
 223  183
          InputNode next = node.getNext();
 224  
         
 225  183
          if(next == null) {
 226  49
             return true;
 227  
          }
 228  134
          if(!key.validate(next)) {
 229  0
             return false;
 230  
          }
 231  134
          if(!value.validate(next)) {
 232  0
             return false;
 233  
          }                     
 234  134
       }
 235  
    }
 236  
 
 237  
    /**
 238  
     * This <code>write</code> method will write the key value pairs
 239  
     * within the provided map to the specified XML node. This will 
 240  
     * write each entry type must contain a key and value so that
 241  
     * the entry can be deserialized in to the map as a pair. If the
 242  
     * key or value object is composite it is read as a root object 
 243  
     * so its <code>Root</code> annotation must be present.
 244  
     * 
 245  
     * @param node this is the node the map is to be written to
 246  
     * @param source this is the source map that is to be written 
 247  
     */
 248  
    public void write(OutputNode node, Object source) throws Exception {
 249  237
       Map map = (Map) source;                
 250  
       
 251  237
       for(Object index : map.keySet()) {
 252  661
          String root = entry.getEntry();
 253  661
          String name = style.getElement(root);
 254  661
          OutputNode next = node.getChild(name);
 255  661
          Object item = map.get(index);            
 256  
          
 257  661
          key.write(next, index);            
 258  660
          value.write(next, item);                  
 259  660
       }
 260  236
    }
 261  
 }