Coverage Report - org.simpleframework.xml.stream.InputNodeMap
 
Classes in this File Line Coverage Branch Coverage Complexity
InputNodeMap
82%
19/23
66%
4/6
1.333
 
 1  
 /*
 2  
  * InputNodeMap.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  
 import java.util.LinkedHashMap;
 22  
 import java.util.Iterator;
 23  
 
 24  
 /**
 25  
  * The <code>InputNodeMap</code> object represents a map to contain
 26  
  * attributes used by an input node. This can be used as an empty
 27  
  * node map, it can be used to extract its values from a start
 28  
  * element. This creates <code>InputAttribute</code> objects for 
 29  
  * each node added to the map, these can then be used by an element
 30  
  * input node to represent attributes as input nodes.
 31  
  *
 32  
  * @author Niall Gallagher
 33  
  */ 
 34  1847777
 class InputNodeMap extends LinkedHashMap<String, InputNode> implements NodeMap<InputNode> {
 35  
 
 36  
    /**
 37  
     * This is the source node that this node map belongs to.
 38  
     */          
 39  
    private final InputNode source;        
 40  
    
 41  
    /**
 42  
     * Constructor for the <code>InputNodeMap</code> object. This
 43  
     * is used to create an empty input node map, which will create
 44  
     * <code>InputAttribute</code> object for each inserted node.
 45  
     *
 46  
     * @param source this is the node this node map belongs to
 47  
     */ 
 48  0
    protected InputNodeMap(InputNode source) {
 49  0
       this.source = source;            
 50  0
    }        
 51  
 
 52  
    /**
 53  
     * Constructor for the <code>InputNodeMap</code> object. This
 54  
     * is used to create an input node map, which will be populated
 55  
     * with the attributes from the <code>StartElement</code> that
 56  
     * is specified.
 57  
     *
 58  
     * @param source this is the node this node map belongs to
 59  
     * @param element the element to populate the node map with
 60  
     */ 
 61  1803293
    public InputNodeMap(InputNode source, EventNode element) {
 62  1803293
       this.source = source;           
 63  1803293
       this.build(element);   
 64  1803293
    }
 65  
    
 66  
    /**
 67  
     * This is used to insert all attributes belonging to the start
 68  
     * element to the map. All attributes acquired from the element
 69  
     * are converted into <code>InputAttribute</code> objects so 
 70  
     * that they can be used as input nodes by an input node.
 71  
     *
 72  
     * @param element the element to acquire attributes from
 73  
     */ 
 74  
    private void build(EventNode element) {
 75  1803293
       for(Attribute entry : element) {
 76  806317
          InputAttribute value = new InputAttribute(source, entry);
 77  
          
 78  806317
          if(!entry.isReserved()) {
 79  806317
             put(value.getName(), value);
 80  
          }
 81  806317
       }
 82  1803293
    }
 83  
    
 84  
    /**
 85  
     * This is used to acquire the actual node this map represents.
 86  
     * The source node provides further details on the context of
 87  
     * the node, such as the parent name, the namespace, and even
 88  
     * the value in the node. Care should be taken when using this. 
 89  
     * 
 90  
     * @return this returns the node that this map represents
 91  
     */
 92  
    public InputNode getNode() {
 93  13563
        return source;
 94  
    }
 95  
 
 96  
    /**
 97  
     * This is used to get the name of the element that owns the
 98  
     * nodes for the specified map. This can be used to determine
 99  
     * which element the node map belongs to.
 100  
     * 
 101  
     * @return this returns the name of the owning element
 102  
     */         
 103  
    public String getName() {
 104  0
       return source.getName();           
 105  
    }   
 106  
    
 107  
    /**
 108  
     * This is used to add a new <code>InputAttribute</code> node to
 109  
     * the map. The created node can be used by an input node to
 110  
     * to represent the attribute as another input node. Once the 
 111  
     * node is created it can be acquired using the specified name.
 112  
     *
 113  
     * @param name this is the name of the node to be created
 114  
     * @param value this is the value to be given to the node
 115  
     * 
 116  
     * @return this returns the node that has just been added
 117  
     */    
 118  
    public InputNode put(String name, String value) {
 119  69
       InputNode node = new InputAttribute(source, name, value);
 120  
       
 121  69
       if(name != null) {
 122  69
          put(name, node);
 123  
       }
 124  69
       return node;
 125  
    }
 126  
    
 127  
    /**
 128  
     * This is used to remove the <code>Node</code> mapped to the
 129  
     * given name.  This returns a name value pair that represents
 130  
     * an attribute. If no node is mapped to the specified name 
 131  
     * then this method will return a null value.
 132  
     *
 133  
     * @param name this is the name of the node to remove
 134  
     * 
 135  
     * @return this will return the node mapped to the given name
 136  
     */    
 137  
    public InputNode remove(String name) {
 138  1834116
       return super.remove(name);
 139  
    }
 140  
    
 141  
    /**
 142  
     * This is used to acquire the <code>Node</code> mapped to the
 143  
     * given name. This returns a name value pair that represents
 144  
     * an attribute. If no node is mapped to the specified name 
 145  
     * then this method will return a null value.
 146  
     *
 147  
     * @param name this is the name of the node to retrieve
 148  
     * 
 149  
     * @return this will return the node mapped to the given name
 150  
     */       
 151  
    public InputNode get(String name) {
 152  708084
       return super.get(name);
 153  
    }
 154  
 
 155  
    /**
 156  
     * This returns an iterator for the names of all the nodes in
 157  
     * this <code>NodeMap</code>. This allows the names to be 
 158  
     * iterated within a for each loop in order to extract nodes.
 159  
     *
 160  
     * @return this returns the names of the nodes in the map
 161  
     */    
 162  
    public Iterator<String> iterator() {
 163  715213
       return keySet().iterator();
 164  
    }
 165  
 }