Java URL is Absolute isAbsolute(String uri)

Here you can find the source of isAbsolute(String uri)

Description

is Absolute

License

Open Source License

Declaration

public static boolean isAbsolute(String uri) 

Method Source Code

//package com.java2s;
/**/*from   w w w . j  a  v  a2  s  .  c om*/
 * This class holds various utility methods.
 * 
 * @author Yanick Duchesne
 *         <dl>
 *         <dt><b>Copyright: </b>
 *         <dd>Copyright &#169; 2002-2007 <a
 *         href="http://www.sapia-oss.org">Sapia Open Source Software </a>. All
 *         Rights Reserved.</dd>
 *         </dt>
 *         <dt><b>License: </b>
 *         <dd>Read the license.txt file of the jar or visit the <a
 *         href="http://www.sapia-oss.org/license.html">license page </a> at the
 *         Sapia OSS web site</dd>
 *         </dt>
 *         </dl>
 */

public class Main {
    private static final char WIN_FILE_SEP = '\\';
    private static final char LINUX_FILE_SEP = '/';

    public static boolean isAbsolute(String uri) {
        if (isWindowsDrive(uri)) {
            return true;
        } else if (hasScheme(uri)) {
            uri = chopScheme(uri);
        }
        if (startsWith(uri, LINUX_FILE_SEP) || startsWith(uri, WIN_FILE_SEP) || isWindowsDrive(uri)) {
            return true;
        }
        return false;
    }

    public static boolean isWindowsDrive(String uri) {
        return uri.length() >= 2 && uri.charAt(1) == ':' && Character.isLetter(uri.charAt(0));
    }

    /**
     * Tests if the given path has a protocol/scheme.
     * 
     * @param path
     *          the path on which to perform the test.
     * @return <code>true</code> if path has a scheme.
     */
    public static boolean hasScheme(String path) {
        if (path == null) {
            return false;
        }

        if (path.indexOf(":") >= 0) {
            if (path.length() >= 2) {
                if (path.charAt(1) == ':') {
                    if (Character.isLetter(path.charAt(0))) {
                        return false;
                    } else {
                        return false;
                    }
                } else {
                    return true;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Chops the scheme/protocol from the given URL path and returns the path
     * without the scheme.
     * 
     * @param path
     *          a URL path.
     * @return the path without the scheme, or the given path, if it has no
     *         scheme.
     */
    public static String chopScheme(String path) {
        int idx = path.indexOf(":");

        if (idx >= 0) {
            if (path.length() >= 2) {
                if (path.charAt(1) == ':') {
                    if (Character.isLetter(path.charAt(0))) {
                        return path;
                    }
                }
            }
            String toReturn = path.substring(idx + 1);
            if (toReturn.startsWith("//")) {
                toReturn = toReturn.substring(1);
            }
            return toReturn;
        }

        return path;
    }

    private static boolean startsWith(String comparee, char comparant) {
        if (comparee.length() == 0)
            return false;
        return comparee.charAt(0) == comparant;
    }
}

Related

  1. getAbsoluteURL(String url, URL base)
  2. getAbsoluteUrl(URL baseUrl, String relativeUrl)
  3. getAbsoluteUrlFromFile(final String file)
  4. getAbsoluteUrlPathFromFile(String file)
  5. isAbsolute(String uri)
  6. isAbsolute(String uri)
  7. isAbsoluteUri(String s)
  8. isAbsoluteURI(String source)
  9. isAbsoluteURI(String uri)