Coverage Report - org.simpleframework.xml.load.CompositeValue
 
Classes in this File Line Coverage Branch Coverage Complexity
CompositeValue
100%
26/26
100%
6/6
3
 
 1  
 /*
 2  
  * CompositeValue.java July 2007
 3  
  *
 4  
  * Copyright (C) 2007, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  
  * GNU Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General 
 16  
  * Public License along with this library; if not, write to the 
 17  
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 18  
  * Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package org.simpleframework.xml.load;
 22  
 
 23  
 import org.simpleframework.xml.stream.InputNode;
 24  
 import org.simpleframework.xml.stream.OutputNode;
 25  
 
 26  
 /**
 27  
  * The <code>CompositeValue</code> object is used to convert an object
 28  
  * to an from an XML element. This accepts only composite objects and
 29  
  * will maintain all references within the object using the cycle
 30  
  * strategy if required. This also ensures that should the value to
 31  
  * be written to the XML element be null that nothing is written. 
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  * 
 35  
  * @see org.simpleframework.xml.ElementMap
 36  
  */
 37  
 class CompositeValue implements Converter {
 38  
    
 39  
    /**
 40  
     * This is the traverser used to read and write the value with.
 41  
     */
 42  
    private final Traverser root;
 43  
    
 44  
    /**
 45  
     * This is the entry object used to provide configuration details.
 46  
     */   
 47  
    private final Entry entry;
 48  
   
 49  
    /**
 50  
     * This represents the type of object the value is written as.
 51  
     */
 52  
    private final Class type;
 53  
    
 54  
    /**
 55  
     * Constructor for the <code>CompositeValue</code> object. This 
 56  
     * will create an object capable of reading an writing composite 
 57  
     * values from an XML element. This also allows a parent element 
 58  
     * to be created to wrap the key object if desired.
 59  
     * 
 60  
     * @param root this is the root context for the serialization
 61  
     * @param entry this is the entry object used for configuration
 62  
     * @param type this is the type of object the value represents
 63  
     */
 64  41
    public CompositeValue(Source root, Entry entry, Class type) throws Exception {
 65  41
       this.root = new Traverser(root);
 66  41
       this.entry = entry;
 67  41
       this.type = type;
 68  41
    }
 69  
    
 70  
    /**
 71  
     * This method is used to read the value object from the node. The 
 72  
     * value read from the node is resolved using the template filter.
 73  
     * If the value data can not be found according to the annotation 
 74  
     * attributes then null is assumed and returned.
 75  
     * 
 76  
     * @param node this is the node to read the value object from
 77  
     * 
 78  
     * @return this returns the value deserialized from the node
 79  
     */ 
 80  
    public Object read(InputNode node) throws Exception { 
 81  30
       InputNode next = node.getNext();
 82  
       
 83  30
       if(next == null) {
 84  2
          return null;
 85  
       }
 86  28
       if(next.isEmpty()) {
 87  1
          return null;
 88  
       }
 89  27
       return root.read(next, type);
 90  
    }
 91  
    
 92  
    /**
 93  
     * This method is used to read the value object from the node. The 
 94  
     * value read from the node is resolved using the template filter.
 95  
     * If the value data can not be found according to the annotation 
 96  
     * attributes then null is assumed and the node is valid.
 97  
     * 
 98  
     * @param node this is the node to read the value object from
 99  
     * 
 100  
     * @return this returns true if this represents a valid value
 101  
     */ 
 102  
    public boolean validate(InputNode node) throws Exception { 
 103  41
       String name = entry.getValue();
 104  
       
 105  41
       if(name == null) {
 106  37
          name = Factory.getName(type);
 107  
       }
 108  41
       return validate(node, name);
 109  
    }  
 110  
    
 111  
    /**
 112  
     * This method is used to read the value object from the node. The 
 113  
     * value read from the node is resolved using the template filter.
 114  
     * If the value data can not be found according to the annotation 
 115  
     * attributes then null is assumed and the node is valid.
 116  
     * 
 117  
     * @param node this is the node to read the value object from
 118  
     * @param name this is the name of the value element
 119  
     * 
 120  
     * @return this returns true if this represents a valid value
 121  
     */    
 122  
    private boolean validate(InputNode node, String name) throws Exception {      
 123  41
       InputNode next = node.getNext(name);
 124  
       
 125  41
       if(next == null) {
 126  1
          return true;
 127  
       }
 128  40
       if(next.isEmpty()) {
 129  6
          return true;
 130  
       }
 131  34
       return root.validate(next, type);
 132  
    }
 133  
    
 134  
    /**
 135  
     * This method is used to write the value to the specified node.
 136  
     * The value written to the node must be a composite object and if
 137  
     * the object provided to this is null then nothing is written.
 138  
     * 
 139  
     * @param node this is the node that the value is written to
 140  
     * @param item this is the item that is to be written
 141  
     */
 142  
    public void write(OutputNode node, Object item) throws Exception {
 143  37
       String name = entry.getValue();
 144  
       
 145  37
       if(name == null) {
 146  33
          name = Factory.getName(type);
 147  
       }
 148  37
       root.write(node, item, type, name);      
 149  37
    }
 150  
 }