org.mmbase.util.xml
Class Instantiator

java.lang.Object
  extended by org.mmbase.util.xml.Instantiator

public abstract class Instantiator
extends Object

Utilities to use an XML to instantiate Java objects, using reflection. This is used by various configuration-read code, which all perform similar tasks of instantiating and configurating certain objects. Supported are schemes like

  <class name="class name">
     <param name="parameter name">parameter value</param>
      ...
  </class>
  and
  <anyelement class="class name">
     <param name="parameter name">parameter value</param>
      ...
  </anyname>
  
Param subtags are matched with 'setters' on the created object.

Since:
MMBase-1.9
Version:
$Id: Instantiator.java 41716 2010-04-01 10:52:24Z michiel $
Author:
Michiel Meeuwissen

Constructor Summary
Instantiator()
           
 
Method Summary
static Object getInstance(Element classElement, Object... args)
          Instantiates any object using an Dom Element and constructor arguments.
static Object getInstanceWithSubElement(Element element, Object... args)
          Instantiates on object with the first child with name 'class'.
static void setParameters(Element element, Object o)
           
static void setProperty(String name, Class claz, Object o, String value)
          Given the name of a property, a clazz, an object and a value, set the property on the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Instantiator

public Instantiator()
Method Detail

getInstance

public static Object getInstance(Element classElement,
                                 Object... args)
                          throws SAXException,
                                 ClassNotFoundException,
                                 NoSuchMethodException,
                                 InstantiationException,
                                 IllegalAccessException,
                                 InvocationTargetException
Instantiates any object using an Dom Element and constructor arguments. Sub-param tags are used on set-methods on the newly created object.

Parameters:
classElement - a 'class' element with a 'name' attribute, or any element with a 'class' attribute. Finally if this any element has no 'class' attribute, it will try 'name' too.
args - Constructor arguments.
Returns:
A newly created object. Never null.
Throws:
NoSuchMethodError - If not matching constructor could be found
ClassNotFoundException - If the specified class does not exist.
SAXException
NoSuchMethodException
InstantiationException
IllegalAccessException
InvocationTargetException

setParameters

public static void setParameters(Element element,
                                 Object o)
Since:
MMBase-2.0

setProperty

public static void setProperty(String name,
                               Class claz,
                               Object o,
                               String value)
Given the name of a property, a clazz, an object and a value, set the property on the object. The point of this method is that it converts the name of the property to an actual method name (set<Name of the property>). The methods needs to have precisely one parameter. The type of this parameter may be anything as long as it can be converted to from String by Casting.toType(Class, Object).

Parameters:
value - The value as a String

getInstanceWithSubElement

public static Object getInstanceWithSubElement(Element element,
                                               Object... args)
                                        throws SAXException,
                                               ClassNotFoundException,
                                               NoSuchMethodException,
                                               InstantiationException,
                                               IllegalAccessException,
                                               InvocationTargetException
Instantiates on object with the first child with name 'class'. So, this searches the child, and then calls getInstance(Element, Object...).

Parameters:
element - Element in which a child specifying a java object must be searched.
Returns:
a new object, or null if no matching child found.
Throws:
SAXException
ClassNotFoundException
NoSuchMethodException
InstantiationException
IllegalAccessException
InvocationTargetException


MMBase 2.0-SNAPSHOT - null