1   /*
2    * OntoGazetteerImpl.java
3    *
4    * Copyright (c) 2002, The University of Sheffield.
5    *
6    * This file is part of GATE (see http://gate.ac.uk/), and is free
7    * software, licenced under the GNU Library General Public License,
8    * Version 2, June1991.
9    *
10   * A copy of this licence is included in the distribution in the file
11   * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
12   *
13   * borislav popov 02/2002
14   *
15   */
16  package gate.creole.gazetteer;
17  
18  import gate.*;
19  import gate.creole.ExecutionException;
20  import gate.creole.ResourceInstantiationException;
21  
22  /** OntoGazetteerImpl <br>
23   *  An ontology-aware gazetteer, producing additional annotations
24   *  with features [class] and [ontology].
25   */
26  public class OntoGazetteerImpl extends AbstractOntoGazetteer {
27  
28    public OntoGazetteerImpl() {
29    }
30  
31    public java.util.Set lookup(String singleItem) {
32      return gaz.lookup(singleItem);
33    }
34  
35    /** Initialize this onto gazetteer
36     *  @return .*/
37    public Resource init() throws ResourceInstantiationException {
38      try {
39        checkParameters();
40  
41        // load gazetteer class from GATE classloader
42        Class cl = Class.forName(gazetteerName, true, Gate.getClassLoader());
43  
44        FeatureMap params = Factory.newFeatureMap();
45  
46        mappingDefinition = new MappingDefinition();
47        mappingDefinition.setURL(mappingURL);
48        mappingDefinition.load();
49  
50        params.put("caseSensitive",caseSensitive);
51        params.put("listsURL",listsURL);
52        params.put("encoding",encoding);
53        params.put("mappingDefinition",mappingDefinition);
54        gaz = (Gazetteer)Factory.createResource(cl.getName(),params);
55  
56      } catch (ClassNotFoundException e) {
57        throw new RuntimeException("ClassNotFoundException : "+e.getMessage());
58      } catch (InvalidFormatException e) {
59        throw new ResourceInstantiationException(e);
60      }
61      return this;
62    } // init
63  
64    /** Executes this onto gazetteer over a pre-set document
65     *  @throws ExecutionException if something goes wrong with the execution */
66    public void execute()throws ExecutionException {
67      if (null == gaz) {
68        throw new ExecutionException("gazetteer not initialized (null).");
69      }
70  
71      gaz.setDocument(document);
72      gaz.setAnnotationSetName(annotationSetName);
73      gaz.setEncoding(encoding);
74      gaz.setCorpus(corpus);
75      gaz.execute();
76    } // execute
77  
78    /**
79     * Checks the parameters set to this gazetteer
80     * @throws ResourceInstantiationException if something goes wrong
81     */
82    private void checkParameters() throws ResourceInstantiationException {
83      boolean set = null!=gazetteerName;
84      set &= null!=listsURL;
85      set&=null!=mappingURL;
86      if (!set) {
87       throw new ResourceInstantiationException("some parameters are not set (e.g.gazetteerName,"
88          +"listURL,mappingDefinition, document");
89      }
90  
91    } // checkParameters
92  
93    /**
94     * Removes a single string item from the gazetteer model
95     * @param singleItem removes a string item from the gazetteer model
96     * @return true if the string is removed from the model, otherwise - false
97     */
98    public boolean remove(String singleItem) {
99      return gaz.remove(singleItem);
100   }
101 
102   /**
103    * Adds a string item to the model and associates it with a Lookup
104    * @param singleItem the string item to be added
105    * @param lookup the lookup to be associated with the string item
106    * @return true if the item has been added, otherwise - false.
107    */
108   public boolean add(String singleItem, Lookup lookup) {
109     return gaz.add(singleItem,lookup);
110   }
111 
112 } // OntoGazetteerImpl
113