Coverage Report - org.simpleframework.xml.core.AttributeParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
AttributeParameter
95%
20/21
N/A
1
AttributeParameter$Contact
66%
2/3
N/A
1
 
 1  
 /*
 2  
  * AttributeParameter.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.Attribute;
 25  
 import org.simpleframework.xml.stream.Format;
 26  
 
 27  
 /**
 28  
  * The <code>AttributeParameter</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 AttributeParameter 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>AttributeParameter</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 index this is the index the parameter appears at
 87  
     * @param format this is the format used to style the paths
 88  
     */
 89  120
    public AttributeParameter(Constructor factory, Attribute value, Format format, int index) throws Exception {
 90  120
       this.contact = new Contact(value, factory, index);
 91  120
       this.label = new AttributeLabel(contact, value, format);
 92  120
       this.expression = label.getExpression();
 93  120
       this.path = label.getPath();
 94  120
       this.type = label.getType();
 95  120
       this.name = label.getName();
 96  120
       this.key = label.getKey();
 97  120
       this.index = index;
 98  120
    }
 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  360
       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  953
       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  1182
       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  118
       return expression;
 144  
    }
 145  
    
 146  
    /**
 147  
     * This is used to acquire the annotated type class. The class
 148  
     * is the type that is to be deserialized from the XML. This
 149  
     * is used to validate against annotated fields and methods.
 150  
     * 
 151  
     * @return this returns the type used for the parameter
 152  
     */
 153  
    public Class getType() {
 154  354
       return type;
 155  
    } 
 156  
    
 157  
    /**
 158  
     * This is used to acquire the annotation that is used for the
 159  
     * parameter. The annotation provided will be an XML annotation
 160  
     * such as the <code>Element</code> or <code>Attribute</code>
 161  
     * annotation.
 162  
     * 
 163  
     * @return this returns the annotation used on the parameter
 164  
     */
 165  
    public Annotation getAnnotation() {
 166  354
       return contact.getAnnotation();
 167  
    }
 168  
    
 169  
    /**
 170  
     * This returns the index position of the parameter in the
 171  
     * constructor. This is used to determine the order of values
 172  
     * that are to be injected in to the constructor.
 173  
     * 
 174  
     * @return this returns the index for the parameter
 175  
     */
 176  
    public int getIndex() {
 177  118
       return index;
 178  
    }
 179  
    
 180  
    /**
 181  
     * This is used to determine if the parameter is required. If 
 182  
     * an attribute is not required then it can be null. Which 
 183  
     * means that we can inject a null value. Also, this means we
 184  
     * can match constructors in a more flexible manner.
 185  
     * 
 186  
     * @return this returns true if the parameter is required
 187  
     */
 188  
    public boolean isRequired() {
 189  0
       return label.isRequired();
 190  
    }
 191  
    
 192  
    /**
 193  
     * This is used to determine if the parameter is primitive. A
 194  
     * primitive parameter must not be null. As there is no way to
 195  
     * provide the value to the constructor. A default value is 
 196  
     * not a good solution as it affects the constructor score.
 197  
     * 
 198  
     * @return this returns true if the parameter is primitive
 199  
     */
 200  
    public boolean isPrimitive() {
 201  118
       return type.isPrimitive();
 202  
    }
 203  
    
 204  
    /**
 205  
     * This method is used to determine if the parameter represents 
 206  
     * an attribute. This is used to style the name so that elements
 207  
     * are styled as elements and attributes are styled as required.
 208  
     * 
 209  
     * @return this is used to determine if this is an attribute
 210  
     */
 211  
    public boolean isAttribute() {
 212  474
       return true;
 213  
    }
 214  
    
 215  
    /**
 216  
     * This is used to provide a textual representation of the 
 217  
     * parameter. Providing a string describing the parameter is
 218  
     * useful for debugging and for exception messages.
 219  
     * 
 220  
     * @return this returns the string representation for this
 221  
     */
 222  
    public String toString() {
 223  118
       return contact.toString();
 224  
    }
 225  
    
 226  
    /**
 227  
     * The <code>Contact</code> represents a contact object that is
 228  
     * to be used for a label in order to extract a parameter name.
 229  
     * The parameter name is taken from the XML annotation.
 230  
     * 
 231  
     * @author Niall Gallagher
 232  
     */
 233  
    private static class Contact extends ParameterContact<Attribute>  {
 234  
       
 235  
       /**
 236  
        * Constructor for the <code>Contact</code> object. This is 
 237  
        * used to create an object that acts like an adapter so that
 238  
        * the label can create a consistent name for the parameter.
 239  
        * 
 240  
        * @param label this is the annotation for the parameter
 241  
        * @param factory this is the constructor the parameter is in
 242  
        * @param index this is the index for the parameter
 243  
        */
 244  
       public Contact(Attribute label, Constructor factory, int index) {
 245  120
          super(label, factory, index);
 246  120
       }
 247  
       
 248  
       /**
 249  
        * This returns the name of the parameter as taken from the XML
 250  
        * annotation. The name provided here is taken by the label
 251  
        * and used to compose a name consistent with how fields and
 252  
        * methods are named by the system.
 253  
        * 
 254  
        * @return this returns the name of the annotated parameter
 255  
        */
 256  
       public String getName() {
 257  0
          return label.name();
 258  
       }
 259  
    }
 260  
 }