jericho 2002/08/17 01:54:13
Modified: src/util/org/apache/util URI.java URIException.java
Log:
- To simplify the programming style,
unify UnsupportedEncodingException and NumberFormatException
to URIException
Revision Changes Path
1.12 +95 -141 jakarta-slide/src/util/org/apache/util/URI.java
Index: URI.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/util/org/apache/util/URI.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- URI.java 15 Aug 2002 15:15:58 -0000 1.11
+++ URI.java 17 Aug 2002 08:54:12 -0000 1.12
@@ -150,7 +150,7 @@
public class URI implements Cloneable, Comparable, Serializable {
- // --------------------------------------------------------- Constructors
+ // ----------------------------------------------------------- Constructors
protected URI() {
}
@@ -159,13 +159,10 @@
* Construct a URI as an escaped form of a character array.
*
* @param escaped the URI character sequence
- * @exception UnsupportedEncodingException
* @exception URIException
* @throws NullPointerException if <code>escaped</code> is <code>null</code>
*/
- public URI(char[] escaped)
- throws UnsupportedEncodingException, URIException {
-
+ public URI(char[] escaped) throws URIException {
parseUriReference(new String(escaped), true);
}
@@ -178,12 +175,9 @@
*
* @param original the string to be represented to URI character sequence
* It is one of absoluteURI and relativeURI.
- * @exception UnsupportedEncodingException
* @exception URIException
*/
- public URI(String original)
- throws UnsupportedEncodingException, URIException {
-
+ public URI(String original) throws URIException {
parseUriReference(original, false);
}
@@ -200,21 +194,20 @@
* @param scheme the scheme string
* @param scheme_specific_part scheme_specific_part
* @param fragment the fragment string
- * @exception UnsupportedEncodingException
* @exception URIException
*/
public URI(String scheme, String scheme_specific_part, String fragment)
- throws UnsupportedEncodingException, URIException {
+ throws URIException {
// validate and contruct the URI character sequence
if (scheme == null) {
- throw new URIException("scheme required");
+ throw new URIException(URIException.PARSING, "scheme required");
}
char[] s = scheme.toLowerCase().toCharArray();
if (validate(s, this.scheme)) {
_scheme = s; // is_absoluteURI
} else {
- throw new URIException("incorrect scheme");
+ throw new URIException(URIException.PARSING, "incorrect scheme");
}
_opaque = encode(scheme_specific_part, allowed_opaque_part);
// Set flag
@@ -242,8 +235,7 @@
* @exception URIException
*/
public URI(String scheme, String authority, String path, String query,
- String fragment)
- throws UnsupportedEncodingException, URIException {
+ String fragment) throws URIException {
// validate and contruct the URI character sequence
StringBuffer buff = new StringBuffer();
@@ -258,7 +250,8 @@
if (path != null) { // accept empty path
if ((scheme != null || authority != null)
&& !path.startsWith("/")) {
- throw new URIException("abs_path requested");
+ throw new URIException(URIException.PARSING,
+ "abs_path requested");
}
buff.append(path);
}
@@ -284,12 +277,10 @@
* @param path the path string
* @param query the query string
* @param fragment the fragment string
- * @exception UnsupportedEncodingException
* @exception URIException
*/
public URI(String scheme, String userinfo, String host, int port,
- String path, String query, String fragment)
- throws UnsupportedEncodingException, URIException {
+ String path, String query, String fragment) throws URIException {
this(scheme, (host == null) ? null :
((userinfo != null) ? userinfo + '@' : "") + host +
@@ -304,11 +295,10 @@
* @param host the host string
* @param path the path string
* @param fragment the fragment string
- * @exception UnsupportedEncodingException
* @exception URIException
*/
public URI(String scheme, String host, String path, String fragment)
- throws UnsupportedEncodingException, URIException {
+ throws URIException {
this(scheme, host, path, null, fragment);
}
@@ -362,14 +352,12 @@
*
* @param base the base URI
* @param relative the relative URI
- * @exception UnsupportedEncodingException
* @exception URIException
*/
- public URI(URI base, URI relative)
- throws UnsupportedEncodingException, URIException {
+ public URI(URI base, URI relative) throws URIException {
if (base._scheme == null) {
- throw new URIException("base URI required");
+ throw new URIException(URIException.PARSING, "base URI required");
}
if (base._scheme != null) {
this._scheme = base._scheme;
@@ -1394,22 +1382,28 @@
* @param original the original character sequence
* @param allowed those characters that are allowed within a component
* @return URI character sequence
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException null component
- * @see #escape
+ * Or unsupported character encoding
*/
protected char[] encode(String original, BitSet allowed)
- throws UnsupportedEncodingException, URIException {
+ throws URIException {
// encode original to uri characters.
if (original == null) {
- throw new URIException("original");
+ throw new URIException(URIException.PARSING, "null");
}
// escape octet to uri characters.
if (allowed == null) {
- throw new URIException("null allowed characters");
+ throw new URIException(URIException.PARSING,
+ "null allowed characters");
+ }
+ byte[] octets;
+ try {
+ octets = original.getBytes(_protocolCharset);
+ } catch (UnsupportedEncodingException error) {
+ throw new URIException(URIException.UNSUPPORTED_ENCODING,
+ _protocolCharset);
}
- byte[] octets = original.getBytes(_protocolCharset);
StringBuffer buf = new StringBuffer(octets.length);
for (int i = 0; i < octets.length; i++) {
char c = (char) octets[i];
@@ -1417,7 +1411,7 @@
buf.append(c);
} else {
buf.append('%');
- byte b = octets[i];
+ byte b = octets[i]; // use the original byte value
char hexadecimal = Character.forDigit((b >> 4) & 0xF, 16);
buf.append(Character.toUpperCase(hexadecimal)); // high
hexadecimal = Character.forDigit(b & 0xF, 16);
@@ -1451,19 +1445,23 @@
* <p>
* The unescape method is internally performed within this method.
*
- * @param uri the URI character sequence
+ * @param component the URI character sequence
* @return original character sequence
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
- * @throws NullPointerException null argument
+ * Or unsupported character encoding
*/
- protected String decode(char[] uri)
- throws UnsupportedEncodingException, URIException {
+ protected String decode(char[] component) throws URIException {
// unescape uri characters to octets
- if (uri == null) return null;
+ if (component == null) return null;
- byte[] octets = new String(uri).getBytes(_protocolCharset);
+ byte[] octets;
+ try {
+ octets = new String(component).getBytes(_protocolCharset);
+ } catch (UnsupportedEncodingException error) {
+ throw new URIException(URIException.UNSUPPORTED_ENCODING,
+ "not supported " + _protocolCharset + " encoding");
+ }
int length = octets.length;
int oi = 0; // output index
for (int ii = 0; ii < length; oi++) {
@@ -1472,15 +1470,24 @@
byte high = (byte) Character.digit((char) octets[ii++], 16);
byte low = (byte) Character.digit((char) octets[ii++], 16);
if (high == -1 || low == -1) {
- throw new URIException(
+ throw new URIException(URIException.ESCAPING,
"incomplete trailing escape pattern");
+
}
aByte = (byte) ((high << 4) + low);
}
octets[oi] = (byte) aByte;
}
- return new String(octets, 0, oi, _protocolCharset);
+ String result;
+ try {
+ result = new String(octets, 0, oi, _protocolCharset);
+ } catch (UnsupportedEncodingException error) {
+ throw new URIException(URIException.UNSUPPORTED_ENCODING,
+ "not supported " + _protocolCharset + " encoding");
+ }
+
+ return result;
}
@@ -1580,11 +1587,10 @@
* @param original the original character sequence
* @param escaped <code>true</code> if <code>original</code> is escaped
* @return the original character sequence
- * @exception UnsupportedEncodingException
* @exception URIException
*/
protected void parseUriReference(String original, boolean escaped)
- throws UnsupportedEncodingException, URIException {
+ throws URIException {
// validate and contruct the URI character sequence
if (original == null) {
@@ -1833,13 +1839,10 @@
*
* @param original the original character sequence of authority component
* @param escaped <code>true</code> if <code>original</code> is escaped
- * @exception NumberFormatException port isn't integer
- * @exception UnsupportedEncodingException
- * @exception URIException incorrect Pv6reference or wrong host
+ * @exception URIException
*/
protected void parseAuthority(String original, boolean escaped)
- throws NumberFormatException, UnsupportedEncodingException,
- URIException {
+ throws URIException {
// Reset flags
_is_reg_name = _is_server =
@@ -1857,7 +1860,7 @@
if (next >= from) {
next = original.indexOf(']', from);
if (next == -1) {
- throw new URIException("IPv6reference");
+ throw new URIException(URIException.PARSING, "IPv6reference");
} else {
next++;
}
@@ -1895,7 +1898,12 @@
next = original.indexOf('/', from);
if (next > 0 && original.charAt(next) == ':') { // not empty
from = next + 1;
- _port = Integer.parseInt(original.substring(from));
+ try {
+ _port = Integer.parseInt(original.substring(from));
+ } catch (NumberFormatException error) {
+ throw new URIException(URIException.PARSING,
+ "invalid port number");
+ }
}
// set a server-based naming authority
StringBuffer buf = new StringBuffer();
@@ -2232,12 +2240,9 @@
* </pre></blockquote><p>
*
* @param the authority
- * @exception UnsupportedEncodingException
* @exception URIException
*/
- public void setAuthority(String authority)
- throws UnsupportedEncodingException, URIException {
-
+ public void setAuthority(String authority) throws URIException {
parseAuthority(authority, false);
setUriReference();
}
@@ -2267,13 +2272,10 @@
* Get the authority.
*
* @return the authority
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
*/
- public String getAuthority()
- throws UnsupportedEncodingException, URIException {
-
+ public String getAuthority() throws URIException {
return decode(_authority);
}
@@ -2305,14 +2307,11 @@
* Get the userinfo.
*
* @return the userinfo
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
* @see #getAuthority
*/
- public String getUserinfo()
- throws UnsupportedEncodingException, URIException {
-
+ public String getUserinfo() throws URIException {
return decode(_userinfo);
}
@@ -2339,14 +2338,11 @@
* </pre></blockquote><p>
*
* @return the host
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
* @see #getAuthority
*/
- public String getHost()
- throws UnsupportedEncodingException, URIException {
-
+ public String getHost() throws URIException {
return decode(_host);
}
@@ -2371,12 +2367,10 @@
* Set the path. The method couldn't be used by API programmers.
*
* @param path the path string
- * @exception UnsupportedEncodingException
* @exception URIException set incorrectly or fragment only
* @see #encode
*/
- protected void setPath(String path)
- throws UnsupportedEncodingException, URIException {
+ protected void setPath(String path) throws URIException {
// set path
if (_is_net_path || _is_abs_path) {
@@ -2394,7 +2388,7 @@
} else if (_is_opaque_part) {
_opaque = encode(path, allowed_opaque_part);
} else {
- throw new URIException("incorrect path");
+ throw new URIException(URIException.PARSING, "incorrect path");
}
}
@@ -2436,17 +2430,15 @@
*
* @param path the path
* @return the current hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException no hierarchy level
*/
- protected char[] getRawCurrentHierPath(char[] path)
- throws UnsupportedEncodingException, URIException {
+ protected char[] getRawCurrentHierPath(char[] path) throws URIException {
if (_is_opaque_part) {
- throw new URIException("no hierarchy level");
+ throw new URIException(URIException.PARSING, "no hierarchy level");
}
if (path == null) {
- throw new URIException("emtpy path");
+ throw new URIException(URIException.PARSING, "emtpy path");
}
String buff = new String(path);
int first = buff.indexOf('/');
@@ -2463,12 +2455,9 @@
* Get the raw-escaped current hierarchy level.
*
* @return the raw-escaped current hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException no hierarchy level
*/
- public char[] getRawCurrentHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public char[] getRawCurrentHierPath() throws URIException {
return getRawCurrentHierPath(_path);
}
@@ -2477,12 +2466,9 @@
* Get the escaped current hierarchy level.
*
* @return the escaped current hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException no hierarchy level
*/
- public String getEscapedCurrentHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public String getEscapedCurrentHierPath() throws URIException {
return new String(getRawCurrentHierPath());
}
@@ -2491,13 +2477,10 @@
* Get the current hierarchy level.
*
* @return the current hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
*/
- public String getCurrentHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public String getCurrentHierPath() throws URIException {
return decode(getRawCurrentHierPath());
}
@@ -2506,12 +2489,9 @@
* Get the level above the this hierarchy level.
*
* @return the raw above hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException
*/
- public char[] getRawAboveHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public char[] getRawAboveHierPath() throws URIException {
return getRawCurrentHierPath(getRawCurrentHierPath());
}
@@ -2520,12 +2500,9 @@
* Get the level above the this hierarchy level.
*
* @return the raw above hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException
*/
- public String getEscapedAboveHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public String getEscapedAboveHierPath() throws URIException {
return new String(getRawAboveHierPath());
}
@@ -2534,13 +2511,10 @@
* Get the level above the this hierarchy level.
*
* @return the above hierarchy level
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
*/
- public String getAboveHierPath()
- throws UnsupportedEncodingException, URIException {
-
+ public String getAboveHierPath() throws URIException {
return decode(getRawAboveHierPath());
}
@@ -2579,13 +2553,10 @@
* path = [ abs_path | opaque_part ]
* </pre></blockquote><p>
* @return the path string
- * @exception UnsupportedEncodingException
* @exception URIException
* @see #decode
*/
- public String getPath()
- throws UnsupportedEncodingException, URIException {
-
+ public String getPath() throws URIException {
return decode(getRawPath());
}
@@ -2629,13 +2600,11 @@
* Get the basename of the path.
*
* @return the basename string
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @see #decode
*/
- public String getName()
- throws UnsupportedEncodingException, URIException {
-
+ public String getName() throws URIException {
char[] basename = getRawName();
return (basename == null) ? null : decode(getRawName());
}
@@ -2648,12 +2617,11 @@
* @return the raw-escaped path and query
* @exception URIException path undefined
*/
- public char[] getRawPathQuery()
- throws URIException {
+ public char[] getRawPathQuery() throws URIException {
// path is never empty
if (_path == null) {
- throw new URIException("path undefined");
+ throw new URIException(URIException.PARSING, "path undefined");
}
int len = _path.length;
if (_query != null) {
@@ -2675,9 +2643,7 @@
* @return the escaped path and query string
* @exception URIException path undefined
*/
- public String getEscapedPathQuery()
- throws URIException {
-
+ public String getEscapedPathQuery() throws URIException {
return new String(getRawPathQuery());
}
@@ -2686,13 +2652,11 @@
* Get the path and query.
*
* @return the path and query string.
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @see #decode
*/
- public String getPathQuery()
- throws UnsupportedEncodingException, URIException {
-
+ public String getPathQuery() throws URIException {
return decode(getRawPathQuery());
}
@@ -2702,14 +2666,12 @@
* Set the query.
*
* @param query the query string.
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @throws NullPointerException null query
* @see #encode
*/
- public void setQuery(String query)
- throws UnsupportedEncodingException, URIException {
-
+ public void setQuery(String query) throws URIException {
_query = encode(query, allowed_query);
setUriReference();
}
@@ -2740,14 +2702,12 @@
* Get the query.
*
* @return the query string.
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @throws NullPointerException undefined query
* @see #decode
*/
- public String getQuery()
- throws UnsupportedEncodingException, URIException {
-
+ public String getQuery() throws URIException {
return decode(_query);
}
@@ -2760,13 +2720,11 @@
* and should be replaced by that URI when transformed into a request.
*
* @param the fragment string.
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException
+ * Or unsupported character encoding
* @throws NullPointerException null fragment
*/
- public void setFragment(String fragment)
- throws UnsupportedEncodingException, URIException {
-
+ public void setFragment(String fragment) throws URIException {
_fragment = encode(fragment, allowed_fragment);
setUriReference();
}
@@ -2807,14 +2765,12 @@
* Get the fragment.
*
* @return the fragment string
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @throws NullPointerException undefined fragment
* @see #decode
*/
- public String getFragment()
- throws UnsupportedEncodingException, URIException {
-
+ public String getFragment() throws URIException {
return decode(_fragment);
}
@@ -3099,13 +3055,11 @@
* It can be gotten the URI character sequence.
*
* @return the URI string
- * @exception UnsupportedEncodingException unsupported character encoding
* @exception URIException incomplete trailing escape pattern
+ * Or unsupported character encoding
* @see #decode
*/
- public String getURI()
- throws UnsupportedEncodingException, URIException {
-
+ public String getURI() throws URIException {
return decode(_uri);
}
1.2 +82 -5 jakarta-slide/src/util/org/apache/util/URIException.java
Index: URIException.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/util/org/apache/util/URIException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- URIException.java 16 Jun 2002 16:46:31 -0000 1.1
+++ URIException.java 17 Aug 2002 08:54:12 -0000 1.2
@@ -72,12 +72,89 @@
public class URIException extends Exception {
+ // ----------------------------------------------------------- constructors
+
/**
- * The constructor.
+ * The constructor with a reason code argument.
+ *
+ * @param reasonCode the reason code
+ */
+ public URIException(int reasonCode) {
+ setReasonCode(reasonCode);
+ }
+
+
+ /**
+ * The constructor with a reason string and its code arguments.
+ *
+ * @param reasonCode the reason code
+ * @param reason the reason
+ */
+ public URIException(int reasonCode, String reason) {
+ super(reason);
+ setReasonCode(reasonCode);
+ }
+
+
+ /**
+ * The constructor with a reason string argument.
*
* @param reason the reason
*/
public URIException(String reason) {
super(reason);
+ setReasonCode(UNKNOWN);
+ }
+
+ // -------------------------------------------------------------- constants
+
+ /**
+ * No specified reason code.
+ */
+ public static final int UNKNOWN = 0;
+
+ /**
+ * The URI parsing error.
+ */
+ public static final int PARSING = 1;
+
+ /**
+ * The unsupported character encoding.
+ */
+ public static final int UNSUPPORTED_ENCODING = 2;
+
+ /**
+ * The URI escape or unescape error.
+ */
+ public static final int ESCAPING = 3;
+
+ // ------------------------------------------------------------- properties
+
+ /**
+ * The reason code.
+ */
+ protected int reasonCode;
+
+ // ---------------------------------------------------------------- methods
+
+ /**
+ * Get the reason code.
+ *
+ * @return the reason code
+ */
+ public int getReasonCode() {
+ return reasonCode;
+ }
+
+
+ /**
+ * Set the reason code.
+ *
+ * @param the reason code
+ */
+ public void setReasonCode(int reasonCode) {
+ this.reasonCode = reasonCode;
}
+
}
+
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>