1 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.URL ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.Map ; 21 import java.util.jar.JarEntry ; 22 import java.util.jar.JarFile ; 23 24 public abstract class URLHandler { 25 public static final String PROPNAME = "urlhandler.props"; 26 27 private static final Map handlers; 28 29 private static final boolean DEBUG = ICUDebug.enabled("URLHandler"); 30 31 static { 32 Map h = null; 33 34 try { 35 InputStream is = URLHandler.class.getResourceAsStream(PROPNAME); 36 37 if (is == null) { 38 is = ClassLoader.getSystemClassLoader().getResourceAsStream(PROPNAME); 39 } 40 41 if (is != null) { 42 Class [] params = { URL .class }; 43 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 44 45 for (String 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 key = line.substring(0, ix).trim(); 60 String value = line.substring(ix+1).trim(); 61 62 try { 63 Class cl = Class.forName(value); 64 Method m = cl.getDeclaredMethod("get", params); 65 66 if (h == null) { 67 h = new HashMap (); 68 } 69 70 h.put(key, m); 71 } 72 catch (ClassNotFoundException e) { 73 if (DEBUG) System.err.println(e); 74 } 75 catch(NoSuchMethodException e) { 76 if (DEBUG) System.err.println(e); 77 } 78 catch(SecurityException e) { 79 if (DEBUG) System.err.println(e); 80 } 81 } 82 } 83 } catch (Throwable t) { 84 if (DEBUG) System.err.println(t); 85 } 86 87 handlers = h; 88 } 89 90 public static URLHandler get(URL url) { 91 if (url == null) { 92 return null; 93 } 94 95 String protocol = url.getProtocol(); 96 97 if (handlers != null) { 98 Method m = (Method )handlers.get(protocol); 99 100 if (m != null) { 101 try { 102 URLHandler handler = (URLHandler)m.invoke(null, new Object [] { url }); 103 104 if (handler != null) { 105 return handler; 106 } 107 } 108 catch(IllegalAccessException e) { 109 if (DEBUG) System.err.println(e); 110 } 111 catch(IllegalArgumentException e) { 112 if (DEBUG) System.err.println(e); 113 } 114 catch(InvocationTargetException e) { 115 if (DEBUG) System.err.println(e); 116 } 117 } 118 } 119 120 return getDefault(url); 121 } 122 123 protected static URLHandler getDefault(URL url) { 124 String 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 file; 137 String root; 138 139 FileURLHandler(URL url) { 140 root = url.getPath(); 141 file = new File (root); 142 143 if (!file.exists()) { 144 if (DEBUG) System.err.println("file does not exist"); 145 throw new IllegalArgumentException (); 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 path, File [] files) { 158 for (int i = 0; i < files.length; i++) { 159 File 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 jarFile; 174 String prefix; 175 176 JarURLHandler(URL url) { 177 try { 178 prefix = url.getPath(); 179 180 int ix = prefix.indexOf("!/"); 181 182 if (ix >= 0) { 183 prefix = prefix.substring(ix + 2); } 185 186 JarURLConnection conn = (JarURLConnection )url.openConnection(); 187 188 jarFile = conn.getJarFile(); 189 } 190 catch (Exception e) { 191 if (DEBUG) System.err.println("icurb jar error: " + e); 192 throw new IllegalArgumentException ("jar error: " + e.getMessage()); 193 } 194 } 195 196 public void guide(URLVisitor v, boolean recurse, boolean strip) { 197 try { 198 Enumeration entries = jarFile.entries(); 199 200 while (entries.hasMoreElements()) { 201 JarEntry entry = (JarEntry )entries.nextElement(); 202 203 if (!entry.isDirectory()) { String 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 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 str); 241 } 242 } 243 244 | Popular Tags |