[prev in list] [next in list] [prev in thread] [next in thread]
List: slide-dev
Subject: cvs commit: jakarta-slide/src/util/org/apache/util URI.java
From: jericho () apache ! org
Date: 2002-08-30 16:24:07
[Download RAW message or body]
jericho 2002/08/30 09:24:06
Modified: src/util/org/apache/util URI.java
Added: src/tests/uri URIUtilTest.java URIUtil.java
Log:
- Make encode and decode methods to static.
- Add URIUtil class uitilizing URI class
Implement private inheritance...
- Add URIUtil test cases....
Revision Changes Path
1.1 jakarta-slide/src/tests/uri/URIUtilTest.java
Index: URIUtilTest.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.URIUtil;
import org.apache.util.URIException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
/**
* The URIUtil testcases {@link org.apache.util.URIUtil}.
*
* @author <a href="mailto:jericho at apache.org">Sung-Gu</a>
* @version $Id: URIUtilTest.java,v 1.1 2002/08/30 16:24:05 jericho Exp $
*/
public class URIUtilTest extends URITestBase {
// ----------------------------------------------------------- constructors
/**
* The constructor.
*
* @param name the name
*/
public URIUtilTest(String name) {
super(name);
}
// ------------------------------------------------------------- properties
/**
* The URI example 1.
*/
protected String uri01;
// ------------------------------------------------------------------- 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(URIUtilTest.class);
suite.setName("URIUtil tests");
return suite;
}
/**
* Set up.
*/
protected void setUp() throws Exception {
uri01 = "http://host/path?query#fragment";
}
/**
* Tear down.
*/
protected void tearDown() throws Exception {
super.tearDown();
}
// ----------------------------------------------------------- test methods
/**
* Test the example 01.
*/
public void testExample01() {
String result = "/path?query#fragment";
assertEquals(result, URIUtil.getFromPath(uri01));
result = "/path?query";
assertEquals(result, URIUtil.getPathQuery(uri01));
result = "/path";
assertEquals(result, URIUtil.getPath(uri01));
}
/**
* Test the example 02.
*/
public void testExample02() throws URIException {
String result = "http://host/path?query%23fragment";
assertEquals(result, URIUtil.encodeQuery(uri01));
result = "http://host/path%3Fquery%23fragment";
assertEquals(result, URIUtil.encodePath(uri01));
result = "%68%74%74%70%3A%2F%2F%68%6F%73%74%2F%70%61%74%68%3F%71%75" +
"%65%72%79%23%66%72%61%67%6D%65%6E%74";
assertEquals(result, URIUtil.encodeAll(uri01));
}
}
1.1 jakarta-slide/src/tests/uri/URIUtil.java
Index: URIUtil.java
===================================================================
/*
* $Header: /home/cvs/jakarta-slide/src/tests/uri/URIUtil.java,v 1.1 2002/08/30 16:24:05 jericho Exp $
* $Revision: 1.1 $
* $Date: 2002/08/30 16:24:05 $
*
* ====================================================================
*
* 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 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 uri;
import java.io.UnsupportedEncodingException;
import java.util.BitSet;
import org.apache.util.URI;
import org.apache.util.URIException;
/**
* The URI escape and character encoding and decoding utility.
*
* @author <a href="mailto:jericho@apache.org">Sung-Gu</a>
* @version $Revision: 1.1 $ $Date: 2002/03/14 15:14:01
*/
public class URIUtil {
// ----------------------------------------------------- Instance variables
protected static final BitSet empty = new BitSet(1);
// ---------------------------------------------------------- URI utilities
/**
* Get the path of an URI.
*
* @param uri a string regarded an URI
* @return the path string
*/
public static String getPath(String uri) {
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", (at >= 0) ? at + 2 : 0);
// Ignore the authority part of URI
int to = uri.length();
// check the query
if (uri.lastIndexOf("?") > from)
to = uri.lastIndexOf("?");
// check the fragment
if (uri.lastIndexOf("#") > from &&
uri.lastIndexOf("#") < to)
to = uri.lastIndexOf("#");
// get only the path.
return (from >= 0) ? uri.substring(from, to) : "/";
}
/**
* Get the path and query of an URI.
*
* @param uri a string regarded an URI
* @return the path and query string
*/
public static String getPathQuery(String uri) {
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", (at >= 0) ? at + 2 : 0);
// Ignore the authority part of URI
int to = uri.length();
// Ignore the '?' mark so to ignore the query.
// check the fragment
if (uri.lastIndexOf("#") > from)
to = uri.lastIndexOf("#");
// get the path and query.
return (from >= 0) ? uri.substring(from, to) : "/";
}
/**
* Get the path of an URI and its rest part.
*
* @param uri a string regarded an URI
* @return the string from the path part
*/
public static String getFromPath(String uri) {
// consider of net_path
int at = uri.indexOf("//");
int from = uri.indexOf("/", (at >= 0) ? at + 2 : 0);
// get the path and its rest.
return (from >= 0) ? uri.substring(from) : "/";
}
// ----------------------------------------------------- Encoding utilities
/**
* Get the all escaped and encoded string.
*
* @param unescaped an unescaped string
* @return the escaped string
* @exception URIException
* @see #encode
*/
public static String encodeAll(String unescaped) throws URIException {
return encode(unescaped, empty);
}
/**
* Escape and encode a string regarded as the path component of an URI.
*
* @param unescaped an unescaped string
* @return the escaped string
* @exception URIException
* @see #encode
*/
public static String encodePath(String unescaped) throws URIException {
return encode(unescaped, URI.allowed_abs_path);
}
/**
* Escape and encode a string regarded as the query component of an URI.
*
* @param unescaped an unescaped string
* @return the escaped string
* @exception URIException
* @see #encode
*/
public static String encodeQuery(String unescaped) throws URIException {
return encode(unescaped, URI.allowed_query);
}
/**
* Escape and encode a given string with allowed characters not to be
* escaped.
*
* @param unescaped a string
* @param allowed allowed characters not to be escaped
* @return the escaped string
* @exception URIException
* @see URI#getProtocolCharset
* @see Coder#encode
*/
public static String encode(String unescaped, BitSet allowed)
throws URIException {
return new String(Coder.encode(unescaped, allowed));
}
/**
* Unescape and decode a given string regarded as an escaped string.
*
* @param escaped a string
* @return the unescaped string
* @exception URIException
* @see URI#getProtocolCharset
* @see Coder#decode
*/
public static String decode(String escaped) throws URIException {
return Coder.decode(escaped.toCharArray());
}
/**
* Convert a target string to the specified character encoded string with
* the document charset.
*
* @param target a target string
* @return the document character encoded string
* @exception URIException
* @see URI#getDocumentCharset
*/
public static String toDocumentCharset(String target)
throws URIException {
try {
return new String(target.getBytes(), URI.getDocumentCharset());
} catch (UnsupportedEncodingException error) {
throw new URIException(URIException.UNSUPPORTED_ENCODING,
error.getMessage());
}
}
// ---------------------------------------------------------- Inner classes
/**
* The basic and internal utility for URI escape and character encoding and
* decoding.
*/
protected static class Coder extends URI {
/**
* Escape and encode a given string with allowed characters not to be
* escaped.
*
* @param unescapedComponent an unescaped component
* @param allowed allowed characters not to be escaped
* @exception URIException
* @return the escaped and encoded string
*/
public static char[] encode(String unescapedComponent, BitSet allowed)
throws URIException {
return URI.encode(unescapedComponent, allowed);
}
/**
* Unescape and decode a given string.
*
* @param unescapedComponent an unescaped component
* @param allowed allowed characters not to be escaped
* @exception URIException
* @return the escaped and encoded string
*/
public static String decode(char[] escapedComponent)
throws URIException {
return URI.decode(escapedComponent);
}
/**
* Verify whether a given string is escaped or not
*
* @param original given characters
* @return true if the given character array is 7 bit ASCII-compatible.
*/
public static boolean verifyEscaped(char[] original) {
for (int i = 0; i < original.length; i++) {
int c = original[i];
if (c > 128) {
return false;
} else if (c == '%') {
if (Character.digit(original[++i], 16) == -1 ||
Character.digit(original[++i], 16) == -1)
return false;
}
}
return true;
}
/**
* Replace from a given character to given character in an array order
* for a given string.
*
* @param original a given string
* @param from a replacing character array
* @param to a replaced character array
* @return the replaced string
*/
public static String replace(String original, char[] from, char[] to) {
for (int i = from.length; i > 0; --i) {
original = replace(original, from[i], to[i]);
}
return original.toString();
}
/**
* Replace from a given character to given character for a given string.
*
* @param original a given string
* @param from a replacing character array
* @param to a replaced character array
* @return the replaced string
*/
public static String replace(String original, char from, char to) {
StringBuffer result = new StringBuffer(original.length());
int at, saved = 0;
do {
at = original.indexOf(from);
if (at >= 0) {
result.append(original.substring(0, at));
result.append(to);
} else {
result.append(original.substring(saved));
}
saved = at;
} while (at >= 0);
return result.toString();
}
}
}
1.15 +6 -6 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.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- URI.java 17 Aug 2002 15:50:58 -0000 1.14
+++ URI.java 30 Aug 2002 16:24:06 -0000 1.15
@@ -1385,7 +1385,7 @@
* @exception URIException null component
* Or unsupported character encoding
*/
- protected char[] encode(String original, BitSet allowed)
+ protected static char[] encode(String original, BitSet allowed)
throws URIException {
// encode original to uri characters.
@@ -1450,7 +1450,7 @@
* @exception URIException incomplete trailing escape pattern
* Or unsupported character encoding
*/
- protected String decode(char[] component) throws URIException {
+ protected static String decode(char[] component) throws URIException {
// unescape uri characters to octets
if (component == null) return null;
--
To unsubscribe, e-mail: <mailto:slide-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:slide-dev-help@jakarta.apache.org>
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic