| 1 | |
package org.apache.fulcrum.mimetype.util; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
import java.util.Locale; |
| 25 | |
import java.util.Map; |
| 26 | |
import java.util.HashMap; |
| 27 | |
import java.util.Hashtable; |
| 28 | |
import java.util.Properties; |
| 29 | |
import java.io.File; |
| 30 | |
import java.io.InputStream; |
| 31 | |
import java.io.FileInputStream; |
| 32 | |
import java.io.IOException; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
public class CharSetMap |
| 47 | |
{ |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public static final String DEFAULT_CHARSET = "ISO-8859-1"; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public static final String CHARSET_RESOURCE = "charset.properties"; |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
private static final int MAP_CACHE = 0; |
| 62 | |
private static final int MAP_PROG = 1; |
| 63 | |
private static final int MAP_HOME = 2; |
| 64 | |
private static final int MAP_SYS = 3; |
| 65 | |
private static final int MAP_JAR = 4; |
| 66 | |
private static final int MAP_COM = 5; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | 3 | private static HashMap commonMapper = new HashMap(); |
| 72 | |
static |
| 73 | |
{ |
| 74 | 3 | commonMapper.put("ar","ISO-8859-6"); |
| 75 | 3 | commonMapper.put("be","ISO-8859-5"); |
| 76 | 3 | commonMapper.put("bg","ISO-8859-5"); |
| 77 | 3 | commonMapper.put("ca","ISO-8859-1"); |
| 78 | 3 | commonMapper.put("cs","ISO-8859-2"); |
| 79 | 3 | commonMapper.put("da","ISO-8859-1"); |
| 80 | 3 | commonMapper.put("de","ISO-8859-1"); |
| 81 | 3 | commonMapper.put("el","ISO-8859-7"); |
| 82 | 3 | commonMapper.put("en","ISO-8859-1"); |
| 83 | 3 | commonMapper.put("es","ISO-8859-1"); |
| 84 | 3 | commonMapper.put("et","ISO-8859-1"); |
| 85 | 3 | commonMapper.put("fi","ISO-8859-1"); |
| 86 | 3 | commonMapper.put("fr","ISO-8859-1"); |
| 87 | 3 | commonMapper.put("hr","ISO-8859-2"); |
| 88 | 3 | commonMapper.put("hu","ISO-8859-2"); |
| 89 | 3 | commonMapper.put("is","ISO-8859-1"); |
| 90 | 3 | commonMapper.put("it","ISO-8859-1"); |
| 91 | 3 | commonMapper.put("iw","ISO-8859-8"); |
| 92 | 3 | commonMapper.put("ja","Shift_JIS"); |
| 93 | 3 | commonMapper.put("ko","EUC-KR"); |
| 94 | 3 | commonMapper.put("lt","ISO-8859-2"); |
| 95 | 3 | commonMapper.put("lv","ISO-8859-2"); |
| 96 | 3 | commonMapper.put("mk","ISO-8859-5"); |
| 97 | 3 | commonMapper.put("nl","ISO-8859-1"); |
| 98 | 3 | commonMapper.put("no","ISO-8859-1"); |
| 99 | 3 | commonMapper.put("pl","ISO-8859-2"); |
| 100 | 3 | commonMapper.put("pt","ISO-8859-1"); |
| 101 | 3 | commonMapper.put("ro","ISO-8859-2"); |
| 102 | 3 | commonMapper.put("ru","ISO-8859-5"); |
| 103 | 3 | commonMapper.put("sh","ISO-8859-5"); |
| 104 | 3 | commonMapper.put("sk","ISO-8859-2"); |
| 105 | 3 | commonMapper.put("sl","ISO-8859-2"); |
| 106 | 3 | commonMapper.put("sq","ISO-8859-2"); |
| 107 | 3 | commonMapper.put("sr","ISO-8859-5"); |
| 108 | 3 | commonMapper.put("sv","ISO-8859-1"); |
| 109 | 3 | commonMapper.put("tr","ISO-8859-9"); |
| 110 | 3 | commonMapper.put("uk","ISO-8859-5"); |
| 111 | 3 | commonMapper.put("zh","GB2312"); |
| 112 | 3 | commonMapper.put("zh_TW","Big5"); |
| 113 | 3 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | 9 | private Map mappers[] = new Map[6]; |
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
protected static Map loadStream(InputStream input) |
| 128 | |
throws IOException |
| 129 | |
{ |
| 130 | 0 | Properties props = new Properties(); |
| 131 | 0 | props.load(input); |
| 132 | 0 | return new HashMap(props); |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
protected static Map loadFile(File file) |
| 143 | |
throws IOException |
| 144 | |
{ |
| 145 | 18 | return loadStream(new FileInputStream(file)); |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
protected static Map loadPath(String path) |
| 156 | |
throws IOException |
| 157 | |
{ |
| 158 | 18 | return loadFile(new File(path)); |
| 159 | |
} |
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
protected static Map loadResource(String name) |
| 168 | |
{ |
| 169 | 12 | InputStream input = CharSetMap.class.getResourceAsStream(name); |
| 170 | 9 | if (input != null) |
| 171 | |
{ |
| 172 | |
try |
| 173 | |
{ |
| 174 | 0 | return loadStream(input); |
| 175 | |
} |
| 176 | 0 | catch (IOException x) |
| 177 | |
{ |
| 178 | 0 | return null; |
| 179 | |
} |
| 180 | |
} |
| 181 | |
else |
| 182 | |
{ |
| 183 | 9 | return null; |
| 184 | |
} |
| 185 | |
} |
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
public CharSetMap() |
| 191 | 9 | { |
| 192 | |
String path; |
| 193 | |
try |
| 194 | |
{ |
| 195 | |
|
| 196 | 9 | path = System.getProperty("user.home"); |
| 197 | 9 | if (path != null) |
| 198 | |
{ |
| 199 | 9 | path = path + File.separator + CHARSET_RESOURCE; |
| 200 | 9 | mappers[MAP_HOME] = loadPath(path); |
| 201 | |
} |
| 202 | |
} |
| 203 | 9 | catch (IOException x) |
| 204 | |
{ |
| 205 | |
|
| 206 | 0 | } |
| 207 | |
|
| 208 | |
try |
| 209 | |
{ |
| 210 | |
|
| 211 | 9 | path = System.getProperty("java.home") + |
| 212 | |
File.separator + "lib" + File.separator + CHARSET_RESOURCE; |
| 213 | 9 | mappers[MAP_SYS] = loadPath(path); |
| 214 | |
} |
| 215 | 9 | catch (IOException x) |
| 216 | |
{ |
| 217 | |
|
| 218 | 0 | } |
| 219 | |
|
| 220 | |
|
| 221 | 9 | mappers[MAP_JAR] = loadResource("/META-INF/" + CHARSET_RESOURCE); |
| 222 | |
|
| 223 | |
|
| 224 | 9 | mappers[MAP_COM] = commonMapper; |
| 225 | |
|
| 226 | |
|
| 227 | 9 | mappers[MAP_CACHE] = new Hashtable(); |
| 228 | 9 | } |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public CharSetMap(Properties props) |
| 236 | |
{ |
| 237 | 0 | this(); |
| 238 | 0 | mappers[MAP_PROG] = new HashMap(props); |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
public CharSetMap(InputStream input) |
| 248 | |
throws IOException |
| 249 | |
{ |
| 250 | 0 | this(); |
| 251 | 0 | mappers[MAP_PROG] = loadStream(input); |
| 252 | 0 | } |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
public CharSetMap(File file) |
| 261 | |
throws IOException |
| 262 | |
{ |
| 263 | 0 | this(); |
| 264 | 0 | mappers[MAP_PROG] = loadFile(file); |
| 265 | 0 | } |
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
public CharSetMap(String path) |
| 274 | |
throws IOException |
| 275 | |
{ |
| 276 | 0 | this(); |
| 277 | 0 | mappers[MAP_PROG] = loadPath(path); |
| 278 | 0 | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
public synchronized void setCharSet(String key, |
| 287 | |
String charset) |
| 288 | |
{ |
| 289 | 0 | HashMap mapper = (HashMap) mappers[MAP_PROG]; |
| 290 | 0 | mapper = mapper != null ? |
| 291 | |
(HashMap) mapper.clone() : new HashMap(); |
| 292 | 0 | mapper.put(key,charset); |
| 293 | 0 | mappers[MAP_PROG] = mapper; |
| 294 | 0 | mappers[MAP_CACHE].clear(); |
| 295 | 0 | } |
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
public String getCharSet(Locale locale) |
| 306 | |
{ |
| 307 | |
|
| 308 | 3 | String key = locale.toString(); |
| 309 | 3 | if (key.length() == 0) |
| 310 | |
{ |
| 311 | 0 | key = "__" + locale.getVariant(); |
| 312 | 0 | if (key.length() == 2) |
| 313 | |
{ |
| 314 | 0 | return DEFAULT_CHARSET; |
| 315 | |
} |
| 316 | |
} |
| 317 | 3 | String charset = searchCharSet(key); |
| 318 | 3 | if (charset.length() == 0) |
| 319 | |
{ |
| 320 | |
|
| 321 | 3 | String[] items = new String[3]; |
| 322 | 3 | items[2] = locale.getVariant(); |
| 323 | 3 | items[1] = locale.getCountry(); |
| 324 | 3 | items[0] = locale.getLanguage(); |
| 325 | 3 | charset = searchCharSet(items); |
| 326 | 3 | if (charset.length() == 0) |
| 327 | |
{ |
| 328 | 0 | charset = DEFAULT_CHARSET; |
| 329 | |
} |
| 330 | 3 | mappers[MAP_CACHE].put(key,charset); |
| 331 | |
} |
| 332 | 3 | return charset; |
| 333 | |
} |
| 334 | |
|
| 335 | |
|
| 336 | |
|
| 337 | |
|
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
public String getCharSet(Locale locale, |
| 352 | |
String variant) |
| 353 | |
{ |
| 354 | |
|
| 355 | 0 | if ((variant != null) && |
| 356 | |
(variant.length() > 0)) |
| 357 | |
{ |
| 358 | 0 | String key = locale.toString(); |
| 359 | 0 | if (key.length() == 0) |
| 360 | |
{ |
| 361 | 0 | key = "__" + locale.getVariant(); |
| 362 | 0 | if (key.length() > 2) |
| 363 | |
{ |
| 364 | 0 | key += '_' + variant; |
| 365 | |
} |
| 366 | |
else |
| 367 | |
{ |
| 368 | 0 | key += variant; |
| 369 | |
} |
| 370 | |
} |
| 371 | 0 | else if (locale.getCountry().length() == 0) |
| 372 | |
{ |
| 373 | 0 | key += "__" + variant; |
| 374 | |
} |
| 375 | |
else |
| 376 | |
{ |
| 377 | 0 | key += '_' + variant; |
| 378 | |
} |
| 379 | 0 | String charset = searchCharSet(key); |
| 380 | 0 | if (charset.length() == 0) |
| 381 | |
{ |
| 382 | |
|
| 383 | 0 | String[] items = new String[4]; |
| 384 | 0 | items[3] = variant; |
| 385 | 0 | items[2] = locale.getVariant(); |
| 386 | 0 | items[1] = locale.getCountry(); |
| 387 | 0 | items[0] = locale.getLanguage(); |
| 388 | 0 | charset = searchCharSet(items); |
| 389 | 0 | if (charset.length() == 0) |
| 390 | |
{ |
| 391 | 0 | charset = DEFAULT_CHARSET; |
| 392 | |
} |
| 393 | 0 | mappers[MAP_CACHE].put(key,charset); |
| 394 | |
} |
| 395 | 0 | return charset; |
| 396 | |
} |
| 397 | |
else |
| 398 | |
{ |
| 399 | 0 | return getCharSet(locale); |
| 400 | |
} |
| 401 | |
} |
| 402 | |
|
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
public String getCharSet(String key) |
| 410 | |
{ |
| 411 | 0 | String charset = searchCharSet(key); |
| 412 | 0 | return charset.length() > 0 ? charset : DEFAULT_CHARSET; |
| 413 | |
} |
| 414 | |
|
| 415 | |
|
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
|
| 421 | |
|
| 422 | |
public String getCharSet(String key, |
| 423 | |
String def) |
| 424 | |
{ |
| 425 | 0 | String charset = searchCharSet(key); |
| 426 | 0 | return charset.length() > 0 ? charset : def; |
| 427 | |
} |
| 428 | |
|
| 429 | |
|
| 430 | |
|
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
|
| 435 | |
private String searchCharSet(String[] items) |
| 436 | |
{ |
| 437 | |
String charset; |
| 438 | 3 | StringBuffer sb = new StringBuffer(); |
| 439 | 9 | for (int i = items.length; i > 0; i--) |
| 440 | |
{ |
| 441 | 9 | charset = searchCharSet(items,sb,i); |
| 442 | 9 | if (charset.length() > 0) |
| 443 | |
{ |
| 444 | 3 | return charset; |
| 445 | |
} |
| 446 | 6 | sb.setLength(0); |
| 447 | |
} |
| 448 | 0 | return ""; |
| 449 | |
} |
| 450 | |
|
| 451 | |
|
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | |
|
| 456 | |
|
| 457 | |
|
| 458 | |
|
| 459 | |
private String searchCharSet(String[] items, |
| 460 | |
StringBuffer base, |
| 461 | |
int count) |
| 462 | |
{ |
| 463 | 12 | if ((--count >= 0) && |
| 464 | |
(items[count] != null) && |
| 465 | |
(items[count].length() > 0)) |
| 466 | |
{ |
| 467 | |
String charset; |
| 468 | 9 | base.insert(0,items[count]); |
| 469 | 9 | int length = base.length(); |
| 470 | 12 | for (int i = count; i > 0; i--) |
| 471 | |
{ |
| 472 | 3 | if ((i == count) || |
| 473 | |
(i <= 1)) |
| 474 | |
{ |
| 475 | 3 | base.insert(0,'_'); |
| 476 | 3 | length++; |
| 477 | |
} |
| 478 | 3 | charset = searchCharSet(items,base,i); |
| 479 | 3 | if (charset.length() > 0) |
| 480 | |
{ |
| 481 | 0 | return charset; |
| 482 | |
} |
| 483 | 3 | base.delete(0,base.length() - length); |
| 484 | |
} |
| 485 | 9 | return searchCharSet(base.toString()); |
| 486 | |
} |
| 487 | |
else |
| 488 | |
{ |
| 489 | 3 | return ""; |
| 490 | |
} |
| 491 | |
} |
| 492 | |
|
| 493 | |
|
| 494 | |
|
| 495 | |
|
| 496 | |
|
| 497 | |
|
| 498 | |
|
| 499 | |
private String searchCharSet(String key) |
| 500 | |
{ |
| 501 | 12 | if ((key != null) && |
| 502 | |
(key.length() > 0)) |
| 503 | |
{ |
| 504 | |
|
| 505 | |
Map mapper; |
| 506 | |
String charset; |
| 507 | 63 | for (int i = 0; i < mappers.length; i++) |
| 508 | |
{ |
| 509 | 57 | mapper = mappers[i]; |
| 510 | 57 | if (mapper != null) |
| 511 | |
{ |
| 512 | 21 | charset = (String) mapper.get(key); |
| 513 | 21 | if (charset != null) |
| 514 | |
{ |
| 515 | |
|
| 516 | 6 | if (i > MAP_CACHE) |
| 517 | |
{ |
| 518 | 3 | mappers[MAP_CACHE].put(key,charset); |
| 519 | |
} |
| 520 | 6 | return charset; |
| 521 | |
} |
| 522 | |
} |
| 523 | |
} |
| 524 | |
|
| 525 | |
|
| 526 | 6 | mappers[MAP_CACHE].put(key,""); |
| 527 | |
} |
| 528 | 6 | return ""; |
| 529 | |
} |
| 530 | |
|
| 531 | |
|
| 532 | |
|
| 533 | |
|
| 534 | |
|
| 535 | |
|
| 536 | |
|
| 537 | |
protected synchronized void setCommonCharSet(String key, |
| 538 | |
String charset) |
| 539 | |
{ |
| 540 | 0 | HashMap mapper = (HashMap) ((HashMap) mappers[MAP_COM]).clone(); |
| 541 | 0 | mapper.put(key,charset); |
| 542 | 0 | mappers[MAP_COM] = mapper; |
| 543 | 0 | mappers[MAP_CACHE].clear(); |
| 544 | 0 | } |
| 545 | |
} |