Handler.java
01 package gate.util.protocols.gate;
02 
03 import java.io.*;
04 import java.io.FileNotFoundException;
05 import java.io.IOException;
06 import java.net.*;
07 import java.util.*;
08 import java.util.Iterator;
09 import java.util.Map;
10 import gate.GateConstants;
11 import gate.util.Files;
12 
13 
14 /**
15  * The handler for the "gate://" URLs.
16  * All this class does is to transparently transform a "gate://" URL into
17  * an URL of the corresponding type and forward all requests through it.
18  */
19 public class Handler extends URLStreamHandler {
20 
21   protected URLConnection openConnection(URL uthrows java.io.IOException {
22     URL actualURL = Handler.class.getResource(
23                       Files.getResourcePath() + u.getPath()
24                     );
25     if(actualURL == null){
26       System.out.println(
27         "WARNING: Starting with the GATE v.3 release the gate:// URLs have " +
28         "been deprecated. All resources used by processing resources are now " +
29         "external to the GATE distribution.\n" +
30         "You should rebuild your application!\n" 
31         "The system will try to guess the location but " +
32         "there are no guarantees:");
33       //try to fix the problem if caused by the externalisation of GATE 
34       //resources
35       File pluginHome = new File(
36               System.getProperty(GateConstants.GATE_HOME_PROPERTY_NAME)
37               "plugins");
38       Iterator pathIter = conversionMap.keySet().iterator();
39       while(pathIter.hasNext()){
40         String aPath = (String)pathIter.next();
41         if(u.getPath().startsWith(aPath)){
42           String oldPath = u.getPath();
43           String newPath = oldPath.replaceFirst(aPath, 
44                   (String)conversionMap.get(aPath));
45           actualURL = new File(pluginHome, newPath).toURI().toURL();
46           System.out.println(u.toExternalForm() " ---> " 
47                   actualURL.toExternalForm() "\n");
48           return actualURL.openConnection();
49         }
50       }
51     }
52     if(actualURL == nullthrow new FileNotFoundException(u.toExternalForm());
53     return actualURL.openConnection();
54   }
55   
56   static Map conversionMap;
57   static{
58     conversionMap = new HashMap();
59     conversionMap.put("/creole/BengaliNE""ANNIE/resources/BengaliNE");
60     conversionMap.put("/creole/chunker/VP""ANNIE/resources/VP/");
61     conversionMap.put("/creole/gazeteer""ANNIE/resources/gazetteer/");
62     conversionMap.put("/creole/heptag""ANNIE/resources/heptag/");
63     conversionMap.put("/creole/morph""Tools/resources/morph/");
64     conversionMap.put("/creole/namematcher""ANNIE/resources/othomatcher/");
65     conversionMap.put("/creole/ontology""Ontology_Tools/resources/");
66     conversionMap.put("/creole/splitter""ANNIE/resources/sentenceSplitter/");
67     conversionMap.put("/creole/tokeniser""ANNIE/resources/tokeniser/");
68     conversionMap.put("/creole/transducer/NE""ANNIE/resources/NE/");
69   }
70 }