a7cf0d474333d95c36905bc5b1d9b61b3ca154e3
[zxing.git] / rim / src / com / google / zxing / client / rim / util / URLDecoder.java
1 /*\r
2  * Copyright 2008 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.client.rim.util;\r
18 \r
19 import java.util.Enumeration;\r
20 import java.util.Hashtable;\r
21 \r
22 /**\r
23  * Used to decode URL encoded characters.\r
24  *\r
25  * This code was contributed by LifeMarks.\r
26  *\r
27  * @author Matt York (matt@lifemarks.mobi)\r
28  */\r
29 public final class URLDecoder {\r
30 \r
31   private URLDecoder() {\r
32   }\r
33 \r
34   private static final Hashtable decodingMap;\r
35   static {\r
36     decodingMap = new Hashtable(37);\r
37     decodingMap.put("%21", "!");\r
38     decodingMap.put("%2A", "*");\r
39     decodingMap.put("%2a", "*");\r
40     decodingMap.put("%27", "'");\r
41     decodingMap.put("%28", "(");\r
42     decodingMap.put("%29", ")");\r
43     decodingMap.put("%3B", ";");\r
44     decodingMap.put("%3b", ";");\r
45     decodingMap.put("%3A", ":");\r
46     decodingMap.put("%3a", ":");\r
47     decodingMap.put("%40", "@");\r
48     decodingMap.put("%26", "&");\r
49     decodingMap.put("%3D", "=");\r
50     decodingMap.put("%3d", "=");\r
51     decodingMap.put("%3B", "+");\r
52     decodingMap.put("%3b", "+");\r
53     decodingMap.put("%24", "$");\r
54     decodingMap.put("%2C", "`");\r
55     decodingMap.put("%2c", "`");\r
56     decodingMap.put("%2F", "/");\r
57     decodingMap.put("%2f", "/");\r
58     decodingMap.put("%3F", "?");\r
59     decodingMap.put("%3f", "?");\r
60     decodingMap.put("%25", "%");\r
61     decodingMap.put("%23", "#");\r
62     decodingMap.put("%5B", "[");\r
63     decodingMap.put("%5b", "[");\r
64     decodingMap.put("%5D", "]");\r
65     decodingMap.put("%5d", "]");\r
66   }\r
67 \r
68   public static String decode(String uri) {\r
69     Log.info("Original uri: " + uri);\r
70     if (uri.indexOf('%') >= 0) { // skip this if no encoded chars\r
71       Enumeration keys = decodingMap.keys();\r
72       while (keys.hasMoreElements()) {\r
73         String encodedChar = (String) keys.nextElement();\r
74         int encodedCharIndex = uri.indexOf(encodedChar);\r
75         while (encodedCharIndex != -1) {\r
76           uri = uri.substring(0, encodedCharIndex) + decodingMap.get(encodedChar) + uri.substring(encodedCharIndex + encodedChar.length());\r
77           encodedCharIndex = uri.indexOf(encodedChar, encodedCharIndex);\r
78         }\r
79       }\r
80     }\r
81     Log.info("Final URI: " + uri);\r
82     return uri;\r
83   }\r
84 }\r