Coverage Report - org.simpleframework.xml.core.AttributeLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeLabel
100%
35/35
100%
4/4
1.2
 
 1  
 /*
 2  
  * AttributeLabel.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.Attribute;
 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>AttributeLabel</code> object is used convert any value
 30  
  * retrieved from an XML attribute to a primitive object. This is
 31  
  * also used to convert from a primitive object to an XML attribute
 32  
  * using the <code>String.valueOf</code> method. 
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  */
 36  706941
 class AttributeLabel extends TemplateLabel {
 37  
    
 38  
    /**
 39  
     * This is the decorator that is associated with the attribute.
 40  
     */
 41  
    private Decorator decorator;
 42  
    
 43  
    /**
 44  
     * This contains the details of the annotated contact object.
 45  
     */
 46  
    private Introspector detail;
 47  
    
 48  
    /**
 49  
     * This is the path that is used to represent this attribute.
 50  
     */
 51  
    private Expression path;
 52  
 
 53  
    /**
 54  
     * Represents the annotation used to label the field.
 55  
     */
 56  
    private Attribute label;
 57  
    
 58  
    /**
 59  
     * This is the format that is used to style the path.
 60  
     */
 61  
    private Format format;
 62  
    
 63  
    /**
 64  
     * This is the type that the field object references. 
 65  
     */
 66  
    private Class type;
 67  
    
 68  
    /**
 69  
     * This is the name of the element for this label instance.
 70  
     */
 71  
    private String name;
 72  
    
 73  
    /**
 74  
     * This is the default value to use if the real value is null.
 75  
     */
 76  
    private String empty;
 77  
    
 78  
    /**
 79  
     * This is used to determine if the attribute is required.
 80  
     */
 81  
    private boolean required;
 82  
    
 83  
    /**
 84  
     * Constructor for the <code>AttributeLabel</code> object. This 
 85  
     * is used to create a label that can convert from an object to an
 86  
     * XML attribute and vice versa. This requires the annotation and
 87  
     * contact extracted from the XML schema class.
 88  
     * 
 89  
     * @param contact this is the field from the XML schema class
 90  
     * @param label represents the annotation for the field
 91  
     * @param format this is the format used to style the path
 92  
     */
 93  978
    public AttributeLabel(Contact contact, Attribute label, Format format) {
 94  978
       this.detail = new Introspector(contact, this, format);
 95  978
       this.decorator = new Qualifier(contact);
 96  978
       this.required = label.required();
 97  978
       this.type = contact.getType();
 98  978
       this.empty = label.empty();
 99  978
       this.name = label.name();      
 100  978
       this.format = format;
 101  978
       this.label = label; 
 102  978
    }  
 103  
    
 104  
    /**
 105  
     * This is used to acquire the <code>Decorator</code> for this.
 106  
     * A decorator is an object that adds various details to the
 107  
     * node without changing the overall structure of the node. For
 108  
     * example comments and namespaces can be added to the node with
 109  
     * a decorator as they do not affect the deserialization.
 110  
     * 
 111  
     * @return this returns the decorator associated with this
 112  
     */
 113  
    public Decorator getDecorator() throws Exception {
 114  858
       return decorator;
 115  
    }
 116  
    
 117  
    /**
 118  
     * Creates a <code>Converter</code> that can convert an attribute
 119  
     * to a primitive object. This requires the context object used
 120  
     * for the current instance of XML serialization being performed.
 121  
     * 
 122  
     * @param context this is context object used for serialization
 123  
     */
 124  
    public Converter getConverter(Context context) throws Exception {
 125  707713
       String ignore = getEmpty(context);
 126  707713
       Type type = getContact();
 127  
       
 128  707713
       return new Primitive(context, type, ignore);
 129  
    }
 130  
    
 131  
    /**
 132  
     * This is used to provide a configured empty value used when the
 133  
     * annotated value is null. This ensures that XML can be created
 134  
     * with required details regardless of whether values are null or
 135  
     * not. It also provides a means for sensible default values.
 136  
     * 
 137  
     * @param context this is the context object for the serialization
 138  
     * 
 139  
     * @return this returns the string to use for default values
 140  
     */
 141  
    public String getEmpty(Context context) {
 142  1414654
       if(detail.isEmpty(empty)) {
 143  1414613
          return null;
 144  
       }
 145  41
       return empty;
 146  
    }
 147  
    
 148  
    /**
 149  
     * This is used to acquire the name of the element or attribute
 150  
     * that is used by the class schema. The name is determined by
 151  
     * checking for an override within the annotation. If it contains
 152  
     * a name then that is used, if however the annotation does not
 153  
     * specify a name the the field or method name is used instead.
 154  
     *
 155  
     * @return returns the name that is used for the XML property
 156  
     */
 157  
    public String getName() throws Exception {
 158  5508
       Style style = format.getStyle();
 159  5508
       String name = detail.getName();
 160  
       
 161  5508
       return style.getAttribute(name);
 162  
    }
 163  
    
 164  
    /**
 165  
     * This is used to acquire the path of the element or attribute
 166  
     * that is used by the class schema. The path is determined by
 167  
     * acquiring the XPath expression and appending the name of the
 168  
     * label to form a fully qualified path.
 169  
     * 
 170  
     * @return returns the path that is used for the XML property
 171  
     */
 172  
    public String getPath() throws Exception {
 173  3672
       Expression path = getExpression();
 174  3672
       String name = getName();
 175  
 
 176  3672
       return path.getAttribute(name); 
 177  
    }
 178  
    
 179  
    /**
 180  
     * This method is used to return an XPath expression that is 
 181  
     * used to represent the position of this label. If there is no
 182  
     * XPath expression associated with this then an empty path is
 183  
     * returned. This will never return a null expression.
 184  
     * 
 185  
     * @return the XPath expression identifying the location
 186  
     */
 187  
    public Expression getExpression() throws Exception {
 188  4650
       if(path == null) {
 189  978
          path = detail.getExpression();
 190  
       }
 191  4650
       return path;
 192  
    }
 193  
    
 194  
    /**
 195  
     * This acquires the annotation associated with this label. This
 196  
     * is typically the annotation acquired from the field or method.
 197  
     * However, in the case of unions this will return the actual
 198  
     * annotation within the union group that this represents.
 199  
     * 
 200  
     * @return this returns the annotation that this represents
 201  
     */
 202  
    public Annotation getAnnotation() {
 203  858
       return label;
 204  
    }
 205  
    
 206  
    /**
 207  
     * This is used to acquire the name of the element or attribute
 208  
     * as taken from the annotation. If the element or attribute
 209  
     * explicitly specifies a name then that name is used for the
 210  
     * XML element or attribute used. If however no overriding name
 211  
     * is provided then the method or field is used for the name. 
 212  
     * 
 213  
     * @return returns the name of the annotation for the contact
 214  
     */
 215  
    public String getOverride(){
 216  6366
       return name;
 217  
    }
 218  
    
 219  
    /**
 220  
     * This is used to acquire the contact object for this label. The 
 221  
     * contact retrieved can be used to set any object or primitive that
 222  
     * has been deserialized, and can also be used to acquire values to
 223  
     * be serialized in the case of object persistance. All contacts
 224  
     * that are retrieved from this method will be accessible. 
 225  
     * 
 226  
     * @return returns the contact that this label is representing
 227  
     */   
 228  
    public Contact getContact() {
 229  708571
       return detail.getContact();
 230  
    }
 231  
    
 232  
    /**
 233  
     * This acts as a convenience method used to determine the type of
 234  
     * the contact this represents. This will be a primitive type of a
 235  
     * primitive type from the <code>java.lang</code> primitives.
 236  
     * 
 237  
     * @return this returns the type of the contact class
 238  
     */  
 239  
    public Class getType() {
 240  978
       return type;
 241  
    }
 242  
    
 243  
    /**
 244  
     * This method is used to determine if the label represents an
 245  
     * attribute. This is used to style the name so that elements
 246  
     * are styled as elements and attributes are styled as required.
 247  
     * 
 248  
     * @return this is used to determine if this is an attribute
 249  
     */
 250  
    public boolean isAttribute() {
 251  1836
       return true;
 252  
    }
 253  
    
 254  
    /**
 255  
     * This is used to determine whether the attribute is required. 
 256  
     * This ensures that if an attribute is missing from a document
 257  
     * that deserialization can continue. Also, in the process of
 258  
     * serialization, if a value is null it does not need to be 
 259  
     * written to the resulting XML document.
 260  
     * 
 261  
     * @return true if the label represents a some required data
 262  
     */  
 263  
    public boolean isRequired() {
 264  858
       return required;
 265  
    }
 266  
    
 267  
    /**
 268  
     * Because the attribute can contain only simple text values it
 269  
     * is never required to specified as anything other than text.
 270  
     * Therefore this will always return false as CDATA does not
 271  
     * apply to the attribute values.
 272  
     *
 273  
     * @return this will always return false for XML attributes
 274  
     */
 275  
    public boolean isData() {
 276  858
       return false;
 277  
    }
 278  
    
 279  
    /**
 280  
     * This is used to describe the annotation and method or field
 281  
     * that this label represents. This is used to provide error
 282  
     * messages that can be used to debug issues that occur when
 283  
     * processing a method. This will provide enough information
 284  
     * such that the problem can be isolated correctly. 
 285  
     * 
 286  
     * @return this returns a string representation of the label
 287  
     */
 288  
    public String toString() {
 289  8
       return detail.toString();
 290  
    }
 291  
 }