📄 uri.java
字号:
*/
public void setHost(String p_host) throws MalformedURIException {
if (p_host == null || p_host.trim().length() == 0) {
m_host = p_host;
m_userinfo = null;
m_port = -1;
}
else
if (!isWellFormedAddress(p_host)) {
throw new MalformedURIException("Host is not a well formed address!");
}
m_host = p_host;
}
/**
* Set the port for this URI. -1 is used to indicate that the port is not specified, otherwise valid port numbers are between 0
* and 65535. If a valid port number is passed in and the host field is null, an exception is thrown.
*
* @param p_port the port number for this URI
*
* @throws MalformedURIException if p_port is not -1 and not a valid port number
*/
public void setPort(int p_port) throws MalformedURIException {
if (p_port >= 0 && p_port <= 65535) {
if (m_host == null) {
throw new MalformedURIException("Port cannot be set when host is null!");
}
}
else
if (p_port != -1) {
throw new MalformedURIException("Invalid port number!");
}
m_port = p_port;
}
/**
* Set the path for this URI. If the supplied path is null, then the query string and fragment are set to null as well. If the
* supplied path includes a query string and/or fragment, these fields will be parsed and set as well. Note that, for URIs
* following the "generic URI" syntax, the path specified should start with a slash. For URIs that do not follow the generic
* URI syntax, this method sets the scheme-specific part.
*
* @param p_path the path for this URI (may be null)
*
* @throws MalformedURIException if p_path contains invalid characters
*/
public void setPath(String p_path) throws MalformedURIException {
if (p_path == null) {
m_path = null;
m_queryString = null;
m_fragment = null;
}
else {
initializePath(p_path);
}
}
/**
* Append to the end of the path of this URI. If the current path does not end in a slash and the path to be appended does not
* begin with a slash, a slash will be appended to the current path before the new segment is added. Also, if the current path
* ends in a slash and the new segment begins with a slash, the extra slash will be removed before the new segment is appended.
*
* @param p_addToPath the new segment to be added to the current path
*
* @throws MalformedURIException if p_addToPath contains syntax errors
*/
public void appendPath(String p_addToPath) throws MalformedURIException {
if (p_addToPath == null || p_addToPath.trim().length() == 0) {
return;
}
if (!isURIString(p_addToPath)) {
throw new MalformedURIException("Path contains invalid character!");
}
if (m_path == null || m_path.trim().length() == 0) {
if (p_addToPath.startsWith("/")) {
m_path = p_addToPath;
}
else {
m_path = "/" + p_addToPath;
}
}
else
if (m_path.endsWith("/")) {
if (p_addToPath.startsWith("/")) {
m_path = m_path.concat(p_addToPath.substring(1));
}
else {
m_path = m_path.concat(p_addToPath);
}
}
else {
if (p_addToPath.startsWith("/")) {
m_path = m_path.concat(p_addToPath);
}
else {
m_path = m_path.concat("/" + p_addToPath);
}
}
}
/**
* Set the query string for this URI. A non-null value is valid only if this is an URI conforming to the generic URI syntax and
* the path value is not null.
*
* @param p_queryString the query string for this URI
*
* @throws MalformedURIException if p_queryString is not null and this URI does not conform to the generic URI syntax or if the
* path is null
*/
public void setQueryString(String p_queryString) throws MalformedURIException {
if (p_queryString == null) {
m_queryString = null;
}
else
if (!isGenericURI()) {
throw new MalformedURIException(
"Query string can only be set for a generic URI!");
}
else
if (getPath() == null) {
throw new MalformedURIException(
"Query string cannot be set when path is null!");
}
else
if (!isURIString(p_queryString)) {
throw new MalformedURIException(
"Query string contains invalid character!");
}
else {
m_queryString = p_queryString;
}
}
/**
* Set the fragment for this URI. A non-null value is valid only if this is a URI conforming to the generic URI syntax and the
* path value is not null.
*
* @param p_fragment the fragment for this URI
*
* @throws MalformedURIException if p_fragment is not null and this URI does not conform to the generic URI syntax or if the
* path is null
*/
public void setFragment(String p_fragment) throws MalformedURIException {
if (p_fragment == null) {
m_fragment = null;
}
else
if (!isGenericURI()) {
throw new MalformedURIException(
"Fragment can only be set for a generic URI!");
}
else
if (getPath() == null) {
throw new MalformedURIException(
"Fragment cannot be set when path is null!");
}
else
if (!isURIString(p_fragment)) {
throw new MalformedURIException("Fragment contains invalid character!");
}
else {
m_fragment = p_fragment;
}
}
/**
* Determines if the passed-in Object is equivalent to this URI.
*
* @param p_test the Object to test for equality.
*
* @return true if p_test is a URI with all values equal to this URI, false otherwise
*/
public boolean equals(Object p_test) {
if (p_test instanceof URI) {
URI testURI = (URI) p_test;
if ( ( (m_scheme == null && testURI.m_scheme == null)
||
(m_scheme != null && testURI.m_scheme != null &&
m_scheme.equals(testURI.m_scheme)))
&& ( (m_userinfo == null && testURI.m_userinfo == null)
||
(m_userinfo != null && testURI.m_userinfo != null &&
m_userinfo.equals(testURI.m_userinfo)))
&& ( (m_host == null && testURI.m_host == null)
||
(m_host != null && testURI.m_host != null &&
m_host.equals(testURI.m_host)))
&& m_port == testURI.m_port
&& ( (m_path == null && testURI.m_path == null)
||
(m_path != null && testURI.m_path != null &&
m_path.equals(testURI.m_path)))
&& ( (m_queryString == null && testURI.m_queryString == null)
||
(m_queryString != null && testURI.m_queryString != null &&
m_queryString.equals(testURI.m_queryString)))
&& ( (m_fragment == null && testURI.m_fragment == null)
||
(m_fragment != null && testURI.m_fragment != null &&
m_fragment.equals(testURI.m_fragment)))) {
return true;
}
}
return false;
}
/**
* Get the URI as a string specification. See RFC 2396 Section 5.2.
*
* @return the URI string specification
*/
public String toString() {
StringBuffer uriSpecString = new StringBuffer();
if (m_scheme != null) {
uriSpecString.append(m_scheme);
uriSpecString.append(':');
}
uriSpecString.append(getSchemeSpecificPart());
return uriSpecString.toString();
}
/**
* Get the indicator as to whether this URI uses the "generic URI" syntax.
*
* @return true if this URI uses the "generic URI" syntax, false otherwise
*/
public boolean isGenericURI() {
// presence of the host (whether valid or empty) means
// double-slashes which means generic uri
return (m_host != null);
}
/**
* Determine whether a scheme conforms to the rules for a scheme name. A scheme is conformant if it starts with an
* alphanumeric, and contains only alphanumerics, '+','-' and '.'.
*
* @param p_scheme The sheme name to check
* @return true if the scheme is conformant, false otherwise
*/
public static boolean isConformantSchemeName(String p_scheme) {
if (p_scheme == null || p_scheme.trim().length() == 0) {
return false;
}
if (!isAlpha(p_scheme.charAt(0))) {
return false;
}
char testChar;
for (int i = 1; i < p_scheme.length(); i++) {
testChar = p_scheme.charAt(i);
if (!isAlphanum(testChar) && SCHEME_CHARACTERS.indexOf(testChar) == -1) {
return false;
}
}
return true;
}
/**
* Determine whether a string is syntactically capable of representing a valid IPv4 address or the domain name of a network
* host. A valid IPv4 address consists of four decimal digit groups separated by a '.'. A hostname consists of domain labels
* (each of which must begin and end with an alphanumeric but may contain '-') separated & by a '.'. See RFC 2396 Section
* 3.2.2.
*
* @param p_address The address string to check
* @return true if the string is a syntactically valid IPv4 address or hostname
*/
public static boolean isWellFormedAddress(String p_address) {
if (p_address == null) {
return false;
}
String address = p_address.trim();
int addrLength = address.length();
if (addrLength == 0 || addrLength > 255) {
return false;
}
if (address.startsWith(".") || address.startsWith("-")) {
return false;
}
// rightmost domain label starting with digit indicates IP address
// since top level domain label can only start with an alpha
// see RFC 2396 Section 3.2.2
int index = address.lastIndexOf('.');
if (address.endsWith(".")) {
index = address.substring(0, index).lastIndexOf('.');
}
if (index + 1 < addrLength && isDigit(p_address.charAt(index + 1))) {
char testChar;
int numDots = 0;
// make sure that 1) we see only digits and dot separators, 2) that
// any dot separator is preceded and followed by a digit and
// 3) that we find 3 dots
for (int i = 0; i < addrLength; i++) {
testChar = address.charAt(i);
if (testChar == '.') {
if (!isDigit(address.charAt(i - 1)) ||
(i + 1 < addrLength && !isDigit(address.charAt(i + 1)))) {
return false;
}
numDots++;
}
else
if (!isDigit(testChar)) {
return false;
}
}
if (numDots != 3) {
return false;
}
}
else {
// domain labels can contain alphanumerics and '-"
// but must start and end with an alphanumeric
char testChar;
for (int i = 0; i < addrLength; i++) {
testChar = address.charAt(i);
if (testChar == '.') {
if (!isAlphanum(address.charAt(i - 1))) {
return false;
}
if (i + 1 < addrLength && !isAlphanum(address.charAt(i + 1))) {
return false;
}
}
else
if (!isAlphanum(testChar) && testChar != '-') {
return false;
}
}
}
return true;
}
/**
* Determine whether a char is a digit.
*
* @param p_char the character to check
* @return true if the char is betweeen '0' and '9', false otherwise
*/
private static boolean isDigit(char p_char) {
return p_char >= '0' && p_char <= '9';
}
/**
* Determine whether a character is a hexadecimal character.
*
* @param p_char the character to check
* @return true if the char is betweeen '0' and '9', 'a' and 'f' or 'A' and 'F', false otherwise
*/
private static boolean isHex(char p_char) {
return (isDigit(p_char) || (p_char >= 'a' && p_char <= 'f') ||
(p_char >= 'A' && p_char <= 'F'));
}
/**
* Determine whether a char is an alphabetic character: a-z or A-Z
*
* @param p_char the character to check
* @return true if the char is alphabetic, false otherwise
*/
private static boolean isAlpha(char p_char) {
return ( (p_char >= 'a' && p_char <= 'z') ||
(p_char >= 'A' && p_char <= 'Z'));
}
/**
* Determine whether a char is an alphanumeric: 0-9, a-z or A-Z
*
* @param p_char the character to check
* @return true if the char is alphanumeric, false otherwise
*/
private static boolean isAlphanum(char p_char) {
return (isAlpha(p_char) || isDigit(p_char));
}
/**
* Determine whether a character is a reserved character: ';', '/', '?', ':', '@', '&', '=', '+', '$' or ','
*
* @param p_char the character to check
* @return true if the string contains any reserved characters
*/
private static boolean isReservedCharacter(char p_char) {
return RESERVED_CHARACTERS.indexOf(p_char) != -1;
}
/**
* Determine whether a char is an unreserved character.
*
* @param p_char the character to check
* @return true if the char is unreserved, false otherwise
*/
private static boolean isUnreservedCharacter(char p_char) {
return (isAlphanum(p_char) || MARK_CHARACTERS.indexOf(p_char) != -1);
}
/**
* Determine whether a given string contains only URI characters (also called "uric" in RFC 2396). uric consist of all reserved
* characters, unreserved characters and escaped characters.
*
* @param p_uric URI string
* @return true if the string is comprised of uric, false otherwise
*/
private static boolean isURIString(String p_uric) {
if (p_uric == null) {
return false;
}
int end = p_uric.length();
char testChar = '\0';
for (int i = 0; i < end; i++) {
testChar = p_uric.charAt(i);
if (testChar == '%') {
if (i + 2 >= end || !isHex(p_uric.charAt(i + 1)) ||
!isHex(p_uric.charAt(i + 2))) {
return false;
}
else {
i += 2;
continue;
}
}
if (isReservedCharacter(testChar) || isUnreservedCharacter(testChar)) {
continue;
}
else {
return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -