2 ******************************************************************************
\r
3 * Copyright (C) 2005, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 ******************************************************************************
\r
8 package com.ibm.icu.impl;
\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
24 public abstract class URLHandler {
\r
25 public static final String PROPNAME = "urlhandler.props";
\r
27 private static final Map handlers;
\r
29 private static final boolean DEBUG = ICUDebug.enabled("URLHandler");
\r
35 InputStream is = URLHandler.class.getResourceAsStream(PROPNAME);
\r
38 is = ClassLoader.getSystemClassLoader().getResourceAsStream(PROPNAME);
\r
42 Class[] params = { URL.class };
\r
43 BufferedReader br = new BufferedReader(new InputStreamReader(is));
\r
45 for (String line = br.readLine(); line != null; line = br.readLine()) {
\r
48 if (line.length() == 0 || line.charAt(0) == '#') {
\r
52 int ix = line.indexOf('=');
\r
55 if (DEBUG) System.err.println("bad urlhandler line: '" + line + "'");
\r
59 String key = line.substring(0, ix).trim();
\r
60 String value = line.substring(ix+1).trim();
\r
63 Class cl = Class.forName(value);
\r
64 Method m = cl.getDeclaredMethod("get", params);
\r
72 catch (ClassNotFoundException e) {
\r
73 if (DEBUG) System.err.println(e);
\r
75 catch(NoSuchMethodException e) {
\r
76 if (DEBUG) System.err.println(e);
\r
78 catch(SecurityException e) {
\r
79 if (DEBUG) System.err.println(e);
\r
83 } catch (Throwable t) {
\r
84 if (DEBUG) System.err.println(t);
\r
90 public static URLHandler get(URL url) {
\r
95 String protocol = url.getProtocol();
\r
97 if (handlers != null) {
\r
98 Method m = (Method)handlers.get(protocol);
\r
102 URLHandler handler = (URLHandler)m.invoke(null, new Object[] { url });
\r
104 if (handler != null) {
\r
108 catch(IllegalAccessException e) {
\r
109 if (DEBUG) System.err.println(e);
\r
111 catch(IllegalArgumentException e) {
\r
112 if (DEBUG) System.err.println(e);
\r
114 catch(InvocationTargetException e) {
\r
115 if (DEBUG) System.err.println(e);
\r
120 return getDefault(url);
\r
123 protected static URLHandler getDefault(URL url) {
\r
124 String protocol = url.getProtocol();
\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
135 private static class FileURLHandler extends URLHandler {
\r
139 FileURLHandler(URL url) {
\r
140 root = url.getPath();
\r
141 file = new File(root);
\r
143 if (!file.exists()) {
\r
144 if (DEBUG) System.err.println("file does not exist");
\r
145 throw new IllegalArgumentException();
\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
153 v.visit(file.getName());
\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
161 if (f.isDirectory()) {
\r
163 process(v, recurse, strip, path + f.getName()+ '/', f.listFiles());
\r
166 v.visit(strip? f.getName() : path + f.getName());
\r
172 private static class JarURLHandler extends URLHandler {
\r
176 JarURLHandler(URL url) {
\r
178 prefix = url.getPath();
\r
180 int ix = prefix.indexOf("!/");
\r
183 prefix = prefix.substring(ix + 2); // truncate after "!/"
\r
186 JarURLConnection conn = (JarURLConnection)url.openConnection();
\r
188 jarFile = conn.getJarFile();
\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
196 public void guide(URLVisitor v, boolean recurse, boolean strip) {
\r
198 Enumeration entries = jarFile.entries();
\r
200 while (entries.hasMoreElements()) {
\r
201 JarEntry entry = (JarEntry)entries.nextElement();
\r
203 if (!entry.isDirectory()) { // skip just directory paths
\r
204 String name = entry.getName();
\r
206 if (name.startsWith(prefix)) {
\r
207 name = name.substring(prefix.length());
\r
209 int ix = name.lastIndexOf('/');
\r
217 name = name.substring(ix+1);
\r
226 catch (Exception e) {
\r
227 if (DEBUG) System.err.println("icurb jar error: " + e);
\r
232 public void guide(URLVisitor visitor, boolean recurse)
\r
234 guide(visitor, recurse, true);
\r
237 public abstract void guide(URLVisitor visitor, boolean recurse, boolean strip);
\r
239 public interface URLVisitor {
\r
240 void visit(String str);
\r