net.sourceforge.cilib.xml
Class XMLObjectFactory

java.lang.Object
  extended by net.sourceforge.cilib.xml.XMLObjectFactory
Direct Known Subclasses:
XMLAlgorithmFactory, XMLMeasurementFactory, XMLProblemFactory, XMLStoppingConditionFactory

public class XMLObjectFactory
extends Object

The XMLObjectFactory can be used to manage the construction of any object based on an XML description. This class uses reflection to set properties and call arbitrary methods of the object under contruction.

Example:

   // Parse the XML document
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   DocumentBuilder db = dbf.newDocumentBuilder();
   Document doc = db.parse(new File("example.xml"));

   // Here, construct is the XML element to read the configuration from
   Element description = (Element) doc.getElementsByTagName("construct").item(0);

   // The document is only necessary for idrefs, there is a
   // second constructor that accepts just the element
   XMLObjectFactory factory = new XMLObjectFactory(doc, description);

   // Finally, call newObject() whenever you want to construct a new
   // object according to the configuration in the given XML element
   Object object = factory.newObject();
 

Each time XMLObjectFactory encounters a class attribute in the XML, an instance of the specified class is instanciated. Nested elements are interpreted as either properties to be set or methods to be called on this newly constructed object. If no class attribute is specified then the element is interpreted as a Java primitive type or a string. Elements can be nested in a hierarchical fashion to handle the construction of complex objects.

Note: Requires the Java API for XML processing (JAXP).

See: Factory Demo for XML samples.

Author:
Edwin Peer

Constructor Summary
XMLObjectFactory(Document xmlDocument, Element xmlObjectDescription)
          Creates a new instance of XMLObjectFactory for constructing objects given an XML description and an XML document for handling idrefs.
XMLObjectFactory(Element xmlObjectDescription)
          Creates a new instance of XMLObjectFactory for constructing objects given an XML description.
 
Method Summary
protected  void error(Element element, String message)
           
 Object newObject()
          Constructs a new Object based on the underlying XML object description.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLObjectFactory

public XMLObjectFactory(Element xmlObjectDescription)
Creates a new instance of XMLObjectFactory for constructing objects given an XML description.

Parameters:
xmlObjectDescription - An XML element that describes the objects to be constructed by this factory.

XMLObjectFactory

public XMLObjectFactory(Document xmlDocument,
                        Element xmlObjectDescription)
Creates a new instance of XMLObjectFactory for constructing objects given an XML description and an XML document for handling idrefs.

Parameters:
xmlObjectDescription - An XML element that describes the objects to be constructed by this factory.
xmlDocument - The XML document to search for any idrefs.
Method Detail

newObject

public Object newObject()
Constructs a new Object based on the underlying XML object description.

Returns:
A new Object constructed according to the given description.
Throws:
FactoryException - In case the object cannot be constructed.

error

protected void error(Element element,
                     String message)


Copyright © 2009 CIRG. All Rights Reserved.