Coverage Report - org.simpleframework.xml.core.CompositeList
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeList
93%
45/48
83%
15/18
3.571
 
 1  
 /*
 2  
  * CompositeList.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.core;
 20  
 
 21  
 import java.util.Collection;
 22  
 
 23  
 import org.simpleframework.xml.strategy.Type;
 24  
 import org.simpleframework.xml.stream.InputNode;
 25  
 import org.simpleframework.xml.stream.OutputNode;
 26  
 
 27  
 /**
 28  
  * The <code>CompositeList</code> object is used to convert an element
 29  
  * list to a collection of element entries. This in effect performs a 
 30  
  * root serialization and deserialization of entry elements for the
 31  
  * collection object. On serialization each objects type must be 
 32  
  * checked against the XML annotation entry so that it is serialized
 33  
  * in a form that can be deserialized. 
 34  
  * <pre>
 35  
  *
 36  
  *    &lt;list&gt;
 37  
  *       &lt;entry attribute="value"&gt;
 38  
  *          &lt;text&gt;example text value&lt;/text&gt;
 39  
  *       &lt;/entry&gt;
 40  
  *       &lt;entry attribute="demo"&gt;
 41  
  *          &lt;text&gt;some other example&lt;/text&gt;
 42  
  *       &lt;/entry&gt;
 43  
  *    &lt;/list&gt;
 44  
  * 
 45  
  * </pre>
 46  
  * For the above XML element list the element <code>entry</code> is
 47  
  * contained within the list. Each entry element is thus deserialized
 48  
  * as a root element and then inserted into the list. This enables
 49  
  * lists to be composed from XML documents. For serialization the
 50  
  * reverse is done, each element taken from the collection is written
 51  
  * as a root element to the owning element to create the list. 
 52  
  * Entry objects do not need to be of the same type.
 53  
  * 
 54  
  * @author Niall Gallagher
 55  
  *
 56  
  * @see org.simpleframework.xml.core.Traverser
 57  
  * @see org.simpleframework.xml.ElementList
 58  
  */ 
 59  
 class CompositeList implements Converter {
 60  
 
 61  
    /**
 62  
     * This factory is used to create a suitable collection list.
 63  
     */         
 64  
    private final CollectionFactory factory;
 65  
 
 66  
    /**
 67  
     * This performs the traversal used for object serialization.
 68  
     */ 
 69  
    private final Traverser root;
 70  
    
 71  
    /**
 72  
     * This represents the name of the entry elements to write.
 73  
     */
 74  
    private final String name;
 75  
    
 76  
    /**
 77  
     * This is the entry type for elements within the list.
 78  
     */   
 79  
    private final Type entry;
 80  
    
 81  
    /**
 82  
     * This is the field or method that has been annotated.
 83  
     */
 84  
    private final Type type;
 85  
 
 86  
    /**
 87  
     * Constructor for the <code>CompositeList</code> object. This is
 88  
     * given the list type and entry type to be used. The list type is
 89  
     * the <code>Collection</code> implementation that deserialized
 90  
     * entry objects are inserted into. 
 91  
     *
 92  
     * @param context this is the context object used for serialization
 93  
     * @param type this is the collection type for the list used
 94  
     * @param entry the entry type to be stored within the list
 95  
     */    
 96  92558
    public CompositeList(Context context, Type type, Type entry, String name) {
 97  92558
       this.factory = new CollectionFactory(context, type); 
 98  92558
       this.root = new Traverser(context);      
 99  92558
       this.entry = entry;
 100  92558
       this.type = type;
 101  92558
       this.name = name;
 102  92558
    }
 103  
 
 104  
    /**
 105  
     * This <code>read</code> method will read the XML element list from
 106  
     * the provided node and deserialize its children as entry types.
 107  
     * This will each entry type is deserialized as a root type, that 
 108  
     * is, its <code>Root</code> annotation must be present and the
 109  
     * name of the entry element must match that root element name.
 110  
     * 
 111  
     * @param node this is the XML element that is to be deserialized
 112  
     * 
 113  
     * @return this returns the item to attach to the object contact
 114  
     */ 
 115  
    public Object read(InputNode node) throws Exception{
 116  70173
       Instance type = factory.getInstance(node);
 117  70166
       Object list = type.getInstance();
 118  
       
 119  70166
       if(!type.isReference()) {
 120  70159
          return populate(node, list);
 121  
       }
 122  7
       return list;      
 123  
    }
 124  
    
 125  
    /**
 126  
     * This <code>read</code> method will read the XML element map from
 127  
     * the provided node and deserialize its children as entry types.
 128  
     * Each entry type must contain a key and value so that the entry 
 129  
     * can be inserted in to the map as a pair. If either the key or 
 130  
     * value is composite it is read as a root object, which means its
 131  
     * <code>Root</code> annotation must be present and the name of the
 132  
     * object element must match that root element name.
 133  
     * 
 134  
     * @param node this is the XML element that is to be deserialized
 135  
     * @param result this is the map object that is to be populated
 136  
     * 
 137  
     * @return this returns the item to attach to the object contact
 138  
     */
 139  
    public Object read(InputNode node, Object result) throws Exception {
 140  1015
       Instance type = factory.getInstance(node);
 141  
       
 142  1015
       if(type.isReference()) {
 143  0
          return type.getInstance();
 144  
       }
 145  1015
       type.setInstance(result);
 146  
       
 147  1015
       if(result != null) {
 148  1015
          return populate(node, result);
 149  
       }
 150  0
       return result;
 151  
    }
 152  
    
 153  
    /**
 154  
     * This <code>populate</code> method wll read the XML element list 
 155  
     * from the provided node and deserialize its children as entry types.
 156  
     * This will each entry type is deserialized as a root type, that 
 157  
     * is, its <code>Root</code> annotation must be present and the
 158  
     * name of the entry element must match that root element name.
 159  
     * 
 160  
     * @param node this is the XML element that is to be deserialized
 161  
     * @param result this is the collection that is to be populated
 162  
     * 
 163  
     * @return this returns the item to attach to the object contact
 164  
     */ 
 165  
    private Object populate(InputNode node, Object result) throws Exception {
 166  71174
       Collection list = (Collection) result;                 
 167  
       
 168  
       while(true) {
 169  428260
          InputNode next = node.getNext();
 170  428260
          Class expect = entry.getType();
 171  
         
 172  428260
          if(next == null) {
 173  71174
             return list;
 174  
          }
 175  357086
          list.add(root.read(next, expect));
 176  357086
       }
 177  
    }      
 178  
    
 179  
    /**
 180  
     * This <code>validate</code> method will validate the XML element 
 181  
     * list from the provided node and deserialize its children as entry 
 182  
     * types. This takes each entry type and validates it as a root type, 
 183  
     * that is, its <code>Root</code> annotation must be present and the
 184  
     * name of the entry element must match that root element name.
 185  
     * 
 186  
     * @param node this is the XML element that is to be validated
 187  
     * 
 188  
     * @return true if the element matches the XML schema class given 
 189  
     */ 
 190  
    public boolean validate(InputNode node) throws Exception{
 191  51
       Instance value = factory.getInstance(node);
 192  
       
 193  51
       if(!value.isReference()) {
 194  49
          Object result = value.setInstance(null);
 195  49
          Class type = value.getType();
 196  
             
 197  49
          return validate(node, type);
 198  
       }
 199  2
       return true; 
 200  
    }
 201  
    
 202  
    /**
 203  
     * This <code>validate</code> method will validate the XML element 
 204  
     * list from the provided node and deserialize its children as entry 
 205  
     * types. This takes each entry type and validates it as a root type, 
 206  
     * that is, its <code>Root</code> annotation must be present and the
 207  
     * name of the entry element must match that root element name.
 208  
     * 
 209  
     * @param node this is the XML element that is to be validated
 210  
     * @param type this is the type to validate against the input node
 211  
     * 
 212  
     * @return true if the element matches the XML schema class given 
 213  
     */ 
 214  
    private boolean validate(InputNode node, Class type) throws Exception {
 215  
       while(true) {
 216  365
          InputNode next = node.getNext();
 217  365
          Class expect = entry.getType();
 218  
          
 219  365
          if(next == null) {
 220  49
             return true;
 221  
          }
 222  316
          root.validate(next, expect);
 223  316
       }
 224  
    }     
 225  
 
 226  
    /**
 227  
     * This <code>write</code> method will write the specified object
 228  
     * to the given XML element as as list entries. Each entry within
 229  
     * the given collection must be assignable from the annotated 
 230  
     * type specified within the <code>ElementList</code> annotation.
 231  
     * Each entry is serialized as a root element, that is, its
 232  
     * <code>Root</code> annotation is used to extract the name. 
 233  
     * 
 234  
     * @param source this is the source collection to be serialized 
 235  
     * @param node this is the XML element container to be populated
 236  
     */ 
 237  
    public void write(OutputNode node, Object source) throws Exception {
 238  21319
       Collection list = (Collection) source;                
 239  
       
 240  21319
       for(Object item : list) {
 241  107877
          if(item != null) {
 242  107839
             Class expect = entry.getType();
 243  107839
             Class actual = item.getClass();
 244  
 
 245  107839
             if(!expect.isAssignableFrom(actual)) {
 246  0
                throw new PersistenceException("Entry %s does not match %s for %s", actual, entry, type);                     
 247  
             }
 248  107839
             root.write(node, item, expect, name);
 249  107877
          }
 250  
       }
 251  21319
    }
 252  
 }