1 18 package org.apache.tools.ant.launch; 19 20 import java.net.MalformedURLException ; 21 22 import java.net.URL ; 23 import java.io.File ; 24 import java.io.FilenameFilter ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.UnsupportedEncodingException ; 27 import java.text.CharacterIterator ; 28 import java.text.StringCharacterIterator ; 29 import java.util.Locale ; 30 31 37 public final class Locator { 38 41 public static final String URI_ENCODING = "UTF-8"; 42 private static boolean[] gNeedEscaping = new boolean[128]; 46 private static char[] gAfterEscaping1 = new char[128]; 48 private static char[] gAfterEscaping2 = new char[128]; 50 private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', 51 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 52 static { 54 for (int i = 0; i <= 0x1f; i++) { 55 gNeedEscaping[i] = true; 56 gAfterEscaping1[i] = gHexChs[i >> 4]; 57 gAfterEscaping2[i] = gHexChs[i & 0xf]; 58 } 59 gNeedEscaping[0x7f] = true; 60 gAfterEscaping1[0x7f] = '7'; 61 gAfterEscaping2[0x7f] = 'F'; 62 char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}', 63 '|', '\\', '^', '~', '[', ']', '`'}; 64 int len = escChs.length; 65 char ch; 66 for (int i = 0; i < len; i++) { 67 ch = escChs[i]; 68 gNeedEscaping[ch] = true; 69 gAfterEscaping1[ch] = gHexChs[ch >> 4]; 70 gAfterEscaping2[ch] = gHexChs[ch & 0xf]; 71 } 72 } 73 76 private Locator() { 77 } 78 79 88 public static File getClassSource(Class c) { 89 String classResource = c.getName().replace('.', '/') + ".class"; 90 return getResourceSource(c.getClassLoader(), classResource); 91 } 92 93 104 public static File getResourceSource(ClassLoader c, String resource) { 105 if (c == null) { 106 c = Locator.class.getClassLoader(); 107 } 108 URL url = null; 109 if (c == null) { 110 url = ClassLoader.getSystemResource(resource); 111 } else { 112 url = c.getResource(resource); 113 } 114 if (url != null) { 115 String u = url.toString(); 116 if (u.startsWith("jar:file:")) { 117 int pling = u.indexOf("!"); 118 String jarName = u.substring(4, pling); 119 return new File (fromURI(jarName)); 120 } else if (u.startsWith("file:")) { 121 int tail = u.indexOf(resource); 122 String dirName = u.substring(0, tail); 123 return new File (fromURI(dirName)); 124 } 125 } 126 return null; 127 } 128 129 146 public static String fromURI(String uri) { 147 Class uriClazz = null; 149 try { 150 uriClazz = Class.forName("java.net.URI"); 151 } catch (ClassNotFoundException cnfe) { 152 } 154 if (uriClazz != null && uri.startsWith("file:/")) { 159 try { 160 java.lang.reflect.Method createMethod 161 = uriClazz.getMethod("create", new Class [] {String .class}); 162 Object uriObj = createMethod.invoke(null, new Object [] {uri}); 163 java.lang.reflect.Constructor fileConst 164 = File .class.getConstructor(new Class [] {uriClazz}); 165 File f = (File ) fileConst.newInstance(new Object [] {uriObj}); 166 return f.getAbsolutePath(); 167 } catch (java.lang.reflect.InvocationTargetException e) { 168 Throwable e2 = e.getTargetException(); 169 if (e2 instanceof IllegalArgumentException ) { 170 throw (IllegalArgumentException ) e2; 172 } else { 173 e2.printStackTrace(); 175 } 176 } catch (Exception e) { 177 e.printStackTrace(); 179 } 180 } 181 182 184 URL url = null; 185 try { 186 url = new URL (uri); 187 } catch (MalformedURLException emYouEarlEx) { 188 } 190 if (url == null || !("file".equals(url.getProtocol()))) { 191 throw new IllegalArgumentException ("Can only handle valid file: URIs"); 192 } 193 StringBuffer buf = new StringBuffer (url.getHost()); 194 if (buf.length() > 0) { 195 buf.insert(0, File.separatorChar).insert(0, File.separatorChar); 196 } 197 String file = url.getFile(); 198 int queryPos = file.indexOf('?'); 199 buf.append((queryPos < 0) ? file : file.substring(0, queryPos)); 200 201 uri = buf.toString().replace('/', File.separatorChar); 202 203 if (File.pathSeparatorChar == ';' && uri.startsWith("\\") && uri.length() > 2 204 && Character.isLetter(uri.charAt(1)) && uri.lastIndexOf(':') > -1) { 205 uri = uri.substring(1); 206 } 207 String path = null; 208 try { 209 path = decodeUri(uri); 210 String cwd = System.getProperty("user.dir"); 211 int posi = cwd.indexOf(":"); 212 if ((posi > 0) && path.startsWith(File.separator)) { 213 path = cwd.substring(0, posi + 1) + path; 214 } 215 } catch (UnsupportedEncodingException exc) { 216 throw new IllegalStateException ("Could not convert URI to path: " 219 + exc.getMessage()); 220 } 221 return path; 222 } 223 224 232 public static String decodeUri(String uri) throws UnsupportedEncodingException { 233 if (uri.indexOf('%') == -1) { 234 return uri; 235 } 236 ByteArrayOutputStream sb = new ByteArrayOutputStream (uri.length()); 237 CharacterIterator iter = new StringCharacterIterator (uri); 238 for (char c = iter.first(); c != CharacterIterator.DONE; 239 c = iter.next()) { 240 if (c == '%') { 241 char c1 = iter.next(); 242 if (c1 != CharacterIterator.DONE) { 243 int i1 = Character.digit(c1, 16); 244 char c2 = iter.next(); 245 if (c2 != CharacterIterator.DONE) { 246 int i2 = Character.digit(c2, 16); 247 sb.write((char) ((i1 << 4) + i2)); 248 } 249 } 250 } else { 251 sb.write(c); 252 } 253 } 254 return sb.toString(URI_ENCODING); 255 } 256 264 public static String encodeURI(String path) throws UnsupportedEncodingException { 265 int i = 0; 266 int len = path.length(); 267 int ch = 0; 268 StringBuffer sb = null; 269 for (; i < len; i++) { 270 ch = path.charAt(i); 271 if (ch >= 128) { 273 break; 274 } 275 if (gNeedEscaping[ch]) { 276 if (sb == null) { 277 sb = new StringBuffer (path.substring(0, i)); 278 } 279 sb.append('%'); 280 sb.append(gAfterEscaping1[ch]); 281 sb.append(gAfterEscaping2[ch]); 282 } else if (sb != null) { 284 sb.append((char) ch); 285 } 286 } 287 288 if (i < len) { 290 if (sb == null) { 291 sb = new StringBuffer (path.substring(0, i)); 292 } 293 byte[] bytes = null; 295 byte b; 296 bytes = path.substring(i).getBytes(URI_ENCODING); 297 len = bytes.length; 298 299 for (i = 0; i < len; i++) { 301 b = bytes[i]; 302 if (b < 0) { 304 ch = b + 256; 305 sb.append('%'); 306 sb.append(gHexChs[ch >> 4]); 307 sb.append(gHexChs[ch & 0xf]); 308 } else if (gNeedEscaping[b]) { 309 sb.append('%'); 310 sb.append(gAfterEscaping1[b]); 311 sb.append(gAfterEscaping2[b]); 312 } else { 313 sb.append((char) b); 314 } 315 } 316 } 317 return sb == null ? path : sb.toString(); 318 } 319 320 333 public static URL fileToURL(File file) 334 throws MalformedURLException { 335 try { 336 return new URL (encodeURI(file.toURL().toString())); 337 } catch (UnsupportedEncodingException ex) { 338 throw new MalformedURLException (ex.toString()); 339 } 340 } 341 342 350 public static File getToolsJar() { 351 boolean toolsJarAvailable = false; 353 try { 354 Class.forName("com.sun.tools.javac.Main"); 356 toolsJarAvailable = true; 357 } catch (Exception e) { 358 try { 359 Class.forName("sun.tools.javac.Main"); 360 toolsJarAvailable = true; 361 } catch (Exception e2) { 362 } 364 } 365 if (toolsJarAvailable) { 366 return null; 367 } 368 String javaHome = System.getProperty("java.home"); 371 File toolsJar = new File (javaHome + "/lib/tools.jar"); 372 if (toolsJar.exists()) { 373 return toolsJar; 375 } 376 if (javaHome.toLowerCase(Locale.US).endsWith(File.separator + "jre")) { 377 javaHome = javaHome.substring(0, javaHome.length() - 4); 378 toolsJar = new File (javaHome + "/lib/tools.jar"); 379 } 380 if (!toolsJar.exists()) { 381 System.out.println("Unable to locate tools.jar. " 382 + "Expected to find it in " + toolsJar.getPath()); 383 return null; 384 } 385 return toolsJar; 386 } 387 388 401 public static URL [] getLocationURLs(File location) 402 throws MalformedURLException { 403 return getLocationURLs(location, new String []{".jar"}); 404 } 405 406 420 public static URL [] getLocationURLs(File location, 421 final String [] extensions) 422 throws MalformedURLException { 423 URL [] urls = new URL [0]; 424 425 if (!location.exists()) { 426 return urls; 427 } 428 if (!location.isDirectory()) { 429 urls = new URL [1]; 430 String path = location.getPath(); 431 for (int i = 0; i < extensions.length; ++i) { 432 if (path.toLowerCase().endsWith(extensions[i])) { 433 urls[0] = fileToURL(location); 434 break; 435 } 436 } 437 return urls; 438 } 439 File [] matches = location.listFiles( 440 new FilenameFilter () { 441 public boolean accept(File dir, String name) { 442 for (int i = 0; i < extensions.length; ++i) { 443 if (name.toLowerCase().endsWith(extensions[i])) { 444 return true; 445 } 446 } 447 return false; 448 } 449 }); 450 urls = new URL [matches.length]; 451 for (int i = 0; i < matches.length; ++i) { 452 urls[i] = fileToURL(matches[i]); 453 } 454 return urls; 455 } 456 } 457 | Popular Tags |