org.andromda.core.common
Class XmlObjectFactory

java.lang.Object
  extended by org.andromda.core.common.XmlObjectFactory

public class XmlObjectFactory
extends Object

Creates and returns Objects based on a set of Apache Digester rules in a consistent manner, providing validation in the process.

This XML object factory allows us to define a consistent/clean of configuring java objects from XML configuration files (i.e. it uses the class name of the java object to find what rule file and what XSD file to use). It also allows us to define a consistent way in which schema validation is performed.

It separates each concern into one file, for example: to configure and perform validation on the MetafacadeMappings class, we need 3 files 1.) the java object (MetafacadeMappings.java), 2.) the rules file which tells the apache digester how to populate the java object from the XML configuration file (MetafacadeMappings-Rules.xml), and 3.) the XSD schema validation file (MetafacadeMappings.xsd). Note that each file is based on the name of the java object: 'java object name'.xsd and 'java object name'-Rules.xml'. After you have these three files then you just need to call the method #getInstance(java.net.URL objectClass) in this class from the java object you want to configure. This keeps the dependency to digester (or whatever XML configuration tool we are using at the time) to this single file.

In order to add/modify an existing element/attribute in your configuration file, first make the modification in your java object, then modify its rules file to instruct the digester on how to configure your new attribute/method in the java object, and then modify your XSD file to provide correct validation for this new method/attribute. Please see the org.andromda.core.metafacade.MetafacadeMappings* files for an example on how to do this.

Author:
Chad Brandon, Bob Fields, Michail Plushnikov

Nested Class Summary
(package private) static class XmlObjectFactory.XmlObjectValidator
          Handles the validation errors.
 
Field Summary
protected static Logger logger
          The class logger.
 
Method Summary
static XmlObjectFactory getInstance(Class objectClass)
          Gets an instance of this XmlObjectFactory using the digester rules belonging to the objectClass.
 Object getObject(Reader objectXml)
          Returns a configured Object based on the objectXml configuration reader.
 Object getObject(String objectXml)
          Returns a configured Object based on the objectXml configuration file passed in as a String.
 Object getObject(String objectXml, URL resource)
          Returns a configured Object based on the objectXml configuration file passed in as a String.
 Object getObject(URL objectXml)
          Returns a configured Object based on the objectXml configuration file
static void setDefaultValidating(boolean validating)
          Allows us to set default validation to true/false for all instances of objects instantiated by this factory.
 void setValidating(boolean validating)
          Sets whether or not the XmlObjectFactory should be validating, default is true.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected static final Logger logger
The class logger. Note: visibility is protected to improve access within XmlObjectFactory.XmlObjectValidator

Method Detail

getInstance

public static XmlObjectFactory getInstance(Class objectClass)
Gets an instance of this XmlObjectFactory using the digester rules belonging to the objectClass.

Parameters:
objectClass - the Class of the object from which to configure this factory.
Returns:
the XmlObjectFactoy instance.

setDefaultValidating

public static void setDefaultValidating(boolean validating)
Allows us to set default validation to true/false for all instances of objects instantiated by this factory. This is necessary in some cases where the underlying parser doesn't support schema validation (such as when performing JUnit tests)

Parameters:
validating - true/false

setValidating

public void setValidating(boolean validating)
Sets whether or not the XmlObjectFactory should be validating, default is true. If it IS set to be validating, then there needs to be a schema named objectClass.xsd in the same package as the objectClass that this factory was created from.

Parameters:
validating - true/false

getObject

public Object getObject(URL objectXml)
Returns a configured Object based on the objectXml configuration file

Parameters:
objectXml - the path to the Object XML config file.
Returns:
Object the created instance.

getObject

public Object getObject(Reader objectXml)
Returns a configured Object based on the objectXml configuration reader.

Parameters:
objectXml - the path to the Object XML config file.
Returns:
Object the created instance.

getObject

public Object getObject(String objectXml)
Returns a configured Object based on the objectXml configuration file passed in as a String.

Parameters:
objectXml - the path to the Object XML config file.
Returns:
Object the created instance.

getObject

public Object getObject(String objectXml,
                        URL resource)
Returns a configured Object based on the objectXml configuration file passed in as a String.

Parameters:
objectXml - the path to the Object XML config file.
resource - the resource from which the objectXml was retrieved (this is needed to resolve any relative references; like XML entities).
Returns:
Object the created instance.


Copyright © 2003-2012 AndroMDA.org. All Rights Reserved.