All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.simpleframework.xml.core.PrimitiveFactory Maven / Gradle / Ivy

Go to download

Simple is a high performance XML serialization and configuration framework for Java

There is a newer version: 2.7.1
Show newest version
/*
 * PrimitiveFactory.java July 2006
 *
 * Copyright (C) 2006, Niall Gallagher 
 *
 * 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.core;

import org.simpleframework.xml.strategy.Value;
import org.simpleframework.xml.stream.InputNode;

/**
 * The PrimitiveFactory 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 Transform 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 Enum.name. 
 * 
 * @author Niall Gallagher
 * 
 * @see org.simpleframework.xml.transform.Transformer
 */ 
class PrimitiveFactory extends Factory {
   
   /**
    * Constructor for the PrimitiveFactory object. This
    * is provided the field type that is to be instantiated. This
    * must be a type that contains a Transform object,
    * typically this is a java.lang primitive object
    * or one of the primitive types such as int. Also
    * this can be given a class for an enumerated type. 
    * 
    * @param context this is the context used by this factory
    * @param field this is the field type to be instantiated
    */
   public PrimitiveFactory(Context context, Class field) {
      super(context, field);           
   }
   
   /**
    * This method will instantiate an object of the field type, or if
    * the Strategy 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 Instance getInstance(InputNode node) throws Exception {
      Value value = getOverride(node);
    
      if(value == null) { 
         return context.getInstance(field);         
      }
      return new ObjectInstance(context, value);      
   }      
   
   /**
    * This will instantiate an object of the field type using the
    * provided string. Typically this string is transformed in to the
    * type using a Transform object. However, if the
    * values is an enumeration then its value is created using the
    * Enum.valueOf 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 getInstance(String text) throws Exception {
      return getInstance(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 Transform object. However, if the
    * values is an enumeration then its value is created using the
    * Enum.valueOf 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
    * @param type this is the type of the primitive to instantiate
    * 
    * @return this returns an instance of the field type
    */         
   public Object getInstance(String text, Class type) throws Exception {
      if(type == String.class) {
         return text;              
      }    
      if(field.isEnum()) {
         return Enum.valueOf(type, text);              
      }           
      return support.read(text, 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.
    * However if the type is an Enum type then the text
    * value is taken from Enum.name so it can later be
    * deserialized easily using the enumeration class and name.
    * 
    * @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();      

      if(type.isEnum()) {
         Enum value = (Enum)source;
         return value.name();
      }
      return support.write(source, type);     
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy