]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/impl/URLHandler.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / impl / URLHandler.java
1 /*\r
2  ******************************************************************************\r
3  * Copyright (C) 2005-2010, International Business Machines Corporation and   *\r
4  * others. All Rights Reserved.                                               *\r
5  ******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.impl;\r
9 \r
10 import java.io.BufferedReader;\r
11 import java.io.File;\r
12 import java.io.InputStream;\r
13 import java.io.InputStreamReader;\r
14 import java.lang.reflect.InvocationTargetException;\r
15 import java.lang.reflect.Method;\r
16 import java.net.JarURLConnection;\r
17 import java.net.URISyntaxException;\r
18 import java.net.URL;\r
19 import java.util.Enumeration;\r
20 import java.util.HashMap;\r
21 import java.util.Map;\r
22 import java.util.jar.JarEntry;\r
23 import java.util.jar.JarFile;\r
24 \r
25 public abstract class URLHandler {\r
26     public static final String PROPNAME = "urlhandler.props";\r
27     \r
28     private static final Map<String, Method> handlers;\r
29     \r
30     private static final boolean DEBUG = ICUDebug.enabled("URLHandler");\r
31     \r
32     static {\r
33         Map<String, Method> h = null;\r
34         \r
35         try {\r
36             InputStream is = URLHandler.class.getResourceAsStream(PROPNAME);\r
37             if (is == null) {\r
38                 ClassLoader loader = Utility.getFallbackClassLoader();\r
39                 is = loader.getResourceAsStream(PROPNAME);\r
40             }\r
41 \r
42             if (is != null) {\r
43                 Class<?>[] params = { URL.class };\r
44                 BufferedReader br = new BufferedReader(new InputStreamReader(is));\r
45                 \r
46                 for (String line = br.readLine(); line != null; line = br.readLine()) {\r
47                     line = line.trim();\r
48                     \r
49                     if (line.length() == 0 || line.charAt(0) == '#') {\r
50                         continue;\r
51                     }\r
52                     \r
53                     int ix = line.indexOf('=');\r
54                     \r
55                     if (ix == -1) {\r
56                         if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");\r
57                         break;\r
58                     }\r
59                     \r
60                     String key = line.substring(0, ix).trim();\r
61                     String value = line.substring(ix+1).trim();\r
62                     \r
63                     try {\r
64                         Class<?> cl = Class.forName(value);\r
65                         Method m = cl.getDeclaredMethod("get", params);\r
66                         \r
67                         if (h == null) {\r
68                             h = new HashMap<String, Method>();\r
69                         }\r
70                         \r
71                         h.put(key, m);\r
72                     }\r
73                     catch (ClassNotFoundException e) {\r
74                         if (DEBUG) System.err.println(e);\r
75                     }\r
76                     catch(NoSuchMethodException e) {\r
77                         if (DEBUG) System.err.println(e);\r
78                     }\r
79                     catch(SecurityException e) {\r
80                         if (DEBUG) System.err.println(e);\r
81                     }\r
82                 }\r
83             }\r
84         } catch (Throwable t) {\r
85             if (DEBUG) System.err.println(t);\r
86         }\r
87         \r
88         handlers = h;\r
89     }\r
90     \r
91     public static URLHandler get(URL url) {\r
92         if (url == null) {\r
93             return null;\r
94         }\r
95         \r
96         String protocol = url.getProtocol();\r
97         \r
98         if (handlers != null) {\r
99             Method m = handlers.get(protocol);\r
100             \r
101             if (m != null) {\r
102                 try {\r
103                     URLHandler handler = (URLHandler)m.invoke(null, new Object[] { url });\r
104                     \r
105                     if (handler != null) {\r
106                         return handler;\r
107                     }\r
108                 }\r
109                 catch(IllegalAccessException e) {\r
110                     if (DEBUG) System.err.println(e);\r
111                 }\r
112                 catch(IllegalArgumentException e) {\r
113                     if (DEBUG) System.err.println(e);\r
114                 }\r
115                 catch(InvocationTargetException e) {\r
116                     if (DEBUG) System.err.println(e);\r
117                 }\r
118             }\r
119         }\r
120         \r
121         return getDefault(url);\r
122     }\r
123     \r
124     protected static URLHandler getDefault(URL url) {\r
125         URLHandler handler = null;\r
126 \r
127         String protocol = url.getProtocol();\r
128         try {\r
129             if (protocol.equals("file")) {\r
130                 handler = new FileURLHandler(url);\r
131             } else if (protocol.equals("jar") || protocol.equals("wsjar")) {\r
132                 handler = new JarURLHandler(url);\r
133             }\r
134         } catch (Exception e) {\r
135             // ignore - just return null\r
136         }\r
137         return handler;\r
138     }\r
139     \r
140     private static class FileURLHandler extends URLHandler {\r
141         File file;\r
142 \r
143         FileURLHandler(URL url) {\r
144             try {\r
145                 file = new File(url.toURI());\r
146             } catch (URISyntaxException use) {\r
147                 // fall through\r
148             }\r
149             if (file == null || !file.exists()) {\r
150                 if (DEBUG) System.err.println("file does not exist - " + url.toString());\r
151                 throw new IllegalArgumentException();\r
152             }\r
153         }\r
154         \r
155         public void guide(URLVisitor v, boolean recurse, boolean strip) {\r
156             if (file.isDirectory()) {\r
157                 process(v, recurse, strip, "/", file.listFiles());\r
158             } else {\r
159                 v.visit(file.getName());\r
160             }\r
161         }\r
162         \r
163         private void process(URLVisitor v, boolean recurse, boolean strip, String path, File[] files) {\r
164             for (int i = 0; i < files.length; i++) {\r
165                 File f = files[i];\r
166                 \r
167                 if (f.isDirectory()) {\r
168                     if (recurse) {\r
169                         process(v, recurse, strip, path + f.getName()+ '/', f.listFiles());\r
170                     }\r
171                 } else {\r
172                     v.visit(strip? f.getName() : path + f.getName());\r
173                 }\r
174             }\r
175         }\r
176     }\r
177     \r
178     private static class JarURLHandler extends URLHandler {\r
179         JarFile jarFile;\r
180         String prefix;\r
181 \r
182         JarURLHandler(URL url) {\r
183             try {\r
184                 prefix = url.getPath();\r
185                 \r
186                 int ix = prefix.indexOf("!/");\r
187                 \r
188                 if (ix >= 0) {\r
189                     prefix = prefix.substring(ix + 2); // truncate after "!/"\r
190                 }\r
191 \r
192                 String protocol = url.getProtocol();\r
193                 if (!protocol.equals("jar")) {\r
194                     // change the protocol to "jar"\r
195                     // Note: is this really OK?\r
196                     String urlStr = url.toString();\r
197                     int idx = urlStr.indexOf(":");\r
198                     if (idx != -1) {\r
199                         url = new URL("jar" + urlStr.substring(idx));\r
200                     }\r
201                 }\r
202 \r
203                 JarURLConnection conn = (JarURLConnection)url.openConnection();\r
204                 jarFile = conn.getJarFile();\r
205             }\r
206             catch (Exception e) {\r
207                 if (DEBUG) System.err.println("icurb jar error: " + e);\r
208                 throw new IllegalArgumentException("jar error: " + e.getMessage());\r
209             }\r
210         }\r
211         \r
212         public void guide(URLVisitor v, boolean recurse, boolean strip) {\r
213             try {\r
214                 Enumeration<JarEntry> entries = jarFile.entries();\r
215                 \r
216                 while (entries.hasMoreElements()) {\r
217                     JarEntry entry = entries.nextElement();\r
218                     \r
219                     if (!entry.isDirectory()) { // skip just directory paths\r
220                         String name = entry.getName();\r
221                         \r
222                         if (name.startsWith(prefix)) {\r
223                             name = name.substring(prefix.length());\r
224                             \r
225                             int ix = name.lastIndexOf('/');\r
226                             \r
227                             if (ix != -1) {\r
228                                 if (!recurse) {\r
229                                     continue;\r
230                                 }\r
231                                 \r
232                                 if (strip) {\r
233                                     name = name.substring(ix+1);\r
234                                 }\r
235                             }\r
236                             \r
237                             v.visit(name);\r
238                         }\r
239                     }\r
240                 }\r
241             }\r
242             catch (Exception e) {\r
243                 if (DEBUG) System.err.println("icurb jar error: " + e);\r
244             }\r
245         }\r
246     }\r
247 \r
248     public void guide(URLVisitor visitor, boolean recurse)\r
249     {\r
250         guide(visitor, recurse, true);\r
251     }\r
252     \r
253     public abstract void guide(URLVisitor visitor, boolean recurse, boolean strip);\r
254     \r
255     public interface URLVisitor {\r
256         void visit(String str);\r
257     }\r
258 }\r