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