Coverage Report - org.simpleframework.xml.core.PrimitiveFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimitiveFactory
100%
14/14
100%
4/4
1.8
 
 1  
 /*
 2  
  * PrimitiveFactory.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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 org.simpleframework.xml.strategy.Type;
 22  
 import org.simpleframework.xml.strategy.Value;
 23  
 import org.simpleframework.xml.stream.InputNode;
 24  
 
 25  
 /**
 26  
  * The <code>PrimitiveFactory</code> object is used to create objects
 27  
  * that are primitive types. This creates primitives and enumerated
 28  
  * types when given a string value. The string value is parsed using
 29  
  * a matched <code>Transform</code> implementation. The transform is
 30  
  * then used to convert the object instance to an from a suitable XML
 31  
  * representation. Only enumerated types are not transformed using 
 32  
  * a transform, instead they use <code>Enum.name</code>. 
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.transform.Transformer
 37  
  */ 
 38  
 class PrimitiveFactory extends Factory {
 39  
    
 40  
    /**
 41  
     * Constructor for the <code>PrimitiveFactory</code> object. This
 42  
     * is provided the field type that is to be instantiated. This
 43  
     * must be a type that contains a <code>Transform</code> object,
 44  
     * typically this is a <code>java.lang</code> primitive object
 45  
     * or one of the primitive types such as <code>int</code>. Also
 46  
     * this can be given a class for an enumerated type. 
 47  
     * 
 48  
     * @param context this is the context used by this factory
 49  
     * @param type this is the field type to be instantiated
 50  
     */
 51  
    public PrimitiveFactory(Context context, Type type) {
 52  2972047
       super(context, type);           
 53  2972047
    }
 54  
    
 55  
    /**
 56  
     * Constructor for the <code>PrimitiveFactory</code> object. This
 57  
     * is provided the field type that is to be instantiated. This
 58  
     * must be a type that contains a <code>Transform</code> object,
 59  
     * typically this is a <code>java.lang</code> primitive object
 60  
     * or one of the primitive types such as <code>int</code>. Also
 61  
     * this can be given a class for an enumerated type. 
 62  
     * 
 63  
     * @param context this is the context used by this factory
 64  
     * @param type this is the field type to be instantiated
 65  
     * @param override this is the override used for this primitve
 66  
     */
 67  
    public PrimitiveFactory(Context context, Type type, Class override) {
 68  935502
       super(context, type, override);           
 69  935502
    }
 70  
    
 71  
    /**
 72  
     * This method will instantiate an object of the field type, or if
 73  
     * the <code>Strategy</code> object can resolve a class from the
 74  
     * XML element then this is used instead. If the resulting type is
 75  
     * abstract or an interface then this method throws an exception.
 76  
     * 
 77  
     * @param node this is the node to check for the override
 78  
     * 
 79  
     * @return this returns an instance of the resulting type
 80  
     */         
 81  
    public Instance getInstance(InputNode node) throws Exception {
 82  1012693
       Value value = getOverride(node);
 83  1012693
       Class type = getType();
 84  
     
 85  1012693
       if(value == null) { 
 86  999242
          return context.getInstance(type);         
 87  
       }
 88  13451
       return new ObjectInstance(context, value);      
 89  
    }      
 90  
    
 91  
    /**
 92  
     * This will instantiate an object of the field type using the
 93  
     * provided string. Typically this string is transformed in to the
 94  
     * type using a <code>Transform</code> object. However, if the
 95  
     * values is an enumeration then its value is created using the
 96  
     * <code>Enum.valueOf</code> method. Also string values typically
 97  
     * do not require conversion of any form and are just returned.
 98  
     * 
 99  
     * @param text this is the value to be transformed to an object
 100  
     * @param type this is the type of the primitive to instantiate
 101  
     * 
 102  
     * @return this returns an instance of the field type
 103  
     */         
 104  
    public Object getInstance(String text, Class type) throws Exception {          
 105  1714125
       return support.read(text, type);
 106  
    }
 107  
    
 108  
    /**
 109  
     * This is used to acquire a text value for the specified object.
 110  
     * This will convert the object to a string using the transformer
 111  
     * so that it can be deserialized from the generate XML document.
 112  
     * However if the type is an <code>Enum</code> type then the text
 113  
     * value is taken from <code>Enum.name</code> so it can later be
 114  
     * deserialized easily using the enumeration class and name.
 115  
     * 
 116  
     * @param source this is the object instance to get the value of
 117  
     * 
 118  
     * @return this returns a string representation of the object
 119  
     * 
 120  
     * @throws Exception if the object could not be transformed
 121  
     */
 122  
    public String getText(Object source) throws Exception {
 123  526196
       Class type = source.getClass();
 124  
       
 125  526196
       if(type.isEnum()) {
 126  353
          return support.write(source, type);
 127  
       }
 128  525843
       return support.write(source, type);
 129  
    }
 130  
 }