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