Coverage Report - org.simpleframework.xml.core.ElementArrayParameter
 
Classes in this File Line Coverage Branch Coverage Complexity
ElementArrayParameter
95%
19/20
N/A
1
ElementArrayParameter$Contact
66%
2/3
N/A
1
 
 1  
 /*
 2  
  * ElementArrayParameter.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.ElementArray;
 25  
 import org.simpleframework.xml.stream.Format;
 26  
 
 27  
 /**
 28  
  * The <code>ElementArrayParameter</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 ElementArrayParameter 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>ElementArrayParameter</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 to style the elements
 87  
     * @param index this is the index the parameter appears at
 88  
     */
 89  3
    public ElementArrayParameter(Constructor factory, ElementArray value, Format format, int index) throws Exception {
 90  3
       this.contact = new Contact(value, factory, index);
 91  3
       this.label = new ElementArrayLabel(contact, value, format);
 92  3
       this.expression = label.getExpression();
 93  3
       this.path = label.getPath();
 94  3
       this.type = label.getType();
 95  3
       this.name = label.getName();
 96  3
       this.key = label.getKey();
 97  3
       this.index = index;
 98  3
    }
 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  9
       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  24
       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  30
       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  3
       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  9
       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  9
       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  3
       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  3
       return type.isPrimitive();
 202  
    }
 203  
    
 204  
    /**
 205  
     * This is used to provide a textual representation of the 
 206  
     * parameter. Providing a string describing the parameter is
 207  
     * useful for debugging and for exception messages.
 208  
     * 
 209  
     * @return this returns the string representation for this
 210  
     */
 211  
    public String toString() {
 212  3
       return contact.toString();
 213  
    }
 214  
    
 215  
    /**
 216  
     * The <code>Contact</code> represents a contact object that is
 217  
     * to be used for a label in order to extract a parameter name.
 218  
     * The parameter name is taken from the XML annotation.
 219  
     */
 220  
    private static class Contact extends ParameterContact<ElementArray>  {
 221  
       
 222  
       /**
 223  
        * Constructor for the <code>Contact</code> object. This is 
 224  
        * used to create an object that acts like an adapter so that
 225  
        * the label can create a consistent name for the parameter.
 226  
        * 
 227  
        * @param label this is the annotation for the parameter
 228  
        * @param factory this is the constructor the parameter is in
 229  
        * @param index this is the index for the parameter
 230  
        */
 231  
       public Contact(ElementArray label, Constructor factory, int index) {
 232  3
          super(label, factory, index);
 233  3
       }
 234  
       
 235  
       /**
 236  
        * This returns the name of the parameter as taken from the XML
 237  
        * annotation. The name provided here is taken by the label
 238  
        * and used to compose a name consistent with how fields and
 239  
        * methods are named by the system.
 240  
        * 
 241  
        * @return this returns the name of the annotated parameter
 242  
        */
 243  
       public String getName() {
 244  0
          return label.name();
 245  
       }
 246  
    }
 247  
 }