Java Utililty Methods URI to Relative URI

List of utility methods to do URI to Relative URI

Description

The list of methods to do URI to Relative URI are organized into topic(s).

Method

FileresolveFileUri(String fullPageURI, File rootPath)
resolve File Uri
URI uri = URI.create(fullPageURI);
try {
    return new File(uri);
} catch (IllegalArgumentException e) {
    if (!"file".equals(uri.getScheme()) || rootPath == null) {
        throw e;
    URI rootUri = rootPath.toURI();
...
URIresolvePartUri(URI sourcePartUri, URI targetUri)
Resolve a source uri against a target.
if (sourcePartUri == null || sourcePartUri.isAbsolute()) {
    throw new IllegalArgumentException("sourcePartUri invalid - " + sourcePartUri);
if (targetUri == null || targetUri.isAbsolute()) {
    throw new IllegalArgumentException("targetUri invalid - " + targetUri);
return sourcePartUri.resolve(targetUri);
URIresolvePath(URI root, String subPath)
root: smb:/host/my path: path/blub ====> smb:/host/my/path/blub
String rootStr = root.toString();
if (subPath.startsWith("/")) {
    subPath = subPath.substring(1, subPath.length());
return new URI(rootStr + (!rootStr.endsWith("/") ? "/" : "") + subPath);
intresolvePort(URI uri)
resolve Port
if (uri == null) {
    return (-1);
int port = uri.getPort();
if (port <= 0) {
    if ("http".equals(uri.getScheme()))
        port = 80;
    else
...
URIresolveReferenceStartingWithQueryString(final URI baseURI, final URI reference)
Resolves a reference starting with a query string.
String baseUri = baseURI.toString();
baseUri = baseUri.indexOf('?') > -1 ? baseUri.substring(0, baseUri.indexOf('?')) : baseUri;
return URI.create(baseUri + reference.toString());
URIresolveRelativeURI(String baseURI, String uriRef)
Constructs an absolute URI from the given URI reference and a base URI.
URI uri = (null != baseURI) ? URI.create(baseURI) : URI.create("");
if (null != baseURI && null == uri.getScheme()) {
    throw new IllegalArgumentException("Base URI has no scheme component: " + baseURI);
return uri.resolve(uriRef);
StringresolveRelativeURI(URI base, String rel)
resolve Relative URI
if (base.getFragment() != null) {
    return base + rel;
return base.resolve(rel).toString();
URIresolveURI(File base_dir, URI uri)
If the uri has a file scheme, return a new absolute file url joining it to the base directory.
StringBuilder path = new StringBuilder();
if (uri.getHost() != null) {
    path.append(uri.getHost());
if (uri.getPath() != null) {
    path.append(uri.getPath());
if (uri.getScheme().equals("file")) {
...
StringresolveURI(final String path)
resolve URI
try {
    URI uri = new URI(path);
    if (uri.getScheme() == null) {
        String fragment = uri.getFragment();
        URI part = new File(uri.getPath()).toURI();
        uri = new URI(part.getScheme(), part.getPath(), fragment);
    return uri.toString();
...
URIresolveURI(String uri)
Resolves the specified uri.
if (uri == null) {
    uri = "";
URI temp = null;
try {
    URL url = new URL(uri);
    temp = url.toURI();
} catch (MalformedURLException | URISyntaxException mue) {
...