01: /*
02: * @(#)URLDecoder.java 1.2 98/06/29
03: *
04: * Copyright 1998 by Sun Microsystems, Inc.,
05: * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
06: * All rights reserved.
07: *
08: * This software is the confidential and proprietary information
09: * of Sun Microsystems, Inc. ("Confidential Information"). You
10: * shall not disclose such Confidential Information and shall use
11: * it only in accordance with the terms of the license agreement
12: * you entered into with Sun.
13: */
14:
15: //package java.net;
16: package com.sun.portal.proxylet.util;
17:
18: /**
19: * The class contains a utility method for converting from
20: * a MIME format called "<code>x-www-form-urlencoded</code>"
21: * to a <code>String</code>
22: * <p>
23: * To convert to a <code>String</code>, each character is examined in turn:
24: * <ul>
25: * <li>The ASCII characters '<code>a</code>' through '<code>z</code>',
26: * '<code>A</code>' through '<code>Z</code>', and '<code>0</code>'
27: * through '<code>9</code>' remain the same.
28: * <li>The plus sign '<code>+</code>'is converted into a
29: * space character '<code> </code>'.
30: * <li>The remaining characters are represented by 3-character
31: * strings which begin with the percent sign,
32: * "<code>%<i>xy</i></code>", where <i>xy</i> is the two-digit
33: * hexadecimal representation of the lower 8-bits of the character.
34: * </ul>
35: *
36: * Code reworked to not do any charset translations by Tom Mueller
37: * 7/26/01
38: *
39: * @author Mark Chamness
40: * @author Michael McCloskey
41: * @version 1.2 06/29/98
42: * @since JDK1.2
43: */
44:
45: public class IURLDecoder {
46:
47: public static String decode(String s) throws Exception {
48: StringBuffer sb = new StringBuffer();
49: for (int i = 0; i < s.length(); i++) {
50: char c = s.charAt(i);
51: switch (c) {
52: case '+':
53: sb.append(' ');
54: break;
55: case '%':
56: try {
57: sb.append((char) Integer.parseInt(s.substring(
58: i + 1, i + 3), 16));
59: } catch (NumberFormatException e) {
60: throw new IllegalArgumentException();
61: }
62: i += 2;
63: break;
64: default:
65: sb.append(c);
66: break;
67: }
68: }
69: return sb.toString();
70: }
71: }
|