gate.creole
Class CreoleAnnotationHandler

java.lang.Object
  extended by gate.creole.CreoleAnnotationHandler

public class CreoleAnnotationHandler
extends Object

Class to take a creole.xml file (as a JDOM tree) and add elements corresponding to the CREOLE annotations on the RESOURCE classes it declares.


Field Summary
private  URL creoleFileUrl
           
 
Constructor Summary
CreoleAnnotationHandler(URL creoleFileUrl)
          Create an annotation handler for the given creole.xml file.
 
Method Summary
private  void addAttribute(org.jdom.Element paramElt, Object value, Object defaultValue, String attrName)
          Add an attribute with the given value to the given element, but only if the element does not already have the attribute, and the value is not equal to the given default value.
private  void addElement(org.jdom.Element parent, String value, String elementName)
          Adds an element with the given name and text value to the parent element, but only if no such child element already exists and the value is not the empty string.
 void addJarsToClassLoader(org.jdom.Document jdomDoc)
          Extract all JAR elements from the given JDOM document and add the jars they reference to the GateClassLoader.
private  void addJarsToClassLoader(org.jdom.Element jdomElt)
          Recursively search the given element for JAR entries and add these jars to the GateClassLoader
 void createResourceElementsForDirInfo(org.jdom.Document jdomDoc)
          Fetches the directory information for this handler's creole plugin and adds additional RESOURCE elements to the given JDOM document so that it contains a RESOURCE for every resource type defined in the plugin's directory info.
private  void determineCollectionElementType(Method method, org.jdom.Element paramElt)
          Given a single-argument method whose parameter is a Collection, use the method's generic type information to determine the collection element type and store it as the ITEM_CLASS_NAME attribute of the given Element.
private  Class<?> findCollectionElementType(Type type)
          Find the collection element type for the given type.
private  Class<?> findCollectionElementType(Type type, Map<TypeVariable<?>,Class<?>> tvMap)
          Recursive method to find the collection element type for the given type.
private  void findResourceElements(Map<String,org.jdom.Element> map, org.jdom.Element elt)
           
 void processAnnotations(org.jdom.Document jdomDoc)
          Processes annotations for resource classes named in the given creole.xml document, adding the relevant XML elements to the document as appropriate.
private  void processAnnotations(org.jdom.Element element)
          Process annotations for the given element.
private  void processAnnotationsForResource(org.jdom.Element element)
          Process the given RESOURCE element, adding extra elements to it based on the annotations on the resource class.
private  void processAutoInstances(CreoleResource cr, org.jdom.Element element)
          Process the AutoInstance annotations contained in the given CreoleResource and add the corresponding AUTOINSTANCE/HIDDEN-AUTOINSTANCE elements to the given parent.
 void processCreoleResourceAnnotations(org.jdom.Element element, Class<?> resourceClass)
           
private  void processInheritableResourceData(Class<?> clazz, org.jdom.Element element)
          Recursive method to process the CreoleResource elements that can be inherited from superclasses and interfaces (everything except the PRIVATE and MAIN_VIEWER flags, the NAME and the AUTOINSTANCEs).
private  void processParameters(Class<?> resourceClass, org.jdom.Element resourceElement, Map<String,org.jdom.Element> parameterMap, Map<String,org.jdom.Element> disjunctionMap)
          Process any CreoleParameter and HiddenCreoleParameter annotations on set methods of the given class and set up the corresponding PARAMETER elements.
private  void processResourceData(Class<?> resourceClass, org.jdom.Element element)
          Process the CreoleResource data for this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

creoleFileUrl

private URL creoleFileUrl
Constructor Detail

CreoleAnnotationHandler

public CreoleAnnotationHandler(URL creoleFileUrl)
Create an annotation handler for the given creole.xml file.

Parameters:
creoleFileUrl - location of the creole.xml file.
Method Detail

addJarsToClassLoader

public void addJarsToClassLoader(org.jdom.Document jdomDoc)
                          throws MalformedURLException
Extract all JAR elements from the given JDOM document and add the jars they reference to the GateClassLoader.

Parameters:
jdomDoc - JDOM document representing a parsed creole.xml file.
Throws:
MalformedURLException

addJarsToClassLoader

private void addJarsToClassLoader(org.jdom.Element jdomElt)
                           throws MalformedURLException
Recursively search the given element for JAR entries and add these jars to the GateClassLoader

Parameters:
jdomElt - JDOM element representing a creole.xml file
Throws:
MalformedURLException

createResourceElementsForDirInfo

public void createResourceElementsForDirInfo(org.jdom.Document jdomDoc)
                                      throws MalformedURLException
Fetches the directory information for this handler's creole plugin and adds additional RESOURCE elements to the given JDOM document so that it contains a RESOURCE for every resource type defined in the plugin's directory info.

Parameters:
jdomDoc - JDOM document which should be the parsed creole.xml that this handler was configured for.
Throws:
MalformedURLException

findResourceElements

private void findResourceElements(Map<String,org.jdom.Element> map,
                                  org.jdom.Element elt)

processAnnotations

public void processAnnotations(org.jdom.Document jdomDoc)
                        throws GateException
