Coverage Report - org.simpleframework.xml.core.PrimitiveInlineList
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimitiveInlineList
93%
46/49
70%
14/20
2.625
 
 1  
 /*
 2  
  * PrimitiveInlineList.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.Mode;
 26  
 import org.simpleframework.xml.stream.OutputNode;
 27  
 
 28  
 /**
 29  
  * The <code>PrimitiveInlineList</code> object is used to convert a
 30  
  * group of elements in to a collection of element entries. This is
 31  
  * used when a containing element for a list is not required. It 
 32  
  * extracts the elements by matching elements to name of the type
 33  
  * that the annotated field or method requires. This enables these
 34  
  * element entries to exist as siblings to other objects within the
 35  
  * object.  One restriction is that the <code>Root</code> annotation
 36  
  * for each of the types within the list must be the same.
 37  
  * <pre> 
 38  
  *    
 39  
  *    &lt;entry&gt;example one&lt;/entry&gt;
 40  
  *    &lt;entry&gt;example two&lt;/entry&gt;
 41  
  *    &lt;entry&gt;example three&lt;/entry&gt;
 42  
  *    &lt;entry&gt;example four&lt;/entry&gt;      
 43  
  * 
 44  
  * </pre>
 45  
  * For the above XML element list the element <code>entry</code> is
 46  
  * used to wrap the primitive string value. This wrapping XML element 
 47  
  * is configurable and defaults to the lower case string for the name
 48  
  * of the class it represents. So, for example, if the primitive type
 49  
  * is an <code>int</code> the enclosing element will be called int.
 50  
  * 
 51  
  * @author Niall Gallagher
 52  
  *
 53  
  * @see org.simpleframework.xml.core.Primitive
 54  
  * @see org.simpleframework.xml.ElementList
 55  
  */ 
 56  
 class PrimitiveInlineList implements Repeater {
 57  
 
 58  
    /**
 59  
     * This factory is used to create a suitable collection list.
 60  
     */         
 61  
    private final CollectionFactory factory;
 62  
 
 63  
    /**
 64  
     * This performs the traversal used for object serialization.
 65  
     */ 
 66  
    private final Primitive root;
 67  
    
 68  
    /**
 69  
     * This is the name that each list element is wrapped with.
 70  
     */
 71  
    private final String parent;
 72  
    
 73  
    /**
 74  
     * This is the type of object that will be held in the list.
 75  
     */
 76  
    private final Type entry;
 77  
    
 78  
    /**
 79  
     * Constructor for the <code>PrimitiveInlineList</code> object. 
 80  
     * This is given the list type and entry type to be used. The list
 81  
     * type is the <code>Collection</code> implementation that is used 
 82  
     * to collect the deserialized entry objects from the XML source. 
 83  
     *
 84  
     * @param context this is the context object used for serialization
 85  
     * @param type this is the collection type for the list used
 86  
     * @param entry the entry type to be stored within the list
 87  
     * @param parent this is the name to wrap the list element with 
 88  
     */    
 89  227
    public PrimitiveInlineList(Context context, Type type, Type entry, String parent) {
 90  227
       this.factory = new CollectionFactory(context, type); 
 91  227
       this.root = new Primitive(context, entry);          
 92  227
       this.parent = parent;
 93  227
       this.entry = entry;
 94  227
    }
 95  
 
 96  
    /**
 97  
     * This <code>read</code> method wll read the XML element list from
 98  
     * the provided node and deserialize its children as entry types.
 99  
     * This will deserialize each entry type as a primitive value. In
 100  
     * order to do this the parent string provided forms the element.
 101  
     * 
 102  
     * @param node this is the XML element that is to be deserialized
 103  
     * 
 104  
     * @return this returns the item to attach to the object contact
 105  
     */ 
 106  
    public Object read(InputNode node) throws Exception{     
 107  32
       Object value = factory.getInstance();
 108  32
       Collection list = (Collection) value;
 109  
       
 110  32
       if(list != null) {
 111  32
          return read(node, list);
 112  
       }
 113  0
       return null;
 114  
    }
 115  
    
 116  
    /**
 117  
     * This <code>read</code> method wll read the XML element list from
 118  
     * the provided node and deserialize its children as entry types.
 119  
     * This will deserialize each entry type as a primitive value. In
 120  
     * order to do this the parent string provided forms the element.
 121  
     * 
 122  
     * @param node this is the XML element that is to be deserialized
 123  
     * 
 124  
     * @return this returns the item to attach to the object contact
 125  
     */ 
 126  
    public Object read(InputNode node, Object value) throws Exception {
 127  36
       Collection list = (Collection) value;
 128  
       
 129  36
       if(list != null) {
 130  36
          return read(node, list);
 131  
       }
 132  0
       return read(node);
 133  
    }
 134  
    
 135  
    /**
 136  
     * This <code>read</code> method wll read the XML element list from
 137  
     * the provided node and deserialize its children as entry types.
 138  
     * This will deserialize each entry type as a primitive value. In
 139  
     * order to do this the parent string provided forms the element.
 140  
     * 
 141  
     * @param node this is the XML element that is to be deserialized
 142  
     * @param list this is the collection that is to be populated
 143  
     * 
 144  
     * @return this returns the item to attach to the object contact
 145  
     */ 
 146  
    private Object read(InputNode node, Collection list) throws Exception {              
 147  68
       InputNode from = node.getParent();
 148  68
       String name = node.getName();
 149  
       
 150  213
       while(node != null) {
 151  145
          Object item = root.read(node);
 152  
          
 153  145
          if(item != null) {
 154  145
             list.add(item);
 155  
          }      
 156  145
          node = from.getNext(name);
 157  145
       }
 158  68
       return list;
 159  
    }
 160  
    
 161  
    /**
 162  
     * This <code>read</code> method wll read the XML element list from
 163  
     * the provided node and deserialize its children as entry types.
 164  
     * This will deserialize each entry type as a primitive value. In
 165  
     * order to do this the parent string provided forms the element.
 166  
     * 
 167  
     * @param node this is the XML element that is to be deserialized
 168  
     * 
 169  
     * @return this returns the item to attach to the object contact
 170  
     */ 
 171  
    public boolean validate(InputNode node) throws Exception{     
 172  22
       InputNode from = node.getParent();
 173  22
       String name = node.getName();
 174  
       
 175  70
       while(node != null) {
 176  48
          boolean valid = root.validate(node);
 177  
          
 178  48
          if(valid == false) {
 179  0
             return false;            
 180  
          }      
 181  48
          node = from.getNext(name);
 182  48
       }
 183  22
       return true;
 184  
    }
 185  
 
 186  
    /**
 187  
     * This <code>write</code> method will write the specified object
 188  
     * to the given XML element as as list entries. Each entry within
 189  
     * the given list must be assignable to the given primitive type.
 190  
     * This will deserialize each entry type as a primitive value. In
 191  
     * order to do this the parent string provided forms the element.
 192  
     * 
 193  
     * @param source this is the source collection to be serialized 
 194  
     * @param node this is the XML element container to be populated
 195  
     */ 
 196  
    public void write(OutputNode node, Object source) throws Exception {               
 197  137
       OutputNode parent = node.getParent();      
 198  137
       Mode mode = node.getMode();
 199  
       
 200  137
       if(!node.isCommitted()) {
 201  104
          node.remove();
 202  
       }      
 203  137
       write(parent, source, mode);
 204  137
    }
 205  
    
 206  
    /**
 207  
     * This <code>write</code> method will write the specified object
 208  
     * to the given XML element as as list entries. Each entry within
 209  
     * the given list must be assignable to the given primitive type.
 210  
     * This will deserialize each entry type as a primitive value. In
 211  
     * order to do this the parent string provided forms the element.
 212  
     * 
 213  
     * @param node this is the parent output node to write values to
 214  
     * @param source this is the source collection to be serialized 
 215  
     * @param mode this is used to determine whether to output CDATA    
 216  
     */ 
 217  
    private void write(OutputNode node, Object source, Mode mode) throws Exception {
 218  137
       Collection list = (Collection) source;
 219  
       
 220  137
       for(Object item : list) {
 221  286
          if(item != null) {
 222  286
             OutputNode child = node.getChild(parent);
 223  
          
 224  286
             if(!isOverridden(child, item)) { 
 225  286
                child.setMode(mode);
 226  286
                root.write(child, item);
 227  
             }
 228  286
          }
 229  
       }
 230  137
    } 
 231  
    
 232  
    /**
 233  
     * This is used to determine whether the specified value has been
 234  
     * overridden by the strategy. If the item has been overridden
 235  
     * then no more serialization is require for that value, this is
 236  
     * effectively telling the serialization process to stop writing.
 237  
     * 
 238  
     * @param node the node that a potential override is written to
 239  
     * @param value this is the object instance to be serialized
 240  
     * 
 241  
     * @return returns true if the strategy overrides the object
 242  
     */
 243  
    private boolean isOverridden(OutputNode node, Object value) throws Exception{
 244  286
       return factory.setOverride(entry, value, node);
 245  
    }
 246  
 }