public static Path coerceToPathOrNull(Object path) { if (path instanceof Path) return (Path) path; if (path instanceof URL) return URLPath.valueOf((URL) path); /* #ifdef use:java.net.URI */ if (path instanceof URI) return URIPath.valueOf((URI) path); /* #endif */ if (path instanceof File) return FilePath.valueOf((File) path); String str; if (path instanceof gnu.lists.FString) // FIXME: || UntypedAtomic str = path.toString(); else if (!(path instanceof String)) return null; else str = (String) path; if (Path.uriSchemeSpecified(str)) return URIPath.valueOf(str); else return FilePath.valueOf(str); }
/** * Convert an absolute URI to one relatve to a given base. This goes beyond * java.net.URI.relativize in that if the arguments have a common prefix, it can create a relative * URI using "../" steps. */ public static String relativize(String in, String base) throws java.net.URISyntaxException, java.io.IOException { String baseStr = base; String inStr = in; /* #ifdef use:java.net.URI */ baseStr = new URI(baseStr).normalize().toString(); inStr = URLPath.valueOf(in).toURI().normalize().toString(); /* #endif */ int baseLen = baseStr.length(); int inLen = inStr.length(); int i = 0; int sl = 0; int colon = 0; for (; i < baseLen && i < inLen; i++) { char cb = baseStr.charAt(i); char ci = inStr.charAt(i); if (cb != ci) break; if (cb == '/') sl = i; if (cb == ':') colon = i; } if (colon > 0 && (sl > colon + 2 || baseLen <= colon + 2 || baseStr.charAt(colon + 2) != '/') /* && (colon + 2 != CLASS_RESOURCE_URI_PREFIX_LENGTH || ! inStr.substring(0, colon + 2).equals(CLASS_RESOURCE_URI_PREFIX) || getClassLoaderForURI(base) == getClassLoaderForURI(in)) */ ) { baseStr = baseStr.substring(sl + 1); inStr = inStr.substring(sl + 1); } else return in; /* #ifdef JAVA5 */ StringBuilder sbuf = new StringBuilder(); /* #else */ // StringBuffer sbuf = new StringBuffer(); /* #endif */ sl = 0; for (i = baseLen = baseStr.length(); --i >= 0; ) if (baseStr.charAt(i) == '/') // sep? sbuf.append("../"); sbuf.append(inStr); return sbuf.toString(); }