MappingDefinition.java
001 /*
002  * MappingDefinition.java
003  *
004  * Copyright (c) 2002, The University of Sheffield.
005  *
006  * This file is part of GATE (see http://gate.ac.uk/), and is free
007  * software, licenced under the GNU Library General Public License,
008  * Version 2, June1991.
009  *
010  * A copy of this licence is included in the distribution in the file
011  * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
012  *
013  * borislav popov 02/2002
014  *
015  */
016 package gate.creole.gazetteer;
017 
018 
019 import java.io.*;
020 import java.net.URL;
021 import java.util.*;
022 
023 import gate.creole.ResourceInstantiationException;
024 import gate.util.BomStrippingInputStreamReader;
025 import gate.util.Files;
026 
027 /** Represents a mapping definition which maps gazetteer lists to ontology classes */
028 public class MappingDefinition extends gate.creole.AbstractLanguageResource
029                               implements List {
030 
031   private static final long serialVersionUID = 3617291212063848503L;
032 
033   /** the default encoding of the mapping */
034   private final static String ENCODING = "UTF-8";
035 
036   /** the list of nodes */
037   private List<MappingNode> nodes = new ArrayList<MappingNode>();
038 
039   /** the url of the mapping definition */
040   private URL url;
041 
042   /** set of gaz lists */
043   private Set<String> lists = new HashSet<String>();
044 
045   /** mapping between a list and a node */
046   private Map<String, MappingNode> nodesByList = new HashMap<String, MappingNode>();
047 
048 
049   /** Creates a new mapping definition */
050   public MappingDefinition() {
051   }
052 
053   /**Gets the urls from this definition
054    @return a list of all the ontology urls present in this mapping def   */
055   public List<String> getUrls() {
056     Set<String> result = new HashSet<String>();
057     for int i = ; i < nodes.size() ; i++ ) {
058       result.add(nodes.get(i).getOntologyID());
059     // for
060     return new ArrayList<String>(result);
061   }  // getUrls()
062 
063   /** Gets the url of this definition
064    *  @return the url of the definition */
065   public URL getURL() {
066     return url;
067   }
068 
069   /** Sets the url of this definition
070    *  @param aUrl the url of the definition*/
071   public void setURL(URL aUrl) {
072     url = aUrl;
073   }
074 
075   /**Loads the mapping definition
076    @throws ResourceInstantiationException if load fails.
077    */
078   public void load() throws ResourceInstantiationException,InvalidFormatException {
079     if (null == url) {
080       throw new ResourceInstantiationException("URL not set (null).");
081     }
082     try {
083       BufferedReader mapReader =
084       new BomStrippingInputStreamReader((url).openStream(), ENCODING);
085 
086       String line;
087       MappingNode node;
088       while (null != (line = mapReader.readLine())) {
089         if (!= line.trim().length()) {
090           node = new MappingNode(line);
091           this.add(node);
092         // if
093       //while
094 
095       mapReader.close();
096 
097     catch (InvalidFormatException ife){
098       throw new InvalidFormatException(url,"on load");
099     catch (IOException ioe) {
100       throw new ResourceInstantiationException(ioe);
101     }
102 
103 
104   // load();
105 
106   /**
107    * Stores the mapping definition
108    @throws ResourceInstantiationException if store fails.
109    */
110   public void store()throws ResourceInstantiationException{
111     if (null == url) {
112       throw new ResourceInstantiationException("URL not set (null).");
113     }
114     try {
115     File fileo = Files.fileFromURL(url);
116     fileo.delete();
117     BufferedWriter mapWriter = new BufferedWriter(new FileWriter(fileo));
118     for (int index = ; index < nodes.size() ; index++) {
119       mapWriter.write(nodes.get(index).toString());
120       mapWriter.newLine();
121     }
122     mapWriter.close();
123     catch (IOException ioe) {
124       throw new ResourceInstantiationException(ioe);
125     }
126   //store();
127 
128   /**
129    * Gets the gaz lists.
130    @return set of the gazetteer lists
131    */
132   public Set<String> getLists() {
133     return new HashSet<String>(lists);
134   }
135 
136   /**
137    * Gets node by list
138    @param list a gazetteer list filename
139    @return the mapping node that matches the list
140    */
141   public MappingNode getNodeByList(String list) {
142     return nodesByList.get(list);
143   }
144 
145   /*---implementation of interface java.util.List---*/
146 
147   public int size() {
148     return nodes.size();
149   }
150 
151   public boolean isEmpty() {
152     return nodes.isEmpty();
153   }
154 
155   public boolean contains(Object o) {
156     return nodes.contains(o);
157   }
158 
159   public Iterator iterator() {
160     return new SafeIterator();
161   }
162 
163   public Object[] toArray() {
164     return nodes.toArray();
165   }
166 
167   public Object[] toArray(Object[] a) {
168     return nodes.toArray(a);
169   }
170 
171   /**
172    * adds a new node, only if its list is new and uniquely mapped to this node.
173    @param o a node
174    @return true if the list of node is not already mapped with another node.
175    */
176   public boolean add(Object o) {
177     boolean result = false;
178     if (instanceof MappingNode) {
179       String list = ((MappingNode)o).getList();
180       if (!nodesByList.containsKey(list)) {
181         result = nodes.add((MappingNode)o);
182         nodesByList.put(list,(MappingNode)o);
183         lists.add(list);
184       // if unique
185     // if a linear node
186     return result;
187   // add()
188 
189   /**
190    * adds a new node at the specified position, only if its list is new and uniquely mapped to this node.
191    @param o a node
192    @param index position in the list
193    */
194   public void add(int index,Object o) {
195     if (instanceof MappingNode) {
196       String list = ((MappingNode)o).getList();
197       if (!nodesByList.containsKey(list)) {
198         nodes.add(index,(MappingNode)o);
199         nodesByList.put(list,(MappingNode)o);
200         lists.add(list);
201       // if unique
202     // if a linear node
203   // add()
204 
205   public Object set(int index, Object o) {
206     throw new UnsupportedOperationException("this method has not been implemented");
207   }
208 
209   public Object get(int index){
210     return nodes.get(index);
211   }
212 
213   public boolean remove(Object o) {
214     boolean result = false;
215     if (instanceof MappingNode) {
216       result = nodes.remove(o);
217       String list = ((MappingNode)o).getList();
218       lists.remove(list);
219       nodesByList.remove(list);
220     // if linear node
221     return result;
222   }// remove
223 
224   public Object remove(int index) {
225     Object result = null;
226     result = nodes.remove(index);
227     if (null!=result) {
228       String list = ((MappingNode)result).getList();
229       lists.remove(list);
230       nodesByList.remove(list);
231     }
232     return result;
233   }
234 
235   public boolean containsAll(Collection c) {
236     return nodes.containsAll(c);
237   }
238 
239   public boolean addAll(Collection c) {
240     boolean result = false;
241     Iterator iter = c.iterator();
242     Object o;
243     while (iter.hasNext()) {
244       o = iter.next();
245       if (instanceof MappingNode)  {
246         result |= add(o);
247       // instance of MappingNode
248     // while
249     return result;
250   // addAll(Collection)
251 
252   public boolean addAll(int index,Collection c) {
253     int size = nodes.size();
254     Iterator iter = c.iterator();
255     Object o;
256     while (iter.hasNext()) {
257       o = iter.next();
258       if (instanceof MappingNode)  {
259         add(index++, o);
260       // instance of MappingNode
261     // while
262     return (size!=nodes.size());
263   }//addAll(int,Collection)
264 
265   public boolean removeAll(Collection c) {
266     boolean result = false;
267     Iterator iter = c.iterator();
268     Object o;
269     while (iter.hasNext()) {
270       o = iter.next();
271       result |= remove(o);
272     }
273     return result;
274   }// removeAll()
275 
276 
277   public boolean retainAll(Collection c) {
278     int aprioriSize = nodes.size();
279     List scrap = new ArrayList();
280 
281     MappingNode node;
282     for (int index = 0; index < nodes.size(); index++) {
283       node = (MappingNodenodes.get(index);
284       if (c.contains(node)) {
285         scrap.add(node);
286       }
287     //for
288 
289     removeAll(scrap);
290 
291     return (aprioriSize != nodes.size());
292   }
293 
294 
295   public void clear() {
296     nodes.clear();
297     lists.clear();
298     nodesByList.clear();
299   }
300 
301   public boolean equals(Object o) {
302     boolean result = false;
303     if instanceof MappingDefinition ) {
304       MappingDefinition def = (MappingDefinitiono;
305       result &= nodes.equals(def.nodes);
306       result &= lists.equals(def.lists);
307       result &= nodesByList.equals(def.lists);
308     }// if
309     return result;
310   // equals()
311 
312   public List subList(int i1, int i2) {
313     return nodes.subList(i1,i2);
314   }
315 
316   public ListIterator listIterator(int index) {
317     throw new UnsupportedOperationException("this method is not implemented");
318   }
319   public ListIterator listIterator() {
320     throw new UnsupportedOperationException("this method is not implemented");
321   }
322 
323   public int lastIndexOf(Object o) {
324     return nodes.lastIndexOf(o);
325   }
326 
327   public int indexOf(Object o) {
328     return nodes.indexOf(o);
329   }
330 
331  /*---end of implementation of interface java.util.List---*/
332 
333  /*-----------internal classes -------------*/
334 
335   /**Provides means for safe iteration over
336    * the entries of the Mapping Definition  */
337   private class SafeIterator implements Iterator {
338     private int index = 0;
339     private boolean removeCalled = false;
340 
341     public boolean hasNext() {
342       return (index < nodes.size());
343     }
344 
345     public Object next() {
346       removeCalled = false;
347       return nodes.get(index++);
348     }
349 
350     public void remove() {
351       if (!removeCalled && index > 0  ) {
352         index--;
353         MappingDefinition.this.remove(nodes.get(index));
354       }// if possible remove
355       removeCalled = true;
356     // remove
357 
358 
359   // class SafeIterator
360 
361 // class MappingDefinition