Search in sources :

Example 1 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleRegisterImpl method registerDirectories.

/**
 * Register a single CREOLE directory. The <CODE>creole.xml</CODE> file at the
 * URL is parsed, and <CODE>CreoleData</CODE> objects added to the register.
 * If the directory URL has not yet been added it is now added. If any other
 * plugins that need to be loaded for this plugin to load (specified by
 * <CODE>REQUIRES</Code> elements in <code>creole.xml</code>) will only be
 * loaded if the <code>loadDependencies</code> param is true. It is useful to
 * be able to ignore dependencies when loading an xgapp where we know they
 * have already been resolved.
 */
@Override
public void registerDirectories(URL directoryUrl, boolean loadDependencies) throws GateException {
    // registerPlugin
    try {
        Plugin plugin = new Plugin.Directory(directoryUrl);
        registerPlugin(plugin);
    } catch (Exception e) {
        throw new GateException("Failed to load plugin", e);
    }
}
Also used : GateException(gate.util.GateException) GateRuntimeException(gate.util.GateRuntimeException) JDOMException(org.jdom.JDOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException)

Example 2 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleRegisterImpl method registerBuiltins.

/**
 * Register resources that are built in to the GATE distribution. These
 * resources are described by the <TT>creole.xml</TT> file in
 * <TT>resources/creole</TT>.
 */
@Override
public void registerBuiltins() throws GateException {
    try {
        URL creoleDirURL = Gate.getBuiltinCreoleDir();
        // URL creoleFileURL = new URL(creoleDirURL, "creole.xml");
        // URL creoleFileURL = Files.getGateResource("/creole/creole.xml");
        // parseDirectory(creoleFileURL.openStream(), creoleDirURL,
        // creoleFileURL,true);*/
        Plugin plugin = new Plugin.Directory(creoleDirURL);
        parseDirectory(plugin, plugin.getCreoleXML(), plugin.getBaseURL(), new URL(plugin.getBaseURL(), "creole.xml"));
        log.info("CREOLE plugin loaded: " + plugin.getName());
    } catch (Exception e) {
        if (DEBUG)
            log.debug(e);
        throw (new GateException(e));
    }
}
Also used : GateException(gate.util.GateException) URL(java.net.URL) GateRuntimeException(gate.util.GateRuntimeException) JDOMException(org.jdom.JDOMException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) GateException(gate.util.GateException)

Example 3 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleRegisterImpl method getAllInstances.

public List<Resource> getAllInstances(String type, boolean includeHidden) throws GateException {
    List<Resource> res = new ArrayList<Resource>();
    Class<? extends Resource> targetClass;
    try {
        targetClass = Gate.getClassLoader().loadClass(type).asSubclass(Resource.class);
    } catch (ClassNotFoundException cnfe) {
        throw new GateException("Invalid type " + type);
    }
    for (Map.Entry<String, ResourceData> entry : entrySet()) {
        String aType = entry.getKey();
        Class<?> aClass;
        try {
            aClass = entry.getValue().getResourceClass();
            if (targetClass.isAssignableFrom(aClass)) {
                // filter out hidden instances
                Iterator<? extends Resource> newInstancesIter = get(aType).getInstantiations().iterator();
                while (newInstancesIter.hasNext()) {
                    Resource instance = newInstancesIter.next();
                    if (includeHidden || !Gate.getHiddenAttribute(instance.getFeatures())) {
                        res.add(instance);
                    }
                }
            }
        } catch (ClassNotFoundException cnfe) {
            throw new GateRuntimeException("A type registered in the creole register does not exist in the VM!");
        }
    }
    return res;
}
Also used : GateException(gate.util.GateException) VisualResource(gate.VisualResource) Resource(gate.Resource) LanguageResource(gate.LanguageResource) ProcessingResource(gate.ProcessingResource) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GateRuntimeException(gate.util.GateRuntimeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleAnnotationHandler method loadRequiredPlugins.

/**
 * Extracts all the the REQUIRES elements from the given JDOM document,
 * expands the URLs and then attempts to load the specified plugin
 *
 * @param jdomDoc JDOM document representing a parsed creole.xml file.
 */
@Deprecated
public void loadRequiredPlugins(Document jdomDoc) throws IOException {
    try {
        XPath jarXPath = XPath.newInstance("//*[translate(local-name(), 'requires', 'REQUIRES') = 'REQUIRES']");
        @SuppressWarnings("unchecked") List<Element> requires = jarXPath.selectNodes(jdomDoc);
        String relativePathMarker = "$relpath$";
        String gatehomePathMarker = "$gatehome$";
        String gatepluginsPathMarker = "$gateplugins$";
        for (Element required : requires) {
            URL url = null;
            String urlString = required.getTextTrim();
            if (urlString.startsWith(relativePathMarker)) {
                url = new URL(plugin.getBaseURL(), urlString.substring(relativePathMarker.length()));
            } else if (urlString.startsWith(gatehomePathMarker)) {
                URL gatehome = Gate.getGateHome().toURI().toURL();
                url = new URL(gatehome, urlString.substring(gatehomePathMarker.length()));
            } else if (urlString.startsWith(gatepluginsPathMarker)) {
                URL gateplugins = Gate.getPluginsHome().toURI().toURL();
                url = new URL(gateplugins, urlString.substring(gatepluginsPathMarker.length()));
            } else {
                url = new URL(plugin.getBaseURL(), urlString);
            }
            Gate.getCreoleRegister().registerPlugin(new Plugin.Directory(url));
        }
    } catch (GateException | JDOMException e) {
        throw new IOException("Unable to load required plugin", e);
    }
}
Also used : XPath(org.jdom.xpath.XPath) Element(org.jdom.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) GateException(gate.util.GateException) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) URL(java.net.URL)

Example 5 with GateException

use of gate.util.GateException in project gate-core by GateNLP.

the class CreoleAnnotationHandler method processParameters.

/**
 * Process any {@link CreoleParameter} and {@link HiddenCreoleParameter}
 * annotations on set methods of the given class and set up the corresponding
 * PARAMETER elements.
 *
 * @param resourceClass
 *          the resource class to process
 * @param resourceElement
 *          the RESOURCE element to which the PARAMETERs are to be added
 * @param 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
 *          {@link HiddenCreoleParameter} annotation are explicitly mapped to
 *          <code>null</code> in this map.
 * @param disjunctionMap
 *          a map from disjunction IDs to the OR elements that define them.
 *          Disjunctive parameters are handled by specifying a disjunction ID
 *          on the {@link CreoleParameter} annotations - parameters with the
 *          same disjunction ID are grouped under the same OR element.
 */
private void processParameters(Class<?> resourceClass, Element resourceElement, Map<String, Element> parameterMap, Map<String, Element> disjunctionMap) throws GateException {
    BeanInfo bi;
    try {
        bi = Introspector.getBeanInfo(resourceClass);
    } catch (IntrospectionException e) {
        throw new GateException("Failed to introspect " + resourceClass, e);
    }
    for (Method method : resourceClass.getDeclaredMethods()) {
        processElement(method, bi, resourceElement, parameterMap, disjunctionMap);
    }
    for (Field field : resourceClass.getDeclaredFields()) {
        processElement(field, bi, resourceElement, parameterMap, disjunctionMap);
    }
    // go up the tree
    Class<?> superclass = resourceClass.getSuperclass();
    if (superclass != null) {
        processParameters(superclass, resourceElement, parameterMap, disjunctionMap);
    }
    for (Class<?> intf : resourceClass.getInterfaces()) {
        processParameters(intf, resourceElement, parameterMap, disjunctionMap);
    }
}
Also used : Field(java.lang.reflect.Field) BeanInfo(java.beans.BeanInfo) GateException(gate.util.GateException) IntrospectionException(java.beans.IntrospectionException) Method(java.lang.reflect.Method)

Aggregations

GateException (gate.util.GateException)23 IOException (java.io.IOException)11 URL (java.net.URL)10 GateRuntimeException (gate.util.GateRuntimeException)8 MalformedURLException (java.net.MalformedURLException)8 Resource (gate.Resource)5 JDOMException (org.jdom.JDOMException)5 Plugin (gate.creole.Plugin)3 InputStream (java.io.InputStream)3 LanguageResource (gate.LanguageResource)2 ProcessingResource (gate.ProcessingResource)2 VisualResource (gate.VisualResource)2 ResourceInstantiationException (gate.creole.ResourceInstantiationException)2 CreoleResource (gate.creole.metadata.CreoleResource)2 GateClassLoader (gate.util.GateClassLoader)2 BeanInfo (java.beans.BeanInfo)2 IntrospectionException (java.beans.IntrospectionException)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 File (java.io.File)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)2