Coverage Report - org.simpleframework.xml.core.ElementLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementLabel
100%
45/45
100%
14/14
1.733
 
 1  
 /*
 2  
  * ElementLabel.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.lang.annotation.Annotation;
 22  
 
 23  
 import org.simpleframework.xml.Element;
 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>ElementLabel</code> represents a label that is used to
 30  
  * represent an XML element in a class schema. This element can be
 31  
  * used to convert an XML node into either a primitive value such as
 32  
  * a string or composite object value, which is itself a schema for
 33  
  * a section of XML within the XML document. 
 34  
  * 
 35  
  * @author Niall Gallagher
 36  
  * 
 37  
  *  @see org.simpleframework.xml.Element
 38  
  */
 39  
 class ElementLabel extends TemplateLabel {
 40  
    
 41  
    /**
 42  
     * This is the decorator that is associated with the element.
 43  
     */
 44  
    private Decorator decorator;
 45  
    
 46  
    /**
 47  
     * The contact that this element label represents.
 48  
     */
 49  
    private Introspector detail;   
 50  
    
 51  
    /**
 52  
     * This is a cache of the expression for this element.
 53  
     */
 54  
    private Expression cache;
 55  
    
 56  
    /**
 57  
     * References the annotation that was used by the field.
 58  
     */
 59  
    private Element label;
 60  
    
 61  
    /**
 62  
     * This is the format used to style this element label.
 63  
     */
 64  
    private Format format;
 65  
    
 66  
    /**
 67  
     * This is the name of the element for this label instance.
 68  
     */
 69  
    private String override;
 70  
    
 71  
    /**
 72  
     * This is the path of the XML element from the annotation.
 73  
     */
 74  
    private String path;
 75  
    
 76  
    /**
 77  
     * This is the name of the XML element from the annotation.
 78  
     */
 79  
    private String name;
 80  
    
 81  
    /**
 82  
     * This is the expected type that has been declared for this.
 83  
     */
 84  
    private Class expect;
 85  
    
 86  
    /**
 87  
     * This is the type of the class that the field references.
 88  
     */
 89  
    private Class type;
 90  
    
 91  
    /**
 92  
     * This is used to determine if the element is required.
 93  
     */
 94  
    private boolean required;
 95  
    
 96  
    /**
 97  
     * This is used to determine if the element is data.
 98  
     */
 99  
    private boolean data;
 100  
    
 101  
    /**
 102  
     * Constructor for the <code>ElementLabel</code> object. This is
 103  
     * used to create a label that can convert a XML node into a 
 104  
     * composite object or a primitive type from an XML element. 
 105  
     * 
 106  
     * @param contact this is the field that this label represents
 107  
     * @param label this is the annotation for the contact 
 108  
     * @param format this is the format used to style this element
 109  
     */
 110  3219
    public ElementLabel(Contact contact, Element label, Format format) {
 111  3219
       this.detail = new Introspector(contact, this, format);
 112  3219
       this.decorator = new Qualifier(contact);
 113  3219
       this.required = label.required();
 114  3219
       this.type = contact.getType();
 115  3219
       this.override = label.name();     
 116  3219
       this.expect = label.type();
 117  3219
       this.data = label.data();
 118  3219
       this.format = format;
 119  3219
       this.label = label; 
 120  3219
    }
 121  
    
 122  
    /**
 123  
     * This is used to acquire the <code>Decorator</code> for this.
 124  
     * A decorator is an object that adds various details to the
 125  
     * node without changing the overall structure of the node. For
 126  
     * example comments and namespaces can be added to the node with
 127  
     * a decorator as they do not affect the deserialization.
 128  
     * 
 129  
     * @return this returns the decorator associated with this
 130  
     */
 131  
    public Decorator getDecorator() throws Exception {
 132  400551
       return decorator;
 133  
    }
 134  
    
 135  
    /**
 136  
     * This is used to acquire the <code>Type</code> that the type
 137  
     * provided is represented by. Typically this will return the
 138  
     * field or method represented by the label. However, in the 
 139  
     * case of unions this will provide an override type.
 140  
     * 
 141  
     * @param type this is the class to acquire the type for
 142  
     * 
 143  
     * @return this returns the type represented by this class
 144  
     */
 145  
    public Type getType(Class type){
 146  397736
       Type contact = getContact();
 147  
       
 148  397736
       if(expect == void.class) {
 149  397682
          return contact;
 150  
       }
 151  54
       return new OverrideType(contact, expect);
 152  
    }
 153  
    
 154  
    /**
 155  
     * Creates a converter that can be used to transform an XML node to
 156  
     * an object and vice versa. The converter created will handles
 157  
     * only XML elements and requires the context object to be provided. 
 158  
     * 
 159  
     * @param context this is the context object used for serialization
 160  
     * 
 161  
     * @return this returns a converter for serializing XML elements
 162  
     */
 163  
    public Converter getConverter(Context context) throws Exception {
 164  1688568
       Type type = getContact();
 165  
       
 166  1688568
       if(context.isPrimitive(type)) {
 167  1326541
          return new Primitive(context, type);
 168  
       }      
 169  362027
       if(expect == void.class) {
 170  361863
          return new Composite(context, type);
 171  
       }
 172  164
       return new Composite(context, type, expect);
 173  
    }
 174  
    
 175  
    /**
 176  
     * This is used to provide a configured empty value used when the
 177  
     * annotated value is null. This ensures that XML can be created
 178  
     * with required details regardless of whether values are null or
 179  
     * not. It also provides a means for sensible default values.
 180  
     * 
 181  
     * @param context this is the context object for the serialization
 182  
     * 
 183  
     * @return this returns the string to use for default values
 184  
     */
 185  
    public Object getEmpty(Context context) {
 186  1288593
       return null;
 187  
    }
 188  
    
 189  
    /**
 190  
     * This is used to acquire the name of the element or attribute
 191  
     * that is used by the class schema. The name is determined by
 192  
     * checking for an override within the annotation. If it contains
 193  
     * a name then that is used, if however the annotation does not
 194  
     * specify a name the the field or method name is used instead.
 195  
     * 
 196  
     * @return returns the name that is used for the XML property
 197  
     */
 198  
    public String getName() throws Exception{
 199  406759
       if(name == null) {
 200  3219
          Style style = format.getStyle();
 201  3219
          String value = detail.getName();
 202  
         
 203  3219
          name = style.getElement(value);
 204  
       }
 205  406759
       return name;
 206  
    }
 207  
    
 208  
    /**
 209  
     * This is used to acquire the path of the element or attribute
 210  
     * that is used by the class schema. The path is determined by
 211  
     * acquiring the XPath expression and appending the name of the
 212  
     * label to form a fully qualified path.
 213  
     * 
 214  
     * @return returns the path that is used for the XML property
 215  
     */
 216  
    public String getPath() throws Exception {
 217  11352
       if(path == null) {
 218  3219
          Expression expression = getExpression();
 219  3219
          String name = getName();
 220  
          
 221  3219
          path = expression.getElement(name);  
 222  
       }
 223  11352
       return path;
 224  
    }
 225  
    
 226  
    /**
 227  
     * This method is used to return an XPath expression that is 
 228  
     * used to represent the position of this label. If there is no
 229  
     * XPath expression associated with this then an empty path is
 230  
     * returned. This will never return a null expression.
 231  
     * 
 232  
     * @return the XPath expression identifying the location
 233  
     */
 234  
    public Expression getExpression() throws Exception {
 235  6438
       if(cache == null) {
 236  3219
          cache = detail.getExpression();
 237  
       }
 238  6438
       return cache;
 239  
    }
 240  
    
 241  
    /**
 242  
     * This acquires the annotation associated with this label. This
 243  
     * is typically the annotation acquired from the field or method.
 244  
     * However, in the case of unions this will return the actual
 245  
     * annotation within the union group that this represents.
 246  
     * 
 247  
     * @return this returns the annotation that this represents
 248  
     */
 249  
    public Annotation getAnnotation() {
 250  2815
       return label;
 251  
    }
 252  
    
 253  
    /**
 254  
     * This is used to acquire the contact object for this label. The 
 255  
     * contact retrieved can be used to set any object or primitive that
 256  
     * has been deserialized, and can also be used to acquire values to
 257  
     * be serialized in the case of object persistence. All contacts 
 258  
     * that are retrieved from this method will be accessible. 
 259  
     * 
 260  
     * @return returns the contact that this label is representing
 261  
     */
 262  
    public Contact getContact() {
 263  2088889
       return detail.getContact();
 264  
    }
 265  
    
 266  
    /**
 267  
     * This is used to acquire the name of the element or attribute
 268  
     * as taken from the annotation. If the element or attribute
 269  
     * explicitly specifies a name then that name is used for the
 270  
     * XML element or attribute used. If however no overriding name
 271  
     * is provided then the method or field is used for the name. 
 272  
     * 
 273  
     * @return returns the name of the annotation for the contact
 274  
     */
 275  
    public String getOverride() {
 276  6034
       return override;
 277  
    }
 278  
    
 279  
    /**
 280  
     * This acts as a convenience method used to determine the type of
 281  
     * contact this represents. This is used when an object is written
 282  
     * to XML. It determines whether a <code>class</code> attribute
 283  
     * is required within the serialized XML element, that is, if the
 284  
     * class returned by this is different from the actual value of the
 285  
     * object to be serialized then that type needs to be remembered.
 286  
     *  
 287  
     * @return this returns the type of the contact class
 288  
     */  
 289  
    public Class getType() {
 290  3219
       if(expect == void.class) {
 291  2676
          return type;
 292  
       }
 293  543
       return expect;
 294  
    }
 295  
    
 296  
    /**
 297  
     * This is used to determine whether the XML element is required. 
 298  
     * This ensures that if an XML element is missing from a document
 299  
     * that deserialization can continue. Also, in the process of
 300  
     * serialization, if a value is null it does not need to be 
 301  
     * written to the resulting XML document.
 302  
     * 
 303  
     * @return true if the label represents a some required data
 304  
     */   
 305  
    public boolean isRequired() {
 306  2815
       return required;
 307  
    }
 308  
    
 309  
    /**
 310  
     * This is used to determine whether the annotation requires it
 311  
     * and its children to be written as a CDATA block. This is done
 312  
     * when a primitive or other such element requires a text value
 313  
     * and that value needs to be encapsulated within a CDATA block.
 314  
     * 
 315  
     * @return this returns true if the element requires CDATA
 316  
     */
 317  
    public boolean isData() {
 318  398353
       return data;
 319  
    }
 320  
    
 321  
    /**
 322  
     * This is used to describe the annotation and method or field
 323  
     * that this label represents. This is used to provide error
 324  
     * messages that can be used to debug issues that occur when
 325  
     * processing a method. This will provide enough information
 326  
     * such that the problem can be isolated correctly. 
 327  
     * 
 328  
     * @return this returns a string representation of the label
 329  
     */
 330  
    public String toString() {
 331  11
       return detail.toString();
 332  
    }
 333  
 }