KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > I18n


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.utils;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19
20 /**
21  *
22  * @author Khue Nguyen
23  */

24
25 public class I18n {
26
27     static Map JavaDoc w1252ToISO;
28     static {
29         w1252ToISO = new HashMap JavaDoc();
30         w1252ToISO.put(new Character JavaDoc('\u0080'), "Euro");
31         w1252ToISO.put(new Character JavaDoc('\u0082'), ",");
32         w1252ToISO.put(new Character JavaDoc('\u0083'), "f");
33         w1252ToISO.put(new Character JavaDoc('\u0085'), "...");
34         w1252ToISO.put(new Character JavaDoc('\u0088'), "^");
35         w1252ToISO.put(new Character JavaDoc('\u008B'), "<");
36         w1252ToISO.put(new Character JavaDoc('\u008C'), "OE");
37         w1252ToISO.put(new Character JavaDoc('\u0091'), "'");
38         w1252ToISO.put(new Character JavaDoc('\u0092'), "'");
39         w1252ToISO.put(new Character JavaDoc('\u0093'), "\"");
40         w1252ToISO.put(new Character JavaDoc('\u0094'), "\"");
41         w1252ToISO.put(new Character JavaDoc('\u0095'), ".");
42         w1252ToISO.put(new Character JavaDoc('\u0096'), "-");
43         w1252ToISO.put(new Character JavaDoc('\u0097'), "-");
44         w1252ToISO.put(new Character JavaDoc('\u0098'), "~");
45         w1252ToISO.put(new Character JavaDoc('\u009B'), ">");
46         w1252ToISO.put(new Character JavaDoc('\u009C'), "oe");
47     }
48
49     /**
50      * Convert a request parameter value to java unicode string
51      */

52     public static String JavaDoc getText(HttpServletRequest JavaDoc request, String JavaDoc paramName ) {
53
54         if ( paramName == null )
55             return null;
56
57         String JavaDoc value = request.getParameter(paramName);
58         if ( value == null )
59             return null;
60
61         try{
62             value = new String JavaDoc(value.getBytes(), request.getCharacterEncoding());
63         }catch(java.io.UnsupportedEncodingException JavaDoc ex){
64             System.err.println(ex);
65         }catch ( NullPointerException JavaDoc nex ){
66             System.err.println(nex);
67         }
68         return value;
69     }
70
71     public static String JavaDoc windows1252ToISO(String JavaDoc windows1252encoded) {
72         StringBuffer JavaDoc transcodedValue = new StringBuffer JavaDoc(windows1252encoded.length());
73         for (int i=0; i < windows1252encoded.length(); i++) {
74             char curChar = windows1252encoded.charAt(i);
75             if ((curChar >= 128) && (curChar <= 159)) {
76                 // this range of characters is unused in ISO-8859-1 but
77
// is used by Windows Central Europe encoding, so we can
78
// detect it and transcode some of it's characters.
79
Character JavaDoc charIndex = new Character JavaDoc(curChar);
80                 String JavaDoc replacementStr = (String JavaDoc) w1252ToISO.get(charIndex);
81                 if (replacementStr != null) {
82                     transcodedValue.append(replacementStr);
83                 }
84             } else {
85                 transcodedValue.append(curChar);
86             }
87         }
88         return transcodedValue.toString();
89     }
90 }
91
92
Popular Tags