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