1 | /* |
2 | Copyright (C) 2002-2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of version 2 of the GNU General Public License as |
6 | published by the Free Software Foundation. |
7 | |
8 | There are special exceptions to the terms and conditions of the GPL |
9 | as it is applied to this software. View the full text of the |
10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
11 | software distribution. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 | |
22 | |
23 | |
24 | */ |
25 | package com.mysql.jdbc; |
26 | |
27 | import java.sql.SQLException; |
28 | import java.util.ArrayList; |
29 | import java.util.Collections; |
30 | import java.util.HashMap; |
31 | import java.util.Iterator; |
32 | import java.util.List; |
33 | import java.util.Locale; |
34 | import java.util.Map; |
35 | import java.util.Properties; |
36 | import java.util.Set; |
37 | import java.util.SortedSet; |
38 | |
39 | /** |
40 | * Mapping between MySQL charset names and Java charset names. I've investigated |
41 | * placing these in a .properties file, but unfortunately under most appservers |
42 | * this complicates configuration because the security policy needs to be |
43 | * changed by the user to allow the driver to read them :( |
44 | * |
45 | * @author Mark Matthews |
46 | */ |
47 | public class CharsetMapping { |
48 | private static final Properties CHARSET_CONFIG = new Properties(); |
49 | |
50 | /** |
51 | * Map of MySQL-4.1 charset indexes to Java encoding names |
52 | */ |
53 | public static final String[] INDEX_TO_CHARSET; |
54 | |
55 | /** Mapping of Java charset names to MySQL charset names */ |
56 | private static final Map JAVA_TO_MYSQL_CHARSET_MAP; |
57 | |
58 | private static final Map JAVA_UC_TO_MYSQL_CHARSET_MAP; |
59 | |
60 | /** Map/List of multibyte character sets (using MySQL names) */ |
61 | private static final Map MULTIBYTE_CHARSETS; |
62 | |
63 | private static final Map MYSQL_TO_JAVA_CHARSET_MAP; |
64 | |
65 | static { |
66 | |
67 | CHARSET_CONFIG.setProperty("javaToMysqlMappings", |
68 | // |
69 | // Note: This used to be stored in Charsets.properties, |
70 | // but turned out to be problematic when dealing with |
71 | // Tomcat classloaders when the security manager was |
72 | // enabled |
73 | // |
74 | // Java Encoding MySQL Name (and version, '*' |
75 | // denotes preferred value) |
76 | // |
77 | "US-ASCII = usa7," |
78 | + "US-ASCII = ascii," |
79 | + "Big5 = big5," |
80 | + "GBK = gbk," |
81 | + "SJIS = sjis," |
82 | + "EUC_CN = gb2312," |
83 | + "EUC_JP = ujis," |
84 | + "EUC_JP_Solaris = >5.0.3 eucjpms," |
85 | + "EUC_KR = euc_kr," |
86 | + "EUC_KR = >4.1.0 euckr," |
87 | + "ISO8859_1 = *latin1," |
88 | + "ISO8859_1 = latin1_de," |
89 | + "ISO8859_1 = german1," |
90 | + "ISO8859_1 = danish," |
91 | + "ISO8859_2 = latin2," |
92 | + "ISO8859_2 = czech," |
93 | + "ISO8859_2 = hungarian," |
94 | + "ISO8859_2 = croat," |
95 | + "ISO8859_7 = greek," |
96 | + "ISO8859_7 = latin7," |
97 | + "ISO8859_8 = hebrew," |
98 | + "ISO8859_9 = latin5," |
99 | + "ISO8859_13 = latvian," |
100 | + "ISO8859_13 = latvian1," |
101 | + "ISO8859_13 = estonia," |
102 | + "Cp437 = *>4.1.0 cp850," |
103 | + "Cp437 = dos," |
104 | + "Cp850 = Cp850," |
105 | + "Cp852 = Cp852," |
106 | + "Cp866 = cp866," |
107 | + "KOI8_R = koi8_ru," |
108 | + "KOI8_R = >4.1.0 koi8r," |
109 | + "TIS620 = tis620," |
110 | + "Cp1250 = cp1250," |
111 | + "Cp1250 = win1250," |
112 | + "Cp1251 = *>4.1.0 cp1251," |
113 | + "Cp1251 = win1251," |
114 | + "Cp1251 = cp1251cias," |
115 | + "Cp1251 = cp1251csas," |
116 | + "Cp1256 = cp1256," |
117 | + "Cp1251 = win1251ukr," |
118 | + "Cp1252 = latin1," |
119 | + "Cp1257 = cp1257," |
120 | + "MacRoman = macroman," |
121 | + "MacCentralEurope = macce," |
122 | + "UTF-8 = utf8," |
123 | + "UnicodeBig = ucs2," |
124 | + "US-ASCII = binary," |
125 | + "Cp943 = sjis," |
126 | + "MS932 = sjis," |
127 | + "MS932 = >4.1.11 cp932," |
128 | + "WINDOWS-31J = sjis," |
129 | + "WINDOWS-31J = >4.1.11 cp932," |
130 | + "CP932 = sjis," |
131 | + "CP932 = *>4.1.11 cp932," |
132 | + "SHIFT_JIS = sjis," |
133 | + "ASCII = ascii," |
134 | + "LATIN5 = latin5," |
135 | + "LATIN7 = latin7," |
136 | + "HEBREW = hebrew," |
137 | + "GREEK = greek," |
138 | + "EUCKR = euckr," |
139 | + "GB2312 = gb2312," |
140 | + "LATIN2 = latin2"); |
141 | |
142 | HashMap javaToMysqlMap = new HashMap(); |
143 | |
144 | populateMapWithKeyValuePairs("javaToMysqlMappings", javaToMysqlMap, |
145 | true, false); |
146 | JAVA_TO_MYSQL_CHARSET_MAP = Collections.unmodifiableMap(javaToMysqlMap); |
147 | |
148 | HashMap mysqlToJavaMap = new HashMap(); |
149 | |
150 | Set keySet = JAVA_TO_MYSQL_CHARSET_MAP.keySet(); |
151 | |
152 | Iterator javaCharsets = keySet.iterator(); |
153 | |
154 | while (javaCharsets.hasNext()) { |
155 | Object javaEncodingName = javaCharsets.next(); |
156 | List mysqlEncodingList = (List) JAVA_TO_MYSQL_CHARSET_MAP |
157 | .get(javaEncodingName); |
158 | |
159 | Iterator mysqlEncodings = mysqlEncodingList.iterator(); |
160 | |
161 | String mysqlEncodingName = null; |
162 | |
163 | while (mysqlEncodings.hasNext()) { |
164 | VersionedStringProperty mysqlProp = (VersionedStringProperty) mysqlEncodings |
165 | .next(); |
166 | mysqlEncodingName = mysqlProp.toString(); |
167 | |
168 | mysqlToJavaMap.put(mysqlEncodingName, javaEncodingName); |
169 | mysqlToJavaMap.put(mysqlEncodingName |
170 | .toUpperCase(Locale.ENGLISH), javaEncodingName); |
171 | } |
172 | } |
173 | |
174 | // we don't want CP932 to map to CP932 |
175 | mysqlToJavaMap.put("cp932", "Windows-31J"); |
176 | mysqlToJavaMap.put("CP932", "Windows-31J"); |
177 | |
178 | MYSQL_TO_JAVA_CHARSET_MAP = Collections.unmodifiableMap(mysqlToJavaMap); |
179 | |
180 | HashMap ucMap = new HashMap(JAVA_TO_MYSQL_CHARSET_MAP.size()); |
181 | |
182 | Iterator javaNamesKeys = JAVA_TO_MYSQL_CHARSET_MAP.keySet().iterator(); |
183 | |
184 | while (javaNamesKeys.hasNext()) { |
185 | String key = (String) javaNamesKeys.next(); |
186 | |
187 | ucMap.put(key.toUpperCase(Locale.ENGLISH), |
188 | JAVA_TO_MYSQL_CHARSET_MAP.get(key)); |
189 | } |
190 | |
191 | JAVA_UC_TO_MYSQL_CHARSET_MAP = Collections.unmodifiableMap(ucMap); |
192 | |
193 | // |
194 | // Character sets that we can't convert |
195 | // ourselves. |
196 | // |
197 | HashMap tempMapMulti = new HashMap(); |
198 | |
199 | CHARSET_CONFIG.setProperty("multibyteCharsets", |
200 | // |
201 | // Note: This used to be stored in Charsets.properties, |
202 | // but turned out to be problematic when dealing with |
203 | // Tomcat classloaders when the security manager was |
204 | // enabled |
205 | // |
206 | // Java Name MySQL Name (not currently used) |
207 | // |
208 | |
209 | "Big5 = big5," |
210 | + "GBK = gbk," |
211 | + "SJIS = sjis," |
212 | + "EUC_CN = gb2312," |
213 | + "EUC_JP = ujis," |
214 | + "EUC_JP_Solaris = eucjpms," |
215 | + "EUC_KR = euc_kr," |
216 | + "EUC_KR = >4.1.0 euckr," |
217 | + "Cp943 = sjis," |
218 | + "Cp943 = cp943," |
219 | + "WINDOWS-31J = sjis," |
220 | + "WINDOWS-31J = cp932," |
221 | + "CP932 = cp932," |
222 | + "MS932 = sjis," |
223 | + "MS932 = cp932," |
224 | + "SHIFT_JIS = sjis," |
225 | + "EUCKR = euckr," |
226 | + "GB2312 = gb2312," |
227 | + "UTF-8 = utf8," |
228 | + "utf8 = utf8," |
229 | + "UnicodeBig = ucs2"); |
230 | |
231 | populateMapWithKeyValuePairs("multibyteCharsets", tempMapMulti, false, |
232 | true); |
233 | |
234 | MULTIBYTE_CHARSETS = Collections.unmodifiableMap(tempMapMulti); |
235 | |
236 | INDEX_TO_CHARSET = new String[99]; |
237 | |
238 | try { |
239 | INDEX_TO_CHARSET[1] = getJavaEncodingForMysqlEncoding("big5", null); |
240 | INDEX_TO_CHARSET[2] = getJavaEncodingForMysqlEncoding("czech", null); |
241 | INDEX_TO_CHARSET[3] = getJavaEncodingForMysqlEncoding("dec8", null); |
242 | INDEX_TO_CHARSET[4] = getJavaEncodingForMysqlEncoding("dos", null); |
243 | INDEX_TO_CHARSET[5] = getJavaEncodingForMysqlEncoding("german1", |
244 | null); |
245 | INDEX_TO_CHARSET[6] = getJavaEncodingForMysqlEncoding("hp8", null); |
246 | INDEX_TO_CHARSET[7] = getJavaEncodingForMysqlEncoding("koi8_ru", |
247 | null); |
248 | INDEX_TO_CHARSET[8] = getJavaEncodingForMysqlEncoding("latin1", |
249 | null); |
250 | INDEX_TO_CHARSET[9] = getJavaEncodingForMysqlEncoding("latin2", |
251 | null); |
252 | INDEX_TO_CHARSET[10] = getJavaEncodingForMysqlEncoding("swe7", null); |
253 | INDEX_TO_CHARSET[11] = getJavaEncodingForMysqlEncoding("usa7", null); |
254 | INDEX_TO_CHARSET[12] = getJavaEncodingForMysqlEncoding("ujis", null); |
255 | INDEX_TO_CHARSET[13] = getJavaEncodingForMysqlEncoding("sjis", null); |
256 | INDEX_TO_CHARSET[14] = getJavaEncodingForMysqlEncoding("cp1251", |
257 | null); |
258 | INDEX_TO_CHARSET[15] = getJavaEncodingForMysqlEncoding("danish", |
259 | null); |
260 | INDEX_TO_CHARSET[16] = getJavaEncodingForMysqlEncoding("hebrew", |
261 | null); |
262 | INDEX_TO_CHARSET[18] = getJavaEncodingForMysqlEncoding("tis620", |
263 | null); |
264 | INDEX_TO_CHARSET[19] = getJavaEncodingForMysqlEncoding("euc_kr", |
265 | null); |
266 | INDEX_TO_CHARSET[20] = getJavaEncodingForMysqlEncoding("estonia", |
267 | null); |
268 | INDEX_TO_CHARSET[21] = getJavaEncodingForMysqlEncoding("hungarian", |
269 | null); |
270 | INDEX_TO_CHARSET[22] = getJavaEncodingForMysqlEncoding("koi8_ukr", |
271 | null); |
272 | INDEX_TO_CHARSET[23] = getJavaEncodingForMysqlEncoding( |
273 | "win1251ukr", null); |
274 | INDEX_TO_CHARSET[24] = getJavaEncodingForMysqlEncoding("gb2312", |
275 | null); |
276 | INDEX_TO_CHARSET[25] = getJavaEncodingForMysqlEncoding("greek", |
277 | null); |
278 | INDEX_TO_CHARSET[26] = getJavaEncodingForMysqlEncoding("win1250", |
279 | null); |
280 | INDEX_TO_CHARSET[27] = getJavaEncodingForMysqlEncoding("croat", |
281 | null); |
282 | INDEX_TO_CHARSET[28] = getJavaEncodingForMysqlEncoding("gbk", null); |
283 | INDEX_TO_CHARSET[29] = getJavaEncodingForMysqlEncoding("cp1257", |
284 | null); |
285 | INDEX_TO_CHARSET[30] = getJavaEncodingForMysqlEncoding("latin5", |
286 | null); |
287 | INDEX_TO_CHARSET[31] = getJavaEncodingForMysqlEncoding("latin1_de", |
288 | null); |
289 | INDEX_TO_CHARSET[32] = getJavaEncodingForMysqlEncoding("armscii8", |
290 | null); |
291 | INDEX_TO_CHARSET[33] = getJavaEncodingForMysqlEncoding("utf8", null); |
292 | INDEX_TO_CHARSET[34] = getJavaEncodingForMysqlEncoding("win1250ch", |
293 | null); |
294 | INDEX_TO_CHARSET[35] = getJavaEncodingForMysqlEncoding("ucs2", null); |
295 | INDEX_TO_CHARSET[36] = getJavaEncodingForMysqlEncoding("cp866", |
296 | null); |
297 | INDEX_TO_CHARSET[37] = getJavaEncodingForMysqlEncoding("keybcs2", |
298 | null); |
299 | INDEX_TO_CHARSET[38] = getJavaEncodingForMysqlEncoding("macce", |
300 | null); |
301 | INDEX_TO_CHARSET[39] = getJavaEncodingForMysqlEncoding("macroman", |
302 | null); |
303 | INDEX_TO_CHARSET[40] = getJavaEncodingForMysqlEncoding("pclatin2", |
304 | null); |
305 | INDEX_TO_CHARSET[41] = getJavaEncodingForMysqlEncoding("latvian", |
306 | null); |
307 | INDEX_TO_CHARSET[42] = getJavaEncodingForMysqlEncoding("latvian1", |
308 | null); |
309 | INDEX_TO_CHARSET[43] = getJavaEncodingForMysqlEncoding("maccebin", |
310 | null); |
311 | INDEX_TO_CHARSET[44] = getJavaEncodingForMysqlEncoding("macceciai", |
312 | null); |
313 | INDEX_TO_CHARSET[45] = getJavaEncodingForMysqlEncoding("maccecias", |
314 | null); |
315 | INDEX_TO_CHARSET[46] = getJavaEncodingForMysqlEncoding("maccecsas", |
316 | null); |
317 | INDEX_TO_CHARSET[47] = getJavaEncodingForMysqlEncoding("latin1bin", |
318 | null); |
319 | INDEX_TO_CHARSET[48] = getJavaEncodingForMysqlEncoding( |
320 | "latin1cias", null); |
321 | INDEX_TO_CHARSET[49] = getJavaEncodingForMysqlEncoding( |
322 | "latin1csas", null); |
323 | INDEX_TO_CHARSET[50] = getJavaEncodingForMysqlEncoding("cp1251bin", |
324 | null); |
325 | INDEX_TO_CHARSET[51] = getJavaEncodingForMysqlEncoding( |
326 | "cp1251cias", null); |
327 | INDEX_TO_CHARSET[52] = getJavaEncodingForMysqlEncoding( |
328 | "cp1251csas", null); |
329 | INDEX_TO_CHARSET[53] = getJavaEncodingForMysqlEncoding( |
330 | "macromanbin", null); |
331 | INDEX_TO_CHARSET[54] = getJavaEncodingForMysqlEncoding( |
332 | "macromancias", null); |
333 | INDEX_TO_CHARSET[55] = getJavaEncodingForMysqlEncoding( |
334 | "macromanciai", null); |
335 | INDEX_TO_CHARSET[56] = getJavaEncodingForMysqlEncoding( |
336 | "macromancsas", null); |
337 | INDEX_TO_CHARSET[57] = getJavaEncodingForMysqlEncoding("cp1256", |
338 | null); |
339 | INDEX_TO_CHARSET[63] = getJavaEncodingForMysqlEncoding("binary", |
340 | null); |
341 | INDEX_TO_CHARSET[64] = getJavaEncodingForMysqlEncoding("armscii", |
342 | null); |
343 | INDEX_TO_CHARSET[65] = getJavaEncodingForMysqlEncoding("ascii", |
344 | null); |
345 | INDEX_TO_CHARSET[66] = getJavaEncodingForMysqlEncoding("cp1250", |
346 | null); |
347 | INDEX_TO_CHARSET[67] = getJavaEncodingForMysqlEncoding("cp1256", |
348 | null); |
349 | INDEX_TO_CHARSET[68] = getJavaEncodingForMysqlEncoding("cp866", |
350 | null); |
351 | INDEX_TO_CHARSET[69] = getJavaEncodingForMysqlEncoding("dec8", null); |
352 | INDEX_TO_CHARSET[70] = getJavaEncodingForMysqlEncoding("greek", |
353 | null); |
354 | INDEX_TO_CHARSET[71] = getJavaEncodingForMysqlEncoding("hebrew", |
355 | null); |
356 | INDEX_TO_CHARSET[72] = getJavaEncodingForMysqlEncoding("hp8", null); |
357 | INDEX_TO_CHARSET[73] = getJavaEncodingForMysqlEncoding("keybcs2", |
358 | null); |
359 | INDEX_TO_CHARSET[74] = getJavaEncodingForMysqlEncoding("koi8r", |
360 | null); |
361 | INDEX_TO_CHARSET[75] = getJavaEncodingForMysqlEncoding("koi8ukr", |
362 | null); |
363 | INDEX_TO_CHARSET[77] = getJavaEncodingForMysqlEncoding("latin2", |
364 | null); |
365 | INDEX_TO_CHARSET[78] = getJavaEncodingForMysqlEncoding("latin5", |
366 | null); |
367 | INDEX_TO_CHARSET[79] = getJavaEncodingForMysqlEncoding("latin7", |
368 | null); |
369 | INDEX_TO_CHARSET[80] = getJavaEncodingForMysqlEncoding("cp850", |
370 | null); |
371 | INDEX_TO_CHARSET[81] = getJavaEncodingForMysqlEncoding("cp852", |
372 | null); |
373 | INDEX_TO_CHARSET[82] = getJavaEncodingForMysqlEncoding("swe7", null); |
374 | INDEX_TO_CHARSET[83] = getJavaEncodingForMysqlEncoding("utf8", null); |
375 | INDEX_TO_CHARSET[84] = getJavaEncodingForMysqlEncoding("big5", null); |
376 | INDEX_TO_CHARSET[85] = getJavaEncodingForMysqlEncoding("euckr", |
377 | null); |
378 | INDEX_TO_CHARSET[86] = getJavaEncodingForMysqlEncoding("gb2312", |
379 | null); |
380 | INDEX_TO_CHARSET[87] = getJavaEncodingForMysqlEncoding("gbk", null); |
381 | INDEX_TO_CHARSET[88] = getJavaEncodingForMysqlEncoding("sjis", null); |
382 | INDEX_TO_CHARSET[89] = getJavaEncodingForMysqlEncoding("tis620", |
383 | null); |
384 | INDEX_TO_CHARSET[90] = getJavaEncodingForMysqlEncoding("ucs2", null); |
385 | INDEX_TO_CHARSET[91] = getJavaEncodingForMysqlEncoding("ujis", null); |
386 | INDEX_TO_CHARSET[92] = getJavaEncodingForMysqlEncoding("geostd8", |
387 | null); |
388 | INDEX_TO_CHARSET[93] = getJavaEncodingForMysqlEncoding("geostd8", |
389 | null); |
390 | INDEX_TO_CHARSET[94] = getJavaEncodingForMysqlEncoding("latin1", |
391 | null); |
392 | INDEX_TO_CHARSET[95] = getJavaEncodingForMysqlEncoding("cp932", |
393 | null); |
394 | INDEX_TO_CHARSET[96] = getJavaEncodingForMysqlEncoding("cp932", |
395 | null); |
396 | INDEX_TO_CHARSET[97] = getJavaEncodingForMysqlEncoding("eucjpms", |
397 | null); |
398 | INDEX_TO_CHARSET[98] = getJavaEncodingForMysqlEncoding("eucjpms", |
399 | null); |
400 | } catch (SQLException sqlEx) { |
401 | // ignore, it won't happen in this case |
402 | } |
403 | } |
404 | |
405 | final static String getJavaEncodingForMysqlEncoding(String mysqlEncoding, |
406 | Connection conn) throws SQLException { |
407 | |
408 | if (conn != null && conn.versionMeetsMinimum(4, 1, 0) && |
409 | "latin1".equalsIgnoreCase(mysqlEncoding)) { |
410 | return "Cp1252"; |
411 | } |
412 | |
413 | return (String) MYSQL_TO_JAVA_CHARSET_MAP.get(mysqlEncoding); |
414 | } |
415 | |
416 | final static String getMysqlEncodingForJavaEncoding(String javaEncodingUC, |
417 | Connection conn) throws SQLException { |
418 | List mysqlEncodings = (List) CharsetMapping.JAVA_UC_TO_MYSQL_CHARSET_MAP |
419 | .get(javaEncodingUC); |
420 | ; |
421 | |
422 | if (mysqlEncodings != null) { |
423 | Iterator iter = mysqlEncodings.iterator(); |
424 | |
425 | VersionedStringProperty versionedProp = null; |
426 | |
427 | while (iter.hasNext()) { |
428 | VersionedStringProperty propToCheck = (VersionedStringProperty) iter |
429 | .next(); |
430 | |
431 | if (conn == null) { |
432 | // Take the first one we get |
433 | |
434 | return propToCheck.toString(); |
435 | } |
436 | |
437 | if (versionedProp != null && !versionedProp.preferredValue) { |
438 | if (versionedProp.majorVersion == propToCheck.majorVersion |
439 | && versionedProp.minorVersion == propToCheck.minorVersion |
440 | && versionedProp.subminorVersion == propToCheck.subminorVersion) { |
441 | return versionedProp.toString(); |
442 | } |
443 | } |
444 | |
445 | if (propToCheck.isOkayForVersion(conn)) { |
446 | if (propToCheck.preferredValue) { |
447 | return propToCheck.toString(); |
448 | } |
449 | |
450 | versionedProp = propToCheck; |
451 | } else { |
452 | break; |
453 | } |
454 | } |
455 | |
456 | if (versionedProp != null) { |
457 | return versionedProp.toString(); |
458 | } |
459 | } |
460 | |
461 | return null; |
462 | } |
463 | |
464 | final static int getNumberOfCharsetsConfigured() { |
465 | return MYSQL_TO_JAVA_CHARSET_MAP.size() / 2; // because we UC every |
466 | // key |
467 | } |
468 | |
469 | final static boolean isAliasForSjis(String encoding) { |
470 | return ("SJIS".equalsIgnoreCase(encoding) |
471 | || "WINDOWS-31J".equalsIgnoreCase(encoding) |
472 | || "MS932".equalsIgnoreCase(encoding) |
473 | || "SHIFT_JIS".equalsIgnoreCase(encoding) || "CP943" |
474 | .equalsIgnoreCase(encoding)); |
475 | |
476 | } |
477 | |
478 | final static boolean isMultibyteCharset(String javaEncodingName) { |
479 | String javaEncodingNameUC = javaEncodingName |
480 | .toUpperCase(Locale.ENGLISH); |
481 | |
482 | return MULTIBYTE_CHARSETS.containsKey(javaEncodingNameUC); |
483 | } |
484 | |
485 | private static void populateMapWithKeyValuePairs(String configKey, |
486 | Map mapToPopulate, boolean addVersionedProperties, |
487 | boolean addUppercaseKeys) { |
488 | String javaToMysqlConfig = CHARSET_CONFIG.getProperty(configKey); |
489 | |
490 | if (javaToMysqlConfig != null) { |
491 | List mappings = StringUtils.split(javaToMysqlConfig, ",", true); |
492 | |
493 | if (mappings != null) { |
494 | Iterator mappingsIter = mappings.iterator(); |
495 | |
496 | while (mappingsIter.hasNext()) { |
497 | String aMapping = (String) mappingsIter.next(); |
498 | |
499 | List parsedPair = StringUtils.split(aMapping, "=", true); |
500 | |
501 | if (parsedPair.size() == 2) { |
502 | String key = parsedPair.get(0).toString(); |
503 | String value = parsedPair.get(1).toString(); |
504 | |
505 | if (addVersionedProperties) { |
506 | List versionedProperties = (List) mapToPopulate |
507 | .get(key); |
508 | |
509 | if (versionedProperties == null) { |
510 | versionedProperties = new ArrayList(); |
511 | mapToPopulate.put(key, versionedProperties); |
512 | } |
513 | |
514 | VersionedStringProperty verProp = new VersionedStringProperty( |
515 | value); |
516 | versionedProperties.add(verProp); |
517 | |
518 | if (addUppercaseKeys) { |
519 | String keyUc = key.toUpperCase(Locale.ENGLISH); |
520 | |
521 | versionedProperties = (List) mapToPopulate |
522 | .get(keyUc); |
523 | |
524 | if (versionedProperties == null) { |
525 | versionedProperties = new ArrayList(); |
526 | mapToPopulate.put(keyUc, |
527 | versionedProperties); |
528 | } |
529 | |
530 | versionedProperties.add(verProp); |
531 | } |
532 | } else { |
533 | mapToPopulate.put(key, value); |
534 | |
535 | if (addUppercaseKeys) { |
536 | mapToPopulate.put(key |
537 | .toUpperCase(Locale.ENGLISH), value); |
538 | } |
539 | } |
540 | } else { |
541 | throw new RuntimeException( |
542 | "Syntax error in Charsets.properties " |
543 | + "resource for token \"" + aMapping |
544 | + "\"."); |
545 | } |
546 | } |
547 | } else { |
548 | throw new RuntimeException("Missing/corrupt entry for \"" |
549 | + configKey + "\" in Charsets.properties."); |
550 | } |
551 | } else { |
552 | throw new RuntimeException("Could not find configuration value " |
553 | + "\"" + configKey + "\" in Charsets.properties resource"); |
554 | } |
555 | } |
556 | } |
557 | |
558 | class VersionedStringProperty { |
559 | int majorVersion, minorVersion, subminorVersion; |
560 | |
561 | boolean preferredValue = false; |
562 | |
563 | String propertyInfo; |
564 | |
565 | VersionedStringProperty(String property) { |
566 | property = property.trim(); |
567 | |
568 | if (property.startsWith("*")) { |
569 | property = property.substring(1); |
570 | preferredValue = true; |
571 | } |
572 | |
573 | if (property.startsWith(">")) { |
574 | property = property.substring(1); |
575 | |
576 | int charPos = 0; |
577 | |
578 | for (charPos = 0; charPos < property.length(); charPos++) { |
579 | char c = property.charAt(charPos); |
580 | |
581 | if (!Character.isWhitespace(c) && !Character.isDigit(c) |
582 | && c != '.') { |
583 | break; |
584 | } |
585 | } |
586 | |
587 | String versionInfo = property.substring(0, charPos); |
588 | List versionParts = StringUtils.split(versionInfo, ".", true); |
589 | |
590 | majorVersion = Integer.parseInt(versionParts.get(0).toString()); |
591 | |
592 | if (versionParts.size() > 1) { |
593 | minorVersion = Integer.parseInt(versionParts.get(1).toString()); |
594 | } else { |
595 | minorVersion = 0; |
596 | } |
597 | |
598 | if (versionParts.size() > 2) { |
599 | subminorVersion = Integer.parseInt(versionParts.get(2) |
600 | .toString()); |
601 | } else { |
602 | subminorVersion = 0; |
603 | } |
604 | |
605 | propertyInfo = property.substring(charPos); |
606 | } else { |
607 | majorVersion = minorVersion = subminorVersion = 0; |
608 | propertyInfo = property; |
609 | } |
610 | } |
611 | |
612 | VersionedStringProperty(String property, int major, int minor, int subminor) { |
613 | propertyInfo = property; |
614 | majorVersion = major; |
615 | minorVersion = minor; |
616 | subminorVersion = subminor; |
617 | } |
618 | |
619 | boolean isOkayForVersion(Connection conn) throws SQLException { |
620 | return conn.versionMeetsMinimum(majorVersion, minorVersion, |
621 | subminorVersion); |
622 | } |
623 | |
624 | public String toString() { |
625 | return propertyInfo; |
626 | } |
627 | } |