Java URI Value Check isValid(String uri)

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

Description

is Valid

License

Open Source License

Return

true if the string represents a valid URI and the URI is a valid ViPR URL else false

Declaration

public static boolean isValid(String uri) 

Method Source Code


//package com.java2s;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    /**//  w  ww . j a  v  a2  s .  com
     * @return true if the string represents a valid URI and the URI is a valid ViPR URL else false
     */
    public static boolean isValid(String uri) {
        try {
            return isValid(new URI(uri));
        } catch (URISyntaxException e) {
            return false;
        }
    }

    /**
     * @return true if the uri represents a valid ViPR URI regardless of class
     */
    public static boolean isValid(final URI uri) {
        if (uri == null || uri.getScheme() == null || uri.getSchemeSpecificPart() == null
                || !uri.getScheme().equals("urn") || !uri.getSchemeSpecificPart().startsWith("storageos")) {
            return false;
        }

        /*
         * (?i) - ignores case of letters in following parentheses
         * urn:storageos: - matches exact stting, this is consistent for all ViPR uris
         * [A-Z]+: - This will match the class with any number of letters followed by a colon
         * [A-F0-9]{8} - used for matchin UUID, This segment is 8 hex characters.
         * The full UUID pattern is all Hex characters seperated by '-' in specific quantities
         * :([A-Z0-9]+)? - any amount of letters or numbers preceded by a colon
         * 
         * Only legal characters (letters(any case), numbers, '-', ':')
         */

        return uri.toString().matches("(?i)(urn:storageos:" + "[A-Z]+:"
                + "[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}" + ":([A-Z0-9]+)?)");
    }
}

Related

  1. isURI(Class type)
  2. isURI(String plainString)
  3. isURI(String uri)
  4. isUriValid(String uri)
  5. isUsingNonDefaultPort(URI uri)
  6. isValid(URI uri)
  7. isValidURI(final String namespace)
  8. isValidURI(String uri)
  9. isValidUri(String uri, StringBuilder errMsg)