|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object net.sf.basedb.util.extensions.xml.XmlLoader
public class XmlLoader
Loads extension points and extensions from extension definition XML files. This class may load extensions from more than one file and register or unregister all loaded extensions and extension points with a registry.
For a description of the XML file format this class can load see the BASE manual.
Factory initialisation
This loader uses reflection to create and initialise factory
instances. The XML file contains the class name of the factory
to create. This class must declare a public no-argument
constructor and must also implement one of the specific
factory interfaces (for example ActionFactory
or RendererFactory
). If the tag <parameters>
is present inside the factory declaration, the loader will
use reflection to find a setter method for each sub-tag inside
the <parameters>
tag. Here is an example:
// XML contents <parameters> <image>button.png</image> <title>Click button</title> </parameters> // Factory initialization factory.setImage("button.png"); factory.setTitle("Click button");The setter methods must be public and accept a single
String
parameter.
The loader also looks for the setParameter(String, String)
method signature. If the method exists it will be called for each
parameter found with the tagname as the first parameter and the
value as the second parameter. Continuing the above example:
factory.setParameter("image", "button.png"); factory.setParameter("title", "Click button");
Tags that doesn't have a corresponding setter method are simply ignored. The XML file format allows any tags as parameters, but only the first level will be parsed.
The parameter values may be subject to conversion by one or more
ValueConverter
:s. Converters are added to the loader
by addValueConverter(ValueConverter)
and are usually
implemented to react on method annotations. For example, the web
client uses this for path and variable substitution.
Field Summary | |
---|---|
private Set<ValueConverter> |
converters
|
private List<ExtensionPoint<Action>> |
extensionPoints
|
private List<Extension<Action>> |
extensions
|
private Map<Object,String> |
factoryParameters
|
private static String |
namespace
The name of the extensions namespace. |
private static String |
schemaFileURL
The URL pointing to the extensions.xsd schema. |
private Document |
validatedDom
|
private XMLOutputter |
xmlOut
|
Constructor Summary | |
---|---|
XmlLoader()
Create a new XML loader instance. |
Method Summary | ||
---|---|---|
void |
addValueConverter(ValueConverter converter)
Add a value converter to this loader |
|
void |
clearValueConverters()
Remove all value converters. |
|
protected
|
createFactory(Element factoryTag,
ClassLoader classLoader,
Class<F> factoryType)
Create a new factory instance. |
|
protected String |
getConvertedValue(String original,
Method method)
|
|
List<ExtensionPoint<Action>> |
getExtensionPoints()
Get a list with all loaded extension points. |
|
List<Extension<Action>> |
getExtensions()
Get a list with all loaded extensions. |
|
String |
getFactoryParameters(Object factory)
Get a XML-like string representation of the parameters that was used to initialise a factory. |
|
protected Method |
getSetParameterMethod(Class<?> beanClass)
Check if the bean class has a setParameter(String, String)
method and return it's reference if it has. |
|
protected Method |
getSetterMethod(Class<?> beanClass,
String tagName)
Get the setter method for a given tag name. |
|
protected String |
getSetterMethodNameFromTag(String tagName)
Convert the tag name to a setter method name. |
|
boolean |
hasValidFile()
Checks if an XML file has passed validation in the validateXmlFile(InputStream, String) method. |
|
protected void |
initBeanWithReflection(Object bean,
Element root)
Initialise a bean using reflection. |
|
protected AboutBean |
loadAbout(Element aboutTag)
Load information in an <about> tag. |
|
protected Document |
loadDocument(InputStream xmlFile,
String filename)
Load and validate the XML file. |
|
protected int |
loadExtensionPoints(Document dom,
ClassLoader classLoader,
About globalAbout)
Create extension points from an XML document. |
|
protected int |
loadExtensions(Document dom,
ClassLoader classLoader,
About globalAbout)
Create extensions from an XML document. |
|
protected AboutBean |
loadGlobalAbout(Document dom)
Load the global about tag from the document. |
|
About |
loadLastValidatedFile(ClassLoader classLoader,
boolean clear)
Continue loading extensions from the last validated XML file. |
|
About |
loadXmlFile(InputStream xmlFile,
String filename,
ClassLoader classLoader,
boolean clear)
Load an extensions definition XML file. |
|
int |
registerExtensionPoints(Registry registry,
boolean update)
Register all loaded extension points with a registry. |
|
int |
registerExtensions(Registry registry,
boolean update)
Register all loaded extensions with a registry. |
|
void |
removeValueConverter(ValueConverter converter)
Remove a converter. |
|
int |
unregisterExtensionPoints(Registry registry)
Unregister all loaded extension points from a registry. |
|
int |
unregisterExtensions(Registry registry)
Unregister all loaded extensions from a registry. |
|
About |
validateXmlFile(InputStream xmlFile,
String filename)
Validate an XML file against the extensions definition schema. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String schemaFileURL
private static final String namespace
private XMLOutputter xmlOut
private List<ExtensionPoint<Action>> extensionPoints
private List<Extension<Action>> extensions
private Set<ValueConverter> converters
private Map<Object,String> factoryParameters
private Document validatedDom
Constructor Detail |
---|
public XmlLoader()
Method Detail |
---|
public void addValueConverter(ValueConverter converter)
converter
- The converter to add
NullPointerException
- If the converter is nullpublic void removeValueConverter(ValueConverter converter)
converter
- The converter to remove, null values are ignoredpublic void clearValueConverters()
public About loadXmlFile(InputStream xmlFile, String filename, ClassLoader classLoader, boolean clear) throws JDOMException, IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException
validateXmlFile(InputStream, String)
and loadLastValidatedFile(ClassLoader, boolean)
.
xmlFile
- An input stream to read the XML data fromfilename
- The original filename the stream is coming from, or null
if not known. This value is only used when generating error messagesclassLoader
- The classloader to use when loading classes that are
named in the XML file, or null to use the default classloader (=the same
class loader that loaded the BASE core classes)clear
- TRUE to clear all already loaded extensions before loading
the extensions in this file
JDOMException
- If validation of the XML file fails
IOException
- If there is an error reading the XML file
ClassNotFoundException
- If a class named in the XML file can't
be found
NoSuchMethodException
- If a factory class doesn't implement a
public, no-argument constructor
IllegalAccessException
- If a factory class constructor isn't
public
InstantiationException
- If a factory class can't be instantiated,
for example, because it is an abstract class or an interfacevalidateXmlFile(InputStream, String)
,
loadLastValidatedFile(ClassLoader, boolean)
public About validateXmlFile(InputStream xmlFile, String filename) throws IOException, JDOMException
loadLastValidatedFile(ClassLoader, boolean)
.
xmlFile
- An input stream to read the XML data fromfilename
- The original filename the stream is coming from, or null
if not known. This value is only used when generating error messages
JDOMException
- If validation of the XML file fails
IOException
- If there is an error reading the XML filepublic About loadLastValidatedFile(ClassLoader classLoader, boolean clear) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException
validateXmlFile(InputStream, String)
has been successfully called
first.
classLoader
- The classloader to use when loading classes that are
named in the XML file, or null to use the default classloader (=the same
class loader that loaded the BASE core classes)clear
- TRUE to clear all already loaded extensions before loading
the extensions in this file
ClassNotFoundException
- If a class named in the XML file can't
be found
NoSuchMethodException
- If a factory class doesn't implement a
public, no-argument constructor
IllegalAccessException
- If a factory class constructor isn't
public
InstantiationException
- If a factory class can't be instantiated,
for example, because it is an abstract class or an interfacepublic boolean hasValidFile()
validateXmlFile(InputStream, String)
method. If so,
the loadLastValidatedFile(ClassLoader, boolean)
can be called
to continue loading the extensions.
Note that once the file has been loaded this flag is reset to FALSE.
public List<ExtensionPoint<Action>> getExtensionPoints()
public List<Extension<Action>> getExtensions()
public String getFactoryParameters(Object factory)
factory
- The factory object
public int registerExtensionPoints(Registry registry, boolean update)
registry
- The registryupdate
- TRUE to update already registered extension
points, FALSE to ignore
Registry.registerExtensionPoint(ExtensionPoint)
public int unregisterExtensionPoints(Registry registry)
registry
- The registry
Registry.unregisterExtensionPoint(String)
public int registerExtensions(Registry registry, boolean update)
registry
- The registryupdate
- TRUE to update already registered extensions
FALSE to ignore
Registry.registerExtension(Extension)
public int unregisterExtensions(Registry registry)
registry
- The registry
Registry.unregisterExtension(String)
protected Document loadDocument(InputStream xmlFile, String filename) throws IOException, JDOMException
IOException
JDOMException
protected AboutBean loadGlobalAbout(Document dom)
protected AboutBean loadAbout(Element aboutTag)
<about>
tag.
aboutTag
- The about tagprotected int loadExtensionPoints(Document dom, ClassLoader classLoader, About globalAbout) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InstantiationException
protected int loadExtensions(Document dom, ClassLoader classLoader, About globalAbout) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InstantiationException
protected <F> F createFactory(Element factoryTag, ClassLoader classLoader, Class<F> factoryType) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException
factoryTag
- The root tag of the factory definitionclassLoader
- The classloader to use, or null to use
the BASE core classloaderfactoryType
- Class type of the factory. The named class
must implement or be a subclass of this class
ClassNotFoundException
NoSuchMethodException
IllegalAccessException
InstantiationException
protected void initBeanWithReflection(Object bean, Element root)
<image>button.png</image>
this will
be converted to the method call: setImage("button.png")
.
Tags that has no matching public setter method are ignored.
bean
- The bean to initializeroot
- The root element, if null nothing is doneprotected Method getSetterMethod(Class<?> beanClass, String tagName)
beanClass
- The class to look for the setter method intagName
- The tag name in the XML file
protected Method getSetParameterMethod(Class<?> beanClass)
setParameter(String, String)
method and return it's reference if it has.
beanClass
- The class to look for the method in
protected String getSetterMethodNameFromTag(String tagName)
tagName
- The tag name
protected String getConvertedValue(String original, Method method)
|
2.17.2: 2011-06-17 | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |