Java URI to Relative URI resolve(String path, URI... relativeTo)

Here you can find the source of resolve(String path, URI... relativeTo)

Description

resolve

License

Open Source License

Declaration

public static URI resolve(String path, URI... relativeTo) throws URISyntaxException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * //  w  w  w  .j  a  v  a2  s.c  o m
 * Contributors:
 *     Dimitrios Kolovos - initial API and implementation
 ******************************************************************************/

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static URI resolve(String path, URI... relativeTo) throws URISyntaxException {
        if (path == null)
            return null;

        final URI uri = new URI(path);

        if (uri.isAbsolute())
            return uri;

        else {
            for (URI parent : relativeTo) {
                if (parent != null) {

                    boolean parentIsJar = false;

                    if (parent.toString().startsWith("jar:file:/")) {
                        parentIsJar = true;
                        parent = new URI(parent.toString().replace("jar:file:/", "jar:/"));
                    }

                    URI resolved = parent.resolve(path);

                    if (resolved.isAbsolute() && resolved.getScheme() != null) {
                        if (parentIsJar) {
                            resolved = new URI(resolved.toString().replace("jar:/", "jar:file:/"));
                        }
                        return resolved;
                    } else {
                        return new URI(parent.toString() + path);
                    }
                }
            }
        }

        return new URI("file://" + uri);
    }
}

Related

  1. relativize(URI basePath, File path)
  2. relativize(URI baseUri, File f)
  3. relativize(URI baseURI, URI uriToRelativize)
  4. relativizeFromBase(String uri, URI base)
  5. resolve(String baseURI, String connectorURI)
  6. resolve(URI base, URI child)
  7. resolve(URI baseURI, String reference)
  8. resolveAbsoluteURI(final URI relativeURI)
  9. resolveFile(final URI relativeURI)