Java URI to Relative URI relativize(URI base, URI child)

Here you can find the source of relativize(URI base, URI child)

Description

relativize

License

Open Source License

Return

the relative , using ".." if needed. <p> http://stackoverflow.com/questions/10801283/get-relative-path-of-two-uris-in-java

Declaration

public static URI relativize(URI base, URI child) 

Method Source Code

//package com.java2s;
/*/*from  w  w  w. ja v  a2 s  .co  m*/
 * Copyright 2012, the Dart project authors.
 * 
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

import java.net.URI;

public class Main {
    /**
     * @return the relative {@link URI}, using ".." if needed.
     *         <p>
     *         http://stackoverflow.com/questions/10801283/get-relative-path-of-two-uris-in-java
     */
    public static URI relativize(URI base, URI child) {
        // Normalize paths to remove . and .. segments
        base = base.normalize();
        child = child.normalize();

        // Split paths into segments
        String[] bParts = base.getPath().split("\\/");
        String[] cParts = child.getPath().split("\\/");

        // Remove common prefix segments
        int i = 0;
        while (i < bParts.length && i < cParts.length && bParts[i].equals(cParts[i])) {
            i++;
        }

        // Construct the relative path
        StringBuilder sb = new StringBuilder();
        for (int j = 0; j < (bParts.length - i); j++) {
            sb.append("../");
        }
        for (int j = i; j < cParts.length; j++) {
            if (j != i) {
                sb.append("/");
            }
            sb.append(cParts[j]);
        }

        return URI.create(uriEncode(sb.toString()));
    }

    /**
     * Convert from a non-uri encoded string to a uri encoded one.
     * 
     * @param str
     * @return the uri encoded input string
     */
    public static String uriEncode(String str) {
        StringBuilder builder = new StringBuilder(str.length() * 2);

        for (char c : str.toCharArray()) {
            switch (c) {
            case '%':
            case '?':
            case ';':
            case '#':
            case '"':
            case '\'':
            case '<':
            case '>':
            case ' ':
                // ' ' ==> "%20"
                builder.append('%');
                builder.append(Integer.toHexString(c));
                break;
            default:
                builder.append(c);
                break;
            }
        }

        return builder.toString();
    }
}

Related

  1. getRelativeURI(URI uri, URI relativeTo)
  2. relativeConfig(URI uri)
  3. relativeFileToURI(File file)
  4. relativePath(final URI baseURI, final URI pathURI)
  5. relativeURI(String basePath, String path)
  6. relativize(URI base, URI child)
  7. relativize(URI base, URI target)
  8. relativize(URI basePath, File path)
  9. relativize(URI baseUri, File f)