Coverage Report - org.simpleframework.xml.stream.InputElement
 
Classes in this File Line Coverage Branch Coverage Complexity
InputElement
95%
21/22
100%
1/1
1.154
 
 1  
 /*
 2  
  * InputElement.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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.stream;
 22  
 
 23  
 import javax.xml.stream.events.StartElement;
 24  
 
 25  
 /**
 26  
  * The <code>InputElement</code> represents a self contained element
 27  
  * that will allow access to its child elements. If the next element
 28  
  * read from the <code>NodeReader</code> is not a child then this
 29  
  * will return null. The input element node also allows the attribute
 30  
  * values associated with the node to be accessed.
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  *
 34  
  * @see org.simpleframework.xml.stream.NodeReader
 35  
  */ 
 36  0
 class InputElement implements InputNode {
 37  
    
 38  
    /**
 39  
     * This is the XML element that this node provides access to.
 40  
     */         
 41  
    private StartElement element;
 42  
    
 43  
    /**
 44  
     * This contains all the attributes associated with the element.
 45  
     */ 
 46  
    private InputNodeMap map;
 47  
 
 48  
    /**
 49  
     * This is the node reader that reads from the XML document.
 50  
     */ 
 51  
    private NodeReader reader;
 52  
    
 53  
    /**
 54  
     * This is the parent node for this XML input element node.
 55  
     */
 56  
    private InputNode parent;
 57  
  
 58  
    /**
 59  
     * Constructor for the <code>InputElement</code> object. This 
 60  
     * is used to create an input node that will provide access to 
 61  
     * an XML element. All attributes associated with the element 
 62  
     * given are extracted and exposed via the attribute node map.
 63  
     *
 64  
     * @param parent this is the parent XML element for this 
 65  
     * @param element this is the XML element wrapped
 66  
     * @param reader this is the reader used to read XML elements
 67  
     */ 
 68  766527
    public InputElement(InputNode parent, NodeReader reader, StartElement element) {
 69  766527
       this.map = new InputNodeMap(this, element);      
 70  766527
       this.element = element;
 71  766527
       this.reader = reader;           
 72  766527
       this.parent = parent;
 73  766527
    }  
 74  
    
 75  
    /**
 76  
     * This is used to acquire the <code>Node</code> that is the
 77  
     * parent of this node. This will return the node that is
 78  
     * the direct parent of this node and allows for siblings to
 79  
     * make use of nodes with their parents if required.  
 80  
     *   
 81  
     * @return this returns the parent node for this node
 82  
     */
 83  
    public InputNode getParent() {
 84  53
            return parent;
 85  
    }
 86  
    
 87  
    /**
 88  
     * This provides the position of this node within the document.
 89  
     * This allows the user of this node to report problems with
 90  
     * the location within the document, allowing the XML to be
 91  
     * debugged if it does not match the class schema.
 92  
     *
 93  
     * @return this returns the position of the XML read cursor
 94  
     */      
 95  
    public Position getPosition() {
 96  919611
       return new InputPosition(element);           
 97  
    }   
 98  
 
 99  
    /**
 100  
     * Returns the name of the node that this represents. This is
 101  
     * an immutable property and should not change for any node.  
 102  
     * This provides the name without the name space part.
 103  
     *  
 104  
     * @return returns the name of the node that this represents
 105  
     */   
 106  
    public String getName() {
 107  577565
       return element.getName().getLocalPart();           
 108  
    }
 109  
    
 110  
    /**
 111  
     * This method is used to determine if this node is the root 
 112  
     * node for the XML document. The root node is the first node
 113  
     * in the document and has no sibling nodes. This is false
 114  
     * if the node has a parent node or a sibling node.
 115  
     * 
 116  
     * @return true if this is the root node within the document
 117  
     */
 118  
    public boolean isRoot() {
 119  766312
       return reader.isRoot(this);
 120  
    }
 121  
 
 122  
    /**
 123  
     * This is used to determine if this node is an element. This
 124  
     * allows users of the framework to make a distinction between
 125  
     * nodes that represent attributes and nodes that represent
 126  
     * elements. This is particularly useful given that attribute
 127  
     * nodes do not maintain a node map of attributes.
 128  
     *
 129  
     * @return this returns true as this instance is an element
 130  
     */ 
 131  
    public boolean isElement() {
 132  426201
       return true;           
 133  
    } 
 134  
 
 135  
    /**
 136  
     * Provides an attribute from the element represented. If an
 137  
     * attribute for the specified name does not exist within the
 138  
     * element represented then this method will return null.
 139  
     *
 140  
     * @param name this is the name of the attribute to retrieve
 141  
     *
 142  
     * @return this returns the value for the named attribute
 143  
     */    
 144  
    public InputNode getAttribute(String name) {
 145  305676
       return map.get(name);
 146  
    }
 147  
 
 148  
    /**
 149  
     * This returns a map of the attributes contained within the
 150  
     * element. If no elements exist within the element then this
 151  
     * returns an empty map. 
 152  
     * 
 153  
     * @return this returns a map of attributes for the element
 154  
     */    
 155  
    public NodeMap getAttributes() {
 156  1072828
       return map;
 157  
    }
 158  
 
 159  
    /**
 160  
     * Returns the value for the node that this represents. This 
 161  
     * is an immutable value for the node and cannot be changed.
 162  
     * If there is a problem reading an exception is thrown.
 163  
     * 
 164  
     * @return the name of the value for this node instance
 165  
     */     
 166  
    public String getValue() throws Exception {
 167  423766
       return reader.readValue(this);           
 168  
    }
 169  
   
 170  
    /**
 171  
     * The method is used to acquire the next child attribute of this 
 172  
     * element. If the next element from the <code>NodeReader</code> 
 173  
     * is not a child node to the element that this represents then
 174  
     * this will return null, which ensures each element represents
 175  
     * a self contained collection of child nodes.
 176  
     *
 177  
     * @return this returns the next child element of this node
 178  
     *
 179  
     * @exception Exception thrown if there is a problem reading
 180  
     */  
 181  
    public InputNode getNext() throws Exception {
 182  1072607
       return reader.readElement(this);
 183  
    }
 184  
    
 185  
    /**
 186  
     * The method is used to acquire the next child attribute of this 
 187  
     * element. If the next element from the <code>NodeReader</code> 
 188  
     * is not a child node to the element that this represents then
 189  
     * this will return null, also if the next element does not match
 190  
     * the specified name then this will return null.
 191  
     *
 192  
     * @param name this is the name expected fromt he next element
 193  
     *
 194  
     * @return this returns the next child element of this node
 195  
     *
 196  
     * @exception Exception thrown if there is a problem reading
 197  
     */  
 198  
    public InputNode getNext(String name) throws Exception {
 199  391
       return reader.readElement(this, name);
 200  
    }
 201  
    
 202  
    /**
 203  
     * This method is used to skip all child elements from this
 204  
     * element. This allows elements to be effectively skipped such
 205  
     * that when parsing a document if an element is not required
 206  
     * then that element can be completely removed from the XML.
 207  
     *
 208  
     * @exception Exception thrown if there was a parse error
 209  
     */ 
 210  
    public void skip() throws Exception {
 211  3
       reader.skipElement(this);           
 212  3
    }
 213  
    
 214  
    /**
 215  
     * This is used to determine if this input node is empty. An
 216  
     * empty node is one with no attributes or children. This can
 217  
     * be used to determine if a given node represents an empty
 218  
     * entity, with which no extra data can be extracted.
 219  
     * 
 220  
     * @return this returns true if the node is an empty element
 221  
     * 
 222  
     * @throws Exception thrown if there was a parse error
 223  
     */
 224  
    public boolean isEmpty() throws Exception {
 225  651
       if(!map.isEmpty()) {
 226  426
          return false;
 227  
       }
 228  225
       return reader.isEmpty(this);           
 229  
    }
 230  
 }
 231  
 
 232