Java URL is Absolute isAbsoluteURI(String source)

Here you can find the source of isAbsoluteURI(String source)

Description

Checks whether the given string is a valid URI .

License

Apache License

Parameter

Parameter Description
source a parameter

Declaration

public static boolean isAbsoluteURI(String source) 

Method Source Code

//package com.java2s;
/**/*  w w  w. j a va  2 s  . c o  m*/
 * Copyright (C) 2014 Ontotext AD (info@ontotext.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Checks whether the given string is a valid {@link URI}.
     * @param source
     * @return
     */
    public static boolean isAbsoluteURI(String source) {
        int schemeSepIdx = source.indexOf(':');
        if ((schemeSepIdx < 0) || (source.indexOf(' ') > -1) || (source.indexOf('|') > 0)) {
            return false;
        } else {
            String rest = source.contains("//") ? source.substring(schemeSepIdx + 3)
                    : source.substring(schemeSepIdx + 1);
            return parse(rest);

        }
    }

    private static boolean parse(CharSequence source) {
        int atIdx = -1;
        int qmIdx = -1;
        int diazIdx = -1;
        int slLastIdx = -1;
        int dcolLastIdx = -1;
        for (int i = 0; i < source.length(); i++) {
            char c = source.charAt(i);
            if (c == ':') {
                if (qmIdx < 0 && diazIdx < 0) {
                    dcolLastIdx = i;
                } else {
                    return false;
                }
            }
            if (c == '@') {
                if (atIdx < 0 && qmIdx < 0 && diazIdx < 0 && slLastIdx < 0) {
                    atIdx = i;
                } else {
                    return false;
                }
            }
            if (c == '/') {
                if (qmIdx < 0 && diazIdx < 0) {
                    slLastIdx = i;
                } else {
                    return false;
                }
            }
            if (c == '?') {
                if (qmIdx < 0 && diazIdx < 0) {
                    qmIdx = i;
                } else {
                    return false;
                }
            }
            if (c == '#') {
                if (diazIdx < 0) {
                    diazIdx = i;
                } else {
                    return false;
                }
            }
        }
        if (atIdx < 0 && qmIdx < 0 && diazIdx < 0 && slLastIdx < 0 && dcolLastIdx < 0) {
            return false;
        }
        return true;
    }
}

Related

  1. getAbsoluteUrlPathFromFile(String file)
  2. isAbsolute(String uri)
  3. isAbsolute(String uri)
  4. isAbsolute(String uri)
  5. isAbsoluteUri(String s)
  6. isAbsoluteURI(String uri)
  7. isAbsoluteURI(String uri)
  8. isAbsoluteUrl(String imageUrl)
  9. isAbsoluteUrl(String ref)