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