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

org.simpleframework.xml.core.ObjectFactory 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
/*
 * ObjectFactory.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 ObjectFactory is the most basic factory. This will
 * basically check to see if there is an override type within the XML
 * node provided, if there is then that is instantiated, otherwise the
 * field type is instantiated. Any type created must have a default
 * no argument constructor. If the override type is an abstract class
 * or an interface then this factory throws an exception.
 *  
 * @author Niall Gallagher
 */ 
class ObjectFactory extends PrimitiveFactory {
   
   /**
    * Constructor for the ObjectFactory class. This is
    * given the field class that this should create object instances
    * of. If the field type is abstract then the XML node must have
    * sufficient information for the Strategy object to
    * resolve the implementation class to be instantiated.
    *
    * @param context the contextual object used by the persister 
    * @param field this is the field type of the object 
    */
   public ObjectFactory(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
    */       
   @Override
   public Instance getInstance(InputNode node) throws Exception {
      Value type = getOverride(node);
    
      if(type == null) { 
         if(!isInstantiable(field)) {
            throw new InstantiationException("Cannot instantiate %s", field);              
         }
         return context.getInstance(field);         
      }
      return new ObjectInstance(context, type);      
   }     
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy