You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by je...@apache.org on 2002/11/25 05:51:05 UTC
cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util URIUtil.java
jericho 2002/11/24 20:51:05
Modified: httpclient/src/java/org/apache/commons/httpclient/util
URIUtil.java
Log:
- Support character encoding tranformation for the both side, protocol <-> document
Revision Changes Path
1.11 +62 -7 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.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- URIUtil.java 3 Nov 2002 17:15:05 -0000 1.10
+++ URIUtil.java 25 Nov 2002 04:51:05 -0000 1.11
@@ -525,6 +525,38 @@
return Coder.decode(escaped.toCharArray(), charset);
}
+ // --------------------------------- transforming a string between charsets
+
+ /**
+ * Convert a target string to the specified character encoded string with
+ * the default protocol charset.
+ *
+ * @param target a target string
+ * @return the protocol character encoded string
+ * @exception URIException
+ * @see URI#getProtocolCharset
+ */
+ public static String toProtocolCharset(String target) throws URIException {
+ return toUsingCharset(target, URI.getDocumentCharset(),
+ URI.getProtocolCharset());
+ }
+
+
+ /**
+ * Convert a target string to the specified character encoded string with
+ * a given protocol charset.
+ *
+ * @param target a target string
+ * @param charset the transformed protocol charset
+ * @return the protocol character encoded string
+ * @exception URIException
+ */
+ public static String toProtocolCharset(String target, String charset)
+ throws URIException {
+
+ return toUsingCharset(target, URI.getDocumentCharset(), charset);
+ }
+
/**
* Convert a target string to the specified character encoded string with
@@ -536,7 +568,8 @@
* @see URI#getDocumentCharset
*/
public static String toDocumentCharset(String target) throws URIException {
- return toDocumentCharset(target, URI.getDocumentCharset());
+ return toUsingCharset(target, URI.getProtocolCharset(),
+ URI.getDocumentCharset());
}
@@ -545,15 +578,37 @@
* a given document charset.
*
* @param target a target string
- * @param charset the charset
+ * @param charset the transformed document charset
* @return the document character encoded string
* @exception URIException
*/
public static String toDocumentCharset(String target, String charset)
throws URIException {
+ return toUsingCharset(target, URI.getProtocolCharset(), charset);
+ }
+
+
+ /**
+ * Convert a target string from the <code>fromCharset</code> charset to
+ * the <code>toCharset</code> charset.
+ * <p>
+ * What if the document charset is ISO-8859-1 and the protocol charset is
+ * UTF-8, when it's read from the document part and is used in the protocol
+ * part, the use of the method will be <code>toUsingCharset(the string,
+ * "ISO-8859-1", "UTF-8")</code>.
+ *
+ * @param target a target string
+ * @param fromCharset the previous charset
+ * @param toCharset the changing charset
+ * @return the document character encoded string
+ * @exception URIException
+ */
+ public static String toUsingCharset(String target, String fromCharset,
+ String toCharset) throws URIException {
+
try {
- return new String(target.getBytes(), charset);
+ return new String(target.getBytes(fromCharset), toCharset);
} catch (UnsupportedEncodingException error) {
throw new URIException(URIException.UNSUPPORTED_ENCODING,
error.getMessage());
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>