Coverage Report - org.simpleframework.xml.core.TextParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextParameter
86%
20/23
N/A
1
TextParameter$Contact
66%
2/3
N/A
1
 
 1  
 /*
 2  
  * TextParameter.java July 2009
 3  
  *
 4  
  * Copyright (C) 2009, 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  
 import java.lang.reflect.Constructor;
 23  
 
 24  
 import org.simpleframework.xml.Text;
 25  
 import org.simpleframework.xml.stream.Format;
 26  
 
 27  
 /**
 28  
  * The <code>TextParameter</code> represents a constructor 
 29  
  * parameter. It contains the XML annotation used on the parameter
 30  
  * as well as the name of the parameter and its position index.
 31  
  * A parameter is used to validate against the annotated methods 
 32  
  * and fields and also to determine the deserialized values that
 33  
  * should be injected in to the constructor to instantiate it.
 34  
  * 
 35  
  * @author Niall Gallagher
 36  
  */
 37  
 class TextParameter extends TemplateParameter {  
 38  
    
 39  
    /**
 40  
     * This is the expression used to represent this parameter.
 41  
     */
 42  
    private final Expression expression;   
 43  
    
 44  
    /**
 45  
     * This is the contact used to determine the parameter name.
 46  
     */
 47  
    private final Contact contact;
 48  
    
 49  
    /**
 50  
     * This is the label that will create the parameter name. 
 51  
     */
 52  
    private final Label label;
 53  
    
 54  
    /**
 55  
     * This is the fully qualified path used for this parameter.
 56  
     */
 57  
    private final String path;
 58  
    
 59  
    /**
 60  
     * This is the actual name that has been determined.
 61  
     */
 62  
    private final String name;
 63  
    
 64  
    /**
 65  
     * This is the type of the label represented by this.
 66  
     */
 67  
    private final Class type;
 68  
    
 69  
    /**
 70  
     * This is the key used to represent this parameter object.
 71  
     */
 72  
    private final Object key;
 73  
    
 74  
    /**
 75  
     * This is the index that the parameter was declared at.
 76  
     */
 77  
    private final int index;
 78  
    
 79  
    /**   
 80  
     * Constructor for the <code>TextParameter</code> object.
 81  
     * This is used to create a parameter that can be used to 
 82  
     * determine a consistent name using the provided XML annotation.
 83  
     * 
 84  
     * @param factory this is the constructor the parameter is in
 85  
     * @param value this is the annotation used for the parameter
 86  
     * @param format this is the format used for this parameter
 87  
     * @param index this is the index the parameter appears at
 88  
     */
 89  21
    public TextParameter(Constructor factory, Text value, Format format, int index) throws Exception {
 90  21
       this.contact = new Contact(value, factory, index);
 91  21
       this.label = new TextLabel(contact, value, format);
 92  21
       this.expression = label.getExpression();
 93  21
       this.path = label.getPath();
 94  21
       this.type = label.getType();
 95  21
       this.name = label.getName();
 96  21
       this.key = label.getKey();
 97  21
       this.index = index;
 98  21
    }
 99  
    
 100  
    /**
 101  
     * This is the key used to represent the parameter. The key is
 102  
     * used to store the parameter in hash containers. Unlike the
 103  
     * path is not necessarily the path for the parameter.
 104  
     * 
 105  
     * @return this is the key used to represent the parameter
 106  
     */
 107  
    public Object getKey() {
 108  58
       return key;
 109  
    }
 110  
    
 111  
    /**
 112  
     * This is used to acquire the path of the element or attribute
 113  
     * represented by this parameter. The path is determined by
 114  
     * acquiring the XPath expression and appending the name of the
 115  
     * label to form a fully qualified path.
 116  
     * 
 117  
     * @return returns the path that is used for this parameter
 118  
     */
 119  
    public String getPath() {
 120  140
       return path;
 121  
    }
 122  
    
 123  
    /**
 124  
     * This is used to acquire the name of the parameter that this
 125  
     * represents. The name is determined using annotation and 
 126  
     * the name attribute of that annotation, if one is provided.    
 127  
     * 
 128  
     * @return this returns the name of the annotated parameter
 129  
     */
 130  
    public String getName() {
 131  160
       return name;
 132  
    }
 133  
    
 134  
    /**
 135  
     * This method is used to return an XPath expression that is 
 136  
     * used to represent the position of this parameter. If there is 
 137  
     * no XPath expression associated with this then an empty path 
 138  
     * is returned. This will never return a null expression.
 139  
     * 
 140  
     * @return the XPath expression identifying the location
 141  
     */
 142  
    public Expression getExpression() {
 143  16
       return expression;
 144  
    }
 145  
    
 146  
    /**
 147  
     * This is used to acquire the path of the element or attribute
 148  
     * represented by this parameter. The path is determined by
 149  
     * acquiring the XPath expression and appending the name of the
 150  
     * label to form a fully qualified styled path.
 151  
     * 
 152  
     * @param context this is the context used to style the path
 153  
     * 
 154  
     * @return returns the path that is used for this parameter
 155  
     */
 156  
    public String getPath(Context context) {
 157  0
       return getPath();
 158  
    }
 159  
    
 160  
    /**
 161  
     * This is used to acquire the name of the parameter that this
 162  
     * represents. The name is determined using annotation and 
 163  
     * the name attribute of that annotation, if one is provided.
 164  
     * 
 165  
     * @param context this is the context used to style the name
 166  
     * 
 167  
     * @return this returns the name of the annotated parameter
 168  
     */
 169  
    public String getName(Context context) {
 170  0
       return getName();
 171  
    }
 172  
    
 173  
    /**
 174  
     * This is used to acquire the annotated type class. The class
 175  
     * is the type that is to be deserialized from the XML. This
 176  
     * is used to validate against annotated fields and methods.
 177  
     * 
 178  
     * @return this returns the type used for the parameter
 179  
     */
 180  
    public Class getType() {
 181  48
       return type;
 182  
    } 
 183  
    
 184  
    /**
 185  
     * This is used to acquire the annotation that is used for the
 186  
     * parameter. The annotation provided will be an XML annotation
 187  
     * such as the <code>Element</code> or <code>Attribute</code>
 188  
     * annotation.
 189  
     * 
 190  
     * @return this returns the annotation used on the parameter
 191  
     */
 192  
    public Annotation getAnnotation() {
 193  50
       return contact.getAnnotation();
 194  
    }
 195  
    
 196  
    /**
 197  
     * This returns the index position of the parameter in the
 198  
     * constructor. This is used to determine the order of values
 199  
     * that are to be injected in to the constructor.
 200  
     * 
 201  
     * @return this returns the index for the parameter
 202  
     */
 203  
    public int getIndex() {
 204  16
       return index;
 205  
    }
 206  
    
 207  
    /**
 208  
     * This is used to determine if the parameter is required. If 
 209  
     * an attribute is not required then it can be null. Which 
 210  
     * means that we can inject a null value. Also, this means we
 211  
     * can match constructors in a more flexible manner.
 212  
     * 
 213  
     * @return this returns true if the parameter is required
 214  
     */
 215  
    public boolean isRequired() {
 216  0
       return label.isRequired();
 217  
    }
 218  
    
 219  
    /**
 220  
     * This is used to determine if the parameter is primitive. A
 221  
     * primitive parameter must not be null. As there is no way to
 222  
     * provide the value to the constructor. A default value is 
 223  
     * not a good solution as it affects the constructor score.
 224  
     * 
 225  
     * @return this returns true if the parameter is primitive
 226  
     */
 227  
    public boolean isPrimitive() {
 228  16
       return type.isPrimitive();
 229  
    }
 230  
    
 231  
    /**
 232  
     * This is used to determine if the parameter represents text. 
 233  
     * If this represents text it typically does not have a name,
 234  
     * instead the empty string represents the name. Also text
 235  
     * parameters can not exist with other text parameters.
 236  
     * 
 237  
     * @return returns true if this parameter represents text
 238  
     */
 239  
    public boolean isText() {
 240  70
       return true;
 241  
    }
 242  
    
 243  
    /**
 244  
     * This is used to provide a textual representation of the 
 245  
     * parameter. Providing a string describing the parameter is
 246  
     * useful for debugging and for exception messages.
 247  
     * 
 248  
     * @return this returns the string representation for this
 249  
     */
 250  
    public String toString() {
 251  16
       return contact.toString();
 252  
    }
 253  
    
 254  
    /**
 255  
     * The <code>Contact</code> represents a contact object that is
 256  
     * to be used for a label in order to extract a parameter name.
 257  
     * The parameter name is taken from the XML annotation.
 258  
     * 
 259  
     * @author Niall Gallagher
 260  
     */
 261  
    private static class Contact extends ParameterContact<Text>  {
 262  
       
 263  
       /**
 264  
        * Constructor for the <code>Contact</code> object. This is 
 265  
        * used to create an object that acts like an adapter so that
 266  
        * the label can create a consistent name for the parameter.
 267  
        * 
 268  
        * @param label this is the annotation for the parameter
 269  
        * @param factory this is the constructor the parameter is in
 270  
        * @param index this is the index for the parameter
 271  
        */
 272  
       public Contact(Text label, Constructor factory, int index) {
 273  21
          super(label, factory, index);
 274  21
       }
 275  
       
 276  
       /**
 277  
        * This returns the name of the parameter as taken from the XML
 278  
        * annotation. The name provided here is taken by the label
 279  
        * and used to compose a name consistent with how fields and
 280  
        * methods are named by the system.
 281  
        * 
 282  
        * @return this returns the name of the annotated parameter
 283  
        */
 284  
       public String getName() {
 285  0
          return "";
 286  
       }
 287  
    }
 288  
 }