Source code: plugins/MsnService/SpecialCharacters.java
1 /*
2 * SpecialCharacters.java
3 *
4 * Created on 01. Oktober 2002, 16:22
5 */
6
7 package plugins.MsnService;
8
9 /**
10 *
11 * @author tobiasnb
12 */
13 import java.util.Vector;
14 import java.io.*;
15 import dexter.core.Dexter;
16
17 public class SpecialCharacters {
18 static SpecialCharacters instance;
19
20 Vector badchars = new Vector();
21 Vector goodchars = new Vector();
22
23 static public SpecialCharacters getInstance() {
24 if (instance == null) instance = new SpecialCharacters();
25 return instance;
26 }
27
28 /** Creates a new instance of SpecialCharacters */
29 public SpecialCharacters() {
30 String fname = getClass().getResource("/plugins/MsnService/specialchars.txt").getFile();
31 fname = Dexter.repairFileName(fname);
32 try {
33
34 BufferedReader in = new BufferedReader(new FileReader(fname));
35 String str;
36 while ((str = in.readLine()) != null) {
37 String tok = "\t";
38 int gindex = str.indexOf(tok);
39 if (gindex >= 0) {
40 String bad = str.substring(0,gindex);
41 String good = str.substring(gindex+tok.length());
42 badchars.add(bad);
43 goodchars.add(good);
44 }
45 }
46 in.close();
47 } catch (IOException e) {
48 }
49 }
50
51 public String decode(String str) {
52 return decode(str,false);
53 }
54
55 public String decode(String str, boolean urldecode) {
56 try {
57 if (urldecode) str = java.net.URLDecoder.decode(str,"UTF-8");
58 } catch ( java.io.UnsupportedEncodingException ex) {}
59 for (int i=0;i<goodchars.size();i++) {
60 str = replace(str, (String) goodchars.get(i), (String) badchars.get(i));
61 }
62 return str;
63 }
64
65 public String encode(String str) {
66 return encode(str, false);
67 }
68
69 public String encode(String str, boolean urlencode) {
70 try {
71 if (urlencode) str = java.net.URLEncoder.encode(str,"UTF-8");
72 } catch ( java.io.UnsupportedEncodingException ex) {}
73 for (int i=0;i<goodchars.size();i++) {
74 str = replace(str, (String) badchars.get(i), (String) goodchars.get(i));
75 }
76 return str;
77 }
78
79 public String replace(String str, String a, String b) {
80 int i = 0;
81 while ( (i = str.indexOf(a)) != -1) {
82 String newStr = new String(str.substring(0,i) + b + str.substring(i+a.length()));
83 str = newStr;
84 }
85 return str;
86 }
87
88 }