src/share/jaxws_classes/com/sun/xml/internal/rngom/util/Uri.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25 /*
aoqi@0 26 * Copyright (C) 2004-2011
aoqi@0 27 *
aoqi@0 28 * Permission is hereby granted, free of charge, to any person obtaining a copy
aoqi@0 29 * of this software and associated documentation files (the "Software"), to deal
aoqi@0 30 * in the Software without restriction, including without limitation the rights
aoqi@0 31 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
aoqi@0 32 * copies of the Software, and to permit persons to whom the Software is
aoqi@0 33 * furnished to do so, subject to the following conditions:
aoqi@0 34 *
aoqi@0 35 * The above copyright notice and this permission notice shall be included in
aoqi@0 36 * all copies or substantial portions of the Software.
aoqi@0 37 *
aoqi@0 38 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
aoqi@0 39 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
aoqi@0 40 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
aoqi@0 41 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
aoqi@0 42 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
aoqi@0 43 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
aoqi@0 44 * THE SOFTWARE.
aoqi@0 45 */
aoqi@0 46 package com.sun.xml.internal.rngom.util;
aoqi@0 47
aoqi@0 48 import java.net.URL;
aoqi@0 49 import java.net.MalformedURLException;
aoqi@0 50 import java.io.UnsupportedEncodingException;
aoqi@0 51
aoqi@0 52 public class Uri {
aoqi@0 53 private static String utf8 = "UTF-8";
aoqi@0 54
aoqi@0 55 public static boolean isValid(String s) {
aoqi@0 56 return isValidPercent(s) && isValidFragment(s) && isValidScheme(s);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59 private static final String HEX_DIGITS = "0123456789abcdef";
aoqi@0 60
aoqi@0 61 public static String escapeDisallowedChars(String s) {
aoqi@0 62 StringBuffer buf = null;
aoqi@0 63 int len = s.length();
aoqi@0 64 int done = 0;
aoqi@0 65 for (;;) {
aoqi@0 66 int i = done;
aoqi@0 67 for (;;) {
aoqi@0 68 if (i == len) {
aoqi@0 69 if (done == 0)
aoqi@0 70 return s;
aoqi@0 71 break;
aoqi@0 72 }
aoqi@0 73 if (isExcluded(s.charAt(i)))
aoqi@0 74 break;
aoqi@0 75 i++;
aoqi@0 76 }
aoqi@0 77 if (buf == null)
aoqi@0 78 buf = new StringBuffer();
aoqi@0 79 if (i > done) {
aoqi@0 80 buf.append(s.substring(done, i));
aoqi@0 81 done = i;
aoqi@0 82 }
aoqi@0 83 if (i == len)
aoqi@0 84 break;
aoqi@0 85 for (i++; i < len && isExcluded(s.charAt(i)); i++)
aoqi@0 86 ;
aoqi@0 87 String tem = s.substring(done, i);
aoqi@0 88 byte[] bytes;
aoqi@0 89 try {
aoqi@0 90 bytes = tem.getBytes(utf8);
aoqi@0 91 }
aoqi@0 92 catch (UnsupportedEncodingException e) {
aoqi@0 93 utf8 = "UTF8";
aoqi@0 94 try {
aoqi@0 95 bytes = tem.getBytes(utf8);
aoqi@0 96 }
aoqi@0 97 catch (UnsupportedEncodingException e2) {
aoqi@0 98 // Give up
aoqi@0 99 return s;
aoqi@0 100 }
aoqi@0 101 }
aoqi@0 102 for (int j = 0; j < bytes.length; j++) {
aoqi@0 103 buf.append('%');
aoqi@0 104 buf.append(HEX_DIGITS.charAt((bytes[j] & 0xFF) >> 4));
aoqi@0 105 buf.append(HEX_DIGITS.charAt(bytes[j] & 0xF));
aoqi@0 106 }
aoqi@0 107 done = i;
aoqi@0 108 }
aoqi@0 109 return buf.toString();
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 private static final String excluded = "<>\"{}|\\^`";
aoqi@0 113
aoqi@0 114 private static boolean isExcluded(char c) {
aoqi@0 115 return c <= 0x20 || c >= 0x7F || excluded.indexOf(c) >= 0;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 private static boolean isAlpha(char c) {
aoqi@0 119 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 private static boolean isHexDigit(char c) {
aoqi@0 123 return ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F') || isDigit(c);
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 private static boolean isDigit(char c) {
aoqi@0 127 return '0' <= c && c <= '9';
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 private static boolean isSchemeChar(char c) {
aoqi@0 131 return isAlpha(c) || isDigit(c) || c == '+' || c == '-' || c =='.';
aoqi@0 132 }
aoqi@0 133
aoqi@0 134 private static boolean isValidPercent(String s) {
aoqi@0 135 int len = s.length();
aoqi@0 136 for (int i = 0; i < len; i++)
aoqi@0 137 if (s.charAt(i) == '%') {
aoqi@0 138 if (i + 2 >= len)
aoqi@0 139 return false;
aoqi@0 140 else if (!isHexDigit(s.charAt(i + 1))
aoqi@0 141 || !isHexDigit(s.charAt(i + 2)))
aoqi@0 142 return false;
aoqi@0 143 }
aoqi@0 144 return true;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 private static boolean isValidFragment(String s) {
aoqi@0 148 int i = s.indexOf('#');
aoqi@0 149 return i < 0 || s.indexOf('#', i + 1) < 0;
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 private static boolean isValidScheme(String s) {
aoqi@0 153 if (!isAbsolute(s))
aoqi@0 154 return true;
aoqi@0 155 int i = s.indexOf(':');
aoqi@0 156 if (i == 0
aoqi@0 157 || i + 1 == s.length()
aoqi@0 158 || !isAlpha(s.charAt(0)))
aoqi@0 159 return false;
aoqi@0 160 while (--i > 0)
aoqi@0 161 if (!isSchemeChar(s.charAt(i)))
aoqi@0 162 return false;
aoqi@0 163 return true;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 public static String resolve(String baseUri, String uriReference) {
aoqi@0 167 if (!isAbsolute(uriReference) && baseUri != null && isAbsolute(baseUri)) {
aoqi@0 168 try {
aoqi@0 169 return new URL(new URL(baseUri), uriReference).toString();
aoqi@0 170 }
aoqi@0 171 catch (MalformedURLException e) { }
aoqi@0 172 }
aoqi@0 173 return uriReference;
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 public static boolean hasFragmentId(String uri) {
aoqi@0 177 return uri.indexOf('#') >= 0;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 public static boolean isAbsolute(String uri) {
aoqi@0 181 int i = uri.indexOf(':');
aoqi@0 182 if (i < 0)
aoqi@0 183 return false;
aoqi@0 184 while (--i >= 0) {
aoqi@0 185 switch (uri.charAt(i)) {
aoqi@0 186 case '#':
aoqi@0 187 case '/':
aoqi@0 188 case '?':
aoqi@0 189 return false;
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192 return true;
aoqi@0 193 }
aoqi@0 194 }

mercurial