/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A class that contains useful routines common to sun.net.www
* @author Mike McCloskey
*/
public class ParseUtil {
static {
// Set the bits corresponding to characters that are encoded in the
// path component of a URI.
// These characters are reserved in the path segment as described in
// RFC2396 section 3.3.
// These characters are defined as excluded in RFC2396 section 2.4.3
// and must be escaped if they occur in the data part of a URI.
// US ASCII control characters 00-1F and 7F.
for (int i=0; i<32; i++)
encodedInPath.set(i);
}
/**
* Constructs an encoded version of the specified path string suitable
* for use in the construction of a URL.
*
* A path separator is replaced by a forward slash. The string is UTF8
* encoded. The % escape sequence is used for characters that are above
* 0x7F or those defined in RFC2396 as reserved or excluded in the path
* component of a URL.
*/
return encodePath(path, true);
}
/*
* flag indicates whether path uses platform dependent
* File.separatorChar or not. True indicates path uses platform
* dependent File.separatorChar.
*/
int retLen = 0;
for (int i=0; i<n; i++) {
char c = pathCC[i];
else {
if (c <= 0x007F) {
if (c >= 'a' && c <= 'z' ||
c >= 'A' && c <= 'Z' ||
c >= '0' && c <= '9') {
} else
if (encodedInPath.get(c))
else
} else if (c > 0x07FF) {
} else {
}
}
//worst case scenario for character [0x7ff-] every single
//character will be encoded into 9 characters.
if (newLen < 0) {
}
}
}
}
/**
* Appends the URL escape sequence for the specified char to the
* specified StringBuffer.
*/
return index;
}
/**
* Un-escape and return the character at position i in string s.
*/
}
/**
* Returns a new String constructed from the specified String by replacing
* the URL escape sequences and UTF8 encoding with the characters they
* represent.
*/
int n = s.length();
return s;
char c = s.charAt(0);
for (int i = 0; i < n;) {
assert c == s.charAt(i);
if (c != '%') {
if (++i >= n)
break;
c = s.charAt(i);
continue;
}
int ui = i;
for (;;) {
assert (n - i >= 2);
try {
} catch (NumberFormatException e) {
throw new IllegalArgumentException();
}
i += 3;
if (i >= n)
break;
c = s.charAt(i);
if (c != '%')
break;
}
throw new IllegalArgumentException("Error decoding percent encoded characters");
throw new IllegalArgumentException("Error decoding percent encoded characters");
}
}
/**
* Returns a canonical version of the specified string.
*/
int i = 0;
// Remove embedded /../
} else {
}
}
// Remove embedded /./
}
// Remove trailing ..
} else {
}
}
// Remove trailing .
return file;
}
throws MalformedURLException
{
}
}
}
//
// In java.net.URI class, a port number of -1 implies the default
// port number. So get it stripped off before creating URI instance.
//
try {
}
return uri;
}
//
// createURI() and its auxiliary code are cloned from java.net.URI.
// Most of the code are just copy and paste, except that quote()
// has been modified to avoid double-escape.
//
// Usually it is unacceptable, but we're forced to do it because
// otherwise we need to change public API, namely java.net.URI's
// multi-argument constructors. It turns out that the changes cause
// incompatibilities so can't be done.
//
{
return new URI(s);
}
int port,
{
}
}
int port,
{
if (opaquePart != null) {
/* check if SSP begins with an IPv6 address
* because we must not quote a literal IPv6 address
*/
doquote = "";
} else {
}
}
} else {
}
} else {
}
}
}
int port)
{
}
if (port != -1) {
}
doquote = "";
} else {
}
H_REG_NAME | H_SERVER));
}
} else {
H_REG_NAME | H_SERVER));
}
}
}
}
}
// Quote any characters in s that are not permitted
// by the given mask pair
//
int n = s.length();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '\u0080') {
sb = new StringBuffer();
}
appendEscape(sb, (byte)c);
} else {
}
} else if (allowNonASCII
&& (Character.isSpaceChar(c)
|| Character.isISOControl(c))) {
sb = new StringBuffer();
}
appendEncoded(sb, c);
} else {
}
}
}
//
// To check if the given string has an escaped triplet
// at the given position
//
return false;
}
try {
} catch (CharacterCodingException x) {
assert false;
}
while (bb.hasRemaining()) {
if (b >= 0x80)
appendEscape(sb, (byte)b);
else
}
}
private final static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
}
// Tell whether the given character is permitted by the given mask pair
if (c < 64)
if (c < 128)
return false;
}
// If a scheme is given then the path, if given, must be absolute
//
throws URISyntaxException
{
throw new URISyntaxException(s,
"Relative path in absolute URI");
}
}
// -- Character classes for parsing --
// Compute a low-order mask for the characters
// between first and last, inclusive
long m = 0;
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
}
// Compute the low-order mask for the characters in the given string
long m = 0;
for (int i = 0; i < n; i++) {
if (c < 64)
m |= (1L << c);
}
return m;
}
// Compute a high-order mask for the characters
// between first and last, inclusive
long m = 0;
for (int i = f; i <= l; i++)
m |= 1L << i;
return m;
}
// Compute the high-order mask for the characters in the given string
long m = 0;
for (int i = 0; i < n; i++) {
if ((c >= 64) && (c < 128))
m |= (1L << (c - 64));
}
return m;
}
// Character-class masks
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |
// "8" | "9"
// hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
// "a" | "b" | "c" | "d" | "e" | "f"
// upalpha = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |
// "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |
// "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z"
// lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
// "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
// "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
// alpha = lowalpha | upalpha
// alphanum = alpha | digit
// mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
// "(" | ")"
// unreserved = alphanum | mark
// reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
// "$" | "," | "[" | "]"
// Added per RFC2732: "[", "]"
// The zero'th bit is used to indicate that escape pairs and non-US-ASCII
// characters are allowed; this is handled by the scanEscape method below.
// Dash, for use in domainlabel and toplabel
// uric = reserved | unreserved | escaped
// pchar = unreserved | escaped |
// ":" | "@" | "&" | "=" | "+" | "$" | ","
private static final long L_PCHAR
private static final long H_PCHAR
// All valid path characters
// userinfo = *( unreserved | escaped |
// ";" | ":" | "&" | "=" | "+" | "$" | "," )
private static final long L_USERINFO
private static final long H_USERINFO
// reg_name = 1*( unreserved | escaped | "$" | "," |
// ";" | ":" | "@" | "&" | "=" | "+" )
private static final long L_REG_NAME
private static final long H_REG_NAME
// All valid characters for server-based authorities
private static final long L_SERVER
private static final long H_SERVER
}