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.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
XmlObjectFactory.XmlObjectValidator
Handles the validation errors.
|
Modifier and Type | Field and Description |
---|---|
protected static Logger |
logger
The class logger.
|
Modifier and Type | Method and Description |
---|---|
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 . |
String |
toString() |
protected static final Logger logger
XmlObjectFactory.XmlObjectValidator
public static XmlObjectFactory getInstance(Class objectClass)
objectClass
.objectClass
- the Class of the object from which to configure this factory.public static void setDefaultValidating(boolean validating)
validating
- true/falsepublic void setValidating(boolean validating)
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.validating
- true/falsepublic Object getObject(URL objectXml)
objectXml
- the path to the Object XML config file.public Object getObject(Reader objectXml)
objectXml
- the path to the Object XML config file.public Object getObject(String objectXml)
objectXml
- the path to the Object XML config file.public Object getObject(String objectXml, URL resource)
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).public String toString()
toString
in class Object
Object.toString()
Copyright © 2003–2014 AndroMDA.org. All rights reserved.