Coverage Report - org.simpleframework.xml.core.ElementMapLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementMapLabel
96%
57/59
95%
19/20
1.737
 
 1  
 /*
 2  
  * ElementMapLabel.java July 2007
 3  
  *
 4  
  * Copyright (C) 2007, 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.lang.annotation.Annotation;
 22  
 
 23  
 import org.simpleframework.xml.ElementMap;
 24  
 import org.simpleframework.xml.strategy.Type;
 25  
 import org.simpleframework.xml.stream.Format;
 26  
 import org.simpleframework.xml.stream.Style;
 27  
 
 28  
 /**
 29  
  * The <code>ElementMapLabel</code> represents a label that is used
 30  
  * to represent an XML element map in a class schema. This element 
 31  
  * list label can be used to convert an XML node into a map object 
 32  
  * of composite or primitive key value pairs. Each element converted 
 33  
  * with the converter this creates must be an XML serializable element.
 34  
  * 
 35  
  * @author Niall Gallagher
 36  
  * 
 37  
  *  @see org.simpleframework.xml.ElementMap
 38  
  */
 39  
 class ElementMapLabel extends TemplateLabel {
 40  
    
 41  
    /**
 42  
     * This is the decorator that is associated with the element.
 43  
     */
 44  
    private Decorator decorator;
 45  
 
 46  
    /**
 47  
     * This contains the details of the annotated contact object.
 48  
     */
 49  
    private Introspector detail;
 50  
    
 51  
    /**
 52  
     * This references the annotation that the field uses.
 53  
     */
 54  
    private ElementMap label;
 55  
    
 56  
    /**
 57  
     * This is a cache of the expression for this element map.
 58  
     */
 59  
    private Expression cache;
 60  
    
 61  
    /**
 62  
     * This is the format used to style the elements for this.
 63  
     */
 64  
    private Format format;
 65  
    
 66  
    /**
 67  
     * The entry object contains the details on how to write the map.
 68  
     */
 69  
    private Entry entry;
 70  
    
 71  
    /**
 72  
     * This is the name of the element for this label instance.
 73  
     */
 74  
    private String override;  
 75  
    
 76  
    /**
 77  
     * This is the name of the XML entry from the annotation.
 78  
     */
 79  
    private String parent;
 80  
    
 81  
    /**
 82  
     * This is the path of the element for this label instance.
 83  
     */
 84  
    private String path;  
 85  
    
 86  
    /**
 87  
     * This is the name of the element for this label instance.
 88  
     */
 89  
    private String name;  
 90  
    
 91  
    /**
 92  
     * Represents the type of objects this map object will hold.
 93  
     */
 94  
    private Class[] items;
 95  
    
 96  
    /**
 97  
     * This is the type of map object this list will instantiate.
 98  
     */
 99  
    private Class type;
 100  
    
 101  
    /**
 102  
     * This is used to determine if the attribute is required.
 103  
     */
 104  
    private boolean required;
 105  
    
 106  
    /**
 107  
     * This is used to determine if the attribute is inline.
 108  
     */
 109  
    private boolean inline;
 110  
    
 111  
    /**
 112  
     * This is used to determine if the attribute is data.
 113  
     */
 114  
    private boolean data;
 115  
    
 116  
    /**
 117  
     * Constructor for the <code>ElementMapLabel</code> object. This
 118  
     * creates a label object, which can be used to convert an XML 
 119  
     * node to a <code>Map</code> of XML serializable objects.
 120  
     * 
 121  
     * @param contact this is the contact that this label represents
 122  
     * @param label the annotation that contains the schema details
 123  
     * @param format this is the format used to style this label
 124  
     */
 125  413
    public ElementMapLabel(Contact contact, ElementMap label, Format format) {
 126  413
       this.detail = new Introspector(contact, this, format);
 127  413
       this.decorator = new Qualifier(contact);
 128  413
       this.entry = new Entry(contact, label);
 129  413
       this.required = label.required();
 130  413
       this.type = contact.getType();
 131  413
       this.inline = label.inline();
 132  413
       this.override = label.name();      
 133  413
       this.data = label.data();
 134  413
       this.format = format;
 135  413
       this.label = label;
 136  413
    }
 137  
    
 138  
    /**
 139  
     * This is used to acquire the <code>Decorator</code> for this.
 140  
     * A decorator is an object that adds various details to the
 141  
     * node without changing the overall structure of the node. For
 142  
     * example comments and namespaces can be added to the node with
 143  
     * a decorator as they do not affect the deserialization.
 144  
     * 
 145  
     * @return this returns the decorator associated with this
 146  
     */
 147  
    public Decorator getDecorator() throws Exception {
 148  631
       return decorator;
 149  
    }
 150  
    
 151  
    /**
 152  
     * This method returns a <code>Converter</code> which can be used to
 153  
     * convert an XML node into an object value and vice versa. The 
 154  
     * converter requires only the context object in order to perform
 155  
     * serialization or deserialization of the provided XML node.
 156  
     * 
 157  
     * @param context this is the context object for the serialization
 158  
     * 
 159  
     * @return this returns an object that is used for conversion
 160  
     */
 161  
    public Converter getConverter(Context context) throws Exception {
 162  662
       Type type = getMap();
 163  
       
 164  662
       if(!label.inline()) {
 165  399
          return new CompositeMap(context, entry, type);
 166  
       }
 167  263
       return new CompositeInlineMap(context, entry, type);     
 168  
    }  
 169  
    
 170  
    /**
 171  
     * This is used to provide a configured empty value used when the
 172  
     * annotated value is null. This ensures that XML can be created
 173  
     * with required details regardless of whether values are null or
 174  
     * not. It also provides a means for sensible default values.
 175  
     * 
 176  
     * @param context this is the context object for the serialization
 177  
     * 
 178  
     * @return this returns the string to use for default values
 179  
     */
 180  
    public Object getEmpty(Context context) throws Exception {
 181  191
       Type map = new ClassType(type);
 182  191
       Factory factory = new MapFactory(context, map);
 183  
       
 184  191
       if(!label.empty()) {
 185  3
          return factory.getInstance();
 186  
       }
 187  188
       return null;
 188  
    }
 189  
    
 190  
    /**
 191  
     * This is used to acquire the dependent type for the annotated
 192  
     * list. This will simply return the type that the map object is
 193  
     * composed to hold. This must be a serializable type, that is,
 194  
     * a type that is annotated with the <code>Root</code> class.
 195  
     * 
 196  
     * @return this returns the component type for the map object
 197  
     */
 198  
    public Type getDependent() throws Exception  {
 199  818
       Contact contact = getContact();
 200  
      
 201  818
       if(items == null) {
 202  412
          items = contact.getDependents();
 203  
       }        
 204  818
       if(items == null) {
 205  0
          throw new ElementException("Unable to determine type for %s", contact);           
 206  
       }    
 207  818
       if(items.length == 0) {
 208  2
          return new ClassType(Object.class);
 209  
       }
 210  816
       return new ClassType(items[0]);
 211  
    }
 212  
    
 213  
    /**
 214  
     * This is used to either provide the entry value provided within
 215  
     * the annotation or compute a entry value. If the entry string
 216  
     * is not provided the the entry value is calculated as the type
 217  
     * of primitive the object is as a simplified class name.
 218  
     * 
 219  
     * @return this returns the name of the XML entry element used 
 220  
     */
 221  
    public String getEntry() throws Exception {      
 222  627
       Style style = format.getStyle();
 223  
       
 224  627
       if(detail.isEmpty(parent)) {
 225  412
          parent = detail.getEntry();
 226  
       }
 227  627
       return style.getElement(parent);
 228  
    }
 229  
    
 230  
    /**
 231  
     * This is used to acquire the name of the element or attribute
 232  
     * that is used by the class schema. The name is determined by
 233  
     * checking for an override within the annotation. If it contains
 234  
     * a name then that is used, if however the annotation does not
 235  
     * specify a name the the field or method name is used instead.
 236  
     * 
 237  
     * @return returns the name that is used for the XML property
 238  
     */
 239  
    public String getName() throws Exception{
 240  1494
       if(name == null) {
 241  413
          Style style = format.getStyle();
 242  413
          String value = entry.getEntry();
 243  
          
 244  413
          if(!label.inline()) {
 245  221
             value = detail.getName();
 246  
          }
 247  413
          name = style.getElement(value);
 248  
       }
 249  1494
       return name;
 250  
    }
 251  
    
 252  
    /**
 253  
     * This is used to acquire the path of the element or attribute
 254  
     * that is used by the class schema. The path is determined by
 255  
     * acquiring the XPath expression and appending the name of the
 256  
     * label to form a fully qualified path.
 257  
     * 
 258  
     * @return returns the path that is used for the XML property
 259  
     */
 260  
    public String getPath() throws Exception {
 261  1452
       if(path == null) {
 262  413
          Expression expression = getExpression();
 263  413
          String name = getName();
 264  
          
 265  413
          path = expression.getElement(name);  
 266  
       }
 267  1452
       return path;
 268  
    }
 269  
    
 270  
    /**
 271  
     * This method is used to return an XPath expression that is 
 272  
     * used to represent the position of this label. If there is no
 273  
     * XPath expression associated with this then an empty path is
 274  
     * returned. This will never return a null expression.
 275  
     * 
 276  
     * @return the XPath expression identifying the location
 277  
     */
 278  
    public Expression getExpression() throws Exception {
 279  826
       if(cache == null) {
 280  413
          cache = detail.getExpression();
 281  
       }
 282  826
       return cache;
 283  
    }
 284  
    
 285  
    /**
 286  
     * This returns the map type for this label. The primary type
 287  
     * is the type of the <code>Map</code> that this creates. The key
 288  
     * and value types are the types used to populate the primary.
 289  
     * 
 290  
     * @return this returns the map type to use for the label
 291  
     */
 292  
    private Type getMap() {
 293  662
       return new ClassType(type);
 294  
    }
 295  
    
 296  
    /**
 297  
     * This acquires the annotation associated with this label. This
 298  
     * is typically the annotation acquired from the field or method.
 299  
     * However, in the case of unions this will return the actual
 300  
     * annotation within the union group that this represents.
 301  
     * 
 302  
     * @return this returns the annotation that this represents
 303  
     */
 304  
    public Annotation getAnnotation() {
 305  406
       return label;
 306  
    }
 307  
    
 308  
    /**
 309  
     * This acts as a convenience method used to determine the type of
 310  
     * contact this represents. This is used when an object is written
 311  
     * to XML. It determines whether a <code>class</code> attribute
 312  
     * is required within the serialized XML element, that is, if the
 313  
     * class returned by this is different from the actual value of the
 314  
     * object to be serialized then that type needs to be remembered.
 315  
     *  
 316  
     * @return this returns the type of the contact class
 317  
     */ 
 318  
    public Class getType() {
 319  413
       return type;      
 320  
    }
 321  
    
 322  
    /**
 323  
     * This is used to acquire the contact object for this label. The 
 324  
     * contact retrieved can be used to set any object or primitive that
 325  
     * has been deserialized, and can also be used to acquire values to
 326  
     * be serialized in the case of object persistence. All contacts 
 327  
     * that are retrieved from this method will be accessible. 
 328  
     * 
 329  
     * @return returns the contact that this label is representing
 330  
     */
 331  
    public Contact getContact() {
 332  1452
       return detail.getContact();
 333  
    }
 334  
    
 335  
    /**
 336  
     * This is used to acquire the name of the element or attribute
 337  
     * as taken from the annotation. If the element or attribute
 338  
     * explicitly specifies a name then that name is used for the
 339  
     * XML element or attribute used. If however no overriding name
 340  
     * is provided then the method or field is used for the name. 
 341  
     * 
 342  
     * @return returns the name of the annotation for the contact
 343  
     */
 344  
    public String getOverride() {
 345  627
       return override;
 346  
    }
 347  
    
 348  
    /**
 349  
     * This is used to determine whether the annotation requires it
 350  
     * and its children to be written as a CDATA block. This is done
 351  
     * when a primitive or other such element requires a text value
 352  
     * and that value needs to be encapsulated within a CDATA block.
 353  
     * 
 354  
     * @return currently the element list does not require CDATA
 355  
     */
 356  
    public boolean isData() {
 357  728
       return data;
 358  
    }
 359  
    
 360  
    /**
 361  
     * This is used to determine if the label is a collection. If the
 362  
     * label represents a collection then any original assignment to
 363  
     * the field or method can be written to without the need to 
 364  
     * create a new collection. This allows obscure collections to be
 365  
     * used and also allows initial entries to be maintained.
 366  
     * 
 367  
     * @return true if the label represents a collection value
 368  
     */
 369  
    public boolean isCollection() {
 370  406
       return true;
 371  
    }
 372  
    
 373  
    /**
 374  
     * This is used to determine whether the XML element is required. 
 375  
     * This ensures that if an XML element is missing from a document
 376  
     * that deserialization can continue. Also, in the process of
 377  
     * serialization, if a value is null it does not need to be 
 378  
     * written to the resulting XML document.
 379  
     * 
 380  
     * @return true if the label represents a some required data
 381  
     */   
 382  
    public boolean isRequired() {
 383  406
       return required;
 384  
    }
 385  
    
 386  
    /**
 387  
     * This is used to determine whether the list has been specified
 388  
     * as inline. If the list is inline then no overrides are needed
 389  
     * and the outer XML element for the list is not used.
 390  
     * 
 391  
     * @return this returns whether the annotation is inline
 392  
     */
 393  
    public boolean isInline() {
 394  1275
       return inline;
 395  
    }
 396  
    
 397  
    /**
 398  
     * This is used to describe the annotation and method or field
 399  
     * that this label represents. This is used to provide error
 400  
     * messages that can be used to debug issues that occur when
 401  
     * processing a method. This will provide enough information
 402  
     * such that the problem can be isolated correctly. 
 403  
     * 
 404  
     * @return this returns a string representation of the label
 405  
     */
 406  
    public String toString() {
 407  0
       return detail.toString();
 408  
    }   
 409  
 }