Coverage Report - org.simpleframework.xml.core.LabelMap
 
Classes in this File Line Coverage Branch Coverage Complexity
LabelMap
97%
33/34
77%
14/18
2
 
 1  
 /*
 2  
  * LabelMap.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.core;
 20  
 
 21  
 import java.util.HashSet;
 22  
 import java.util.Iterator;
 23  
 import java.util.LinkedHashMap;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * The <code>LabelMap</code> object represents a map that contains 
 28  
  * string label mappings. This is used for convenience as a typedef
 29  
  * like construct to avoid having declare the generic type whenever
 30  
  * it is referenced. Also this allows <code>Label</code> values 
 31  
  * from the map to be iterated within for each loops.
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  *
 35  
  * @see org.simpleframework.xml.core.Label
 36  
  */
 37  
 class LabelMap extends LinkedHashMap<String, Label> implements Iterable<Label> { 
 38  
    
 39  
    /**
 40  
     * This is policy used to determine the type of mappings used.
 41  
     */        
 42  
    private final Policy policy;
 43  
         
 44  
    /**
 45  
     * Constructor for the <code>LabelMap</code> object is used to 
 46  
     * create an empty map. This is used for convenience as a typedef
 47  
     * like construct which avoids having to use the generic type.
 48  
     */ 
 49  
    public LabelMap() {
 50  7661
       this(null);
 51  7661
    }
 52  
    
 53  
    /**
 54  
     * Constructor for the <code>LabelMap</code> object is used to 
 55  
     * create an empty map. This is used for convenience as a typedef
 56  
     * like construct which avoids having to use the generic type.
 57  
     */ 
 58  1885213
    public LabelMap(Policy policy) {
 59  1885213
       this.policy = policy;
 60  1885213
    }
 61  
 
 62  
    /**
 63  
     * This allows the <code>Label</code> objects within the label map
 64  
     * to be iterated within for each loops. This will provide all
 65  
     * remaining label objects within the map. The iteration order is
 66  
     * not maintained so label objects may be given in any sequence.
 67  
     *
 68  
     * @return this returns an iterator for existing label objects
 69  
     */ 
 70  
    public Iterator<Label> iterator() {
 71  3528218
       return values().iterator();
 72  
    }
 73  
 
 74  
    /**
 75  
     * This performs a <code>remove</code> that will remove the label
 76  
     * from the map and return that label. This method allows the 
 77  
     * values within the map to be exclusively taken one at a time,
 78  
     * which enables the user to determine which labels remain.
 79  
     *
 80  
     * @param name this is the name of the element of attribute
 81  
     *
 82  
     * @return this is the label object representing the XML node
 83  
     */ 
 84  
    public Label getLabel(String name) {
 85  3861183
       return remove(name);    
 86  
    }
 87  
 
 88  
    /**
 89  
     * This is used to acquire the paths and names for each label in
 90  
     * this map. Extracting the names and paths in this manner allows
 91  
     * a labels to be referred to by either. If there are no elements
 92  
     * registered with this map this will return an empty set.
 93  
     * 
 94  
     * @return this returns the names and paths for each label
 95  
     */
 96  
    public String[] getKeys() throws Exception {
 97  488
       Set<String> list = new HashSet<String>();
 98  
       
 99  488
       for(Label label : this) {
 100  1538
          if(label != null) {
 101  1538
             String path = label.getPath();
 102  1538
             String name = label.getName();
 103  
             
 104  1538
             list.add(path);
 105  1538
             list.add(name);
 106  1538
          }
 107  
       }
 108  488
       return getArray(list);
 109  
    }
 110  
    
 111  
    /**
 112  
     * This is used to acquire the paths for each label in this map. 
 113  
     * Extracting the paths in this manner allows them to be used to
 114  
     * reference the labels using paths only.
 115  
     * 
 116  
     * @return this returns the paths for each label in this map
 117  
     */
 118  
    public String[] getPaths() throws Exception {
 119  1010
       Set<String> list = new HashSet<String>();
 120  
       
 121  1010
       for(Label label : this) {
 122  3166
          if(label != null) {
 123  3166
             String path = label.getPath();
 124  
             
 125  3166
             list.add(path);
 126  3166
          }
 127  
       }
 128  1010
       return getArray(list);
 129  
    }
 130  
    
 131  
    /**
 132  
     * This method is used to clone the label map such that mappings
 133  
     * can be maintained in the original even if they are modified
 134  
     * in the clone. This is used to that the <code>Schema</code> can
 135  
     * remove mappings from the label map as they are visited. 
 136  
     *
 137  
     * @return this returns a cloned representation of this map
 138  
     */ 
 139  
    public LabelMap getLabels() throws Exception {
 140  1864877
       LabelMap map = new LabelMap(policy);
 141  
       
 142  1864877
       for(Label label : this) {
 143  2704439
          if(label != null) {
 144  2704439
             String name = label.getPath();
 145  
             
 146  2704439
             map.put(name, label);
 147  2704439
          }
 148  
       }         
 149  1864877
       return map;      
 150  
    }   
 151  
    
 152  
    /**
 153  
     * Convert a set in to an array. Conversion is required as the keys
 154  
     * and paths must be arrays. Converting from the set in this manner
 155  
     * helps the performance on android which works faster with arrays.
 156  
     * 
 157  
     * @param list this is the set to be converted to an array
 158  
     * 
 159  
     * @return this returns an array of strings from the provided set
 160  
     */
 161  
    private String[] getArray(Set<String> list) {
 162  1498
       return list.toArray(new String[]{});
 163  
    }
 164  
 
 165  
    /**
 166  
     * This method is used to determine whether strict mappings are
 167  
     * required. Strict mapping means that all labels in the class
 168  
     * schema must match the XML elements and attributes in the
 169  
     * source XML document. When strict mapping is disabled, then
 170  
     * XML elements and attributes that do not exist in the schema
 171  
     * class will be ignored without breaking the parser.
 172  
     * 
 173  
     * @param context this is used to determine if this is strict
 174  
     *
 175  
     * @return true if strict parsing is enabled, false otherwise
 176  
     */ 
 177  
    public boolean isStrict(Context context) {
 178  40
       if(policy == null) {
 179  0
          return context.isStrict();
 180  
       }
 181  40
       return context.isStrict() && policy.isStrict();           
 182  
    }
 183  
 }