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