KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > URLHandler


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