001: /*
002: Copyright (c) 2001, 2002 Thai Open Source Software Center Ltd
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are
007: met:
008:
009: Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: Redistributions in binary form must reproduce the above copyright
013: notice, this list of conditions and the following disclaimer in
014: the documentation and/or other materials provided with the
015: distribution.
016:
017: Neither the name of the Thai Open Source Software Center Ltd nor
018: the names of its contributors may be used to endorse or promote
019: products derived from this software without specific prior written
020: permission.
021:
022: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
023: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
024: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
025: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
026: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
027: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
028: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
029: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
030: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
031: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033: */
034: package com.sun.xml.xsom.impl.util;
035:
036: import java.io.IOException;
037: import java.io.UnsupportedEncodingException;
038: import java.net.URL;
039:
040: public class Uri {
041: private static String utf8 = "UTF-8";
042:
043: public static boolean isValid(String s) {
044: return isValidPercent(s) && isValidFragment(s)
045: && isValidScheme(s);
046: }
047:
048: private static final String HEX_DIGITS = "0123456789abcdef";
049:
050: public static String escapeDisallowedChars(String s) {
051: StringBuffer buf = null;
052: int len = s.length();
053: int done = 0;
054: for (;;) {
055: int i = done;
056: for (;;) {
057: if (i == len) {
058: if (done == 0)
059: return s;
060: break;
061: }
062: if (isExcluded(s.charAt(i)))
063: break;
064: i++;
065: }
066: if (buf == null)
067: buf = new StringBuffer();
068: if (i > done) {
069: buf.append(s.substring(done, i));
070: done = i;
071: }
072: if (i == len)
073: break;
074: for (i++; i < len && isExcluded(s.charAt(i)); i++)
075: ;
076: String tem = s.substring(done, i);
077: byte[] bytes;
078: try {
079: bytes = tem.getBytes(utf8);
080: } catch (UnsupportedEncodingException e) {
081: utf8 = "UTF8";
082: try {
083: bytes = tem.getBytes(utf8);
084: } catch (UnsupportedEncodingException e2) {
085: // Give up
086: return s;
087: }
088: }
089: for (int j = 0; j < bytes.length; j++) {
090: buf.append('%');
091: buf.append(HEX_DIGITS.charAt((bytes[j] & 0xFF) >> 4));
092: buf.append(HEX_DIGITS.charAt(bytes[j] & 0xF));
093: }
094: done = i;
095: }
096: return buf.toString();
097: }
098:
099: private static String excluded = "<>\"{}|\\^`";
100:
101: private static boolean isExcluded(char c) {
102: return c <= 0x20 || c >= 0x7F || excluded.indexOf(c) >= 0;
103: }
104:
105: private static boolean isAlpha(char c) {
106: return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
107: }
108:
109: private static boolean isHexDigit(char c) {
110: return ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')
111: || isDigit(c);
112: }
113:
114: private static boolean isDigit(char c) {
115: return '0' <= c && c <= '9';
116: }
117:
118: private static boolean isSchemeChar(char c) {
119: return isAlpha(c) || isDigit(c) || c == '+' || c == '-'
120: || c == '.';
121: }
122:
123: private static boolean isValidPercent(String s) {
124: int len = s.length();
125: for (int i = 0; i < len; i++)
126: if (s.charAt(i) == '%') {
127: if (i + 2 >= len)
128: return false;
129: else if (!isHexDigit(s.charAt(i + 1))
130: || !isHexDigit(s.charAt(i + 2)))
131: return false;
132: }
133: return true;
134: }
135:
136: private static boolean isValidFragment(String s) {
137: int i = s.indexOf('#');
138: return i < 0 || s.indexOf('#', i + 1) < 0;
139: }
140:
141: private static boolean isValidScheme(String s) {
142: if (!isAbsolute(s))
143: return true;
144: int i = s.indexOf(':');
145: if (i == 0 || i + 1 == s.length() || !isAlpha(s.charAt(0)))
146: return false;
147: while (--i > 0)
148: if (!isSchemeChar(s.charAt(i)))
149: return false;
150: return true;
151: }
152:
153: public static String resolve(String baseUri, String uriReference)
154: throws IOException {
155: if (isAbsolute(uriReference))
156: return uriReference;
157:
158: if (baseUri == null)
159: throw new IOException("Unable to resolve relative URI "
160: + uriReference + " without a base URI");
161:
162: if (!isAbsolute(baseUri))
163: throw new IOException("Unable to resolve relative URI "
164: + uriReference
165: + " because base URI is not absolute: " + baseUri);
166:
167: return new URL(new URL(baseUri), uriReference).toString();
168: }
169:
170: public static boolean hasFragmentId(String uri) {
171: return uri.indexOf('#') >= 0;
172: }
173:
174: public static boolean isAbsolute(String uri) {
175: int i = uri.indexOf(':');
176: if (i < 0)
177: return false;
178: while (--i >= 0) {
179: switch (uri.charAt(i)) {
180: case '#':
181: case '/':
182: case '?':
183: return false;
184: }
185: }
186: return true;
187: }
188: }
|