jericho 2002/10/09 21:05:54
Modified: httpclient/src/java/org/apache/commons/httpclient/util
URIUtil.java
Log:
- Fix a bug of the getName method.
Reported by Christoph.Reck@dlr.de
Thank you. ;)
- Consider even the relative path of the methods
and make it compatible with httpclient.HttpURL totally, I hope.
Revision Changes Path
1.4 +25 -20 jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java
Index: URIUtil.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- URIUtil.java 9 Oct 2002 12:37:31 -0000 1.3
+++ URIUtil.java 10 Oct 2002 04:05:54 -0000 1.4
@@ -70,6 +70,8 @@
/**
* The URI escape and character encoding and decoding utility.
+ * It's compatible with {@link org.apache.commons.httpclient.HttpURL} rather
+ * than {@link org.apache.commons.httpclient.URI}.
*
* @author <a href="mailto:jericho@apache.org">Sung-Gu</a>
* @version $Revision$ $Date: 2002/03/14 15:14:01
@@ -84,15 +86,17 @@
// ---------------------------------------------------------- URI utilities
/**
- * Get the basename of an URI.
+ * Get the basename of an URI. It's possibly an empty string.
*
* @param uri a string regarded an URI
- * @return the basename string
+ * @return the basename string; an empty string if the path ends with slash
*/
public static String getName(String uri) {
if (uri == null || uri.length() == 0) return uri;
- int at = uri.lastIndexOf("/");
- return uri.substring(at + 1);
+ String path = URIUtil.getPath(uri);
+ int at = path.lastIndexOf("/");
+ int to = path.length();
+ return (at >= 0) ? path.substring(at + 1, to) : path;
}
@@ -100,10 +104,10 @@
* Get the query of an URI.
*
* @param uri a string regarded an URI
- * @return the query string
+ * @return the query string; <code>null</code> if empty or undefined
*/
public static String getQuery(String uri) {
- if (uri == null || uri.length() == 0) return uri;
+ if (uri == null || uri.length() == 0) return null;
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", at >= 0 ?
@@ -112,12 +116,14 @@
int to = uri.length();
// reuse the at and from variables to consider the query
at = uri.indexOf("?", from);
- from = (at > from) ? at + 1 : -1;
+ if (at >= 0) {
+ from = at + 1;
+ } else return null;
// check the fragment
if (uri.lastIndexOf("#") > from)
to = uri.lastIndexOf("#");
// get the path and query.
- return (from >= 0) ? uri.substring(from, to) : "/";
+ return (from < 0 || from == to) ? null : uri.substring(from, to);
}
@@ -128,7 +134,7 @@
* @return the path string
*/
public static String getPath(String uri) {
- if (uri == null || uri.length() == 0) return uri;
+ if (uri == null) return null;
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", at >= 0 ?
@@ -139,11 +145,10 @@
if (uri.lastIndexOf("?") > from)
to = uri.lastIndexOf("?");
// check the fragment
- if (uri.lastIndexOf("#") > from &&
- uri.lastIndexOf("#") < to)
+ if (uri.lastIndexOf("#") > from && uri.lastIndexOf("#") < to)
to = uri.lastIndexOf("#");
// get only the path.
- return (from >= 0) ? uri.substring(from, to) : "/";
+ return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to);
}
@@ -154,7 +159,7 @@
* @return the path and query string
*/
public static String getPathQuery(String uri) {
- if (uri == null || uri.length() == 0) return uri;
+ if (uri == null) return null;
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", at >= 0 ?
@@ -166,7 +171,7 @@
if (uri.lastIndexOf("#") > from)
to = uri.lastIndexOf("#");
// get the path and query.
- return (from >= 0) ? uri.substring(from, to) : "/";
+ return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to);
}
@@ -177,13 +182,13 @@
* @return the string from the path part
*/
public static String getFromPath(String uri) {
- if (uri == null || uri.length() == 0) return uri;
+ if (uri == null) return null;
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", at >= 0 ?
(uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0);
// get the path and its rest.
- return (from >= 0) ? uri.substring(from) : "/";
+ return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from);
}
// ----------------------------------------------------- Encoding utilities
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>
|