Java URI to Relative URI resolveUri(URI base, String rel)

Here you can find the source of resolveUri(URI base, String rel)

Description

resolve Uri

License

Apache License

Declaration

public static String resolveUri(URI base, String rel) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

public class Main {
    public static String resolveUri(URI base, String rel) {
        // FIXME/* w w  w  . j a  v  a 2  s . c o  m*/
        try {
            URI relUri = new URI(rel);
            if (relUri.isAbsolute()) {
                return rel;
            }
        } catch (URISyntaxException e) {
            // silent
        }
        String res;
        try {
            res = resolveRelativeURI(base, rel);
            new URI(res);
            return res;
        } catch (Exception ex) {
            //try encoding
            String encodedRel;
            try {
                encodedRel = URLEncoder.encode(rel, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                //silent
                return "";
            }
            return resolveRelativeURI(base, encodedRel);
        }
    }

    private static String resolveRelativeURI(URI base, String rel) {
        if (base.getFragment() != null) {
            return base + rel;
        }
        return base.resolve(rel).toString();
    }
}

Related

  1. resolveRelativeURI(String baseURI, String uriRef)
  2. resolveRelativeURI(URI base, String rel)
  3. resolveURI(File base_dir, URI uri)
  4. resolveURI(final String path)
  5. resolveURI(String uri)
  6. resolveUri(URI baseUri, String resourceUriStr)
  7. resolveUri(URI baseUri, URI requestedUri, URI matchedUri)