Processes annotations for resource classes named in the given creole.xml document, adding the relevant XML elements to the document as appropriate.

Parameters:
jdomDoc - the parsed creole.xml file
Throws:
GateException

processAnnotations

private void processAnnotations(org.jdom.Element element)
                         throws GateException
Process annotations for the given element. If the element is a RESOURCE it is processed, otherwise the method calls itself recursively for all the children of the given element.

Parameters:
element - the element to process.
Throws:
GateException

processAnnotationsForResource

private void processAnnotationsForResource(org.jdom.Element element)
                                    throws GateException
Process the given RESOURCE element, adding extra elements to it based on the annotations on the resource class.

Parameters:
element - the RESOURCE element to process.
Throws:
GateException

processCreoleResourceAnnotations

public void processCreoleResourceAnnotations(org.jdom.Element element,
                                             Class<?> resourceClass)
                                      throws GateException
Throws:
GateException

processResourceData

private void processResourceData(Class<?> resourceClass,
                                 org.jdom.Element element)
Process the CreoleResource data for this class. This method first extracts the non-inheritable data (PRIVATE, MAIN_VIEWER, NAME and TOOL), then calls processInheritableResourceData(java.lang.Class, org.jdom.Element) to process the inheritable data, then deals with any specified AutoInstances.

Parameters:
resourceClass - the resource class to process, which must be annotated with CreoleResource.
element - the RESOURCE element to which data should be added.

processInheritableResourceData

private void processInheritableResourceData(Class<?> clazz,
                                            org.jdom.Element element)
Recursive method to process the CreoleResource elements that can be inherited from superclasses and interfaces (everything except the PRIVATE and MAIN_VIEWER flags, the NAME and the AUTOINSTANCEs). Once data has been extracted from the current class the method calls itself recursively for the superclass and any implemented interfaces. For any given attribute, the first value specified wins (i.e. the one on the most specific class).

Parameters:
clazz - the class to process
element - the RESOURCE element to which data should be added.

addElement

private void addElement(org.jdom.Element parent,
                        String value,
                        String elementName)
Adds an element with the given name and text value to the parent element, but only if no such child element already exists and the value is not the empty string.

Parameters:
parent - the parent element
value - the text value for the new child
elementName - the name of the new child element

processAutoInstances

private void processAutoInstances(CreoleResource cr,
                                  org.jdom.Element element)
Process the AutoInstance annotations contained in the given CreoleResource and add the corresponding AUTOINSTANCE/HIDDEN-AUTOINSTANCE elements to the given parent.

Parameters:
cr - the CreoleResource annotation
element - the parent element

processParameters

private void processParameters(Class<?> resourceClass,
                               org.jdom.Element resourceElement,
                               Map<String,org.jdom.Element> parameterMap,
                               Map<String,org.jdom.Element> disjunctionMap)
                        throws GateException
Process any CreoleParameter and HiddenCreoleParameter annotations on set methods of the given class and set up the corresponding PARAMETER elements.

Parameters:
resourceClass - the resource class to process
resourceElement - the RESOURCE element to which the PARAMETERs are to be added
parameterMap - a map from parameter names to the PARAMETER elements that define them. This is used as we combine information from the original creole.xml, the parameter annotation on the target method and the annotations on the same method of its superclasses and interfaces. Parameter names that have been hidden by a HiddenCreoleParameter annotation are explicitly mapped to null in this map.
disjunctionMap - a map from disjunction IDs to the OR elements that define them. Disjunctive parameters are handled by specifying a disjunction ID on the CreoleParameter annotations - parameters with the same disjunction ID are grouped under the same OR element.
Throws:
GateException

determineCollectionElementType

private void determineCollectionElementType(Method method,
                                            org.jdom.Element paramElt)
Given a single-argument method whose parameter is a Collection, use the method's generic type information to determine the collection element type and store it as the ITEM_CLASS_NAME attribute of the given Element.

Parameters:
method - the setter method
paramElt - the PARAMETER element

findCollectionElementType

private Class<?> findCollectionElementType(Type type)
Find the collection element type for the given type.

Parameters:
type - the type to use. To be able to find the element type, this must be a Class that is assignable from Collection or a ParameterizedType whose raw type is assignable from Collection.
Returns:
the Class representing the collection element type, or null if this cannot be determined

findCollectionElementType

private Class<?> findCollectionElementType(Type type,
                                           Map<TypeVariable<?>,Class<?>> tvMap)
Recursive method to find the collection element type for the given type.

Parameters:
type - the type to use
tvMap - map from type variables to the classes they are ultimately bound to. The reflection APIs can tell us that List<String> is an instantiation of List<X> and List<X> implements Collection<X>, but we have to keep track of the fact that X maps to String ourselves.
Returns:
the collection element type, or null if it cannot be determined.

addAttribute

private void addAttribute(org.jdom.Element paramElt,
                          Object value,
                          Object defaultValue,
                          String attrName)
Add an attribute with the given value to the given element, but only if the element does not already have the attribute, and the value is not equal to the given default value.

Parameters:
paramElt - the element
value - the attribute value (which will be converted to a string)
defaultValue - if value.equals(defaultValue) we do not add the attribute.
attrName - the name of the attribute to add.