Java URI Normalize normalizeURI(final URI uri)

Here you can find the source of normalizeURI(final URI uri)

Description

Canonicalizes the URI into "TFS format".

License

Open Source License

Parameter

Parameter Description
uri the server URI

Return

the server URI, canonicalized

Declaration

public static URI normalizeURI(final URI uri) 

Method Source Code


//package com.java2s;
// Licensed under the MIT license. See License.txt in the repository root.

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Locale;

public class Main {
    /**/* w  w w . j a  v  a 2  s .com*/
     * Canonicalizes the URI into "TFS format". This function preserves the
     * given case on the URI, and is not suitable for serialization or sharing
     * with Visual Studio (which lowercases URIs.)
     *
     * @param uri
     *        the server URI
     * @return the server URI, canonicalized
     */
    public static URI normalizeURI(final URI uri) {
        return normalizeURI(uri, false);
    }

    /**
     * Canonicalizes the URI into TFS format.
     *
     * @param uri
     *        the server URI
     * @param lowercase
     *        <code>true</code> to flatten the URI into lowercase,
     *        <code>false</code> to preserve case
     * @return the server URI, canonicalized
     */
    public static URI normalizeURI(final URI uri, final boolean lowercase) {
        if (uri == null) {
            return null;
        }

        // Convert null or empty path to "/"
        String pathPart = (uri.getPath() == null || uri.getPath().length() == 0) ? "/" : uri.getPath(); //$NON-NLS-1$

        // Remove trailing slash if there is more than one character
        while (pathPart.length() > 1 && pathPart.endsWith("/")) //$NON-NLS-1$
        {
            pathPart = pathPart.substring(0, pathPart.length() - 1);
        }

        /* Always lowercase scheme for sanity. */
        final String scheme = (uri.getScheme() == null) ? null : uri.getScheme().toLowerCase(Locale.ENGLISH);
        String host = uri.getHost();

        if (lowercase) {
            pathPart = pathPart.toLowerCase(Locale.ENGLISH);
            host = (host == null) ? null : host.toLowerCase(Locale.ENGLISH);
        }

        try {
            // Use null query and fragment as these are not important for TFS
            // URIs.
            return new URI(scheme, uri.getUserInfo(), host, uri.getPort(), pathPart, null, null);
        } catch (final URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. normalize(URI uri)
  2. normalizedSetCookiePath(final String path, final URI originUri)
  3. normalizedUri(URI uri)
  4. normalizeGitRepoLocation(URI location)
  5. normalizeLink(String link, URI parent, boolean removeParams)
  6. normalizeURI(String uri)
  7. normalizeURIPath(String uri)
  8. normalizeUriPath(String uriPath)