public class XmlLoader
extends java.lang.Object
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.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.ClassLoader> |
classLoaders |
private java.util.Set<ValueConverter> |
converters |
private java.util.List<ExtensionPoint<Action>> |
extensionPoints |
private java.util.List<Extension<Action>> |
extensions |
private java.util.Map<java.lang.Object,java.lang.String> |
factoryParameters |
private Filter<Element> |
filter |
private java.lang.String |
lastName |
private static org.slf4j.Logger |
log |
static java.lang.String |
NAMESPACE
The name of the extensions namespace.
|
private java.util.List<PluginInfo> |
pluginDefinitions |
static java.lang.String |
SCHEMA_FILE_URL
The URL pointing to the extensions.xsd schema.
|
private Document |
validatedDom |
private XMLOutputter |
xmlOut |
Constructor and Description |
---|
XmlLoader()
Create a new XML loader instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addValueConverter(ValueConverter converter)
Add a value converter to this loader
|
void |
clearValueConverters()
Remove all value converters.
|
protected <F> F |
createFactory(Element factoryTag,
java.lang.ClassLoader classLoader,
java.lang.Class<F> factoryType,
Presets.Preset config)
Create a new factory instance.
|
protected java.lang.String |
getConvertedValue(java.lang.String original,
java.lang.reflect.Method method) |
java.util.List<ExtensionPoint<Action>> |
getExtensionPoints()
Get a list with all loaded extension points.
|
java.util.List<Extension<Action>> |
getExtensions()
Get a list with all loaded extensions.
|
java.lang.String |
getFactoryParameters(java.lang.Object factory)
Get a XML-like string representation of the parameters that was
used to initialise a factory.
|
java.util.List<PluginInfo> |
getPluginDefinitions()
Get a list with all loaded plug-in definitions.
|
protected java.lang.reflect.Method |
getSetParameterMethod(java.lang.Class<?> beanClass)
Check if the bean class has a
setParameter(String, String)
method and return it's reference if it has. |
protected java.lang.reflect.Method |
getSetterMethod(java.lang.Class<?> beanClass,
java.lang.String tagName)
Get the setter method for a given tag name.
|
protected java.lang.String |
getSetterMethodNameFromTag(java.lang.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(java.lang.Object bean,
Element root,
Presets.Preset config)
Initialise a bean using reflection.
|
protected AboutBean |
loadAbout(Element aboutTag)
Load information in an
<about> tag. |
protected Document |
loadDocument(java.io.InputStream xmlFile,
java.lang.String filename)
Load and validate the XML file.
|
protected int |
loadExtensionPoints(Document dom,
java.lang.ClassLoader classLoader,
About globalAbout)
Create extension points from an XML document.
|
protected int |
loadExtensions(Document dom,
java.lang.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(java.lang.ClassLoader classLoader,
boolean clear)
Continue loading extensions from the last validated XML file.
|
protected int |
loadPluginDefinitions(Document dom,
java.lang.ClassLoader classLoader,
About globalAbout)
Load plug-in information from an XML document.
|
About |
loadXmlFile(java.io.InputStream xmlFile,
java.lang.String filename,
java.lang.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.
|
void |
setFilter(Filter<Element> filter)
Set a filter that all <extension-point>, <extension>,
<ref> and <plugin-definition> tags must pass to be loaded
and registered.
|
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(java.io.InputStream xmlFile,
java.lang.String filename)
Validate an XML file against the extensions definition schema.
|
protected boolean |
verifyBaseVersion(About about,
boolean throwException) |
private static final org.slf4j.Logger log
public static final java.lang.String SCHEMA_FILE_URL
public static final java.lang.String NAMESPACE
private XMLOutputter xmlOut
private final java.util.List<ExtensionPoint<Action>> extensionPoints
private final java.util.List<PluginInfo> pluginDefinitions
private java.util.Set<ValueConverter> converters
private final java.util.Map<java.lang.Object,java.lang.String> factoryParameters
private final java.util.Map<java.lang.String,java.lang.ClassLoader> classLoaders
private Document validatedDom
private java.lang.String lastName
public void addValueConverter(ValueConverter converter)
converter
- The converter to addjava.lang.NullPointerException
- If the converter is nullpublic void removeValueConverter(ValueConverter converter)
converter
- The converter to remove, null values are ignoredpublic void clearValueConverters()
public void setFilter(Filter<Element> filter)
filter
- A filter or null to remove an existing filterpublic About loadXmlFile(java.io.InputStream xmlFile, java.lang.String filename, java.lang.ClassLoader classLoader, boolean clear) throws java.io.IOException, java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.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 filejava.io.IOException
- If there is an error reading the XML filejava.lang.ClassNotFoundException
- If a class named in the XML file can't
be foundjava.lang.NoSuchMethodException
- If a factory class doesn't implement a
public, no-argument constructorjava.lang.IllegalAccessException
- If a factory class constructor isn't
publicjava.lang.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(java.io.InputStream xmlFile, java.lang.String filename) throws java.io.IOException
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 messagesjava.io.IOException
- If there is an error reading the XML filepublic About loadLastValidatedFile(java.lang.ClassLoader classLoader, boolean clear) throws java.lang.ClassNotFoundException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.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 filejava.lang.ClassNotFoundException
- If a class named in the XML file can't
be foundjava.lang.NoSuchMethodException
- If a factory class doesn't implement a
public, no-argument constructorjava.lang.IllegalAccessException
- If a factory class constructor isn't
publicjava.lang.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 java.util.List<ExtensionPoint<Action>> getExtensionPoints()
public java.util.List<Extension<Action>> getExtensions()
public java.util.List<PluginInfo> getPluginDefinitions()
public java.lang.String getFactoryParameters(java.lang.Object factory)
factory
- The factory objectpublic int registerExtensionPoints(Registry registry, boolean update)
registry
- The registryupdate
- TRUE to update already registered extension
points, FALSE to ignoreRegistry.registerExtensionPoint(ExtensionPoint)
public int unregisterExtensionPoints(Registry registry)
registry
- The registryRegistry.unregisterExtensionPoint(String)
public int registerExtensions(Registry registry, boolean update)
registry
- The registryupdate
- TRUE to update already registered extensions
FALSE to ignoreRegistry.registerExtension(Extension)
public int unregisterExtensions(Registry registry)
registry
- The registryRegistry.unregisterExtension(String)
protected Document loadDocument(java.io.InputStream xmlFile, java.lang.String filename) throws java.io.IOException
java.io.IOException
protected boolean verifyBaseVersion(About about, boolean throwException)
protected AboutBean loadGlobalAbout(Document dom)
protected AboutBean loadAbout(Element aboutTag)
<about>
tag.aboutTag
- The about tagprotected int loadExtensionPoints(Document dom, java.lang.ClassLoader classLoader, About globalAbout) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.InstantiationException
protected int loadExtensions(Document dom, java.lang.ClassLoader classLoader, About globalAbout) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.InstantiationException
protected int loadPluginDefinitions(Document dom, java.lang.ClassLoader classLoader, About globalAbout) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.InstantiationException
protected <F> F createFactory(Element factoryTag, java.lang.ClassLoader classLoader, java.lang.Class<F> factoryType, Presets.Preset config) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.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 classjava.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.IllegalAccessException
java.lang.InstantiationException
protected void initBeanWithReflection(java.lang.Object bean, Element root, Presets.Preset config)
<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 java.lang.reflect.Method getSetterMethod(java.lang.Class<?> beanClass, java.lang.String tagName)
beanClass
- The class to look for the setter method intagName
- The tag name in the XML fileprotected java.lang.reflect.Method getSetParameterMethod(java.lang.Class<?> beanClass)
setParameter(String, String)
method and return it's reference if it has.beanClass
- The class to look for the method inprotected java.lang.String getSetterMethodNameFromTag(java.lang.String tagName)
tagName
- The tag nameprotected java.lang.String getConvertedValue(java.lang.String original, java.lang.reflect.Method method)