Java String Shorten shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier)

Here you can find the source of shortenUrl(String protocolHostPart, String pathPart, String queryPart, String fragmentIdentifier)

Description

Shortens a URL.

License

Apache License

Parameter

Parameter Description
protocolHostPart the protocol and host part of the URL including the port
pathPart the path part of the URL; can be null
queryPart the query part of the URL; can be null
fragmentIdentifier the fragment identifier of the URL; can be null

Return

the shortened URL

Declaration

public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
        String fragmentIdentifier) 

Method Source Code

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

public class Main {
    private static final int URL_MAX_LENGTH = 30;
    private static final String URL_SHORTENING_INDICATOR = "...";

    /**//  ww w.  j  a va  2  s. c om
     * Shortens a URL.
     *
     * @param protocolHostPart
     *            the protocol and host part of the URL including the port
     * @param pathPart
     *            the path part of the URL; can be null
     * @param queryPart
     *            the query part of the URL; can be null
     * @param fragmentIdentifier
     *            the fragment identifier of the URL; can be null
     * @return the shortened URL
     */
    public static String shortenUrl(String protocolHostPart, String pathPart, String queryPart,
            String fragmentIdentifier) {
        // always full protocol host part
        StringBuilder url = new StringBuilder();
        url.append(protocolHostPart);
        StringBuilder pathQueryFragmentPart = new StringBuilder();
        if (pathPart != null) {
            pathQueryFragmentPart.append(pathPart);
        }
        if (queryPart != null) {
            pathQueryFragmentPart.append(queryPart);
        }
        if (fragmentIdentifier != null) {
            pathQueryFragmentPart.append(fragmentIdentifier);
        }
        int pathQueryFragmentLength = pathQueryFragmentPart.length();
        if (protocolHostPart.length() + pathQueryFragmentLength - URL_MAX_LENGTH > URL_SHORTENING_INDICATOR
                .length()) {
            int pathCharLimit = URL_MAX_LENGTH - protocolHostPart.length();
            if (pathCharLimit <= 0) {
                int limit = 0;
                // take at least one character from path part
                if (pathQueryFragmentLength > 0) {
                    limit = pathQueryFragmentLength > URL_SHORTENING_INDICATOR.length() + 1 ? 1
                            : pathQueryFragmentLength;
                }
                pathCharLimit = limit;
            }
            url.append(pathQueryFragmentPart.substring(0, pathCharLimit));
            if (pathCharLimit < pathQueryFragmentLength) {
                url.append(URL_SHORTENING_INDICATOR);
            }
        } else {
            url.append(pathQueryFragmentPart);
        }

        return url.toString();
    }
}

Related

  1. shortenTo(final String value, final int maxLength)
  2. shortenTo(String string, int lenght)
  3. shortenToFirstLast(String filename)
  4. shortenType(String typeText)
  5. shortenURI(String uri)
  6. ShortenVector(double[] vector, int new_length)
  7. shortenXPathForPropertyExpansion(String xpath, String value)