Coverage Report - org.simpleframework.xml.core.CacheLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
CacheLabel
96%
48/50
N/A
1
 
 1  
 /*
 2  
  * CacheLabel.java July 2007
 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.strategy.Type;
 24  
 
 25  
 /**
 26  
  * The <code>CacheLabel</code> object is used to acquire details from an
 27  
  * inner label object so that details can be retrieved repeatedly without
 28  
  * the need to perform any logic for extracting the values. This ensures
 29  
  * that a class XML schema requires only initial processing the first
 30  
  * time the class XML schema is required. 
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  */
 34  
 class CacheLabel implements Label {
 35  
    
 36  
    /**
 37  
     * This is the annotation that this label instance represents.
 38  
     */
 39  
    private final Annotation annotation;
 40  
    
 41  
    /**
 42  
     * This is the expression that is used to represent this label.
 43  
     */
 44  
    private final Expression expression;
 45  
    
 46  
    /**
 47  
     * This is the decorator that is associated with the label.
 48  
     */
 49  
    private final Decorator decorator;
 50  
    
 51  
    /**
 52  
     * This is the contact used to set and get the value for the node.
 53  
     */
 54  
    private final Contact contact;
 55  
    
 56  
    /**
 57  
     * This represents the names that identify this label instance.
 58  
     */
 59  
    private final String[] names;
 60  
    
 61  
    /**
 62  
     * This represents the paths that identify this label instance.
 63  
     */
 64  
    private final String[] paths;
 65  
    
 66  
    /**
 67  
     * This is used to represent the label class that this will use.
 68  
     */
 69  
    private final Class type;
 70  
    
 71  
    /**
 72  
     * This is used to represent the name of the entry item use.
 73  
     */
 74  
    private final String entry;
 75  
    
 76  
    /**
 77  
     * This represents the path location within the XML for this.
 78  
     */
 79  
    private final String path;
 80  
    
 81  
    /**
 82  
     * This is used to represent the name override for the annotation.
 83  
     */
 84  
    private final String override;
 85  
    
 86  
    /**
 87  
     * This is used to represent the name of the annotated element.
 88  
     */
 89  
    private final String name;
 90  
    
 91  
    /**
 92  
     * This is the label the this cache is wrapping the values for.
 93  
     */
 94  
    private final Label label;
 95  
    
 96  
    /**
 97  
     * This is the key is used to represent this label object.
 98  
     */
 99  
    private final Object key;
 100  
    
 101  
    /**
 102  
     * This is used to represent the dependent type to be used.
 103  
     */
 104  
    private final Type depend;
 105  
    
 106  
    /**
 107  
     * This is used to represent whether the data is written as data. 
 108  
     */
 109  
    private final boolean data;
 110  
    
 111  
    /**
 112  
     * This is used to determine if this label represents text.
 113  
     */
 114  
    private final boolean text;
 115  
    
 116  
    /**
 117  
     * This is used to determine if the label represents a union.
 118  
     */
 119  
    private final boolean union;
 120  
    
 121  
    /**
 122  
     * This is used to determine the styling of the label name.
 123  
     */
 124  
    private final boolean attribute;
 125  
    
 126  
    /**
 127  
     * This is used to represent whether the entity is required or not.
 128  
     */
 129  
    private final boolean required;
 130  
    
 131  
    /**
 132  
     * This is used to determine if the label represents a collection.
 133  
     */
 134  
    private final boolean collection;
 135  
    
 136  
    /**
 137  
     * This is used to determine whether the entity is inline or not. 
 138  
     */
 139  
    private final boolean inline;
 140  
    
 141  
    /**
 142  
     * This is used to determine if the label represents a text list.
 143  
     */
 144  
    private final boolean list;
 145  
    
 146  
    /**
 147  
     * Constructor for the <code>CacheLabel</code> object. This is used
 148  
     * to create a <code>Label</code> that acquires details from another
 149  
     * label in such a way that any logic involved in acquiring details
 150  
     * is performed only once.
 151  
     * 
 152  
     * @param label this is the label to acquire the details from  
 153  
     */
 154  5449
    public CacheLabel(Label label) throws Exception { 
 155  5449
       this.annotation = label.getAnnotation();
 156  5449
       this.expression = label.getExpression();
 157  5449
       this.decorator = label.getDecorator();
 158  5449
       this.attribute = label.isAttribute();
 159  5449
       this.collection = label.isCollection();
 160  5449
       this.contact = label.getContact();
 161  5449
       this.depend = label.getDependent();
 162  5449
       this.required = label.isRequired();
 163  5449
       this.override = label.getOverride();
 164  5449
       this.list = label.isTextList();
 165  5449
       this.inline = label.isInline();
 166  5449
       this.union = label.isUnion();
 167  5449
       this.names = label.getNames();
 168  5449
       this.paths = label.getPaths();
 169  5449
       this.path = label.getPath();
 170  5449
       this.type = label.getType();
 171  5449
       this.name = label.getName();
 172  5449
       this.entry = label.getEntry();
 173  5449
       this.data = label.isData();
 174  5449
       this.text = label.isText();
 175  5449
       this.key = label.getKey();
 176  5449
       this.label = label;
 177  5449
    }
 178  
    
 179  
    /**
 180  
     * This is used to acquire the <code>Type</code> that the type
 181  
     * provided is represented by. Typically this will return the
 182  
     * field or method represented by the label. However, in the 
 183  
     * case of unions this will provide an override type.
 184  
     * 
 185  
     * @param type this is the class to acquire the type for
 186  
     * 
 187  
     * @return this returns the type represented by this class
 188  
     */
 189  
    public Type getType(Class type) throws Exception {
 190  420110
       return label.getType(type);
 191  
    }
 192  
    
 193  
    /**
 194  
     * This is used to acquire the <code>Label</code> that the type
 195  
     * provided is represented by. Typically this will return the
 196  
     * same instance. However, in the case of unions this will
 197  
     * look for an individual label to match the type provided.
 198  
     * 
 199  
     * @param type this is the type to acquire the label for
 200  
     * 
 201  
     * @return this returns the label represented by this type
 202  
     */
 203  
    public Label getLabel(Class type) throws Exception {
 204  420111
       return label.getLabel(type);
 205  
    }
 206  
    
 207  
    /**
 208  
     * This returns a <code>Collection</code> of element names. This
 209  
     * will typically contain both the name and path of the label. 
 210  
     * However, if this is a union it can contain many names and
 211  
     * paths. This method should never return null. 
 212  
     * 
 213  
     * @return this returns the names of each of the elements
 214  
     */
 215  
    public String[] getNames() throws Exception {
 216  973
       return names;
 217  
    }
 218  
    
 219  
    /**
 220  
     * This returns a <code>Collection</code> of element paths. This
 221  
     * will typically contain only the path of the label, which is
 222  
     * composed using the <code>Path</code> annotation and the name
 223  
     * of the label. However, if this is a union it can contain many 
 224  
     * paths. This method should never return null.
 225  
     * 
 226  
     * @return this returns the names of each of the elements
 227  
     */
 228  
    public String[] getPaths() throws Exception {
 229  3864397
       return paths;
 230  
    }
 231  
    
 232  
    /**
 233  
     * This acquires the annotation associated with this label. This
 234  
     * is typically the annotation acquired from the field or method.
 235  
     * However, in the case of unions this will return the actual
 236  
     * annotation within the union group that this represents.
 237  
     * 
 238  
     * @return this returns the annotation that this represents
 239  
     */
 240  
    public Annotation getAnnotation(){
 241  973
       return annotation;
 242  
    }
 243  
    /**
 244  
     * This is used to acquire the contact object for this label. The 
 245  
     * contact retrieved can be used to set any object or primitive that
 246  
     * has been deserialized, and can also be used to acquire values to
 247  
     * be serialized in the case of object persistence. All contacts 
 248  
     * that are retrieved from this method will be accessible. 
 249  
     * 
 250  
     * @return returns the field that this label is representing
 251  
     */
 252  
    public Contact getContact() {
 253  2782869
       return contact;
 254  
    }
 255  
    
 256  
    /**
 257  
     * This is used to acquire the <code>Decorator</code> for this.
 258  
     * A decorator is an object that adds various details to the
 259  
     * node without changing the overall structure of the node. For
 260  
     * example comments and namespaces can be added to the node with
 261  
     * a decorator as they do not affect the deserialization.
 262  
     * 
 263  
     * @return this returns the decorator associated with this
 264  
     */
 265  
    public Decorator getDecorator() throws Exception {
 266  208992
       return decorator;
 267  
    }
 268  
    
 269  
    /**
 270  
     * This method returns a <code>Converter</code> which can be used to
 271  
     * convert an XML node into an object value and vice versa. The 
 272  
     * converter requires only the context object in order to perform
 273  
     * serialization or deserialization of the provided XML node.
 274  
     * 
 275  
     * @param context this is the context object for the serialization
 276  
     * 
 277  
     * @return this returns an object that is used for conversion
 278  
     */
 279  
    public Converter getConverter(Context context) throws Exception {
 280  2074918
       return label.getConverter(context);
 281  
    }
 282  
      
 283  
    /**
 284  
     * This is used to provide a configured empty value used when the
 285  
     * annotated value is null. This ensures that XML can be created
 286  
     * with required details regardless of whether values are null or
 287  
     * not. It also provides a means for sensible default values.
 288  
     * 
 289  
     * @param context this is the context object for the serialization
 290  
     * 
 291  
     * @return this returns the string to use for default values
 292  
     */
 293  
    public Object getEmpty(Context context) throws Exception {
 294  2068567
       return label.getEmpty(context);
 295  
    }
 296  
    
 297  
    /**
 298  
     * This returns the dependent type for the annotation. This type
 299  
     * is the type other than the annotated field or method type that
 300  
     * the label depends on. For the <code>ElementList</code> and 
 301  
     * the <code>ElementArray</code> this is the component type that
 302  
     * is deserialized individually and inserted into the container. 
 303  
     * 
 304  
     * @return this is the type that the annotation depends on
 305  
     */
 306  
    public Type getDependent() throws Exception {
 307  26
       return depend;
 308  
    }
 309  
    
 310  
    /**
 311  
     * This is the key used to represent this label. The key is used
 312  
     * to store the parameter in hash containers. Typically the
 313  
     * key is generated from the paths associated with the label.
 314  
     * 
 315  
     * @return this is the key used to represent the label
 316  
     */
 317  
    public Object getKey() throws Exception {
 318  2993985
       return key;
 319  
    }
 320  
    
 321  
    /**
 322  
     * This is used to either provide the entry value provided within
 323  
     * the annotation or compute a entry value. If the entry string
 324  
     * is not provided the the entry value is calculated as the type
 325  
     * of primitive the object is as a simplified class name.
 326  
     * 
 327  
     * @return this returns the name of the XML entry element used 
 328  
     */
 329  
    public String getEntry() throws Exception {
 330  16
       return entry;
 331  
    }
 332  
    
 333  
    /**
 334  
     * This is used to acquire the name of the element or attribute
 335  
     * that is used by the class schema. The name is determined by
 336  
     * checking for an override within the annotation. If it contains
 337  
     * a name then that is used, if however the annotation does not
 338  
     * specify a name the the field or method name is used instead.
 339  
     * 
 340  
     * @return returns the name that is used for the XML property
 341  
     */
 342  
    public String getName() throws Exception {
 343  223859
       return name;
 344  
    }
 345  
    
 346  
    /**
 347  
     * This is used to acquire the path of the element or attribute
 348  
     * that is used by the class schema. The path is determined by
 349  
     * acquiring the XPath expression and appending the name of the
 350  
     * label to form a fully qualified path.
 351  
     * 
 352  
     * @return returns the path that is used for the XML property
 353  
     */
 354  
    public String getPath() throws Exception {
 355  2720941
       return path;
 356  
    }
 357  
    
 358  
    /**
 359  
     * This method is used to return an XPath expression that is 
 360  
     * used to represent the position of this label. If there is no
 361  
     * XPath expression associated with this then an empty path is
 362  
     * returned. This will never return a null expression.
 363  
     * 
 364  
     * @return the XPath expression identifying the location
 365  
     */
 366  
    public Expression getExpression() throws Exception {
 367  7862
       return expression;
 368  
    }
 369  
    
 370  
    /**
 371  
     * This is used to acquire the name of the element or attribute
 372  
     * as taken from the annotation. If the element or attribute
 373  
     * explicitly specifies a name then that name is used for the
 374  
     * XML element or attribute used. If however no overriding name
 375  
     * is provided then the method or field is used for the name. 
 376  
     * 
 377  
     * @return returns the name of the annotation for the contact
 378  
     */
 379  
    public String getOverride() {
 380  0
       return override;
 381  
    }
 382  
    
 383  
    /**
 384  
     * This acts as a convenience method used to determine the type of
 385  
     * the field this represents. This is used when an object is written
 386  
     * to XML. It determines whether a <code>class</code> attribute
 387  
     * is required within the serialized XML element, that is, if the
 388  
     * class returned by this is different from the actual value of the
 389  
     * object to be serialized then that type needs to be remembered.
 390  
     *  
 391  
     * @return this returns the type of the field class
 392  
     */
 393  
    public Class getType() {
 394  14
       return type;
 395  
    }
 396  
    
 397  
    /**
 398  
     * This is used to determine whether the annotation requires it
 399  
     * and its children to be written as a CDATA block. This is done
 400  
     * when a primitive or other such element requires a text value
 401  
     * and that value needs to be encapsulated within a CDATA block.
 402  
     * 
 403  
     * @return this returns true if the element requires CDATA
 404  
     */
 405  
    public boolean isData() {
 406  516
       return data;
 407  
    }
 408  
    
 409  
    /**
 410  
     * This is used to determine if the label represents text. If
 411  
     * a label represents text it typically does not have a name,
 412  
     * instead the empty string represents the name. Also text
 413  
     * labels can not exist with other text labels, or elements.
 414  
     * 
 415  
     * @return this returns true if this label represents text
 416  
     */
 417  
    public boolean isText() {
 418  6146
       return text;
 419  
    }
 420  
    
 421  
    /**
 422  
     * This is used to determine if an annotated list is a text 
 423  
     * list. A text list is a list of elements that also accepts
 424  
     * free text. Typically this will be an element list union that
 425  
     * will allow unstructured XML such as XHTML to be parsed.
 426  
     * 
 427  
     * @return returns true if the label represents a text list
 428  
     */
 429  
    public boolean isTextList() {
 430  3581
       return list;
 431  
    }
 432  
    
 433  
    /**
 434  
     * This is used to determine whether the label represents an
 435  
     * inline XML entity. The <code>ElementList</code> annotation
 436  
     * and the <code>Text</code> annotation represent inline 
 437  
     * items. This means that they contain no containing element
 438  
     * and so can not specify overrides or special attributes.
 439  
     * 
 440  
     * @return this returns true if the annotation is inline
 441  
     */
 442  
    public boolean isInline() {
 443  1375029
       return inline;
 444  
    }
 445  
    
 446  
    /**
 447  
     * This method is used to determine if the label represents an
 448  
     * attribute. This is used to style the name so that elements
 449  
     * are styled as elements and attributes are styled as required.
 450  
     * 
 451  
     * @return this is used to determine if this is an attribute
 452  
     */
 453  
    public boolean isAttribute() {
 454  7910
       return attribute;
 455  
    }
 456  
    
 457  
    /**
 458  
     * This is used to determine if the label is a collection. If the
 459  
     * label represents a collection then any original assignment to
 460  
     * the field or method can be written to without the need to 
 461  
     * create a new collection. This allows obscure collections to be
 462  
     * used and also allows initial entries to be maintained.
 463  
     * 
 464  
     * @return true if the label represents a collection value
 465  
     */
 466  
    public boolean isCollection() {
 467  2068503
       return collection;
 468  
    }
 469  
    
 470  
    /**
 471  
     * Determines whether the XML attribute or element is required. 
 472  
     * This ensures that if an XML element is missing from a document
 473  
     * that deserialization can continue. Also, in the process of
 474  
     * serialization, if a value is null it does not need to be 
 475  
     * written to the resulting XML document.
 476  
     * 
 477  
     * @return true if the label represents a some required data
 478  
     */
 479  
    public boolean isRequired() {
 480  8801
       return required;
 481  
    }
 482  
    
 483  
    /**
 484  
     * This is used to determine if this label is a union. If this
 485  
     * is true then this label represents a number of labels and
 486  
     * is simply a wrapper for these labels. 
 487  
     * 
 488  
     * @return this returns true if the label represents a union
 489  
     */
 490  
    public boolean isUnion() {
 491  0
       return union;
 492  
    }
 493  
    
 494  
    /**
 495  
     * This is used to describe the annotation and method or field
 496  
     * that this label represents. This is used to provide error
 497  
     * messages that can be used to debug issues that occur when
 498  
     * processing a method. This should provide enough information
 499  
     * such that the problem can be isolated correctly. 
 500  
     * 
 501  
     * @return this returns a string representation of the label
 502  
     */
 503  
    public String toString() {
 504  30
       return label.toString();
 505  
    }
 506  
 }