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