1 package gnu.text; 2 3 import java.net.URI ; 4 5 import java.net.URL ; 6 import java.net.URLConnection ; 7 import java.io.*; 8 import gnu.lists.FString; 9 import gnu.mapping.CallContext; 10 import gnu.mapping.InPort; 11 import gnu.mapping.Table2D; 12 13 public class URI_utils 14 { 15 public static boolean isAbsolute (Object uri) 16 { 17 if (uri instanceof URL ) 18 return true; 19 if (uri instanceof File) 20 return ((File) uri).isAbsolute(); 21 22 if (uri instanceof URI ) 23 return ((URI ) uri).isAbsolute(); 24 25 return InPort.uriSchemeSpecified(uri.toString()); 26 } 27 28 29 static URL resourceURL (Object uri, String str) 30 throws java.io.IOException 31 { 32 ClassLoader loader = getClassLoaderForURI(uri); 37 if (loader == null) 38 throw new IOException("unknown class-loader for URI '"+str+'\''); 39 URL url 40 = loader.getResource(str.substring(CLASS_RESOURCE_URI_PREFIX_LENGTH)); 41 if (url == null) 42 throw new FileNotFoundException(str); 43 return url; 44 } 45 46 public static URL FiletoURL (File file) 47 { 48 try 49 { 50 51 52 return file.toURI().toURL(); 53 54 56 57 60 } 61 catch (Throwable ex) 62 { 63 throw gnu.mapping.WrappedException.wrapIfNeeded(ex); 64 } 65 } 66 67 public static URL toURL (Object uri) 68 throws java.io.IOException 69 { 70 uri = toFileOrURL(uri); 71 if (uri instanceof File) 72 return FiletoURL((File) uri); 73 return (URL ) uri; 74 } 75 76 public static long lastModified (Object uri) 77 { 78 try 79 { 80 uri = toFileOrURL(uri); 81 if (uri instanceof File) 82 return ((File) uri).lastModified(); 83 return ((URL ) uri).openConnection().getLastModified(); 84 } 85 catch (Throwable ex) 86 { 87 return 0; 88 } 89 } 90 91 public static Object toFileOrURL (Object uri) 92 throws java.io.IOException 93 { 94 if (uri instanceof URL || uri instanceof File) 95 return uri; 96 String str = uri.toString(); 97 if (str.startsWith(CLASS_RESOURCE_URI_PREFIX)) 98 return resourceURL(uri, str); 99 if (! InPort.uriSchemeSpecified(str)) 100 { 101 CallContext ctx = CallContext.getInstance(); 102 String base = ctx.getBaseUriRaw(); 103 if (base == null || ! InPort.uriSchemeSpecified(base)) 104 { 105 char fileSep = File.separatorChar; 106 str = str.replace('/', fileSep); 107 File file = new File(str); 108 if (base == null || file.isAbsolute()) 109 return file; 110 File dir = new File(base.replace('/', fileSep)); 111 if (! dir.isDirectory()) 112 { 113 114 dir = dir.getParentFile(); 115 116 118 } 119 return new File(dir, str); 120 } 121 try 122 { 123 str = resolve(str, base).toString(); 124 } 125 catch (java.net.URISyntaxException ex) 126 { 127 throw new IOException("invalid URI syntax: '"+str+'\''); 128 } 129 } 130 if (str.startsWith("file:")) 134 { 135 136 try { return new File(new URI (str)); } 137 catch (Throwable ex) { } 138 139 142 } 143 144 return new URL (str); 145 } 146 147 public static InputStream getInputStream (Object uri) 148 throws java.io.IOException 149 { 150 uri = toFileOrURL(uri); 151 if (uri instanceof File) 152 return new FileInputStream((File) uri); 153 return ((URL ) uri).openConnection().getInputStream(); 154 } 155 156 public static OutputStream getOutputStream (Object uri) 157 throws java.io.IOException 158 { 159 uri = toFileOrURL(uri); 160 if (uri instanceof File) 161 return new FileOutputStream((File) uri); 162 URLConnection conn = ((URL ) uri).openConnection(); 163 conn.setDoInput(false); 164 conn.setDoOutput(true); 165 return conn.getOutputStream(); 166 } 167 168 169 public static URI toURI (Object uri) 170 throws java.net.URISyntaxException 171 { 172 if (uri instanceof String || uri instanceof FString) 173 return nametoURI(uri.toString()); 174 if (uri instanceof File) 175 return FiletoURI((File) uri); 176 if (uri instanceof URL ) 177 uri = URLtoURI((URL ) uri); 178 return (URI ) uri; 179 } 180 181 190 191 192 static URI URLtoURI (URL url) 193 throws java.net.URISyntaxException 194 { 195 196 198 return new URI (url.toString()); 199 200 } 201 202 207 208 public static String toURIString (Object uri) 209 210 throws java.net.URISyntaxException 211 212 { 213 if (uri instanceof String || uri instanceof FString) 214 return nametoURIString(uri.toString()); 215 if (uri instanceof File) 216 return FiletoURI((File) uri).toString(); 217 218 if (uri instanceof URL ) 219 uri = URLtoURI((URL ) uri); 220 return ((URI ) uri).toString(); 221 222 224 } 225 226 public static String nametoURIString (String str) 227 228 throws java.net.URISyntaxException 229 230 { 231 if (InPort.uriSchemeSpecified(str)) 232 return str; 233 char fileSep = File.separatorChar; 234 if (fileSep != '/') 235 str = str.replace('/', fileSep); 236 return FiletoURI(new File(str)).toString(); 237 } 238 239 240 public static URI nametoURI (String str) 241 throws java.net.URISyntaxException 242 { 243 try 244 { 245 return new URI (str); 246 } 247 catch (java.net.URISyntaxException ex) 248 { 249 if (InPort.uriSchemeSpecified(str)) 250 throw ex; 251 char fileSep = File.separatorChar; 252 if (fileSep != '/') 253 str = str.replace('/', fileSep); 254 return FiletoURI(new File(str)); 255 } 256 } 257 258 public static URI FiletoURI (File file) 259 throws java.net.URISyntaxException 260 { 261 if (file.isAbsolute()) 262 return file.toURI(); 263 265 String fname = file.toString(); 266 char fileSep = File.separatorChar; 267 if (fileSep != '/') 268 fname = fname.replace(fileSep, '/'); 269 return new URI (null, null, fname, null); 270 } 271 272 280 281 private static gnu.mapping.Symbol keyClassLoader = 282 new gnu.mapping.Symbol(null, "(class-loader)"); 283 284 static ClassLoader getClassLoaderForURI (Object uri) 285 { 286 Table2D table = Table2D.getInstance(); 293 synchronized (table) 294 { 295 return (ClassLoader ) table.get(uri, keyClassLoader, null); 296 } 297 } 298 299 static private void setClassLoaderForURI (Object uri, ClassLoader loader) 300 { 301 Table2D table = Table2D.getInstance(); 302 synchronized (table) 303 { 304 table.put(uri, keyClassLoader, loader); 305 } 306 } 307 308 311 public static final String CLASS_RESOURCE_URI_PREFIX = "class-resource:/"; 312 313 314 public static final int CLASS_RESOURCE_URI_PREFIX_LENGTH = 16; 315 316 public static 317 318 URI 319 320 322 makeClassResourceURI (Class clas) 323 { 324 String cname = clas.getName(); 325 int dot = cname.lastIndexOf('.'); 326 327 329 StringBuffer sbuf = new StringBuffer (); 330 331 sbuf.append(CLASS_RESOURCE_URI_PREFIX); 332 if (dot >= 0) 333 { 334 sbuf.append(cname.substring(0, dot)); 335 sbuf.append('/'); 336 cname = cname.substring(dot+1); 337 } 338 sbuf.append(cname); 339 String str = sbuf.toString(); 340 341 URI uri; 342 try 343 { 344 uri = new URI (str); 345 } 346 catch (Throwable ex) 347 { 348 throw gnu.mapping.WrappedException.wrapIfNeeded(ex); 349 } 350 351 353 ClassLoader loader = clas.getClassLoader(); 354 setClassLoaderForURI(uri, loader); 355 return uri; 356 } 357 358 public static 359 360 URI 361 362 364 resolve (Object relative, Object base) 365 throws java.net.URISyntaxException 366 { 367 String rstr; 368 if (relative instanceof URL ) 369 { return URLtoURI((URL ) relative); 371 } 372 373 else if (relative instanceof URI ) 374 { 375 URI rel = (URI ) relative; 376 if (rel.isAbsolute()) 377 return rel; 378 rstr = rel.toString(); 379 } 380 381 else if (relative instanceof String || relative instanceof FString) 382 { 383 rstr = relative.toString(); 384 if (InPort.uriSchemeSpecified(rstr)) 385 return toURI(rstr); 386 } 387 else if (relative instanceof File) 388 { 389 File file = (File) relative; 390 if (file.isAbsolute()) 391 { 392 393 return file.toURI(); 394 395 } 396 rstr = file.toString(); 397 } 398 else 399 throw new gnu.mapping.WrongType("resolve-uri", 1, "uri/file/string"); 400 char fileSep = File.separatorChar; 401 if (fileSep != '/') 402 { 403 if (rstr.length() >= 2 405 && ((rstr.charAt(1) == ':' && Character.isLetter(rstr.charAt(0))) 406 || (rstr.charAt(0) == fileSep && rstr.charAt(1) == fileSep))) 407 { 408 return toURI(new File(rstr)); 409 } 410 rstr = rstr.replace(fileSep, '/'); 411 } 412 413 URI resolved = toURI(base).resolve(new URI (null, rstr, null)); 414 415 447 if (resolved.toString().startsWith(CLASS_RESOURCE_URI_PREFIX)) 448 { 449 ClassLoader loader = getClassLoaderForURI(base); 450 setClassLoaderForURI(resolved, loader); 451 } 452 return resolved; 453 } 454 455 459 public static Object relativize (Object in, Object base) 460 throws java.net.URISyntaxException , java.io.IOException 461 { 462 String baseStr = URI_utils.toURI(base).normalize().toString(); 463 String inStr = URI_utils.toURI(URI_utils.toURL(in)).normalize().toString(); 464 int baseLen = baseStr.length(); 465 int inLen = inStr.length(); 466 int i = 0; 467 int sl = 0; 468 int colon = 0; 469 for (; i < baseLen && i < inLen; i++) 470 { 471 char cb = baseStr.charAt(i); 472 char ci = inStr.charAt(i); 473 if (cb != ci) 474 break; 475 if (cb == '/') 476 sl = i; 477 if (cb == ':') 478 colon = i; 479 } 480 if (colon > 0 481 && (sl > colon + 2 || baseLen <= colon+2 || baseStr.charAt(colon+2) != '/') 482 && (colon + 2 != CLASS_RESOURCE_URI_PREFIX_LENGTH 483 || ! inStr.substring(0, colon + 2).equals(CLASS_RESOURCE_URI_PREFIX) 484 || getClassLoaderForURI(base) == getClassLoaderForURI(in))) 485 { 486 baseStr = baseStr.substring(sl+1); 487 inStr = inStr.substring(sl+1); 488 } 489 else 490 return in; 491 492 494 StringBuffer sbuf = new StringBuffer (); 495 496 sl = 0; 497 for (i = baseLen = baseStr.length(); --i >= 0; ) 498 if (baseStr.charAt(i) == '/') sbuf.append("../"); 500 sbuf.append(inStr); 501 return sbuf.toString(); 502 } 503 } 504 | Popular Tags |