KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > CharsetMapping


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27
28 /**
29  * Mapping between MySQL charset names
30  * and Java charset names.
31  *
32  * I've investigated placing these in a .properties file,
33  * but unfortunately under most appservers this complicates
34  * configuration because the security policy needs to be changed
35  * by the user to allow the driver to read them :(
36  *
37  * @author Mark Matthews
38  */

39 public class CharsetMapping {
40     //~ Static fields/initializers ---------------------------------------------
41

42     /**
43      * Mapping of Java charset names to MySQL charset names
44      */

45     public static final Map JavaDoc JAVA_TO_MYSQL_CHARSET_MAP;
46
47     /**
48      * Mapping of MySQL charset names to Java charset names
49      */

50     public static final Map JavaDoc CHARSETMAP;
51
52     /**
53      * Map/List of multibyte character sets (using MySQL names)
54      */

55     public static final Map JavaDoc MULTIBYTE_CHARSETS;
56
57     /**
58      * Map of MySQL-4.1 charset indexes to Java encoding names
59      */

60     public static final String JavaDoc[] INDEX_TO_CHARSET;
61
62     static {
63         HashMap JavaDoc tempMap = new HashMap JavaDoc();
64
65         tempMap.put("usa7", "US-ASCII");
66         tempMap.put("big5", "Big5");
67         tempMap.put("gbk", "GBK");
68         tempMap.put("sjis", "SJIS");
69         tempMap.put("gb2312", "EUC_CN");
70         tempMap.put("ujis", "EUC_JP");
71         tempMap.put("euc_kr", "EUC_KR");
72         tempMap.put("latin1", "ISO8859_1");
73         tempMap.put("latin1_de", "ISO8859_1");
74         tempMap.put("german1", "ISO8859_1");
75         tempMap.put("danish", "ISO8859_1");
76         tempMap.put("latin2", "ISO8859_2");
77         tempMap.put("czech", "ISO8859_2");
78         tempMap.put("hungarian", "ISO8859_2");
79         tempMap.put("croat", "ISO8859_2");
80         tempMap.put("greek", "ISO8859_7");
81         tempMap.put("hebrew", "ISO8859_8");
82         tempMap.put("latin5", "ISO8859_9");
83         tempMap.put("latvian", "ISO8859_13");
84         tempMap.put("latvian1", "ISO8859_13");
85         tempMap.put("estonia", "ISO8859_13");
86         tempMap.put("dos", "Cp437");
87         tempMap.put("pclatin2", "Cp852");
88         tempMap.put("cp866", "Cp866");
89         tempMap.put("koi8_ru", "KOI8_R");
90         tempMap.put("tis620", "TIS620");
91         tempMap.put("win1250", "Cp1250");
92         tempMap.put("win1250ch", "Cp1250");
93         tempMap.put("win1251", "Cp1251");
94         tempMap.put("cp1251", "Cp1251");
95         tempMap.put("win1251ukr", "Cp1251");
96         tempMap.put("cp1257", "Cp1257");
97         tempMap.put("macroman", "MacRoman");
98         tempMap.put("macce", "MacCentralEurope");
99         tempMap.put("utf8", "UTF-8");
100         tempMap.put("ucs2", "UnicodeBig");
101
102         CHARSETMAP = Collections.unmodifiableMap(tempMap);
103
104         HashMap JavaDoc javaToMysqlMap = new HashMap JavaDoc();
105
106         Set JavaDoc keySet = CHARSETMAP.keySet();
107
108         Iterator JavaDoc keys = keySet.iterator();
109
110         while (keys.hasNext()) {
111             Object JavaDoc mysqlEncodingName = keys.next();
112             Object JavaDoc javaEncodingName = CHARSETMAP.get(mysqlEncodingName);
113
114             //
115
// Use 'closest' encodings here...as Java encoding names
116
// overlap with some MySQL character sets.
117
//
118
if ("ISO8859_1".equals(javaEncodingName)) {
119                 if ("latin1".equals(mysqlEncodingName)) {
120                     javaToMysqlMap.put(javaEncodingName, mysqlEncodingName);
121                 }
122             } else if ("ISO8859_2".equals(javaEncodingName)) {
123                 if ("latin2".equals(mysqlEncodingName)) {
124                     javaToMysqlMap.put(javaEncodingName, mysqlEncodingName);
125                 }
126             }
127             else if ("ISO8859_13".equals(javaEncodingName)) {
128                 if ("latin7".equals(mysqlEncodingName)) {
129                     javaToMysqlMap.put(javaEncodingName, mysqlEncodingName);
130                 }
131             } else {
132                 javaToMysqlMap.put(javaEncodingName, mysqlEncodingName);
133             }
134         }
135
136         JAVA_TO_MYSQL_CHARSET_MAP = Collections.unmodifiableMap(javaToMysqlMap);
137
138         //
139
// Character sets that we can't convert
140
// ourselves.
141
//
142
HashMap JavaDoc tempMapMulti = new HashMap JavaDoc();
143
144         tempMapMulti.put("big5", "big5");
145         tempMapMulti.put("euc_kr", "euc_kr");
146         tempMapMulti.put("gb2312", "gb2312");
147         tempMapMulti.put("gbk", "gbk");
148         tempMapMulti.put("sjis", "sjis");
149         tempMapMulti.put("ujis", "ujist");
150         tempMapMulti.put("utf8", "utf8");
151         tempMapMulti.put("ucs2", "UnicodeBig");
152
153         MULTIBYTE_CHARSETS = Collections.unmodifiableMap(tempMapMulti);
154
155         INDEX_TO_CHARSET = new String JavaDoc[64];
156
157         INDEX_TO_CHARSET[1] = (String JavaDoc) CHARSETMAP.get("big5");
158         INDEX_TO_CHARSET[2] = (String JavaDoc) CHARSETMAP.get(" czech");
159         INDEX_TO_CHARSET[3] = (String JavaDoc) CHARSETMAP.get("dec8");
160         INDEX_TO_CHARSET[4] = (String JavaDoc) CHARSETMAP.get("dos");
161         INDEX_TO_CHARSET[5] = (String JavaDoc) CHARSETMAP.get("german1");
162         INDEX_TO_CHARSET[6] = (String JavaDoc) CHARSETMAP.get("hp8");
163         INDEX_TO_CHARSET[7] = (String JavaDoc) CHARSETMAP.get("koi8_ru");
164         INDEX_TO_CHARSET[8] = (String JavaDoc) CHARSETMAP.get("latin1");
165         INDEX_TO_CHARSET[9] = (String JavaDoc) CHARSETMAP.get("latin2");
166         INDEX_TO_CHARSET[10] = (String JavaDoc) CHARSETMAP.get("swe7");
167         INDEX_TO_CHARSET[11] = (String JavaDoc) CHARSETMAP.get("usa7");
168         INDEX_TO_CHARSET[12] = (String JavaDoc) CHARSETMAP.get("ujis");
169         INDEX_TO_CHARSET[13] = (String JavaDoc) CHARSETMAP.get("sjis");
170         INDEX_TO_CHARSET[14] = (String JavaDoc) CHARSETMAP.get("cp1251");
171         INDEX_TO_CHARSET[15] = (String JavaDoc) CHARSETMAP.get("danish");
172         INDEX_TO_CHARSET[16] = (String JavaDoc) CHARSETMAP.get("hebrew");
173         INDEX_TO_CHARSET[18] = (String JavaDoc) CHARSETMAP.get("tis620");
174         INDEX_TO_CHARSET[19] = (String JavaDoc) CHARSETMAP.get("euc_kr");
175         INDEX_TO_CHARSET[20] = (String JavaDoc) CHARSETMAP.get("estonia");
176         INDEX_TO_CHARSET[21] = (String JavaDoc) CHARSETMAP.get("hungarian");
177         INDEX_TO_CHARSET[22] = (String JavaDoc) CHARSETMAP.get("koi8_ukr");
178         INDEX_TO_CHARSET[23] = (String JavaDoc) CHARSETMAP.get("win1251ukr");
179         INDEX_TO_CHARSET[24] = (String JavaDoc) CHARSETMAP.get("gb2312");
180         INDEX_TO_CHARSET[25] = (String JavaDoc) CHARSETMAP.get("greek");
181         INDEX_TO_CHARSET[26] = (String JavaDoc) CHARSETMAP.get("win1250");
182         INDEX_TO_CHARSET[27] = (String JavaDoc) CHARSETMAP.get("croat");
183         INDEX_TO_CHARSET[28] = (String JavaDoc) CHARSETMAP.get("gbk");
184         INDEX_TO_CHARSET[29] = (String JavaDoc) CHARSETMAP.get("cp1257");
185         INDEX_TO_CHARSET[30] = (String JavaDoc) CHARSETMAP.get("latin5");
186         INDEX_TO_CHARSET[31] = (String JavaDoc) CHARSETMAP.get("latin1_de");
187         INDEX_TO_CHARSET[32] = (String JavaDoc) CHARSETMAP.get("armscii8");
188         INDEX_TO_CHARSET[33] = (String JavaDoc) CHARSETMAP.get("utf8");
189         INDEX_TO_CHARSET[34] = (String JavaDoc) CHARSETMAP.get("win1250ch");
190         INDEX_TO_CHARSET[35] = (String JavaDoc) CHARSETMAP.get("ucs2");
191         INDEX_TO_CHARSET[36] = (String JavaDoc) CHARSETMAP.get("cp866");
192         INDEX_TO_CHARSET[37] = (String JavaDoc) CHARSETMAP.get("keybcs2");
193         INDEX_TO_CHARSET[38] = (String JavaDoc) CHARSETMAP.get("macce");
194         INDEX_TO_CHARSET[39] = (String JavaDoc) CHARSETMAP.get("macroman");
195         INDEX_TO_CHARSET[40] = (String JavaDoc) CHARSETMAP.get("pclatin2");
196         INDEX_TO_CHARSET[41] = (String JavaDoc) CHARSETMAP.get("latvian");
197         INDEX_TO_CHARSET[42] = (String JavaDoc) CHARSETMAP.get("latvian1");
198         INDEX_TO_CHARSET[43] = (String JavaDoc) CHARSETMAP.get("maccebin");
199         INDEX_TO_CHARSET[44] = (String JavaDoc) CHARSETMAP.get("macceciai");
200         INDEX_TO_CHARSET[45] = (String JavaDoc) CHARSETMAP.get("maccecias");
201         INDEX_TO_CHARSET[46] = (String JavaDoc) CHARSETMAP.get("maccecsas");
202         INDEX_TO_CHARSET[47] = (String JavaDoc) CHARSETMAP.get("latin1bin");
203         INDEX_TO_CHARSET[48] = (String JavaDoc) CHARSETMAP.get("latin1cias");
204         INDEX_TO_CHARSET[49] = (String JavaDoc) CHARSETMAP.get("latin1csas");
205         INDEX_TO_CHARSET[50] = (String JavaDoc) CHARSETMAP.get("cp1251bin");
206         INDEX_TO_CHARSET[51] = (String JavaDoc) CHARSETMAP.get("cp1251cias");
207         INDEX_TO_CHARSET[52] = (String JavaDoc) CHARSETMAP.get("cp1251csas");
208         INDEX_TO_CHARSET[53] = (String JavaDoc) CHARSETMAP.get("macromanbin");
209         INDEX_TO_CHARSET[54] = (String JavaDoc) CHARSETMAP.get("macromancias");
210         INDEX_TO_CHARSET[55] = (String JavaDoc) CHARSETMAP.get("macromanciai");
211         INDEX_TO_CHARSET[56] = (String JavaDoc) CHARSETMAP.get("macromancsas");
212         INDEX_TO_CHARSET[57] = (String JavaDoc) CHARSETMAP.get("cp1256");
213         INDEX_TO_CHARSET[63] = (String JavaDoc) CHARSETMAP.get("binary");
214     }
215 }
216
Popular Tags