/*
 * PrimitiveFactory.java July 2006
 *
 * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General 
 * Public License along with this library; if not, write to the 
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA  02111-1307  USA
 */

package org.simpleframework.xml.load;

import org.simpleframework.xml.transform.Transformer;
import org.simpleframework.xml.stream.InputNode;

/**
 * The <code>PrimitiveFactory</code> object is used to create objects
 * that are primitive types. This creates primitives and enumerated
 * types when given a string value. The string value is parsed using
 * a matched <code>Transform</code> implementation. The transform is
 * then used to convert the object instance to an from a suitable XML
 * representation. Only enumerated types are not transformed using 
 * a transform, instead they use <code>Enum.name</code>. 
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.xml.transform.Transformer
 */ 
class PrimitiveFactory extends Factory {
   
   /**
    * Caches the constructors used to transform primitive types.
    * 
    * @see org.simpleframework.xml.load.Primitive
    */
   private static Transformer transform;

   static {
      transform = new Transformer();           
   } 
   
   /**
    * Constructor for the <code>PrimitiveFactory</code> object. This
    * is provided the field type that is to be instantiated. This
    * must be a type that contains a <code>Transform</code> object,
    * typically this ia a <code>java.lang</code> primitive object
    * or one of the primitive types such as <code>int</code>. Also
    * this can be given a class for an enumerated type. 
    * 
    * @param field this is the field type to be instantiated
    */
   public PrimitiveFactory(Source root, Class field) {
      super(root, field);           
   }
   
   /**
    * This method will instantiate an object of the field type, or if
    * the <code>Strategy</code> object can resolve a class from the
    * XML element then this is used instead. If the resulting type is
    * abstract or an interface then this method throws an exception.
    * 
    * @param node this is the node to check for the override
    * 
    * @return this returns an instance of the resulting type
    */         
   public Type getInstance(InputNode node) throws Exception {
      Type type = getOverride(node);
    
      if(type == null) { 
         return new ClassType(field);         
      }
      return type;      
   }    
   
   
   /**
    * This will instantiate an object of the field type using the
    * provided string. Typically this string is transformed in to the
    * type using a <code>Transform</code> object. However, if the
    * values is an enumeration then its value is created using the
    * <code>Enum.valueOf</code> method. Also string values typically
    * do not require conversion of any form and are just returned.
    * 
    * @param text this is the value to be transformed to an object
    * 
    * @return this returns an instance of the field type
    */         
   public Object getPrimitive(String text) throws Exception {
      if(field == String.class) {
         return text;              
      }    
      if(field.isEnum()) {
         return getEnumeration(text);              
      }           
      return getPrimitive(text, field);
   }
   
   /**
    * This will instantiate an object of the field type using the
    * provided string. Typically this string is transformed in to the
    * type using a <code>Transform</code> object. However, if the
    * values is an enumeration then its value is created using the
    * <code>Enum.valueOf</code> method. Also string values typically
    * do not require conversion of any form and are just returned.
    * 
    * @param text this is the value to be transformed to an object
    * 
    * @return this returns an instance of the field type
    */         
   private Object getPrimitive(String text, Class type) throws Exception {        
      return transform.read(text, type);
   } 
   
   /**
    * This will construct the enumerated type from the provided text
    * value. This uses <code>Enum.valueOf</code> to determine the 
    * enumerated type value that is to be instantiated.
    * 
    * @param text this is the enumerated type value to be created
    * 
    * @return this returns an instance of the enumerated type 
    * 
    * @throws Exception thrown if there was a problem instantiating
    */
   private Object getEnumeration(String text) throws Exception {
      return Enum.valueOf(field, text);
   }
   
   /**
    * This is used to acquire a text value for the specified object.
    * This will convert the object to a string using the transformer
    * so that it can be deserialized from the generate XML document.
    * 
    * @param source this is the object instance to get the value of
    * 
    * @return this returns a string representation of the object
    * 
    * @throws Exception if the object could not be transformed
    */
   public String getText(Object source) throws Exception {
      Class type = source.getClass();      

      return getText(source, type);
   }
   
   /**
    * This is used to acquire a text value for the specified object.
    * This will convert the object to a string using the transformer
    * so that it can be deserialized from the generate XML document.
    * 
    * @param source this is the object instance to get the value of
    * @param type this is the type of the object instance
    * 
    * @return this returns a string representation of the object
    * 
    * @throws Exception if the object could not be transformed
    */
   private String getText(Object source, Class type) throws Exception {
      if(type.isEnum()) {
         Enum value = (Enum)source;
         return value.name();
      }
      return transform.write(source, type);
   }
}

@@ -3005,18 +3005,16 @@
    %7D    
-%0A 
   %0A   %0A 
@@ -3769,1338 +3769,107 @@
 urn 
-getEnumeration(text);              %0A      %7D           %0A      return getPrimitive(text, field);%0A   %7D%0A   %0A   /**%0A    * This will instantiate an object of the field type using the%0A    * provided string. Typically this string is transformed in to the%0A    * type using a %3Ccode%3ETransform%3C/code%3E object. However, if the%0A    * values is an enumeration then its value is created using the%0A    * %3Ccode%3EEnum.valueOf%3C/code%3E method. Also string values typically%0A    * do not require conversion of any form and are just returned.%0A    * %0A    * @param text this is the value to be transformed to an object%0A    * %0A    * @return this returns an instance of the field type%0A    */         %0A   private Object getPrimitive(String text, Class type) throws Exception %7B        %0A      return transform.read(text, type
+Enum.valueOf(field, text
 );
-%0A
    
-%7D %0A
+ 
    
-%0A
    
-/**%0A    * This will construct the enumerated type from the provided text%0A    * value. This uses %3Ccode%3EEnum.valueOf%3C/code%3E to determine the %0A    * enumerated type value that is to be instantiated.%0A    * %0A    * @param text this is the enumerated type value to be created%0A    * %0A    * @return this returns an instance of the enumerated type %0A    * %0A    * @throws Exception thrown if there was a problem instantiating%0A    */%0A   private Object getEnumeration(String text) throws Exception %7B%0A      return Enum.valueOf(field, text
+    %0A      %7D           %0A      return transform.read(text, field
 );%0A 
@@ -4422,621 +4422,8 @@
   %0A%0A
-      return getText(source, type);%0A   %7D%0A   %0A   /**%0A    * This is used to acquire a text value for the specified object.%0A    * This will convert the object to a string using the transformer%0A    * so that it can be deserialized from the generate XML document.%0A    * %0A    * @param source this is the object instance to get the value of%0A    * @param type this is the type of the object instance%0A    * %0A    * @return this returns a string representation of the object%0A    * %0A    * @throws Exception if the object could not be transformed%0A    */%0A   private String getText(Object source, Class type) throws Exception %7B%0A
     
@@ -4561,16 +4561,21 @@
 , type);
+     
 %0A   %7D%0A%7D%0A

Simplify the primitive factory