You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by je...@apache.org on 2002/09/04 02:21:43 UTC
cvs commit: jakarta-slide/src/util/org/apache/util URI.java
jericho 2002/09/03 17:21:43
Modified: src/tests/uri URIUtilTest.java URIUtil.java
URITestSuite.java
src/util/org/apache/util URI.java
Added: src/tests/uri HttpURLTest.java HttpURL.java
Log:
- URI:
support the different charsets concurrently, when using URIUtil
add the root_path for char array and
fix a bug in the root path for the getCurrentHierPath method
- HttpURL:
add a new HttpURL class and its test case
- Test cases:
Add HttpURL and URIUtil test cases and put them into the suite case.
Revision Changes Path
1.2 +2 -1 jakarta-slide/src/tests/uri/URIUtilTest.java
Index: URIUtilTest.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/tests/uri/URIUtilTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- URIUtilTest.java 30 Aug 2002 16:24:05 -0000 1.1
+++ URIUtilTest.java 4 Sep 2002 00:21:42 -0000 1.2
@@ -111,6 +111,7 @@
return suite;
}
+
/**
* Set up.
*/
1.2 +131 -22 jakarta-slide/src/tests/uri/URIUtil.java
Index: URIUtil.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/tests/uri/URIUtil.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- URIUtil.java 30 Aug 2002 16:24:05 -0000 1.1
+++ URIUtil.java 4 Sep 2002 00:21:42 -0000 1.2
@@ -145,47 +145,105 @@
// ----------------------------------------------------- Encoding utilities
/**
- * Get the all escaped and encoded string.
+ * Get the all escaped and encoded string with the default protocl charset.
*
* @param unescaped an unescaped string
+ * @param charset the charset
* @return the escaped string
* @exception URIException
+ * @see URI#getProtocolCharset
* @see #encode
*/
public static String encodeAll(String unescaped) throws URIException {
- return encode(unescaped, empty);
+ return encode(unescaped, empty, URI.getProtocolCharset());
}
+
+ /**
+ * Get the all escaped and encoded string with a given charset.
+ *
+ * @param unescaped an unescaped string
+ * @param charset the charset
+ * @return the escaped string
+ * @exception URIException
+ * @see #encode
+ */
+ public static String encodeAll(String unescaped, String charset)
+ throws URIException {
+
+ return encode(unescaped, empty, charset);
+ }
+
/**
- * Escape and encode a string regarded as the path component of an URI.
+ * Escape and encode a string regarded as the path component of an URI with
+ * the default protocol charset.
*
* @param unescaped an unescaped string
+ * @param charset the charset
* @return the escaped string
* @exception URIException
+ * @see URI#getProtocolCharset
* @see #encode
*/
public static String encodePath(String unescaped) throws URIException {
- return encode(unescaped, URI.allowed_abs_path);
+ return encode(unescaped, URI.allowed_abs_path,
+ URI.getProtocolCharset());
+ }
+
+
+ /**
+ * Escape and encode a string regarded as the path component of an URI with
+ * a given charset.
+ *
+ * @param unescaped an unescaped string
+ * @param charset the charset
+ * @return the escaped string
+ * @exception URIException
+ * @see #encode
+ */
+ public static String encodePath(String unescaped, String charset)
+ throws URIException {
+
+ return encode(unescaped, URI.allowed_abs_path, charset);
}
/**
- * Escape and encode a string regarded as the query component of an URI.
+ * Escape and encode a string regarded as the query component of an URI with
+ * the default protocol charset.
*
* @param unescaped an unescaped string
* @return the escaped string
* @exception URIException
+ * @see URI#getProtocolCharset
* @see #encode
*/
public static String encodeQuery(String unescaped) throws URIException {
- return encode(unescaped, URI.allowed_query);
+ return encode(unescaped, URI.allowed_query, URI.getProtocolCharset());
+ }
+
+
+ /**
+ * Escape and encode a string regarded as the query component of an URI with
+ * a given charset.
+ *
+ * @param unescaped an unescaped string
+ * @param charset the charset
+ * @return the escaped string
+ * @exception URIException
+ * @see #encode
+ */
+ public static String encodeQuery(String unescaped, String charset)
+ throws URIException {
+
+ return encode(unescaped, URI.allowed_query, charset);
}
/**
* Escape and encode a given string with allowed characters not to be
- * escaped.
+ * escaped and the default protocol charset.
*
* @param unescaped a string
* @param allowed allowed characters not to be escaped
@@ -197,12 +255,31 @@
public static String encode(String unescaped, BitSet allowed)
throws URIException {
- return new String(Coder.encode(unescaped, allowed));
+ return encode(unescaped, allowed, URI.getProtocolCharset());
}
/**
- * Unescape and decode a given string regarded as an escaped string.
+ * Escape and encode a given string with allowed characters not to be
+ * escaped and a given charset.
+ *
+ * @param unescaped a string
+ * @param allowed allowed characters not to be escaped
+ * @param charset the charset
+ * @return the escaped string
+ * @exception URIException
+ * @see Coder#encode
+ */
+ public static String encode(String unescaped, BitSet allowed,
+ String charset) throws URIException {
+
+ return new String(Coder.encode(unescaped, allowed, charset));
+ }
+
+
+ /**
+ * Unescape and decode a given string regarded as an escaped string with the
+ * default protocol charset.
*
* @param escaped a string
* @return the unescaped string
@@ -211,24 +288,54 @@
* @see Coder#decode
*/
public static String decode(String escaped) throws URIException {
- return Coder.decode(escaped.toCharArray());
+ return Coder.decode(escaped.toCharArray(), URI.getProtocolCharset());
+ }
+
+
+ /**
+ * Unescape and decode a given string regarded as an escaped string.
+ *
+ * @param escaped a string
+ * @param charset the charset
+ * @return the unescaped string
+ * @exception URIException
+ * @see Coder#decode
+ */
+ public static String decode(String escaped, String charset)
+ throws URIException {
+
+ return Coder.decode(escaped.toCharArray(), charset);
}
/**
* Convert a target string to the specified character encoded string with
- * the document charset.
+ * the default document charset.
*
* @param target a target string
* @return the document character encoded string
* @exception URIException
* @see URI#getDocumentCharset
*/
- public static String toDocumentCharset(String target)
+ public static String toDocumentCharset(String target) throws URIException {
+ return toDocumentCharset(target, URI.getDocumentCharset());
+ }
+
+
+ /**
+ * Convert a target string to the specified character encoded string with
+ * a given document charset.
+ *
+ * @param target a target string
+ * @param charset the charset
+ * @return the document character encoded string
+ * @exception URIException
+ */
+ public static String toDocumentCharset(String target, String charset)
throws URIException {
try {
- return new String(target.getBytes(), URI.getDocumentCharset());
+ return new String(target.getBytes(), charset);
} catch (UnsupportedEncodingException error) {
throw new URIException(URIException.UNSUPPORTED_ENCODING,
error.getMessage());
@@ -249,13 +356,14 @@
*
* @param unescapedComponent an unescaped component
* @param allowed allowed characters not to be escaped
+ * @param charset the charset to encode
* @exception URIException
* @return the escaped and encoded string
*/
- public static char[] encode(String unescapedComponent, BitSet allowed)
- throws URIException {
+ public static char[] encode(String unescapedComponent, BitSet allowed,
+ String charset) throws URIException {
- return URI.encode(unescapedComponent, allowed);
+ return URI.encode(unescapedComponent, allowed, charset);
}
@@ -264,13 +372,14 @@
*
* @param unescapedComponent an unescaped component
* @param allowed allowed characters not to be escaped
+ * @param charset the charset to decode
* @exception URIException
* @return the escaped and encoded string
*/
- public static String decode(char[] escapedComponent)
+ public static String decode(char[] escapedComponent, String charset)
throws URIException {
- return URI.decode(escapedComponent);
+ return URI.decode(escapedComponent, charset);
}
1.3 +3 -1 jakarta-slide/src/tests/uri/URITestSuite.java
Index: URITestSuite.java
===================================================================
RCS file: /home/cvs/jakarta-slide/src/tests/uri/URITestSuite.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- URITestSuite.java 23 Aug 2002 04:32:12 -0000 1.2
+++ URITestSuite.java 4 Sep 2002 00:21:42 -0000 1.3
@@ -101,6 +101,8 @@
suite.addTest(BasicURITest.suite());
suite.addTest(RelativeURITest.suite());
suite.addTest(KoreanURITest.suite());
+ suite.addTest(URIUtilTest.suite());
+ suite.addTest(HttpURLTest.suite());
// TODO: add other all URI suites
return suite;
}
1.1 jakarta-slide/src/tests/uri/HttpURLTest.java
Index: HttpURLTest.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Slide", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package uri;
import org.apache.util.URIException;
// import org.apache.util.URIUtil;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* The HttpURL testcases {@link org.apache.util.HttpURL}.
*
* @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
* @version $Id: HttpURLTest.java,v 1.1 2002/09/04 00:21:42 jericho Exp $
*/
public class HttpURLTest extends URITestBase {
// ----------------------------------------------------------- constructors
/**
* The constructor.
*
* @param name the name
*/
public HttpURLTest(String name) {
super(name);
}
// ------------------------------------------------------------------- main
/**
* The command line interface with java compiler.
*
* @param args arguments
*/
public static void main(String[] args) {
TestRunner.run(suite());
}
// ---------------------------------------------------------- suite methods
/**
* Return the suite.
*
* @return Test
*/
public static Test suite() {
TestSuite suite = new TestSuite(HttpURLTest.class);
suite.setName("HttpURL tests");
return suite;
}
// ----------------------------------------------------------- test methods
/**
* Test the example 01, HttpURL constructor.
*/
public void testExample01() throws URIException {
String result = "http://host/path?query#fragment";
HttpURL http = new HttpURL(result);
assertEquals(result, http.getURI());
}
/**
* Test the example 02, HttpURL constructor.
*/
public void testExample02() throws URIException {
String result = "http://host/path";
HttpURL http = new HttpURL("host", "/path");
assertEquals(result, http.getURI());
}
/**
* Test the example 03, HttpURL constructor.
*/
public void testExample03() throws URIException {
String result = "http://host/path";
HttpURL http = new HttpURL("host", -1, "/path");
assertEquals(result, http.getURI());
result = "http://host:8080/path";
http = new HttpURL("host", 8080, "/path");
assertEquals(result, http.getURI());
}
/**
* Test the example 04, HttpURL constructor.
*/
public void testExample04() throws URIException {
String result = "http://host/path?query";
HttpURL http = new HttpURL("host", -1, "/path", "query");
assertEquals(result, http.getURI());
result = "http://host:8080/path?query";
http = new HttpURL("host", 8080, "/path", "query");
assertEquals(result, http.getURI());
}
/**
* Test the example 05, HttpURL constructor.
*/
public void testExample05() throws URIException {
String uri = "http://user:password@host";
HttpURL http = new HttpURL("user", "password", "host");
String result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host";
assertEquals(result, http.getURI());
uri = "http://user@host";
http = new HttpURL("user", null, "host");
result = "user";
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getPassword());
result = "http://host";
assertEquals(result, http.getURI());
}
/**
* Test the example 06, HttpURL constructor.
*/
public void testExample06() throws URIException {
String result = "http://user:password@host:8080";
HttpURL http = new HttpURL("user", "password", "host", 8080);
result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://user:password@host:80";
http = new HttpURL("user", "password", "host", 80);
result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host:80";
assertEquals(result, http.getURI());
result = "http://user@host";
http = new HttpURL("user", null, "host", -1);
result = "user";
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getPassword());
result = "http://host";
assertEquals(result, http.getURI());
}
/**
* Test the example 07, HttpURL constructor.
*/
public void testExample07() throws URIException {
String uri = "http://user:password@host:8080/path";
HttpURL http = new HttpURL("user", "password", "host", 8080, "/path");
String result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host:8080/path";
assertEquals(result, http.getURI());
result = "http://user:password@host:80/path";
http = new HttpURL("user", "password", "host", 80, "/path");
result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host:80/path";
assertEquals(result, http.getURI());
result = "http://user@host/path";
http = new HttpURL("user", null, "host", -1, "/path");
result = "user";
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getPassword());
result = "http://host/path";
assertEquals(result, http.getURI());
}
/**
* Test the example 08, HttpURL constructor.
*/
public void testExample08() throws URIException {
String uri = "http://user:password@host:8080/path?query";
HttpURL http = new HttpURL("user", "password", "host", 8080, "/path",
"query");
String result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host:8080/path?query";
assertEquals(result, http.getURI());
uri = "http://user:password@host:80/path?query";
http = new HttpURL("user", "password", "host", 80, "/path", "query");
result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host:80/path?query";
assertEquals(result, http.getURI());
uri = "http://user@host/path?query";
http = new HttpURL("user", null, "host", -1, "/path", "query");
result = "user";
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getPassword());
result = "http://host/path?query";
assertEquals(result, http.getURI());
uri = "http://user@host/path";
http = new HttpURL("user", null, "host", -1, "/path", null);
result = "user";
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getPassword());
result = "http://host/path";
assertEquals(result, http.getURI());
uri = "http://host/";
http = new HttpURL(null, null, "host", -1, "/", null);
result = "http://host/";
assertEquals(result, http.getURI());
}
/**
* Test the example 09, HttpURL constructor.
*/
public void testExample09() throws URIException {
String result = "http://host/path?query";
HttpURL http = new HttpURL("host", "/path", "query", null);
assertEquals(result, http.getURI());
result = "http://host/path";
http = new HttpURL("host", "/path", null, null);
assertEquals(result, http.getURI());
}
/**
* Test the example 10, HttpURL constructor.
*/
public void testExample10() throws URIException {
String uri = "http://user:password@host:8080/path?query#fragment";
HttpURL http = new HttpURL("user:password", "host", 8080, "/path",
"query", "fragment");
String result = "http://host:8080/path?query#fragment";
assertEquals(result, http.getURI());
result = "/path?query#fragment";
http = new HttpURL(null, null, -1, "/path", "query", "fragment");
assertEquals(result, http.getURI());
}
/**
* Test the example 11, HttpURL constructor.
*/
public void testExample11() throws URIException {
String uri = "http://user:password@host/path?query#fragment";
HttpURL http = new HttpURL("user:password", "host", "/path", "query",
"fragment");
String result = "user";
assertEquals(result, http.getUser());
result = "password";
assertEquals(result, http.getPassword());
result = "http://host/path?query#fragment";
assertEquals(result, http.getURI());
result = "/path?query#fragment";
http = new HttpURL(null, null, "/path", "query", "fragment");
result = null;
assertEquals(result, http.getUser());
result = null;
assertEquals(result, http.getUser());
result = "/path?query#fragment";
assertEquals(result, http.getURI());
}
/**
* Test the example 12, HttpURL constructor.
*/
public void testExample12() throws URIException {
String uri = "/ns1/ns2/ns3/";
HttpURL http = new HttpURL(uri);
String result = "/ns1/ns2/ns3/";
assertEquals(result, http.getPath());
result = "/ns1/ns2/ns3";
assertEquals(result, http.getCurrentHierPath());
result = "/ns1/ns2";
assertEquals(result, http.getAboveHierPath());
uri = "/ns1/ns2/ns3";
http = new HttpURL(uri);
result = "/ns1/ns2/ns3";
assertEquals(result, http.getPath());
result = "/ns1/ns2";
assertEquals(result, http.getCurrentHierPath());
result = "/ns1";
assertEquals(result, http.getAboveHierPath());
uri = "/ns1/ns2/";
http = new HttpURL(uri);
result = "/ns1/ns2/";
assertEquals(result, http.getPath());
result = "/ns1/ns2";
assertEquals(result, http.getCurrentHierPath());
result = "/ns1";
assertEquals(result, http.getAboveHierPath());
uri = "/ns1/ns2";
http = new HttpURL(uri);
result = "/ns1/ns2";
assertEquals(result, http.getPath());
result = "/ns1";
assertEquals(result, http.getCurrentHierPath());
result = "/";
assertEquals(result, http.getAboveHierPath());
uri = "/ns1/";
http = new HttpURL(uri);
result = "/ns1/";
assertEquals(result, http.getPath());
result = "/ns1";
assertEquals(result, http.getCurrentHierPath());
result = "/";
assertEquals(result, http.getAboveHierPath());
uri = "/ns1";
http = new HttpURL(uri);
result = "/ns1";
assertEquals(result, http.getPath());
result = "/";
assertEquals(result, http.getCurrentHierPath());
result = "/";
assertEquals(result, http.getAboveHierPath());
uri = "/";
http = new HttpURL(uri);
result = "/";
assertEquals(result, http.getPath());
result = "/";
assertEquals(result, http.getCurrentHierPath());
result = "/";
assertEquals(result, http.getAboveHierPath());
}
}
1.1 jakarta-slide/src/tests/uri/HttpURL.java
Index: HttpURL.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/tests/uri/HttpURL.java,v 1.1 2002/09/04 00:21:42 jericho Exp $
* $Revision: 1.1 $
* $Date: 2002/09/04 00:21:42 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Slide", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
// package org.apache.util;
package uri;
import org.apache.util.URI;
import org.apache.util.URIException;
/**
* The HTTP URL.
*
* @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
*/
public class HttpURL extends URI {
// ----------------------------------------------------------- Constructors
protected HttpURL() {
}
/**
* Construct a HTTP URL as an escaped form of a character array.
*
* @param escaped the HTTP URL character sequence
* @exception URIException
* @throws NullPointerException if <code>escaped</code> is <code>null</code>
*/
public HttpURL(char[] escaped) throws URIException {
parseUriReference(new String(escaped), true);
checkValid();
}
/**
* Construct a HTTP URL from a given string.
*
* @param original the HTTP URL string
* @exception URIException
*/
public HttpURL(String original) throws URIException {
parseUriReference(original, false);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param host the host string
* @param path the path string
* @exception URIException
*/
public HttpURL(String host, String path) throws URIException {
this(null, host, -1, path, null, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param host the host string
* @param port the port number
* @param path the path string
* @exception URIException
*/
public HttpURL(String host, int port, String path) throws URIException {
this(null, host, port, path, null, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param host the host string
* @param port the port number
* @param path the path string
* @param query the query string
* @exception URIException
*/
public HttpURL(String host, int port, String path, String query)
throws URIException {
this(null, host, port, path, query, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param user the user name
* @param password his or her password
* @param host the host string
* @exception URIException
*/
public HttpURL(String user, String password, String host)
throws URIException {
this((user == null) ? null : user +
((password == null) ? "" : ':' + password),
host, -1, null, null, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param user the user name
* @param password his or her password
* @param host the host string
* @param port the port number
* @exception URIException
*/
public HttpURL(String user, String password, String host, int port)
throws URIException {
this((user == null) ? null : user +
((password == null) ? "" : ':' + password),
host, port, null, null, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param user the user name
* @param password his or her password
* @param host the host string
* @param port the port number
* @param path the path string
* @exception URIException
*/
public HttpURL(String user, String password, String host, int port,
String path) throws URIException {
this((user == null) ? null : user +
((password == null) ? "" : ':' + password),
host, port, path, null, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param user the user name
* @param password his or her password
* @param host the host string
* @param port the port number
* @param path the path string
* @exception URIException
*/
public HttpURL(String user, String password, String host, int port,
String path, String query) throws URIException {
this((user == null) ? null : user +
((password == null) ? "" : ':' + password),
host, port, path, query, null);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param host the host string
* @param path the path string
* @param query the query string
* @param fragment the fragment string
* @exception URIException
*/
public HttpURL(String host, String path, String query, String fragment)
throws URIException {
this(null, host, -1, path, query, fragment);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param userinfo the userinfo string
* @param host the host string
* @param path the path string
* @param query the query string
* @param fragment the fragment string
* @exception URIException
*/
public HttpURL(String userinfo, String host, String path, String query,
String fragment) throws URIException {
this(userinfo, host, -1, path, query, fragment);
checkValid();
}
/**
* Construct a HTTP URL from given components.
*
* @param userinfo the userinfo string
* @param host the host string
* @param port the port number
* @param path the path string
* @param query the query string
* @param fragment the fragment string
* @exception URIException
*/
public HttpURL(String userinfo, String host, int port, String path,
String query, String fragment) throws URIException {
// validate and contruct the URI character sequence
StringBuffer buff = new StringBuffer();
if (userinfo != null || host != null || port != -1) {
_scheme = _default_scheme; // in order to verify the own protocol
buff.append(_default_scheme);
buff.append("://");
if (userinfo != null) {
buff.append(userinfo);
buff.append('@');
}
if (host != null) {
buff.append(host);
if (port != -1 || port != _default_port) {
buff.append(':');
buff.append(port);
}
}
}
if (path != null) { // accept empty path
if (scheme != null && !path.startsWith("/")) {
throw new URIException(URIException.PARSING,
"abs_path requested");
}
buff.append(path);
}
if (query != null) {
buff.append('?');
buff.append(query);
}
if (fragment != null) {
buff.append('#');
buff.append(fragment);
}
parseUriReference(buff.toString(), false);
checkValid();
}
/**
* Construct a HTTP URL with a given relative URL.
*
* @param base the base HttpURL
* @param relative the relative HttpURL
* @exception URIException
*/
public HttpURL(HttpURL base, HttpURL relative) throws URIException {
super(base, relative);
checkValid();
}
// -------------------------------------------------------------- Constants
/**
* Default scheme for HTTP URL.
*/
public static final char[] _default_scheme = { 'h', 't', 't', 'p' };
/**
* Default port for HTTP URL.
*/
public static final int _default_port = 80;
/**
* The serialVersionUID.
*/
static final long serialVersionUID = -7158031098595039458L;
// ------------------------------------------------------------- The scheme
/**
* Get the scheme. You can get the scheme explicitly.
*
* @return the scheme
*/
public char[] getRawScheme() {
return (_scheme == null) ? null : _default_scheme;
}
/**
* Get the scheme. You can get the scheme explicitly.
*
* @return the scheme null if empty or undefined
*/
public String getScheme() {
return (_scheme == null) ? null : new String(_default_scheme);
}
// --------------------------------------------------------------- The port
/**
* Get the port number.
*/
public int getPort() {
return (_port == -1) ? _default_port : _port;
}
// ----------------------------------------------------------- The userinfo
/**
* Get the raw-escaped user.
*
* @return the raw-escaped user
*/
public char[] getRawUser() {
if (_userinfo == null || _userinfo.length == 0) {
return null;
}
int to = indexFirstOf(_userinfo, ':');
// String.indexOf(':', 0, _userinfo.length, _userinfo, 0, 1, 0);
if (to == -1) {
return _userinfo; // only user.
}
char[] result = new char[to];
System.arraycopy(_userinfo, 0, result, 0, to);
return result;
}
/**
* Get the escaped user
*
* @return the escaped user
*/
public String getEscapedUser() {
char[] user = getRawUser();
return (user == null) ? null : new String(user);
}
/**
* Get the user.
*
* @return the user name
* @exception URIException
*/
public String getUser() throws URIException {
char[] user = getRawUser();
return (user == null) ? null : decode(user);
}
/**
* Get the raw-escaped password.
*
* @return the raw-escaped password
*/
public char[] getRawPassword() {
int from = indexFirstOf(_userinfo, ':');
if (from == -1) {
return null; // null or only user.
}
int len = _userinfo.length - from - 1;
char[] result = new char[len];
System.arraycopy(_userinfo, from + 1, result, 0, len);
return result;
}
/**
* Get the escaped password.
*
* @return the escaped password
*/
public String getEscapedPassword() {
char[] password = getRawPassword();
return (password == null) ? null : new String(password);
}
/**
* Get the password.
*
* @return the password
* @exception URIException
*/
public String getPassword() throws URIException {
char[] password = getRawPassword();
return (password == null) ? null : decode(password);
}
// --------------------------------------------------------------- The path
/**
* Get the raw-escaped current hierarchy level.
*
* @return the raw-escaped current hierarchy level
* @exception URIException no hierarchy level
*/
public char[] getRawCurrentHierPath() throws URIException {
return (_path == null || _path.length == 0) ? root_path :
super.getRawCurrentHierPath(_path);
}
/**
* Get the level above the this hierarchy level.
*
* @return the raw above hierarchy level
* @exception URIException
*/
public char[] getRawAboveHierPath() throws URIException {
char[] path = getRawCurrentHierPath();
return (path == null || path.length == 0) ? root_path :
getRawCurrentHierPath(path);
}
/**
* Get the raw escaped path.
*
* @return the path '/' if empty or undefined
*/
public char[] getRawPath() {
char[] path = super.getRawPath();
return (path == null || path.length == 0) ? root_path : path;
}
// ---------------------------------------------------------------- Utility
/**
* Verify the valid class use for construction.
*
* @exception URIException the wrong scheme use
*/
protected void checkValid() throws URIException {
// could be explicit protocol or undefined.
if (!(equals(_scheme, _default_scheme) || _scheme == null)) {
throw new URIException(URIException.PARSING, "wrong class use");
}
}
}
1.16 +56 -19 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.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- URI.java 30 Aug 2002 16:24:06 -0000 1.15
+++ URI.java 4 Sep 2002 00:21:42 -0000 1.16
@@ -449,7 +449,7 @@
/**
* The default charset of the protocol. RFC 2277, 2396
*/
- protected static String _protocolCharset = "UTF8";
+ protected static String _protocolCharset = "UTF-8";
/**
@@ -524,6 +524,12 @@
/**
+ * The root path.
+ */
+ protected static char[] root_path = { '/' };
+
+
+ /**
* The debug.
*/
protected static int debug = 0;
@@ -1351,6 +1357,21 @@
protected boolean _is_only_fragment;
// ------------------------------------------ Character and escape encoding
+
+ /**
+ * Encode with the default protocol charset.
+ *
+ * @param original the original character sequence
+ * @param allowed those characters that are allowed within a component
+ * @return URI character sequence
+ * @exception URIException null component or unsupported character encoding
+ */
+ protected static char[] encode(String original, BitSet allowed)
+ throws URIException {
+
+ return encode(original, allowed, _protocolCharset);
+ }
+
/**
* This is a two mapping, one from original characters to octets, and
@@ -1381,12 +1402,12 @@
*
* @param original the original character sequence
* @param allowed those characters that are allowed within a component
+ * @param charset the protocol charset
* @return URI character sequence
- * @exception URIException null component
- * Or unsupported character encoding
+ * @exception URIException null component or unsupported character encoding
*/
- protected static char[] encode(String original, BitSet allowed)
- throws URIException {
+ protected static char[] encode(String original, BitSet allowed,
+ String charset) throws URIException {
// encode original to uri characters.
if (original == null) {
@@ -1399,10 +1420,9 @@
}
byte[] octets;
try {
- octets = original.getBytes(_protocolCharset);
+ octets = original.getBytes(charset);
} catch (UnsupportedEncodingException error) {
- throw new URIException(URIException.UNSUPPORTED_ENCODING,
- _protocolCharset);
+ throw new URIException(URIException.UNSUPPORTED_ENCODING, charset);
}
StringBuffer buf = new StringBuffer(octets.length);
for (int i = 0; i < octets.length; i++) {
@@ -1424,6 +1444,19 @@
/**
+ * Decode with the default protocol charset.
+ *
+ * @param component the URI character sequence
+ * @return original character sequence
+ * @exception URIException incomplete trailing escape pattern
+ * or unsupported character encoding
+ */
+ protected static String decode(char[] component) throws URIException {
+ return decode(component, _protocolCharset);
+ }
+
+
+ /**
* This is a two mapping, one from URI characters to octets, and
* subsequently a second from octets to original characters:
* <p><blockquote><pre>
@@ -1446,21 +1479,23 @@
* The unescape method is internally performed within this method.
*
* @param component the URI character sequence
+ * @param charset the protocol charset
* @return original character sequence
* @exception URIException incomplete trailing escape pattern
- * Or unsupported character encoding
+ * or unsupported character encoding
*/
- protected static String decode(char[] component) throws URIException {
+ protected static String decode(char[] component, String charset)
+ throws URIException {
// unescape uri characters to octets
if (component == null) return null;
byte[] octets;
try {
- octets = new String(component).getBytes(_protocolCharset);
+ octets = new String(component).getBytes(charset);
} catch (UnsupportedEncodingException error) {
throw new URIException(URIException.UNSUPPORTED_ENCODING,
- "not supported " + _protocolCharset + " encoding");
+ "not supported " + charset + " encoding");
}
int length = octets.length;
int oi = 0; // output index
@@ -1481,10 +1516,10 @@
String result;
try {
- result = new String(octets, 0, oi, _protocolCharset);
+ result = new String(octets, 0, oi, charset);
} catch (UnsupportedEncodingException error) {
throw new URIException(URIException.UNSUPPORTED_ENCODING,
- "not supported " + _protocolCharset + " encoding");
+ "not supported " + charset + " encoding");
}
return result;
@@ -2445,7 +2480,9 @@
String buff = new String(path);
int first = buff.indexOf('/');
int last = buff.lastIndexOf('/');
- if (first != last && last != -1) {
+ if (last == 0) {
+ return root_path;
+ } else if (first != last && last != -1) {
return buff.substring(0, last).toCharArray();
}
// FIXME: it could be a document on the server side
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>