View Javadoc

1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
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 License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 }