Coverage Report - org.simpleframework.xml.core.TextList
 
Classes in this File Line Coverage Branch Coverage Complexity
TextList
90%
19/21
83%
5/6
1.8
 
 1  
 /*
 2  
  * TextList.java December 2012
 3  
  *
 4  
  * Copyright (C) 2012, 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  
  * This <code>TextList</code> object is a converter that is used
 29  
  * to read free text and insert that text in to a list. Collecting
 30  
  * free text in this way allows unstructured XML to be processed
 31  
  * and stored in an ordered way, which will allow it to be written
 32  
  * as identical XML during the serialization process.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  */
 36  
 class TextList implements Repeater {
 37  
    
 38  
    /**
 39  
     * This is the factory that is used to create the containing list.
 40  
     */
 41  
    private final CollectionFactory factory;
 42  
    
 43  
    /**
 44  
     * This is the primitive object used to read the free text.
 45  
     */
 46  
    private final Primitive primitive;
 47  
    
 48  
    /**
 49  
     * This is a string type which is used for all free text data.
 50  
     */
 51  
    private final Type type;
 52  
    
 53  
    /**
 54  
     * Constructor for the <code>TextList</code> object. This is used
 55  
     * to create a converter that can read free text from between
 56  
     * elements in an <code>ElementListUnion</code>. Converting the
 57  
     * free text in this way allows unstructured XML to be processed.
 58  
     * 
 59  
     * @param context this is the context that is used for this
 60  
     * @param list this is the list type this will be used with
 61  
     * @param label this is the label this the text is declared with
 62  
     */
 63  106
    public TextList(Context context, Type list, Label label) {
 64  106
       this.type = new ClassType(String.class);
 65  106
       this.factory = new CollectionFactory(context, list);
 66  106
       this.primitive = new Primitive(context, type);
 67  106
    }
 68  
 
 69  
    /**
 70  
     * The <code>read</code> method reads an object to a specific type
 71  
     * from the provided node. If the node provided is an attribute
 72  
     * then the object must be a primitive such as a string, integer,
 73  
     * boolean, or any of the other Java primitive types.  
 74  
     * 
 75  
     * @param node contains the details used to deserialize the object
 76  
     * 
 77  
     * @return a fully deserialized object will all its fields 
 78  
     */
 79  
    public Object read(InputNode node) throws Exception {
 80  15
       Instance value = factory.getInstance(node); 
 81  15
       Object data = value.getInstance();
 82  
       
 83  15
       if(value.isReference()) {      
 84  0
          return value.getInstance(); 
 85  
       }
 86  15
       return read(node, data);
 87  
    }
 88  
 
 89  
    /**
 90  
     * The <code>read</code> method reads an object to a specific type
 91  
     * from the provided node. If the node provided is an attribute
 92  
     * then the object must be a primitive such as a string, integer,
 93  
     * boolean, or any of the other Java primitive types.  
 94  
     * 
 95  
     * @param node contains the details used to deserialize the object
 96  
     * @param value this is the value to read the objects in to
 97  
     * 
 98  
     * @return a fully deserialized object will all its fields 
 99  
     */
 100  
    public Object read(InputNode node, Object result) throws Exception {
 101  45
       Collection list = (Collection) result;                 
 102  45
       Object value = primitive.read(node);
 103  
       
 104  45
       if(value != null) {
 105  41
          list.add(value);
 106  
       }
 107  45
       return result;
 108  
    } 
 109  
    
 110  
    /**
 111  
     * The <code>validate</code> method is used to validate the class
 112  
     * XML schema against an input source. This will traverse the class
 113  
     * fields and methods ensuring that the input XML document contains
 114  
     * a valid structure when compared against the class XML schema.
 115  
     * 
 116  
     * @param node contains the details used to validate the object
 117  
     * 
 118  
     * @return true if the document matches the class XML schema 
 119  
     */
 120  
    public boolean validate(InputNode node) throws Exception {
 121  0
       return true;
 122  
    }
 123  
    
 124  
    /**
 125  
     * The <code>write</code> method writes the fields from the given 
 126  
     * object to the XML element. After this has finished the element
 127  
     * contains all attributes and sub-elements from the object.
 128  
     * 
 129  
     * @param object this is the object to be written to the element
 130  
     * @param node this is the element that is to be populated
 131  
     */
 132  
    public void write(OutputNode node, Object object) throws Exception {
 133  61
       Collection list = (Collection) object;
 134  61
       OutputNode parent = node.getParent();
 135  
       
 136  61
       for(Object item : list) {
 137  61
          primitive.write(parent, item);
 138  
       }
 139  61
    }
 140  
 }