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