2 ******************************************************************************
3 * Copyright (C) 2005-2011, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 ******************************************************************************
8 package com.ibm.icu.impl;
10 import java.io.BufferedReader;
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;
19 import java.util.Enumeration;
20 import java.util.HashMap;
22 import java.util.jar.JarEntry;
23 import java.util.jar.JarFile;
25 public abstract class URLHandler {
26 public static final String PROPNAME = "urlhandler.props";
28 private static final Map<String, Method> handlers;
30 private static final boolean DEBUG = ICUDebug.enabled("URLHandler");
33 Map<String, Method> h = null;
36 InputStream is = URLHandler.class.getResourceAsStream(PROPNAME);
38 ClassLoader loader = Utility.getFallbackClassLoader();
39 is = loader.getResourceAsStream(PROPNAME);
43 Class<?>[] params = { URL.class };
44 BufferedReader br = new BufferedReader(new InputStreamReader(is));
46 for (String line = br.readLine(); line != null; line = br.readLine()) {
49 if (line.length() == 0 || line.charAt(0) == '#') {
53 int ix = line.indexOf('=');
56 if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");
60 String key = line.substring(0, ix).trim();
61 String value = line.substring(ix+1).trim();
64 Class<?> cl = Class.forName(value);
65 Method m = cl.getDeclaredMethod("get", params);
68 h = new HashMap<String, Method>();
73 catch (ClassNotFoundException e) {
74 if (DEBUG) System.err.println(e);
76 catch(NoSuchMethodException e) {
77 if (DEBUG) System.err.println(e);
79 catch(SecurityException e) {
80 if (DEBUG) System.err.println(e);
85 } catch (Throwable t) {
86 if (DEBUG) System.err.println(t);
92 public static URLHandler get(URL url) {
97 String protocol = url.getProtocol();
99 if (handlers != null) {
100 Method m = handlers.get(protocol);
104 URLHandler handler = (URLHandler)m.invoke(null, new Object[] { url });
106 if (handler != null) {
110 catch(IllegalAccessException e) {
111 if (DEBUG) System.err.println(e);
113 catch(IllegalArgumentException e) {
114 if (DEBUG) System.err.println(e);
116 catch(InvocationTargetException e) {
117 if (DEBUG) System.err.println(e);
122 return getDefault(url);
125 protected static URLHandler getDefault(URL url) {
126 URLHandler handler = null;
128 String protocol = url.getProtocol();
130 if (protocol.equals("file")) {
131 handler = new FileURLHandler(url);
132 } else if (protocol.equals("jar") || protocol.equals("wsjar")) {
133 handler = new JarURLHandler(url);
135 } catch (Exception e) {
136 // ignore - just return null
141 private static class FileURLHandler extends URLHandler {
144 FileURLHandler(URL url) {
146 file = new File(url.toURI());
147 } catch (URISyntaxException use) {
150 if (file == null || !file.exists()) {
151 if (DEBUG) System.err.println("file does not exist - " + url.toString());
152 throw new IllegalArgumentException();
156 public void guide(URLVisitor v, boolean recurse, boolean strip) {
157 if (file.isDirectory()) {
158 process(v, recurse, strip, "/", file.listFiles());
160 v.visit(file.getName());
164 private void process(URLVisitor v, boolean recurse, boolean strip, String path, File[] files) {
165 for (int i = 0; i < files.length; i++) {
168 if (f.isDirectory()) {
170 process(v, recurse, strip, path + f.getName()+ '/', f.listFiles());
173 v.visit(strip? f.getName() : path + f.getName());
179 private static class JarURLHandler extends URLHandler {
183 JarURLHandler(URL url) {
185 prefix = url.getPath();
187 int ix = prefix.lastIndexOf("!/");
190 prefix = prefix.substring(ix + 2); // truncate after "!/"
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(":");
200 url = new URL("jar" + urlStr.substring(idx));
204 JarURLConnection conn = (JarURLConnection)url.openConnection();
205 jarFile = conn.getJarFile();
207 catch (Exception e) {
208 if (DEBUG) System.err.println("icurb jar error: " + e);
209 throw new IllegalArgumentException("jar error: " + e.getMessage());
213 public void guide(URLVisitor v, boolean recurse, boolean strip) {
215 Enumeration<JarEntry> entries = jarFile.entries();
217 while (entries.hasMoreElements()) {
218 JarEntry entry = entries.nextElement();
220 if (!entry.isDirectory()) { // skip just directory paths
221 String name = entry.getName();
223 if (name.startsWith(prefix)) {
224 name = name.substring(prefix.length());
226 int ix = name.lastIndexOf('/');
234 name = name.substring(ix+1);
243 catch (Exception e) {
244 if (DEBUG) System.err.println("icurb jar error: " + e);
249 public void guide(URLVisitor visitor, boolean recurse)
251 guide(visitor, recurse, true);
254 public abstract void guide(URLVisitor visitor, boolean recurse, boolean strip);
256 public interface URLVisitor {
257 void visit(String str);