KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > http > URLDecoder


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.http;
21
22 /**
23  *
24  * @author Lee David Painter <a HREF="mailto:lee@3sp.com">&lt;lee@3sp.com&gt;</a>
25  */

26 public class URLDecoder {
27
28     public static String JavaDoc decode(String JavaDoc s) {
29         return decode(s, true);
30     }
31
32     public static String JavaDoc decode(String JavaDoc s, boolean decodePlus) {
33
34         boolean needToChange = false;
35         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
36         int numChars = s.length();
37         int i = 0;
38
39         while (i < numChars) {
40             char c = s.charAt(i);
41             switch (c) {
42                 case '+':
43                     if (decodePlus) {
44                         sb.append(' ');
45                         i++;
46                         needToChange = true;
47                     } else {
48                         sb.append(c);
49                         i++;
50                     }
51                     break;
52                 case '%':
53
54                     try {
55
56                         byte[] bytes = new byte[(numChars - i) / 3];
57                         int pos = 0;
58
59                         while (((i + 2) < numChars) && (c == '%')) {
60                             bytes[pos++] = (byte) Integer.parseInt(s.substring(i + 1, i + 3), 16);
61                             i += 3;
62                             if (i < numChars)
63                                 c = s.charAt(i);
64                         }
65
66                         if ((i < numChars) && (c == '%'))
67                             throw new IllegalArgumentException JavaDoc(Messages.getString("URLDecoder.incompleteTrailingEscape")); //$NON-NLS-1$
68

69                         sb.append(new String JavaDoc(bytes, 0, pos));
70                     } catch (NumberFormatException JavaDoc e) {
71                         throw new IllegalArgumentException JavaDoc(Messages.getString("URLDecoder.illegalHexCharacter") //$NON-NLS-1$
72
+ e.getMessage());
73                     }
74                     needToChange = true;
75                     break;
76                 default:
77                     sb.append(c);
78                     i++;
79                     break;
80             }
81         }
82
83         return (needToChange ? sb.toString() : s);
84     }
85
86 }
87
Popular Tags