org.andromda.core.common
Class XmlObjectFactory

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

public class XmlObjectFactory
extends java.lang.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 seperates 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 it's 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

Field Summary
protected  java.lang.String JAXP_SCHEMA_LANGUAGE
          The JAXP 1.2 property to set up the schemaLanguage used.
protected static java.lang.String JAXP_SCHEMA_SOURCE
          The JAXP 1.2 property required to set up the schema location.
protected static org.apache.log4j.Logger logger
          The class logger.
 
Method Summary
static XmlObjectFactory getInstance(java.lang.Class objectClass)
          Gets an instance of this XmlObjectFactory using the digester rules belonging to the objectClass.
 java.lang.Object getObject(java.io.Reader objectXml)
          Returns a configured Object based on the objectXml configuration reader.
 java.lang.Object getObject(java.lang.String objectXml)
          Returns a configured Object based on the objectXml configuration file passed in as a String.
 java.lang.Object getObject(java.lang.String objectXml, java.net.URL resource)
          Returns a configured Object based on the objectXml configuration file passed in as a String.
 java.lang.Object getObject(java.net.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 org.apache.log4j.Logger logger
The class logger. Note: visibility is protected to improve access within XmlObjectValidator


JAXP_SCHEMA_SOURCE

protected static final java.lang.String JAXP_SCHEMA_SOURCE
The JAXP 1.2 property required to set up the schema location.

See Also:
Constant Field Values

JAXP_SCHEMA_LANGUAGE

protected java.lang.String JAXP_SCHEMA_LANGUAGE
The JAXP 1.2 property to set up the schemaLanguage used.

Method Detail

getInstance

public static XmlObjectFactory getInstance(java.lang.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 java.lang.Object getObject(java.net.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 java.lang.Object getObject(java.io.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 java.lang.Object getObject(java.lang.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 java.lang.Object getObject(java.lang.String objectXml,
                                  java.net.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-2008 AndroMDA.org. All Rights Reserved.