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