Coverage Report - org.simpleframework.xml.stream.InputElement
 
Classes in this File Line Coverage Branch Coverage Complexity
InputElement
96%
25/26
100%
2/2
1.118
 
 1  
 /*
 2  
  * InputElement.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.stream;
 20  
 
 21  
 /**
 22  
  * The <code>InputElement</code> represents a self contained element
 23  
  * that will allow access to its child elements. If the next element
 24  
  * read from the <code>NodeReader</code> is not a child then this
 25  
  * will return null. The input element node also allows the attribute
 26  
  * values associated with the node to be accessed.
 27  
  * 
 28  
  * @author Niall Gallagher
 29  
  *
 30  
  * @see org.simpleframework.xml.stream.NodeReader
 31  
  */ 
 32  0
 class InputElement implements InputNode {
 33  
    
 34  
    /**
 35  
     * This contains all the attributes associated with the element.
 36  
     */ 
 37  
    private final InputNodeMap map;
 38  
 
 39  
    /**
 40  
     * This is the node reader that reads from the XML document.
 41  
     */ 
 42  
    private final NodeReader reader;
 43  
    
 44  
    /**
 45  
     * This is the parent node for this XML input element node.
 46  
     */
 47  
    private final InputNode parent;
 48  
    
 49  
    /**
 50  
     * This is the XML element that this node provides access to.
 51  
     */         
 52  
    private final EventNode node;
 53  
  
 54  
    /**
 55  
     * Constructor for the <code>InputElement</code> object. This 
 56  
     * is used to create an input node that will provide access to 
 57  
     * an XML element. All attributes associated with the element 
 58  
     * given are extracted and exposed via the attribute node map.
 59  
     *
 60  
     * @param parent this is the parent XML element for this 
 61  
     * @param reader this is the reader used to read XML elements
 62  
     * @param node this is the XML element wrapped by this node
 63  
     */ 
 64  1803293
    public InputElement(InputNode parent, NodeReader reader, EventNode node) {
 65  1803293
       this.map = new InputNodeMap(this, node);      
 66  1803293
       this.reader = reader;           
 67  1803293
       this.parent = parent;
 68  1803293
       this.node = node;
 69  1803293
    }
 70  
    
 71  
    /**
 72  
     * This is used to return the source object for this node. This
 73  
     * is used primarily as a means to determine which XML provider
 74  
     * is parsing the source document and producing the nodes. It
 75  
     * is useful to be able to determine the XML provider like this.
 76  
     * 
 77  
     * @return this returns the source of this input node
 78  
     */
 79  
    public Object getSource() {
 80  476
       return node.getSource();
 81  
    }
 82  
    
 83  
    /**
 84  
     * This is used to acquire the <code>Node</code> that is the
 85  
     * parent of this node. This will return the node that is
 86  
     * the direct parent of this node and allows for siblings to
 87  
     * make use of nodes with their parents if required.  
 88  
     *   
 89  
     * @return this returns the parent node for this node
 90  
     */
 91  
    public InputNode getParent() {
 92  1481
       return parent;
 93  
    }
 94  
    
 95  
    /**
 96  
     * This provides the position of this node within the document.
 97  
     * This allows the user of this node to report problems with
 98  
     * the location within the document, allowing the XML to be
 99  
     * debugged if it does not match the class schema.
 100  
     *
 101  
     * @return this returns the position of the XML read cursor
 102  
     */      
 103  
    public Position getPosition() {
 104  2248655
       return new InputPosition(node);           
 105  
    }   
 106  
 
 107  
    /**
 108  
     * Returns the name of the node that this represents. This is
 109  
     * an immutable property and should not change for any node.  
 110  
     * This provides the name without the name space part.
 111  
     *  
 112  
     * @return returns the name of the node that this represents
 113  
     */   
 114  
    public String getName() {
 115  2735704
       return node.getName();           
 116  
    }
 117  
    
 118  
    /**
 119  
     * This is used to acquire the namespace prefix for the node.
 120  
     * If there is no namespace prefix for the node then this will
 121  
     * return null. Acquiring the prefix enables the qualification
 122  
     * of the node to be determined. It also allows nodes to be
 123  
     * grouped by its prefix and allows group operations.
 124  
     * 
 125  
     * @return this returns the prefix associated with this node
 126  
     */
 127  
    public String getPrefix() {
 128  12
       return node.getPrefix();
 129  
    }
 130  
    
 131  
    /**
 132  
     * This allows the namespace reference URI to be determined.
 133  
     * A reference is a globally unique string that allows the
 134  
     * node to be identified. Typically the reference will be a URI
 135  
     * but it can be any unique string used to identify the node.
 136  
     * This allows the node to be identified within the namespace.
 137  
     * 
 138  
     * @return this returns the associated namespace reference URI 
 139  
     */
 140  
    public String getReference() {
 141  136
       return node.getReference();
 142  
    }
 143  
    
 144  
    /**
 145  
     * This method is used to determine if this node is the root 
 146  
     * node for the XML document. The root node is the first node
 147  
     * in the document and has no sibling nodes. This is false
 148  
     * if the node has a parent node or a sibling node.
 149  
     * 
 150  
     * @return true if this is the root node within the document
 151  
     */
 152  
    public boolean isRoot() {
 153  13402
       return reader.isRoot(this);
 154  
    }
 155  
 
 156  
    /**
 157  
     * This is used to determine if this node is an element. This
 158  
     * allows users of the framework to make a distinction between
 159  
     * nodes that represent attributes and nodes that represent
 160  
     * elements. This is particularly useful given that attribute
 161  
     * nodes do not maintain a node map of attributes.
 162  
     *
 163  
     * @return this returns true as this instance is an element
 164  
     */ 
 165  
    public boolean isElement() {
 166  1014614
       return true;           
 167  
    } 
 168  
 
 169  
    /**
 170  
     * Provides an attribute from the element represented. If an
 171  
     * attribute for the specified name does not exist within the
 172  
     * element represented then this method will return null.
 173  
     *
 174  
     * @param name this is the name of the attribute to retrieve
 175  
     *
 176  
     * @return this returns the value for the named attribute
 177  
     */    
 178  
    public InputNode getAttribute(String name) {
 179  708055
       return map.get(name);
 180  
    }
 181  
 
 182  
    /**
 183  
     * This returns a map of the attributes contained within the
 184  
     * element. If no elements exist within the element then this
 185  
     * returns an empty map. 
 186  
     * 
 187  
     * @return this returns a map of attributes for the element
 188  
     */    
 189  
    public NodeMap<InputNode> getAttributes() {
 190  2517647
       return map;
 191  
    }
 192  
 
 193  
    /**
 194  
     * Returns the value for the node that this represents. This 
 195  
     * is an immutable value for the node and cannot be changed.
 196  
     * If there is a problem reading an exception is thrown.
 197  
     * 
 198  
     * @return the name of the value for this node instance
 199  
     */     
 200  
    public String getValue() throws Exception {
 201  1007176
       return reader.readValue(this);           
 202  
    }
 203  
   
 204  
    /**
 205  
     * The method is used to acquire the next child attribute of this 
 206  
     * element. If the next element from the <code>NodeReader</code> 
 207  
     * is not a child node to the element that this represents then
 208  
     * this will return null, which ensures each element represents
 209  
     * a self contained collection of child nodes.
 210  
     *
 211  
     * @return this returns the next child element of this node
 212  
     *
 213  
     * @exception Exception thrown if there is a problem reading
 214  
     */  
 215  
    public InputNode getNext() throws Exception {
 216  2511705
       return reader.readElement(this);
 217  
    }
 218  
    
 219  
    /**
 220  
     * The method is used to acquire the next child attribute of this 
 221  
     * element. If the next element from the <code>NodeReader</code> 
 222  
     * is not a child node to the element that this represents then
 223  
     * this will return null, also if the next element does not match
 224  
     * the specified name then this will return null.
 225  
     *
 226  
     * @param name this is the name expected fromt he next element
 227  
     *
 228  
     * @return this returns the next child element of this node
 229  
     *
 230  
     * @exception Exception thrown if there is a problem reading
 231  
     */  
 232  
    public InputNode getNext(String name) throws Exception {
 233  5791
       return reader.readElement(this, name);
 234  
    }
 235  
    
 236  
    /**
 237  
     * This method is used to skip all child elements from this
 238  
     * element. This allows elements to be effectively skipped such
 239  
     * that when parsing a document if an element is not required
 240  
     * then that element can be completely removed from the XML.
 241  
     *
 242  
     * @exception Exception thrown if there was a parse error
 243  
     */ 
 244  
    public void skip() throws Exception {
 245  16
       reader.skipElement(this);           
 246  16
    }
 247  
    
 248  
    /**
 249  
     * This is used to determine if this input node is empty. An
 250  
     * empty node is one with no attributes or children. This can
 251  
     * be used to determine if a given node represents an empty
 252  
     * entity, with which no extra data can be extracted.
 253  
     * 
 254  
     * @return this returns true if the node is an empty element
 255  
     * 
 256  
     * @throws Exception thrown if there was a parse error
 257  
     */
 258  
    public boolean isEmpty() throws Exception {
 259  762
       if(!map.isEmpty()) {
 260  506
          return false;
 261  
       }
 262  256
       return reader.isEmpty(this);           
 263  
    }
 264  
    
 265  
    /**
 266  
     * This is the string representation of the element. It is
 267  
     * used for debugging purposes. When evaluating the element
 268  
     * the to string can be used to print out the element name.
 269  
     * 
 270  
     * @return this returns a text description of the element
 271  
     */
 272  
    public String toString() {
 273  474
       return String.format("element %s", getName());
 274  
    }
 275  
 }
 276  
 
 277