Coverage Report - org.simpleframework.xml.core.TextListLabel
 
Classes in this File Line Coverage Branch Coverage Complexity
TextListLabel
33%
10/30
50%
1/2
1.091
 
 1  
 /*
 2  
  * TextListLabel.java December 2012
 3  
  *
 4  
  * Copyright (C) 2012, 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.Text;
 24  
 import org.simpleframework.xml.strategy.Type;
 25  
 
 26  
 /**
 27  
  * The <code>TextListLabel</code> object is used to create a label
 28  
  * that will create a converter used to read free text. Free text is
 29  
  * text that exists between elements. An <code>ElementListUnion</code>
 30  
  * can be declared to consume text that exists between the individual
 31  
  * elements by declaring a <code>Text</code> annotation with it. In 
 32  
  * such a declaration unstructured text can be processed.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.core.GroupExtractor
 37  
  */
 38  0
 class TextListLabel extends TemplateLabel {
 39  
    
 40  
    /**
 41  
     * This is the empty value that is declared for the annotation.
 42  
     */
 43  
    private final String empty;
 44  
    
 45  
    /**
 46  
     * This is the element list union label that is declared with this.
 47  
     */
 48  
    private final Label label;
 49  
    
 50  
    /**
 51  
     * This is the text annotation declaration on the element list.
 52  
     */
 53  
    private final Text text;
 54  
    
 55  
    /**
 56  
     * Constructor for the <code>TextListLabel</code> object. This is
 57  
     * used to create a label used for reading free text declared 
 58  
     * between the elements of an element list union. Such a label
 59  
     * enables serialization and deserialization of unstructured XML.
 60  
     * 
 61  
     * @param label this is the label that is declared with this
 62  
     * @param text this is the text annotation delcaration
 63  
     */
 64  84
    public TextListLabel(Label label, Text text) {
 65  84
       this.empty = text.empty();
 66  84
       this.label = label;
 67  84
       this.text = text;
 68  84
    }
 69  
 
 70  
    /**
 71  
     * This is used to acquire the <code>Decorator</code> for this.
 72  
     * A decorator is an object that adds various details to the
 73  
     * node without changing the overall structure of the node. For
 74  
     * example comments and namespaces can be added to the node with
 75  
     * a decorator as they do not affect the deserialization.
 76  
     * 
 77  
     * @return this returns the decorator associated with this
 78  
     */
 79  
    public Decorator getDecorator() throws Exception {
 80  0
       return null;
 81  
    }
 82  
 
 83  
    /**
 84  
     * This returns a <code>Collection</code> of element names. This
 85  
     * will typically contain both the name and path of the label. 
 86  
     * However, if this is a union it can contain many names and
 87  
     * paths. This method should never return null. 
 88  
     * 
 89  
     * @return this returns the names of each of the elements
 90  
     */
 91  
    public String[] getNames() throws Exception {
 92  0
       return label.getNames();
 93  
    }
 94  
 
 95  
    /**
 96  
     * This returns a <code>Collection</code> of element paths. This
 97  
     * will typically contain only the path of the label, which is
 98  
     * composed using the <code>Path</code> annotation and the name
 99  
     * of the label. However, if this is a union it can contain many 
 100  
     * paths. This method should never return null.
 101  
     * 
 102  
     * @return this returns the names of each of the elements
 103  
     */
 104  
    public String[] getPaths() throws Exception {
 105  0
       return label.getPaths();
 106  
    }
 107  
 
 108  
    /**
 109  
     * This is used to provide a configured empty value used when the
 110  
     * annotated value is null. This ensures that XML can be created
 111  
     * with required details regardless of whether values are null or
 112  
     * not. It also provides a means for sensible default values.
 113  
     * 
 114  
     * @param context this is the context object for the serialization
 115  
     * 
 116  
     * @return this returns the string to use for default values
 117  
     */
 118  
    public String getEmpty(Context context) throws Exception {
 119  0
       return empty;
 120  
    }
 121  
    
 122  
    /**
 123  
     * This method returns a <code>Converter</code> which can be used to
 124  
     * convert an XML node into an object value and vice versa. The 
 125  
     * converter requires only the context object in order to perform
 126  
     * serialization or deserialization of the provided XML node.
 127  
     * 
 128  
     * @param context this is the context object for the serialization
 129  
     * 
 130  
     * @return this returns an object that is used for conversion
 131  
     */
 132  
    public Converter getConverter(Context context) throws Exception {
 133  106
       Type type = getContact();
 134  
       
 135  106
       if(!label.isCollection()) {
 136  0
          throw new TextException("Cannot use %s to represent %s", type, label);
 137  
       }
 138  106
       return new TextList(context, type, label);
 139  
    }
 140  
 
 141  
    /**
 142  
     * This is used to acquire the name of the element or attribute
 143  
     * that is used by the class schema. The name is determined by
 144  
     * checking for an override within the annotation. If it contains
 145  
     * a name then that is used, if however the annotation does not
 146  
     * specify a name the the field or method name is used instead.
 147  
     * 
 148  
     * @return returns the name that is used for the XML property
 149  
     */
 150  
    public String getName() throws Exception {
 151  0
       return label.getName();
 152  
    }
 153  
 
 154  
    /**
 155  
     * This is used to acquire the path of the element or attribute
 156  
     * that is used by the class schema. The path is determined by
 157  
     * acquiring the XPath expression and appending the name of the
 158  
     * label to form a fully qualified path.
 159  
     * 
 160  
     * @return returns the path that is used for the XML property
 161  
     */
 162  
    public String getPath() throws Exception {
 163  0
       return label.getPath();
 164  
    }
 165  
 
 166  
    /**
 167  
     * This method is used to return an XPath expression that is 
 168  
     * used to represent the position of this label. If there is no
 169  
     * XPath expression associated with this then an empty path is
 170  
     * returned. This will never return a null expression.
 171  
     * 
 172  
     * @return the XPath expression identifying the location
 173  
     */
 174  
    public Expression getExpression() throws Exception {
 175  0
       return label.getExpression();
 176  
    }
 177  
 
 178  
    /**
 179  
     * This is used to acquire the dependent class for this label. 
 180  
     * This returns null as there are no dependents to the element
 181  
     * annotation as it can only hold primitives with no dependents.
 182  
     * 
 183  
     * @return this is used to return the dependent type of null
 184  
     */
 185  
    public Type getDependent() throws Exception {
 186  0
       return label.getDependent();
 187  
    }
 188  
 
 189  
    /**
 190  
     * This is typically used to acquire the entry value as acquired
 191  
     * from the annotation. However given that the annotation this
 192  
     * represents does not have a entry attribute this will always
 193  
     * provide a null value for the entry string.
 194  
     * 
 195  
     * @return this will always return null for the entry value 
 196  
     */
 197  
    public String getEntry() throws Exception {
 198  0
       return label.getEntry();
 199  
    }
 200  
 
 201  
    /**
 202  
     * This is the key used to represent this label. The key is used
 203  
     * to store the parameter in hash containers. Typically the
 204  
     * key is generated from the paths associated with the label.
 205  
     * 
 206  
     * @return this is the key used to represent the label
 207  
     */
 208  
    public Object getKey() throws Exception {
 209  0
       return label.getKey();
 210  
    }
 211  
 
 212  
    /**
 213  
     * This acquires the annotation associated with this label. This
 214  
     * is typically the annotation acquired from the field or method.
 215  
     * However, in the case of unions this will return the actual
 216  
     * annotation within the union group that this represents.
 217  
     * 
 218  
     * @return this returns the annotation that this represents
 219  
     */
 220  
    public Annotation getAnnotation() {
 221  0
       return label.getAnnotation();
 222  
    }
 223  
 
 224  
    /**
 225  
     * This is used to acquire the contact object for this label. The 
 226  
     * contact retrieved can be used to set any object or primitive that
 227  
     * has been deserialized, and can also be used to acquire values to
 228  
     * be serialized in the case of object persistence. All contacts 
 229  
     * that are retrieved from this method will be accessible. 
 230  
     * 
 231  
     * @return returns the field that this label is representing
 232  
     */
 233  
    public Contact getContact() {
 234  106
       return label.getContact();
 235  
    }
 236  
 
 237  
    /**
 238  
     * This acts as a convenience method used to determine the type of
 239  
     * the field this represents. This is used when an object is written
 240  
     * to XML. It determines whether a <code>class</code> attribute
 241  
     * is required within the serialized XML element, that is, if the
 242  
     * class returned by this is different from the actual value of the
 243  
     * object to be serialized then that type needs to be remembered.
 244  
     *  
 245  
     * @return this returns the type of the field class
 246  
     */
 247  
    public Class getType() {
 248  0
       return label.getType();
 249  
    }
 250  
 
 251  
    /**
 252  
     * This is used to acquire the name of the element or attribute
 253  
     * as taken from the annotation. If the element or attribute
 254  
     * explicitly specifies a name then that name is used for the
 255  
     * XML element or attribute used. If however no overriding name
 256  
     * is provided then the method or field is used for the name. 
 257  
     * 
 258  
     * @return returns the name of the annotation for the contact
 259  
     */
 260  
    public String getOverride() {
 261  0
       return label.getOverride();
 262  
    }
 263  
 
 264  
    /**
 265  
     * This is used to determine whether the annotation requires it
 266  
     * and its children to be written as a CDATA block. This is done
 267  
     * when a primitive or other such element requires a text value
 268  
     * and that value needs to be encapsulated within a CDATA block.
 269  
     * 
 270  
     * @return this returns true if the element requires CDATA
 271  
     */
 272  
    public boolean isData() {
 273  0
       return label.isData();
 274  
    }
 275  
 
 276  
    /**
 277  
     * Determines whether the XML attribute or element is required. 
 278  
     * This ensures that if an XML element is missing from a document
 279  
     * that deserialization can continue. Also, in the process of
 280  
     * serialization, if a value is null it does not need to be 
 281  
     * written to the resulting XML document.
 282  
     * 
 283  
     * @return true if the label represents a some required data
 284  
     */
 285  
    public boolean isRequired() {
 286  0
       return label.isRequired();
 287  
    }
 288  
 
 289  
    /**
 290  
     * This is used to determine if the label is a collection. If the
 291  
     * label represents a collection then any original assignment to
 292  
     * the field or method can be written to without the need to 
 293  
     * create a new collection. This allows obscure collections to be
 294  
     * used and also allows initial entries to be maintained.
 295  
     * 
 296  
     * @return true if the label represents a collection value
 297  
     */
 298  
    public boolean isCollection() {
 299  0
       return true;
 300  
    }
 301  
 
 302  
    /**
 303  
     * This is used to determine whether the label represents an
 304  
     * inline XML entity. The <code>ElementList</code> annotation
 305  
     * and the <code>Text</code> annotation represent inline 
 306  
     * items. This means that they contain no containing element
 307  
     * and so can not specify overrides or special attributes.
 308  
     * 
 309  
     * @return this returns true if the annotation is inline
 310  
     */
 311  
    public boolean isInline() {
 312  61
       return label.isInline();
 313  
    }
 314  
 
 315  
    /**
 316  
     * This is used to determine if an annotated list is a text 
 317  
     * list. A text list is a list of elements that also accepts
 318  
     * free text. Typically this will be an element list union that
 319  
     * will allow unstructured XML such as XHTML to be parsed.
 320  
     * 
 321  
     * @return returns true if the label represents a text list
 322  
     */
 323  
    public boolean isTextList() {
 324  0
       return true;
 325  
    }
 326  
    
 327  
    /**
 328  
     * This is used to create a string used to represent this text
 329  
     * list label. This will basically create a string representing
 330  
     * the other label in addition to the <code>Text</code> label.
 331  
     * 
 332  
     * @return this is used to build a string for the list label 
 333  
     */
 334  
    public String toString() {
 335  0
       return String.format("%s %s", text, label);
 336  
    }
 337  
 }