3 import java
.io
.UnsupportedEncodingException
;
4 import java
.util
.HashMap
;
7 * This is a little class to abstract the character encoding strings that Java
8 * uses into classes which can be checked at compile time.
10 * @author Kevin Scaldeferri
12 public abstract class CharacterEncoding
{
15 private static final HashMap
<String
, CharacterEncoding
> ENCODING_HASH
= new HashMap
<String
, CharacterEncoding
>();
16 private static final String CANONICAL_ASCII_NAME
= "ASCII";
17 private static final String CANONICAL_ISO_8859_1_NAME
= "ISO8859_1";
18 private static final String CANONICAL_UTF_8_NAME
= "UTF8";
19 private static final String CANONICAL_SHIFT_JIS_NAME
= "SJIS";
20 private static final String CANONICAL_EUC_JP_NAME
= "EUC_JP";
21 private static final String CANONICAL_EUC_KR_NAME
= "EUC_KR";
22 private static final String
[] ASCII_ALIASES
= { CANONICAL_ASCII_NAME
,
23 "US-ASCII", "ISO646-US" };
24 private static final String
[] ISO_8859_1_ALIASES
= {
25 CANONICAL_ISO_8859_1_NAME
, "ISO-8859-1", "ISO-LATIN-1", "8859_1" };
26 private static final String
[] UTF_8_ALIASES
= { CANONICAL_UTF_8_NAME
,
28 private static final String
[] SHIFT_JIS_ALIASES
= {
29 CANONICAL_SHIFT_JIS_NAME
, "SHIFTJIS", "SHIFT-JIS", "SHIFT_JIS" };
30 private static final String
[] EUC_JP_ALIASES
= { CANONICAL_EUC_JP_NAME
,
32 private static final String
[] EUC_KR_ALIASES
= { CANONICAL_EUC_KR_NAME
,
36 * Returns Java's canonical form of the encoding.
38 public abstract String
getEncodingString();
41 * Returns the official IANA name for the encoding. Everything expect Java
44 public abstract String
getIANAString();
46 public static final CharacterEncoding ISO_8859_1
= new CharacterEncoding() {
47 public String
getEncodingString() {
51 public String
getIANAString() {
56 public static final CharacterEncoding UTF_8
= new CharacterEncoding() {
57 public String
getEncodingString() {
61 public String
getIANAString() {
66 public static final CharacterEncoding ASCII
= new CharacterEncoding() {
67 public String
getEncodingString() {
71 public String
getIANAString() {
76 public static final CharacterEncoding SHIFT_JIS
= new CharacterEncoding() {
77 public String
getEncodingString() {
81 public String
getIANAString() {
86 public static final CharacterEncoding EUC_JP
= new CharacterEncoding() {
87 public String
getEncodingString() {
91 public String
getIANAString() {
96 public static final CharacterEncoding EUC_KR
= new CharacterEncoding() {
97 public String
getEncodingString() {
101 public String
getIANAString() {
107 * This is a highly limited implementation at the moment, so don't expect
109 * @param enc the String representation of the encoding.
110 * @return CharacterEncoding
111 * @throws java.io.UnsupportedEncodingException if the encoding doesnt exist.
113 public static CharacterEncoding
getInstance(String enc
)
114 throws UnsupportedEncodingException
{
115 if (ENCODING_HASH
.containsKey(enc
.toUpperCase())) {
116 return ENCODING_HASH
.get(enc
.toUpperCase());
118 throw new UnsupportedEncodingException(enc
);
123 public int hashCode() {
124 return getEncodingString().hashCode();
127 public boolean equals(Object o
) {
128 return (o
instanceof CharacterEncoding
)
129 && getEncodingString().equals(((CharacterEncoding
) o
).getEncodingString());
134 for (i
= 0; i
< ASCII_ALIASES
.length
; i
++) {
135 ENCODING_HASH
.put(ASCII_ALIASES
[i
], ASCII
);
137 for (i
= 0; i
< ISO_8859_1_ALIASES
.length
; i
++) {
138 ENCODING_HASH
.put(ISO_8859_1_ALIASES
[i
], ISO_8859_1
);
140 for (i
= 0; i
< UTF_8_ALIASES
.length
; i
++) {
141 ENCODING_HASH
.put(UTF_8_ALIASES
[i
], UTF_8
);
143 for (i
= 0; i
< SHIFT_JIS_ALIASES
.length
; i
++) {
144 ENCODING_HASH
.put(SHIFT_JIS_ALIASES
[i
], SHIFT_JIS
);
146 for (i
= 0; i
< EUC_JP_ALIASES
.length
; i
++) {
147 ENCODING_HASH
.put(EUC_JP_ALIASES
[i
], EUC_JP
);
149 for (i
= 0; i
< EUC_KR_ALIASES
.length
; i
++) {
150 ENCODING_HASH
.put(EUC_KR_ALIASES
[i
], EUC_KR
);