01: /**
02: * $Id: NetFileURLDecoder.java,v 1.6 2005/11/30 11:26:35 ss150821 Exp $
03: * Copyright 2002 Sun Microsystems, Inc. All
04: * rights reserved. Use of this product is subject
05: * to license terms. Federal Acquisitions:
06: * Commercial Software -- Government Users
07: * Subject to Standard License Terms and
08: * Conditions.
09: *
10: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
11: * are trademarks or registered trademarks of Sun Microsystems,
12: * Inc. in the United States and other countries.
13: */package com.sun.portal.netfile.servlet.java1;
14:
15: import java.io.*;
16: import com.sun.portal.log.common.PortalLogger;
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: * @author Mark Chamness
37: * @author Michael McCloskey
38: * @version 1.9, 02/02/00
39: * @since 1.2
40: */
41:
42: public class NetFileURLDecoder {
43:
44: /**
45: * Decodes a "x-www-form-urlencoded"
46: * to a <tt>String</tt>.
47: * @param s the <code>String</code> to decode
48: * @return the newly decoded <code>String</code>
49: */
50: /*
51: *Made this method non-static so that
52: */
53: public String decode(String s, String encoding) {
54: StringBuffer sb = new StringBuffer();
55: for (int i = 0; i < s.length(); i++) {
56: char c = s.charAt(i);
57: switch (c) {
58: case '+':
59: sb.append(' ');
60: break;
61: case '%':
62: try {
63: sb.append((char) Integer.parseInt(s.substring(
64: i + 1, i + 3), 16));
65: } catch (NumberFormatException e) {
66: throw new IllegalArgumentException();
67: }
68: i += 2;
69: break;
70: default:
71: sb.append(c);
72: break;
73: }
74: }
75: // Undo conversion to external encoding
76: String result = sb.toString();
77: try {
78: byte[] inputBytes = result.getBytes("8859_1");
79: result = new String(inputBytes, encoding);
80: } catch (UnsupportedEncodingException e) {
81: System.out.println("unsupported encoding:" + e);
82: }
83: return result;
84: }
85: }
|