Java URI to toUnencodedString(URI uri)

Here you can find the source of toUnencodedString(URI uri)

Description

Returns a string representation of the given URI that doesn't have illegal characters encoded.

License

Open Source License

Parameter

Parameter Description
uri The URI to convert to string format

Return

An unencoded string representation of the URI

Declaration

public static String toUnencodedString(URI uri) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

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

public class Main {
    private static final String UNC_PREFIX = "//";

    /**//ww  w  . jav a 2s. c  o m
     * Returns a string representation of the given URI that doesn't have illegal characters encoded. This string is
     * suitable for later passing to {@link #fromString(String)}.
     * 
     * @param uri The URI to convert to string format
     * @return An unencoded string representation of the URI
     */
    public static String toUnencodedString(URI uri) {
        StringBuffer result = new StringBuffer();
        String scheme = uri.getScheme();
        if (scheme != null) {
            result.append(scheme).append(':');
        }
        // there is always a ssp
        result.append(uri.getSchemeSpecificPart());
        String fragment = uri.getFragment();
        if (fragment != null) {
            result.append('#').append(fragment);
        }
        return result.toString();
    }

    /**
     * Appends the given extension to the path of the give base URI and returns the corresponding new path.
     * 
     * @param base The base URI to append to
     * @param extension The path extension to be added
     * @return The appended URI
     */
    public static URI append(URI base, String extension) {
        try {
            String path = base.getPath();
            if (path == null) {
                return appendOpaque(base, extension);
            }
            // if the base is already a directory then resolve will just do the right thing
            if (path.endsWith("/")) {//$NON-NLS-1$
                URI result = base.resolve(extension);
                // Fix UNC paths that are incorrectly normalized by URI#resolve (see Java bug 4723726)
                String resultPath = result.getPath();
                if (path.startsWith(UNC_PREFIX) && (resultPath == null || !resultPath.startsWith(UNC_PREFIX))) {
                    result = new URI(result.getScheme(), "///" + result.getSchemeSpecificPart(), //$NON-NLS-1$
                            result.getFragment());
                }
                return result;
            }
            path = path + "/" + extension; //$NON-NLS-1$
            return new URI(base.getScheme(), base.getUserInfo(), base.getHost(), base.getPort(), path,
                    base.getQuery(), base.getFragment());
        } catch (URISyntaxException e) {
            // shouldn't happen because we started from a valid URI
            throw new RuntimeException(e);
        }
    }

    /**
     * Special case of appending to an opaque URI. Since opaque URIs have no path segment the best we can do is append
     * to the scheme-specific part
     */
    private static URI appendOpaque(URI base, String extension) throws URISyntaxException {
        String ssp = base.getSchemeSpecificPart();
        if (ssp.endsWith("/")) {
            ssp += extension;
        } else {
            ssp = ssp + "/" + extension; //$NON-NLS-1$
        }
        return new URI(base.getScheme(), ssp, base.getFragment());
    }
}

Related

  1. toRepositoryItemUri(String uriString)
  2. toString(URI pathUri)
  3. toString(URI uri)
  4. toStringList(List list)
  5. toStringSet(Collection uris)
  6. URItoString(URI uri)
  7. uriToString(URI[] uris)
  8. uriToString(URI[] uris)