Coverage Report - org.simpleframework.xml.stream.InputAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
InputAttribute
66%
20/30
N/A
1
 
 1  
 /*
 2  
  * InputAttribute.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>InputAttribute</code> is used to represent an attribute
 23  
  * within an element. Rather than representing an attribute as a
 24  
  * name value pair of strings, an attribute is instead represented
 25  
  * as an input node, in the same manner as an element. The reason
 26  
  * for representing an attribute in this way is such that a uniform
 27  
  * means of extracting and parsing values can be used for inputs.
 28  
  *
 29  
  * @author Niall Gallagher
 30  
  */ 
 31  0
 class InputAttribute implements InputNode {
 32  
 
 33  
    /**
 34  
     * This is the parent node to this attribute instance.
 35  
     */
 36  
    private InputNode parent;
 37  
    
 38  
    /**
 39  
     * This is the reference associated with this attribute node.
 40  
     */
 41  
    private String reference;
 42  
    
 43  
    /**
 44  
     * This is the prefix associated with this attribute node.
 45  
     */
 46  
    private String prefix;
 47  
         
 48  
    /**
 49  
     * Represents the name of this input attribute instance.
 50  
     */         
 51  
    private String name;
 52  
 
 53  
    /**
 54  
     * Represents the value for this input attribute instance.
 55  
     */ 
 56  
    private String value;    
 57  
    
 58  
    /**
 59  
     * This is the source associated with this input attribute.
 60  
     */
 61  
    private Object source;
 62  
    
 63  
    /**
 64  
     * Constructor for the <code>InputAttribute</code> object. This
 65  
     * is used to create an input attribute using the provided name
 66  
     * and value, all other values for this input node will be null.
 67  
     * 
 68  
     * @param parent this is the parent node to this attribute
 69  
     * @param name this is the name for this attribute object
 70  
     * @param value this is the value for this attribute object
 71  
     */     
 72  69
    public InputAttribute(InputNode parent, String name, String value) {
 73  69
       this.parent = parent;
 74  69
       this.value = value;
 75  69
       this.name = name;           
 76  69
    }
 77  
    
 78  
    /**
 79  
     * Constructor for the <code>InputAttribute</code> object. This
 80  
     * is used to create an input attribute using the provided name
 81  
     * and value, all other values for this input node will be null.
 82  
     * 
 83  
     * @param parent this is the parent node to this attribute
 84  
     * @param attribute this is the attribute containing the details
 85  
     */     
 86  806317
    public InputAttribute(InputNode parent, Attribute attribute) {
 87  806317
       this.reference = attribute.getReference();
 88  806317
       this.prefix = attribute.getPrefix();
 89  806317
       this.source = attribute.getSource();
 90  806317
       this.value = attribute.getValue();
 91  806317
       this.name = attribute.getName();
 92  806317
       this.parent = parent;           
 93  806317
    }
 94  
    
 95  
    /**
 96  
     * This is used to return the source object for this node. This
 97  
     * is used primarily as a means to determine which XML provider
 98  
     * is parsing the source document and producing the nodes. It
 99  
     * is useful to be able to determine the XML provider like this.
 100  
     * 
 101  
     * @return this returns the source of this input node
 102  
     */
 103  
    public Object getSource() {
 104  0
       return source;
 105  
    }
 106  
    
 107  
    /**
 108  
     * This is used to acquire the <code>Node</code> that is the
 109  
     * parent of this node. This will return the node that is
 110  
     * the direct parent of this node and allows for siblings to
 111  
     * make use of nodes with their parents if required.  
 112  
     *   
 113  
     * @return this returns the parent node for this node
 114  
     */
 115  
    public InputNode getParent() {
 116  0
       return parent;
 117  
    }
 118  
    
 119  
    /**
 120  
     * This provides the position of this node within the document.
 121  
     * This allows the user of this node to report problems with
 122  
     * the location within the document, allowing the XML to be
 123  
     * debugged if it does not match the class schema.
 124  
     *
 125  
     * @return this returns the position of the XML read cursor
 126  
     */  
 127  
    public Position getPosition() {
 128  1686
       return parent.getPosition();           
 129  
    }
 130  
 
 131  
    /**
 132  
     * Returns the name of the node that this represents. This is
 133  
     * an immutable property and will not change for this node. 
 134  
     *  
 135  
     * @return returns the name of the node that this represents
 136  
     */   
 137  
    public String getName() {
 138  1514052
       return name;
 139  
    }
 140  
    
 141  
    /**
 142  
     * This is used to acquire the namespace prefix for the node.
 143  
     * If there is no namespace prefix for the node then this will
 144  
     * return null. Acquiring the prefix enables the qualification
 145  
     * of the node to be determined. It also allows nodes to be
 146  
     * grouped by its prefix and allows group operations.
 147  
     * 
 148  
     * @return this returns the prefix associated with this node
 149  
     */
 150  
    public String getPrefix() {
 151  5
       return prefix;
 152  
    }
 153  
    
 154  
    /**
 155  
     * This allows the namespace reference URI to be determined.
 156  
     * A reference is a globally unique string that allows the
 157  
     * node to be identified. Typically the reference will be a URI
 158  
     * but it can be any unique string used to identify the node.
 159  
     * This allows the node to be identified within the namespace.
 160  
     * 
 161  
     * @return this returns the associated namespace reference URI 
 162  
     */
 163  
    public String getReference() {
 164  5
       return reference;
 165  
    }
 166  
 
 167  
    /**
 168  
     * Returns the value for the node that this represents. This 
 169  
     * is an immutable value for the node and cannot be changed.
 170  
     * 
 171  
     * @return the name of the value for this node instance
 172  
     */   
 173  
    public String getValue() {
 174  806313
       return value;
 175  
    }
 176  
    
 177  
    /**
 178  
     * This method is used to determine if this node is the root 
 179  
     * node for the XML document. This will return false as this 
 180  
     * node can never be the root node because it is an attribute.
 181  
     * 
 182  
     * @return this will always return false for attribute nodes
 183  
     */
 184  
    public boolean isRoot() {
 185  0
       return false;
 186  
    }
 187  
 
 188  
    /**
 189  
     * This is used to determine if this node is an element. This
 190  
     * node instance can not be an element so this method returns
 191  
     * false. Returning null tells the users of this node that any
 192  
     * attributes added to the node map will be permenantly lost.
 193  
     *
 194  
     * @return this returns false as this is an attribute node
 195  
     */ 
 196  
    public boolean isElement() {
 197  707956
       return false;           
 198  
    } 
 199  
 
 200  
    /**
 201  
     * Because the <code>InputAttribute</code> object represents an
 202  
     * attribute this method will return null. If nodes are added 
 203  
     * to the node map the values will not be available here.
 204  
     *
 205  
     * @return this always returns null for a requested attribute
 206  
     */ 
 207  
    public InputNode getAttribute(String name) {
 208  0
       return null;
 209  
    }
 210  
 
 211  
    /**
 212  
     * Because the <code>InputAttribute</code> object represents an
 213  
     * attribute this method will return an empty map. If nodes are
 214  
     * added to the node map the values will not be maintained.
 215  
     *
 216  
     * @return this always returns an empty node map of attributes
 217  
     */
 218  
    public NodeMap<InputNode> getAttributes() {
 219  0
       return new InputNodeMap(this);
 220  
    }
 221  
    
 222  
    /**
 223  
     * Because the <code>InputAttribute</code> object represents an
 224  
     * attribute this method will return null. An attribute is a
 225  
     * simple name value pair an so can not contain any child nodes.
 226  
     *
 227  
     * @return this always returns null for a requested child node
 228  
     */
 229  
    public InputNode getNext() {
 230  0
       return null;           
 231  
    }
 232  
    
 233  
    /**
 234  
     * Because the <code>InputAttribute</code> object represents an
 235  
     * attribute this method will return null. An attribute is a
 236  
     * simple name value pair an so can not contain any child nodes.
 237  
     *
 238  
     * @param name this is the name of the next expected element
 239  
     *
 240  
     * @return this always returns null for a requested child node
 241  
     */
 242  
    public InputNode getNext(String name) {
 243  0
       return null;           
 244  
    }
 245  
    
 246  
    /**
 247  
     * This method is used to skip all child elements from this
 248  
     * element. This allows elements to be effectively skipped such
 249  
     * that when parsing a document if an element is not required
 250  
     * then that element can be completely removed from the XML.
 251  
     */ 
 252  
    public void skip() {
 253  0
       return;           
 254  
    }
 255  
    
 256  
    /**
 257  
     * This is used to determine if this input node is empty. An
 258  
     * empty node is one with no attributes or children. This can
 259  
     * be used to determine if a given node represents an empty
 260  
     * entity, with which no extra data can be extracted.
 261  
     * 
 262  
     * @return this will always return false as it has a value
 263  
     */
 264  
    public boolean isEmpty() {
 265  0
       return false;
 266  
    }
 267  
    
 268  
    /**
 269  
     * This is the string representation of the attribute. It is
 270  
     * used for debugging purposes. When evaluating the attribute
 271  
     * the to string can be used to print out the attribute name.
 272  
     * 
 273  
     * @return this returns a text description of the attribute
 274  
     */
 275  
    public String toString() {
 276  5
       return String.format("attribute %s='%s'", name, value);
 277  
    }
 278  
 }