Coverage Report - org.simpleframework.xml.stream.OutputAttribute
 
Classes in this File Line Coverage Branch Coverage Complexity
OutputAttribute
39%
13/33
N/A
1
 
 1  
 /*
 2  
  * OutputAttribute.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>OutputAttribute</code> object is used to represent a 
 23  
  * node added to the output node map. It represents a simple name 
 24  
  * value pair that is used as an attribute by an output element.
 25  
  * This shares its namespaces with the parent element so that any
 26  
  * namespaces added to the attribute are actually added to the
 27  
  * parent element, which ensures correct scoping.
 28  
  *
 29  
  * @see org.simpleframework.xml.stream.Node
 30  
  */ 
 31  0
 class OutputAttribute implements OutputNode {
 32  
    
 33  
    /**
 34  
     * This contains the namespaces for the parent element.
 35  
     */         
 36  
    private NamespaceMap scope;
 37  
    
 38  
    /**
 39  
     * Represents the output node that this node requires.
 40  
     */
 41  
    private OutputNode source;
 42  
 
 43  
    /**
 44  
     * Represents the namespace reference for this node.
 45  
     */
 46  
    private String reference;
 47  
    
 48  
    /**
 49  
     * Represents the name of this node object instance.
 50  
     */            
 51  
    private String name;
 52  
 
 53  
    /**
 54  
     * Represents the value of this node object instance.
 55  
     */  
 56  
    private String value;
 57  
         
 58  
    /**
 59  
     * Constructor for the <code>OutputAttribute</code> object. This 
 60  
     * is used to create a simple name value pair attribute holder.
 61  
     *
 62  
     * @param name this is the name that is used for the node
 63  
     * @param value this is the value used for the node
 64  
     */ 
 65  271292
    public OutputAttribute(OutputNode source, String name, String value) {
 66  271292
       this.scope = source.getNamespaces();
 67  271292
       this.source = source;
 68  271292
       this.value = value;              
 69  271292
       this.name = name;              
 70  271292
    }         
 71  
    
 72  
    /**
 73  
     * Returns the value for the node that this represents. This   
 74  
     * is a modifiable property for the node and can be changed.
 75  
     * When set this forms the value the attribute contains in
 76  
     * the parent XML element, which is written in quotations.
 77  
     *    
 78  
     * @return the name of the value for this node instance
 79  
     */      
 80  
    public String getValue() {
 81  271297
       return value;              
 82  
    }
 83  
 
 84  
    /**
 85  
     * This is used to set a text value to the attribute. This should
 86  
     * be added to the attribute if the attribute is to be written 
 87  
     * to the parent element. Without a value this is invalid.
 88  
     *
 89  
     * @param value this is the text value to add to this attribute
 90  
     */
 91  
    public void setValue(String value) {
 92  47
       this.value = value;           
 93  47
    }
 94  
    
 95  
    /**
 96  
     * This is used to change the name of an output node. This will
 97  
     * only affect the name of the node if the node has not yet been
 98  
     * committed. If the node is committed then this will not be
 99  
     * reflected in the resulting XML generated.
 100  
     * 
 101  
     * @param name this is the name to change the node to
 102  
     */
 103  
    public void setName(String name) {
 104  0
       this.name = name;
 105  0
    }
 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  
     * If this is null then the attribute will not be added to 
 111  
     * the node map, all attributes must have a valid key.
 112  
     *  
 113  
     * @return returns the name of the node that this represents
 114  
     */          
 115  
    public String getName() {
 116  0
       return name;              
 117  
    }
 118  
    
 119  
    /**
 120  
     * This is used to acquire the <code>Node</code> that is the
 121  
     * parent of this node. This will return the node that is
 122  
     * the direct parent of this node and allows for siblings to
 123  
     * make use of nodes with their parents if required.  
 124  
     *   
 125  
     * @return this returns the parent node for this node
 126  
     */
 127  
    public OutputNode getParent() {
 128  0
      return source;
 129  
    }      
 130  
 
 131  
    /**
 132  
     * This returns a <code>NodeMap</code> which can be used to add
 133  
     * nodes to the element before that element has been committed. 
 134  
     * Nodes can be removed or added to the map and will appear as
 135  
     * attributes on the written element when it is committed.
 136  
     *
 137  
     * @return returns the node map used to manipulate attributes
 138  
     */ 
 139  
    public NodeMap<OutputNode> getAttributes() {
 140  0
       return new OutputNodeMap(this);
 141  
    }
 142  
 
 143  
    /**
 144  
     * This is used to create a child element within the element that
 145  
     * this object represents. When a new child is created with this
 146  
     * method then the previous child is committed to the document.
 147  
     * The created <code>OutputNode</code> object can be used to add
 148  
     * attributes to the child element as well as other elements.
 149  
     *
 150  
     * @param name this is the name of the child element to create
 151  
     */ 
 152  
    public OutputNode getChild(String name) {
 153  0
       return null;
 154  
    }
 155  
    
 156  
    /**
 157  
     * This is used to get the text comment for the element. This can
 158  
     * be null if no comment has been set. If no comment is set on 
 159  
     * the node then no comment will be written to the resulting XML.
 160  
     * 
 161  
     * @return this is the comment associated with this element
 162  
     */
 163  
    public String getComment() {
 164  0
       return null;
 165  
    }
 166  
    
 167  
    /**
 168  
     * This is used to set a text comment to the element. This will
 169  
     * be written just before the actual element is written. Only a
 170  
     * single comment can be set for each output node written. 
 171  
     * 
 172  
     * @param comment this is the comment to set on the node
 173  
     */
 174  
    public void setComment(String comment) {
 175  0
       return;
 176  
    } 
 177  
 
 178  
    /**
 179  
     * The <code>Mode</code> is used to indicate the output mode
 180  
     * of this node. Three modes are possible, each determines
 181  
     * how a value, if specified, is written to the resulting XML
 182  
     * document. This is determined by the <code>setData</code>
 183  
     * method which will set the output to be CDATA or escaped, 
 184  
     * if neither is specified the mode is inherited.
 185  
     * 
 186  
     * @return this returns the mode of this output node object
 187  
     */
 188  
    public Mode getMode() {
 189  0
       return Mode.INHERIT;
 190  
    }
 191  
 
 192  
    /**
 193  
     * This is used to set the output mode of this node to either
 194  
     * be CDATA, escaped, or inherited. If the mode is set to data
 195  
     * then any value specified will be written in a CDATA block, 
 196  
     * if this is set to escaped values are escaped. If however 
 197  
     * this method is set to inherited then the mode is inherited
 198  
     * from the parent node.
 199  
     * 
 200  
     * @param mode this is the output mode to set the node to 
 201  
     */
 202  
    public void setMode(Mode mode) {
 203  0
       return;           
 204  
    }
 205  
    
 206  
    /**
 207  
     * This is used to set the output mode of this node to either
 208  
     * be CDATA or escaped. If this is set to true the any value
 209  
     * specified will be written in a CDATA block, if this is set
 210  
     * to false the values is escaped. If however this method is
 211  
     * never invoked then the mode is inherited from the parent.
 212  
     * 
 213  
     * @param data if true the value is written as a CDATA block
 214  
     */
 215  
    public void setData(boolean data) {
 216  0
       return;           
 217  
    } 
 218  
 
 219  
    /**
 220  
     * This is used to acquire the prefix for this output node. If
 221  
     * the output node is an element then this will search its parent
 222  
     * nodes until the prefix that is currently in scope is found. 
 223  
     * If however this node is an attribute then the hierarchy of 
 224  
     * nodes is not searched as attributes to not inherit namespaces.
 225  
     *
 226  
     * @return this returns the prefix associated with this node
 227  
     */  
 228  
    public String getPrefix() {
 229  0
       return scope.getPrefix(reference);          
 230  
    }
 231  
    
 232  
    /**
 233  
     * This is used to acquire the prefix for this output node. If
 234  
     * the output node is an element then this will search its parent
 235  
     * nodes until the prefix that is currently in scope is found. 
 236  
     * If however this node is an attribute then the hierarchy of 
 237  
     * nodes is not searched as attributes to not inherit namespaces.
 238  
     *
 239  
     * @param inherit if there is no explicit prefix then inherit
 240  
     *
 241  
     * @return this returns the prefix associated with this node
 242  
     */  
 243  
    public String getPrefix(boolean inherit) {
 244  271196
       return scope.getPrefix(reference);
 245  
    }
 246  
    
 247  
    /**
 248  
     * This is used to acquire the namespace URI reference associated
 249  
     * with this node. Although it is recommended that the namespace
 250  
     * reference is a URI it does not have to be, it can be any unique
 251  
     * identifier that can be used to distinguish the qualified names.
 252  
     *
 253  
     * @return this returns the namespace URI reference for this
 254  
     */
 255  
    public String getReference() {
 256  0
       return reference;           
 257  
    }
 258  
   
 259  
    /**
 260  
     * This is used to set the reference for the node. Setting the
 261  
     * reference implies that the node is a qualified node within the
 262  
     * XML document. Both elements and attributes can be qualified.
 263  
     * Depending on the prefix set on this node or, failing that, any
 264  
     * parent node for the reference, the element will appear in the
 265  
     * XML document with that string prefixed to the node name.
 266  
     *
 267  
     * @param reference this is used to set the reference for the node
 268  
     */  
 269  
    public void setReference(String reference) {
 270  8
       this.reference = reference;           
 271  8
    }
 272  
   
 273  
    /**
 274  
     * This returns the <code>NamespaceMap</code> for this node. Only
 275  
     * an element can have namespaces, so if this node represents an
 276  
     * attribute the elements namespaces will be provided when this is
 277  
     * requested. By adding a namespace it becomes in scope for the
 278  
     * current element all all child elements of that element.
 279  
     *
 280  
     * @return this returns the namespaces associated with the node
 281  
     */  
 282  
    public NamespaceMap getNamespaces() {
 283  208850
       return scope;           
 284  
    }
 285  
 
 286  
    /**
 287  
     * This method is used for convinience to add an attribute node 
 288  
     * to the attribute <code>NodeMap</code>. The attribute added
 289  
     * can be removed from the element by useing the node map.
 290  
     * 
 291  
     * @param name this is the name of the attribute to be added
 292  
     * @param value this is the value of the node to be added
 293  
     * 
 294  
     * @return this returns the node that has just been added
 295  
     */ 
 296  
    public OutputNode setAttribute(String name, String value) {
 297  0
       return null;           
 298  
    }
 299  
 
 300  
    /**
 301  
     * This is used to remove any uncommitted changes. Removal of an
 302  
     * output node can only be done if it has no siblings and has
 303  
     * not yet been committed. If the node is committed then this 
 304  
     * will throw an exception to indicate that it cannot be removed. 
 305  
     */
 306  
    public void remove() {
 307  0
       return;
 308  
    }
 309  
    
 310  
    /**
 311  
     * The <code>commit</code> method is used flush and commit any 
 312  
     * child nodes that have been created by this node. This allows
 313  
     * the output to be completed when building of the XML document
 314  
     * has been completed. If output fails an exception is thrown.
 315  
     */ 
 316  
    public void commit() {
 317  0
       return;           
 318  
    }
 319  
 
 320  
    /**
 321  
     * This method is used to determine if this node is the root 
 322  
     * node for the XML document. The root node is the first node
 323  
     * in the document and has no sibling nodes. This is false
 324  
     * if the node has a parent node or a sibling node.
 325  
     * 
 326  
     * @return true if this is the root node within the document
 327  
     */
 328  
    public boolean isRoot() {
 329  0
       return false;           
 330  
    }
 331  
 
 332  
    /**
 333  
     * This is used to determine whether the node has been committed.
 334  
     * If the node has been committed, then this will return true.
 335  
     * When committed the node can no longer produce child nodes.
 336  
     *
 337  
     * @return true if this node has already been committed
 338  
     */
 339  
    public boolean isCommitted() {
 340  0
       return true;           
 341  
    }
 342  
    
 343  
    /**
 344  
     * This is used to acquire the name and value of the attribute.
 345  
     * Implementing this method ensures that debugging the output
 346  
     * node is simplified as it is possible to get the actual value.
 347  
     * 
 348  
     * @return this returns the details of this output node
 349  
     */
 350  
    public String toString() {
 351  0
        return String.format("attribute %s='%s'", name, value);
 352  
    }
 353  
 }