jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michel Jacobson <jacob...@idf.ext.jussieu.fr>
Subject Unicode decoding problem
Date Thu, 07 Dec 2000 13:24:40 GMT
Hy,

I find a trouble using Tomcat (on win95 with Apache) with unicode chars in
url. Tomcat send me the error:
"Decode error" from unUrlDecode(String) function in the package
org.apache.tomcat.util;

Watching this function, I see that the algorithme only handle chars in %xx
format, but for some unicode chars the value can be in %x%xx format or in
%x%x (for example the char: 014b send by the web client is %1%4b, the char
0303 is %3%3, etc.)

So as I need this function to work well, I change it myself, and I give you
my new version. 

Michel Jacobson CNRS.LACITO
jacobson@idf.ext.jussieu.fr
------------------------------------------------------

    public static String unUrlDecode(String data) {
	StringBuffer buf = new StringBuffer();
	for (int i = 0; i < data.length(); i++) {
	    char c = data.charAt(i);
	    switch (c) {
	    case '+':
		buf.append(' ');
		break;
	    case '%':
		try {
		    if(data.charAt(i+2) == '%') {
				String hexachars ="0123456789abcdefABCDEF";
			    if(hexachars.indexOf(data.charAt(i+4)) == -1) {
					buf.append((char)
Integer.parseInt(data.substring(i+1,i+2).concat("0").concat(data.substring(i
+3,i+4)), 16));
					i += 3;
				} else {
					buf.append((char)
Integer.parseInt(data.substring(i+1,i+2).concat(data.substring(i+3,i+5)),
16));
					i += 4;
				}
			} else {
				buf.append((char) Integer.parseInt(data.substring(i+1, i+3), 16));
				i += 2;
			}
		} catch (NumberFormatException e) {
                    String msg = "Decode error ";
		    throw new IllegalArgumentException(msg);
		} catch (StringIndexOutOfBoundsException e) {
		    String rest  = data.substring(i);
		    buf.append(rest);
		    if (rest.length()==2)
			i++;
		}

		break;
	    default:
		buf.append(c);
		break;
	    }
	}
	return buf.toString();
    }



Mime
View raw message