Uri.java revision 325
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This code is free software; you can redistribute it and/or modify it
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * under the terms of the GNU General Public License version 2 only, as
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * published by the Free Software Foundation. Oracle designates this
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * particular file as subject to the "Classpath" exception as provided
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * by Oracle in the LICENSE file that accompanied this code.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * This code is distributed in the hope that it will be useful, but WITHOUT
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * version 2 for more details (a copy is included in the LICENSE file that
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * accompanied this code).
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * You should have received a copy of the GNU General Public License version
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * 2 along with this work; if not, write to the Free Software Foundation,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * or visit www.oracle.com if you need additional information or have any
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * questions.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk/*
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Copyright (C) 2004-2011
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * Permission is hereby granted, free of charge, to any person obtaining a copy
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * of this software and associated documentation files (the "Software"), to deal
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * in the Software without restriction, including without limitation the rights
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * copies of the Software, and to permit persons to whom the Software is
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk * furnished to do so, subject to the following conditions:
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * The above copyright notice and this permission notice shall be included in
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * all copies or substantial portions of the Software.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk *
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk * THE SOFTWARE.
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk */
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpackage com.sun.xml.internal.rngom.util;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.net.URL;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.net.MalformedURLException;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkimport java.io.UnsupportedEncodingException;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenkpublic class Uri {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static String utf8 = "UTF-8";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static boolean isValid(String s) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return isValidPercent(s) && isValidFragment(s) && isValidScheme(s);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String HEX_DIGITS = "0123456789abcdef";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static String escapeDisallowedChars(String s) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk StringBuffer buf = null;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int len = s.length();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int done = 0;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (;;) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int i = done;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (;;) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (i == len) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (done == 0)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return s;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk break;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (isExcluded(s.charAt(i)))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk break;
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk i++;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (buf == null)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk buf = new StringBuffer();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (i > done) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk buf.append(s.substring(done, i));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk done = i;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk if (i == len)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk break;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (i++; i < len && isExcluded(s.charAt(i)); i++)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk ;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk String tem = s.substring(done, i);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk byte[] bytes;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk bytes = tem.getBytes(utf8);
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk catch (UnsupportedEncodingException e) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk utf8 = "UTF8";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk bytes = tem.getBytes(utf8);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk catch (UnsupportedEncodingException e2) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk // Give up
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return s;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk for (int j = 0; j < bytes.length; j++) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk buf.append('%');
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk buf.append(HEX_DIGITS.charAt((bytes[j] & 0xFF) >> 4));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk buf.append(HEX_DIGITS.charAt(bytes[j] & 0xF));
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk done = i;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return buf.toString();
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static final String excluded = "<>\"{}|\\^`";
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isExcluded(char c) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return c <= 0x20 || c >= 0x7F || excluded.indexOf(c) >= 0;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isAlpha(char c) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isHexDigit(char c) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') || isDigit(c);
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isDigit(char c) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return '0' <= c && c <= '9';
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isSchemeChar(char c) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return isAlpha(c) || isDigit(c) || c == '+' || c == '-' || c =='.';
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isValidPercent(String s) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int len = s.length();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk for (int i = 0; i < len; i++)
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk if (s.charAt(i) == '%') {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (i + 2 >= len)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk else if (!isHexDigit(s.charAt(i + 1))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk || !isHexDigit(s.charAt(i + 2)))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return true;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isValidFragment(String s) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int i = s.indexOf('#');
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk return i < 0 || s.indexOf('#', i + 1) < 0;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk private static boolean isValidScheme(String s) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (!isAbsolute(s))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return true;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int i = s.indexOf(':');
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (i == 0
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk || i + 1 == s.length()
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk || !isAlpha(s.charAt(0)))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk while (--i > 0)
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk if (!isSchemeChar(s.charAt(i)))
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return true;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static String resolve(String baseUri, String uriReference) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (!isAbsolute(uriReference) && baseUri != null && isAbsolute(baseUri)) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk try {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return new URL(new URL(baseUri), uriReference).toString();
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk catch (MalformedURLException e) { }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk return uriReference;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static boolean hasFragmentId(String uri) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return uri.indexOf('#') >= 0;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk public static boolean isAbsolute(String uri) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk int i = uri.indexOf(':');
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk if (i < 0)
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk while (--i >= 0) {
dff2cc5646d4437ab9e0cb1dcb59da65462a5938jeff.schenk switch (uri.charAt(i)) {
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk case '#':
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk case '/':
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk case '?':
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return false;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk return true;
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk }
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk}
5b64d5d44892834ba97f003080f3467299b7c5c5jeff.schenk