Java URI Value Check isValidUri(String uri, StringBuilder errMsg)

Here you can find the source of isValidUri(String uri, StringBuilder errMsg)

Description

Tests whether the given URI matches RFC-2396 specs.

License

Open Source License

Parameter

Parameter Description
uri The string to be tested.
errMsg On false, will be populated with brief failure description.

Return

Whether the given string is a valid URI.

Declaration

static public boolean isValidUri(String uri, StringBuilder errMsg) 

Method Source Code

//package com.java2s;
/*/*  w w  w  . j  a va  2 s  .  co  m*/
 * IRIS -- Intelligent Roadway Information System
 * Copyright (C) 2014-2015  AHMCT, University of California
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

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

public class Main {
    /**
     * Tests whether the given URI matches RFC-2396 specs.
     * @param uri The string to be tested.
     * @param errMsg On false, will be populated with brief failure description.
     * @return Whether the given string is a valid URI.
     */
    static public boolean isValidUri(String uri, StringBuilder errMsg) {
        boolean ret = true;
        if (uri == null) {
            if (errMsg != null) {
                errMsg.append("URI is null");
            }
            ret = false;
        } else {
            try {
                new URI(uri);
            } catch (URISyntaxException se) {
                ret = false;
                if (errMsg != null) {
                    errMsg.append(se.getMessage());
                }
            }
        }

        return ret;
    }

    /**
     * Tests whether the given URI matches RFC-3986 specs.
     * @param uri The string to be tested.
     * @return Whether the given string is a valid URI.
     */
    static public boolean isValidUri(String uri) {
        return isValidUri(uri, null);
    }
}

Related

  1. isUsingNonDefaultPort(URI uri)
  2. isValid(String uri)
  3. isValid(URI uri)
  4. isValidURI(final String namespace)
  5. isValidURI(String uri)
  6. isValidUri(String uriCandidate)
  7. isValidURI(String uriRef)
  8. isValidURIorEmpty(String uri)
  9. isValidURIReference(String uriRef)