* @throws java.net.URISyntaxException if either of the strings is not a valid URI or
* if the resolution fails
*/
public static URI makeAbsolute(String relativeURI, String base) throws URI.URISyntaxException {
URI absoluteURI;
// System.err.println("makeAbsolute " + relativeURI + " against base " + base);
if (relativeURI == null) {
absoluteURI = new URI(ResolveURI.escapeSpaces(base), true);
if (!absoluteURI.isAbsolute()) {
throw new URI.URISyntaxException(base + ": Relative URI not supplied, so base URI must be absolute");
} else {
return absoluteURI;
}
}
relativeURI = ResolveURI.escapeSpaces(relativeURI);
base = ResolveURI.escapeSpaces(base);
try {
if (base==null || base.length() == 0) {
absoluteURI = new URI(relativeURI, true);
if (!absoluteURI.isAbsolute()) {
String expandedBase = ResolveURI.tryToExpand(base);
if (!expandedBase.equals(base)) { // prevent infinite recursion
return makeAbsolute(relativeURI, expandedBase);
}
}
} else {
URI baseURI;
try {
baseURI = new URI(base);
} catch (URI.URISyntaxException e) {
throw new URI.URISyntaxException(base + ": Invalid base URI: " + e.getMessage());
}
if (baseURI.getFragment() != null) {
int hash = base.indexOf('#');
if (hash >= 0) {
base = base.substring(0, hash);
}
try {
baseURI = new URI(base);
} catch (URI.URISyntaxException e) {
throw new URI.URISyntaxException(base + ": Invalid base URI: " + e.getMessage());
}
}
try {
new URI(relativeURI, true); // for validation only
} catch (URI.URISyntaxException e) {
throw new URI.URISyntaxException(base + ": Invalid relative URI: " + e.getMessage());
}
absoluteURI = (relativeURI.length() == 0 ? baseURI : baseURI.resolve(relativeURI));
}
} catch (IllegalArgumentException err0) {
// can be thrown by resolve() when given a bad URI
throw new URI.URISyntaxException(relativeURI + ": Cannot resolve URI against base " + Err.wrap(base));
}