Coverage Report - org.simpleframework.xml.core.PrimitiveArray
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimitiveArray
93%
42/45
83%
15/18
3
 
 1  
 /*
 2  
  * PrimitiveArray.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>PrimitiveArray</code> object is used to convert a list of
 30  
  * elements to an array of object entries. This in effect performs a 
 31  
  * serialization and deserialization of primitive elements for the
 32  
  * array object. On serialization each primitive 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&gt;
 38  
  *       &lt;entry&gt;example text one&lt;/entry&gt;
 39  
  *       &lt;entry&gt;example text two&lt;/entry&gt;
 40  
  *       &lt;entry&gt;example text three&lt;/entry&gt;
 41  
  *    &lt;/array&gt;
 42  
  * 
 43  
  * </pre>
 44  
  * For the above XML element list the element <code>entry</code> is
 45  
  * contained within the array. Each entry element is deserialized as 
 46  
  * a from a parent XML element, which is specified in the annotation.
 47  
  * For serialization the reverse is done, each element taken from the 
 48  
  * array is written into an element created from the parent element.
 49  
  * 
 50  
  * @author Niall Gallagher
 51  
  *
 52  
  * @see org.simpleframework.xml.core.Primitive
 53  
  * @see org.simpleframework.xml.ElementArray
 54  
  */ 
 55  
 class PrimitiveArray implements Converter {
 56  
 
 57  
    /**
 58  
     * This factory is used to create an array for the contact.
 59  
     */
 60  
    private final ArrayFactory factory;
 61  
 
 62  
    /**
 63  
     * This performs the serialization of the primitive element.
 64  
     */ 
 65  
    private final Primitive root;
 66  
       
 67  
    /**
 68  
     * This is the name that each array element is wrapped with.
 69  
     */
 70  
    private final String parent;
 71  
    
 72  
    /**
 73  
     * This is the type of object that will be held in the list.
 74  
     */
 75  
    private final Type entry;
 76  
    
 77  
    /**
 78  
     * This represents the actual field or method for the array.
 79  
     */
 80  
    private final Type type;
 81  
    
 82  
    /**
 83  
     * Constructor for the <code>PrimitiveArray</code> object. This is
 84  
     * given the array type for the contact that is to be converted. An
 85  
     * array of the specified type is used to hold the deserialized
 86  
     * elements and will be the same length as the number of elements.
 87  
     *
 88  
     * @param context this is the context object used for serialization
 89  
     * @param type this is the actual field type from the schema
 90  
     * @param entry the entry type to be stored within the array
 91  
     * @param parent this is the name to wrap the array element with     
 92  
     */    
 93  118
    public PrimitiveArray(Context context, Type type, Type entry, String parent) {
 94  118
       this.factory = new ArrayFactory(context, type); 
 95  118
       this.root = new Primitive(context, entry);          
 96  118
       this.parent = parent;
 97  118
       this.entry = entry;
 98  118
       this.type = type;
 99  118
    }
 100  
 
 101  
    /**
 102  
     * This <code>read</code> method will read the XML element list from
 103  
     * the provided node and deserialize its children as entry types.
 104  
     * This will deserialize each entry type as a primitive value. In
 105  
     * order to do this the parent string provided forms the element.
 106  
     * 
 107  
     * @param node this is the XML element that is to be deserialized
 108  
     * 
 109  
     * @return this returns the item to attach to the object contact
 110  
     */ 
 111  
    public Object read(InputNode node) throws Exception{
 112  37
       Instance type = factory.getInstance(node);
 113  37
       Object list = type.getInstance();
 114  
       
 115  37
       if(!type.isReference()) {
 116  37
          return read(node, list);
 117  
       }
 118  0
       return list;
 119  
    }
 120  
 
 121  
    /**
 122  
     * This <code>read</code> method will read the XML element list from
 123  
     * the provided node and deserialize its children as entry types.
 124  
     * This will deserialize each entry type as a primitive value. In
 125  
     * order to do this the parent string provided forms the element.
 126  
     * 
 127  
     * @param node this is the XML element that is to be deserialized
 128  
     * @param list this is the array to read the array values in to
 129  
     * 
 130  
     * @return this returns the item to attach to the object contact
 131  
     */ 
 132  
    public Object read(InputNode node, Object list) throws Exception{
 133  37
       int length = Array.getLength(list);
 134  
 
 135  106
       for(int pos = 0; true; pos++) {
 136  106
          Position line = node.getPosition();
 137  106
          InputNode next = node.getNext();
 138  
     
 139  106
          if(next == null) {
 140  35
             return list;            
 141  
          }
 142  71
          if(pos >= length){
 143  2
             throw new ElementException("Array length missing or incorrect for %s at %s", type, line);
 144  
          }
 145  69
          Array.set(list, pos, root.read(next));
 146  
       } 
 147  
    }    
 148  
    
 149  
    /**
 150  
     * This <code>validate</code> method will validate the XML element list 
 151  
     * from the provided node and validate its children as entry types.
 152  
     * This will validate each entry type as a primitive value. In order 
 153  
     * to do this the parent string provided forms the element.
 154  
     * 
 155  
     * @param node this is the XML element that is to be validated
 156  
     * 
 157  
     * @return true if the element matches the XML schema class given 
 158  
     */ 
 159  
    public boolean validate(InputNode node) throws Exception{
 160  14
       Instance value = factory.getInstance(node);
 161  
       
 162  14
       if(!value.isReference()) {
 163  14
          Object result = value.setInstance(null);
 164  14
          Class expect = value.getType();
 165  
             
 166  14
          return validate(node, expect);
 167  
       }
 168  0
       return true; 
 169  
    }
 170  
 
 171  
    /**
 172  
     * This <code>validate</code> method will validate the XML element list 
 173  
     * from the provided node and validate its children as entry types.
 174  
     * This will validate each entry type as a primitive value. In order 
 175  
     * to do this the parent string provided forms the element.
 176  
     * 
 177  
     * @param node this is the XML element that is to be validated
 178  
     * @param type this is the array type used to create the array
 179  
     * 
 180  
     * @return true if the element matches the XML schema class given 
 181  
     */ 
 182  
    private boolean validate(InputNode node, Class type) throws Exception{
 183  
       while(true) {
 184  40
          InputNode next = node.getNext();
 185  
     
 186  40
          if(next == null) {
 187  14
             return true;            
 188  
          }
 189  26
          root.validate(next);
 190  26
       } 
 191  
    }    
 192  
 
 193  
    /**
 194  
     * This <code>write</code> method will write the specified object
 195  
     * to the given XML element as as array entries. Each entry within
 196  
     * the given array must be assignable to the array component type.
 197  
     * This will deserialize each entry type as a primitive value. In
 198  
     * order to do this the parent string provided forms the element.
 199  
     * 
 200  
     * @param source this is the source object array to be serialized 
 201  
     * @param node this is the XML element container to be populated
 202  
     */ 
 203  
    public void write(OutputNode node, Object source) throws Exception {
 204  69
       int size = Array.getLength(source);
 205  
       
 206  203
       for(int i = 0; i < size; i++) {
 207  134
          OutputNode child = node.getChild(parent);
 208  
          
 209  134
          if(child == null) {
 210  0
             break;
 211  
          }
 212  134
          write(child, source, i);
 213  
       }
 214  69
    }   
 215  
 
 216  
    /**
 217  
     * This <code>write</code> method will write the specified object
 218  
     * to the given XML element as as array entries. Each entry within
 219  
     * the given array must be assignable to the array component type.
 220  
     * This will deserialize each entry type as a primitive value. In
 221  
     * order to do this the parent string provided forms the element.
 222  
     * 
 223  
     * @param source this is the source object array to be serialized 
 224  
     * @param node this is the XML element container to be populated
 225  
     * @param index this is the position in the array to set the item
 226  
     */ 
 227  
    private void write(OutputNode node, Object source, int index) throws Exception {   
 228  134
       Object item = Array.get(source, index);         
 229  
       
 230  134
       if(item != null) {         
 231  104
          if(!isOverridden(node, item)) {
 232  98
             root.write(node, item);
 233  
          }
 234  
       }      
 235  134
    }
 236  
    
 237  
    /**
 238  
     * This is used to determine whether the specified value has been
 239  
     * overridden by the strategy. If the item has been overridden
 240  
     * then no more serialization is require for that value, this is
 241  
     * effectively telling the serialization process to stop writing.
 242  
     * 
 243  
     * @param node the node that a potential override is written to
 244  
     * @param value this is the object instance to be serialized
 245  
     * 
 246  
     * @return returns true if the strategy overrides the object
 247  
     */
 248  
    private boolean isOverridden(OutputNode node, Object value) throws Exception{
 249  104
       return factory.setOverride(entry, value, node);
 250  
    }
 251  
 }