Coverage Report - org.simpleframework.xml.core.CompositeArray
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeArray
100%
46/46
100%
16/16
3.143
 
 1  
 /*
 2  
  * CompositeArray.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.lang.reflect.Array;
 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.Position;
 27  
 
 28  
 /**
 29  
  * The <code>CompositeArray</code> object is used to convert a list of
 30  
  * elements to an array of object entries. This in effect performs a 
 31  
  * root serialization and deserialization of entry elements for the
 32  
  * array object. On serialization each objects type must be checked 
 33  
  * against the array component type so that it is serialized in a form 
 34  
  * that can be deserialized dynamically. 
 35  
  * <pre>
 36  
  *
 37  
  *    &lt;array length="2"&gt;
 38  
  *       &lt;entry&gt;
 39  
  *          &lt;text&gt;example text value&lt;/text&gt;
 40  
  *       &lt;/entry&gt;
 41  
  *       &lt;entry&gt;
 42  
  *          &lt;text&gt;some other example&lt;/text&gt;
 43  
  *       &lt;/entry&gt;
 44  
  *    &lt;/array&gt;
 45  
  * 
 46  
  * </pre>
 47  
  * For the above XML element list the element <code>entry</code> is
 48  
  * contained within the array. Each entry element is deserialized as 
 49  
  * a root element and then inserted into the array. For serialization 
 50  
  * the reverse is done, each element taken from the array is written
 51  
  * as a root element to the parent element to create the list. Entry
 52  
  * 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.ElementArray
 58  
  */ 
 59  
 class CompositeArray implements Converter {
 60  
 
 61  
    /**
 62  
     * This factory is used to create an array for the contact.
 63  
     */
 64  
    private final ArrayFactory factory;
 65  
 
 66  
    /**
 67  
     * This performs the traversal used for object serialization.
 68  
     */ 
 69  
    private final Traverser root;
 70  
 
 71  
    /**
 72  
     * This is the name to wrap each entry that is represented.
 73  
     */
 74  
    private final String parent;
 75  
    
 76  
    /**
 77  
     * This is the entry type for elements within the array.
 78  
     */   
 79  
    private final Type entry;
 80  
    
 81  
    /**
 82  
     * This represents the actual field or method for the array.
 83  
     */
 84  
    private final Type type;
 85  
    
 86  
    /**
 87  
     * Constructor for the <code>CompositeArray</code> object. This is
 88  
     * given the array type for the contact that is to be converted. An
 89  
     * array of the specified type is used to hold the deserialized
 90  
     * elements and will be the same length as the number of elements.
 91  
     *
 92  
     * @param context this is the context object used for serialization
 93  
     * @param type this is the field type for the array being used
 94  
     * @param entry this is the entry type for the array elements
 95  
     * @param parent this is the name to wrap the array element with
 96  
     */    
 97  186
    public CompositeArray(Context context, Type type, Type entry, String parent) {
 98  186
       this.factory = new ArrayFactory(context, type);           
 99  186
       this.root = new Traverser(context);     
 100  186
       this.parent = parent;
 101  186
       this.entry = entry;
 102  186
       this.type = type;
 103  186
    }
 104  
 
 105  
    /**
 106  
     * This <code>read</code> method will read the XML element list from
 107  
     * the provided node and deserialize its children as entry types.
 108  
     * This ensures each entry type is deserialized as a root type, that 
 109  
     * is, its <code>Root</code> annotation must be present and the
 110  
     * name of the entry element must match that root element name.
 111  
     * 
 112  
     * @param node this is the XML element that is to be deserialized
 113  
     * 
 114  
     * @return this returns the item to attach to the object contact
 115  
     */ 
 116  
    public Object read(InputNode node) throws Exception{
 117  65
       Instance type = factory.getInstance(node);
 118  65
       Object list = type.getInstance();
 119  
       
 120  65
       if(!type.isReference()) {
 121  53
          return read(node, list);         
 122  
       }
 123  12
       return list;
 124  
    }
 125  
 
 126  
    /**
 127  
     * This <code>read</code> method will read the XML element list from
 128  
     * the provided node and deserialize its children as entry types.
 129  
     * This ensures each entry type is deserialized as a root type, that 
 130  
     * is, its <code>Root</code> annotation must be present and the
 131  
     * name of the entry element must match that root element name.
 132  
     * 
 133  
     * @param node this is the XML element that is to be deserialized
 134  
     * @param list this is the array that is to be deserialized
 135  
     * 
 136  
     * @return this returns the item to attach to the object contact
 137  
     */  
 138  
    public Object read(InputNode node, Object list) throws Exception{
 139  53
       int length = Array.getLength(list);
 140  
        
 141  195
       for(int pos = 0; true; pos++) {
 142  195
          Position line = node.getPosition();
 143  195
          InputNode next = node.getNext();
 144  
         
 145  195
          if(next == null) {
 146  52
             return list;
 147  
          }
 148  143
          if(pos >= length){
 149  1
              throw new ElementException("Array length missing or incorrect for %s at %s", type, line);
 150  
          }
 151  142
          read(next, list, pos);
 152  
       } 
 153  
    }    
 154  
    
 155  
    /**
 156  
     * This is used to read the specified node from in to the list. If
 157  
     * the node is null then this represents a null element value in
 158  
     * the array. The node can be null only if there is a parent and
 159  
     * that parent contains no child XML elements.
 160  
     * 
 161  
     * @param node this is the node to read the array value from 
 162  
     * @param list this is the list to add the array value in to
 163  
     * @param index this is the offset to set the value in the array
 164  
     */
 165  
    private void read(InputNode node, Object list, int index) throws Exception {
 166  142
       Class type = entry.getType();
 167  142
       Object value = null;     
 168  
       
 169  142
       if(!node.isEmpty()) {
 170  119
          value = root.read(node, type);
 171  
       }
 172  142
       Array.set(list, index, value);      
 173  142
    }
 174  
 
 175  
    /**
 176  
     * This <code>validate</code> method will validate the XML element 
 177  
     * list against the provided node and validate its children as entry 
 178  
     * types. This ensures each entry type is validated as a root type, 
 179  
     * that is, its <code>Root</code> annotation must be present and the
 180  
     * name of the entry element must match that root element name.
 181  
     * 
 182  
     * @param node this is the XML element that is to be validated
 183  
     * 
 184  
     * @return true if the element matches the XML schema class given 
 185  
     */ 
 186  
    public boolean validate(InputNode node) throws Exception{
 187  24
       Instance value = factory.getInstance(node);
 188  
       
 189  24
       if(!value.isReference()) {
 190  21
          Object result = value.setInstance(null);
 191  21
          Class type = value.getType();
 192  
             
 193  21
          return validate(node, type);
 194  
       }
 195  3
       return true; 
 196  
    }
 197  
 
 198  
    /**
 199  
     * This <code>validate</code> method wll validate the XML element 
 200  
     * list against the provided node and validate its children as entry 
 201  
     * types. This ensures each entry type is validated as a root type, 
 202  
     * that is, its <code>Root</code> annotation must be present and the
 203  
     * name of the entry element must match that root element name.
 204  
     * 
 205  
     * @param node this is the XML element that is to be validated
 206  
     * @param type this is the array type used to create the array
 207  
     * 
 208  
     * @return true if the element matches the XML schema class given 
 209  
     */  
 210  
    private boolean validate(InputNode node, Class type) throws Exception{
 211  
       while(true) {
 212  76
          InputNode next = node.getNext();
 213  
         
 214  76
          if(next == null) {
 215  19
             return true;
 216  
          }
 217  57
          if(!next.isEmpty()) {
 218  48
             root.validate(next, type);
 219  
          }
 220  55
       }
 221  
    }    
 222  
    
 223  
    /**
 224  
     * This <code>write</code> method will write the specified object
 225  
     * to the given XML element as as array entries. Each entry within
 226  
     * the given array must be assignable to the array component type.
 227  
     * Each array entry is serialized as a root element, that is, its
 228  
     * <code>Root</code> annotation is used to extract the name. 
 229  
     * 
 230  
     * @param source this is the source object array to be serialized 
 231  
     * @param node this is the XML element container to be populated
 232  
     */ 
 233  
    public void write(OutputNode node, Object source) throws Exception {
 234  96
       int size = Array.getLength(source);                
 235  
       
 236  359
       for(int i = 0; i < size; i++) {   
 237  263
          Object item = Array.get(source, i); 
 238  263
          Class type = entry.getType();
 239  
       
 240  263
          root.write(node, item, type, parent);   
 241  
       }
 242  96
       node.commit();
 243  96
    }
 244  
 }