1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.dexterim.msn.util;
20
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Properties;
28
29 /***
30 * Description of the Class
31 *
32 * @author christoph
33 */
34 public class SpecialCharacters {
35 private static SpecialCharacters instance;
36
37 private List encodedChars;
38 private List decodedChars;
39
40 /***
41 * Creates a new instance of SpecialCharacters
42 */
43 protected SpecialCharacters() {
44 encodedChars = new ArrayList();
45 decodedChars = new ArrayList();
46
47 initialize(encodedChars, decodedChars);
48 }
49
50 protected final void initialize(List encodedChars, List decodedChars) {
51 Properties props = new Properties();
52
53 try {
54 props.load(
55 new FileInputStream(
56 getClass().getResource("characters.properties").getFile()));
57
58 Iterator propertyIter = props.keySet().iterator();
59
60 while (propertyIter.hasNext()) {
61 String decodeChar = (String)propertyIter.next();
62 String encodeChar = (String)props.get(decodeChar);
63
64 decodedChars.add(decodeChar);
65 encodedChars.add(encodeChar);
66 }
67 } catch (FileNotFoundException e) {
68 e.printStackTrace();
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73 /***
74 * Gets the instance attribute of the SpecialCharacters class
75 *
76 * @return The instance value
77 */
78 public static synchronized SpecialCharacters getInstance() {
79 if (instance == null) {
80 instance = new SpecialCharacters();
81 }
82
83 return instance;
84 }
85
86 /***
87 * Description of the Method
88 *
89 * @param str
90 * Description of the Parameter
91 * @return Description of the Return Value
92 */
93 public String decode(String str) {
94 return decode(str, false);
95 }
96
97 /***
98 * Description of the Method
99 *
100 * @param str
101 * Description of the Parameter
102 * @param urldecode
103 * Description of the Parameter
104 * @return Description of the Return Value
105 */
106 public String decode(String str, boolean urldecode) {
107
108 String decodedString;
109
110 if (urldecode) {
111 decodedString = urlDecode(str);
112 } else {
113 decodedString = str;
114 }
115
116 for (int i = 0; i < decodedChars.size(); i++) {
117 decodedString =
118 replace(decodedString, getDecodedChar(i), getEncodedChar(i));
119 }
120
121 return decodedString;
122 }
123
124 private String urlDecode(String str) {
125 try {
126 return java.net.URLDecoder.decode(str, "UTF-8");
127 } catch (java.io.UnsupportedEncodingException ex) {
128 ex.printStackTrace();
129 }
130
131 return str;
132 }
133
134 /***
135 * Description of the Method
136 *
137 * @param str
138 * Description of the Parameter
139 * @return Description of the Return Value
140 */
141 public String encode(String str) {
142 return encode(str, false);
143 }
144
145 /***
146 * Description of the Method
147 *
148 * @param str
149 * Description of the Parameter
150 * @param urlencode
151 * Description of the Parameter
152 * @return Description of the Return Value
153 */
154 public String encode(String str, boolean urlencode) {
155 String encodedString;
156
157 if (urlencode) {
158 encodedString = urlEncode(str);
159 } else {
160 encodedString = str;
161 }
162
163 for (int i = 0; i < decodedChars.size(); i++) {
164 encodedString =
165 replace(encodedString, getEncodedChar(i), getDecodedChar(i));
166 }
167
168 return encodedString;
169 }
170
171 private String urlEncode(String string) {
172 try {
173 return java.net.URLEncoder.encode(string, "UTF-8");
174 } catch (java.io.UnsupportedEncodingException ex) {
175 ex.printStackTrace();
176 }
177
178 return string;
179 }
180
181 protected String getEncodedChar(int index) {
182 return (String)decodedChars.get(index);
183 }
184
185 protected String getDecodedChar(int index) {
186 return (String)encodedChars.get(index);
187 }
188
189 /***
190 * Description of the Method
191 *
192 * @param str
193 * Description of the Parameter
194 * @param a
195 * Description of the Parameter
196 * @param b
197 * Description of the Parameter
198 * @return Description of the Return Value
199 */
200 public String replace(String string, String a, String b) {
201 int i = 0;
202
203 String returnValue = string;
204
205 while ((i = returnValue.indexOf(a)) != -1) {
206 returnValue =
207 returnValue.substring(0, i) + b + returnValue.substring(i + a.length());
208 }
209
210 return returnValue;
211 }
212 }