org.objectweb.easybeans.xmlconfig
Class XMLConfiguration

java.lang.Object
  extended by org.objectweb.easybeans.xmlconfig.XMLConfiguration

public class XMLConfiguration
extends java.lang.Object

XML configuration class that configure a given object with an xml configuration file.
Additional information will be searched on the namespace with xxx-mapping.xml file with xxx the name of the element defining the namespace.

Author:
Florent Benoit

Field Summary
private  java.util.HashMap<java.lang.String,java.lang.Object> configuredElements
          Link between name and the objects.
private static java.lang.String HTTP_NAMESPACE
          Http:// namespace.
private  JLog logger
          Logger.
private static java.lang.String MAPPING_SUFFIX
          Suffix for mapping file.
private  java.util.Map<java.lang.String,XMLMapping> mappings
          Mapping between namespace and XMLMapping object.
private  boolean validating
          Validating when parsing xml ?
private static boolean VALIDATING_OFF
          By default, no validation.
private  java.net.URL xmlConfigurationURL
          URL of the configuration file.
 
Constructor Summary
XMLConfiguration(java.net.URL xmlConfigurationURL)
          Build an xml configuration with the given xml configuration file.
 
Method Summary
private  void addSetElement(java.lang.Object configuringObject, java.lang.Object newObject)
          Add or set element on the given object.
private  org.w3c.dom.Element analyzeXmlFile()
          Gets the root element of the XML parsed file.
 void configure(java.lang.Object object)
          Configure the given object with the configuration that was analyzed.
 void configure(java.lang.Object object, org.w3c.dom.Element element)
          Configure the given object by looking at the element (of the configuration).
private  XMLMapping createXMLMapping(java.lang.String nameSpace, java.net.URL mappingURL)
          Create the XML mapping object for the given namespace with the given XML mapping file.
private  java.lang.reflect.Method[] findListGetterSetterMethod(java.lang.Object configuringObject, java.lang.Object paramObject)
          Finds the get(List<Class>) and set(List<Class>) methods.
private  java.lang.reflect.Method findSingleSetterMethod(java.lang.Object configuringObject, java.lang.Object paramObject)
          Finds the add(Class c) or set(Class c) methods.
The parameter needs to be a single entry and not a list.
private  ClassMapping getClassMapping(java.lang.String nameSpaceURI, java.lang.String name)
          Gets the Mapping for the given nameSpaceURI and the given classname.
private  XMLMapping getXMLMapping(java.lang.String nameSpace, java.lang.String nodeName)
          Gets the XML mapping for the given namespace and the node name.
private  java.lang.Object newInstance(org.w3c.dom.Node node, ClassMapping classMapping)
          Build class instance for the given node.
private  java.lang.reflect.Method[] searchListGetterSetterMethod(java.lang.reflect.Method[] methods, java.lang.String argName, java.lang.Class parameterClass)
          Search getter and setter method in the given set of methods.
private  void setAttribute(java.lang.Object configuringObject, java.lang.String propertyName, java.lang.String propertyValue, ClassMapping classMapping)
          Set attribute on the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_NAMESPACE

private static final java.lang.String HTTP_NAMESPACE
Http:// namespace.

See Also:
Constant Field Values

VALIDATING_OFF

private static final boolean VALIDATING_OFF
By default, no validation.

See Also:
Constant Field Values

MAPPING_SUFFIX

private static final java.lang.String MAPPING_SUFFIX
Suffix for mapping file.

See Also:
Constant Field Values

logger

private JLog logger
Logger.


validating

private boolean validating
Validating when parsing xml ?


xmlConfigurationURL

private java.net.URL xmlConfigurationURL
URL of the configuration file.


mappings

private java.util.Map<java.lang.String,XMLMapping> mappings
Mapping between namespace and XMLMapping object.


configuredElements

private java.util.HashMap<java.lang.String,java.lang.Object> configuredElements
Link between name and the objects.

Constructor Detail

XMLConfiguration

public XMLConfiguration(java.net.URL xmlConfigurationURL)
Build an xml configuration with the given xml configuration file.

Parameters:
xmlConfigurationURL - the given xml configuration file
Method Detail

configure

public void configure(java.lang.Object object)
               throws XMLConfigurationException
Configure the given object with the configuration that was analyzed.

