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