Parameters:
object - the object to configure.
Throws:
XMLConfigurationException - if configuration fails

analyzeXmlFile

private org.w3c.dom.Element analyzeXmlFile()
                                    throws XMLConfigurationException
Gets the root element of the XML parsed file.

Returns:
the element object
Throws:
XMLConfigurationException - if analyze fails.

configure

public void configure(java.lang.Object object,
                      org.w3c.dom.Element element)
               throws XMLConfigurationException
Configure the given object by looking at the element (of the configuration).

Parameters:
object - the given object to configure.
element - the element that contains configuration.
Throws:
XMLConfigurationException - if configuration fails

getClassMapping

private ClassMapping getClassMapping(java.lang.String nameSpaceURI,
                                     java.lang.String name)
                              throws XMLConfigurationException
Gets the Mapping for the given nameSpaceURI and the given classname.

Parameters:
nameSpaceURI - the namespace of the xml element
name - the name of the class
Returns:
the class mapping object
Throws:
XMLConfigurationException - if no configuration is found

newInstance

private java.lang.Object newInstance(org.w3c.dom.Node node,
                                     ClassMapping classMapping)
                              throws XMLConfigurationException
Build class instance for the given node.

Parameters:
node - the element of the xml
classMapping - the mapping between node name and class name
Returns:
the instance of the object
Throws:
XMLConfigurationException - if no new instance can be done.

setAttribute

private void setAttribute(java.lang.Object configuringObject,
                          java.lang.String propertyName,
                          java.lang.String propertyValue,
                          ClassMapping classMapping)
                   throws XMLConfigurationException
Set attribute on the given object.

Parameters:
configuringObject - the object to configure
propertyName - the name of the property (for the setter)
propertyValue - the value of the property
classMapping - the mapping (if there are attributes aliasing)
Throws:
XMLConfigurationException - if attribute cannot be set.

findSingleSetterMethod

private java.lang.reflect.Method findSingleSetterMethod(java.lang.Object configuringObject,
                                                        java.lang.Object paramObject)
Finds the add(Class c) or set(Class c) methods.
The parameter needs to be a single entry and not a list.

Parameters:
configuringObject - the object on which call this setter method.
paramObject - the parameter object
Returns:
the found method if any.

findListGetterSetterMethod

private java.lang.reflect.Method[] findListGetterSetterMethod(java.lang.Object configuringObject,
                                                              java.lang.Object paramObject)
Finds the get(List<Class>) and set(List<Class>) methods. The parameter needs to be a list with generics.

Parameters:
configuringObject - the object on which call this setter method.
paramObject - the parameter object
Returns:
the getter and setter method with List type.

searchListGetterSetterMethod

private java.lang.reflect.Method[] searchListGetterSetterMethod(java.lang.reflect.Method[] methods,
                                                                java.lang.String argName,
                                                                java.lang.Class parameterClass)
Search getter and setter method in the given set of methods. The getter/setter are used for a List parameter/return type.

Parameters:
methods - the set of methods to search in
argName - the name of the argument
parameterClass - the parameter class (for the generics on the List)
Returns:
an array of methods, array[0] = getter, array[1] = setter.

addSetElement

private void addSetElement(java.lang.Object configuringObject,
                           java.lang.Object newObject)
                    throws XMLConfigurationException
Add or set element on the given object.

Parameters:
configuringObject - the object to configure
newObject - the object to add/set on the configuringObject object
Throws:
XMLConfigurationException - if newObject cannot be set.

getXMLMapping

private XMLMapping getXMLMapping(java.lang.String nameSpace,
                                 java.lang.String nodeName)
                          throws XMLConfigurationException
Gets the XML mapping for the given namespace and the node name.

Parameters:
nameSpace - current XML namespace.
nodeName - the name of the node.
Returns:
the XML mapping object.
Throws:
XMLConfigurationException - if xml mapping is not found.

createXMLMapping

private XMLMapping createXMLMapping(java.lang.String nameSpace,
                                    java.net.URL mappingURL)
                             throws XMLConfigurationException
Create the XML mapping object for the given namespace with the given XML mapping file.

Parameters:
nameSpace - the namespace of the mapping
mappingURL - the URL to the XML mapping file
Returns:
an instance of XML mapping
Throws:
XMLConfigurationException - if no XML mapping have been created.