1 /*
2  * Copyright 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.google.zxing.common;
18
19 import com.google.zxing.FormatException;
20
21 import java.nio.charset.Charset;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 /**
27  * Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1
28  * of ISO 18004.
29  *
30  * @author Sean Owen
31  */

32 public enum CharacterSetECI {
33
34   // Enum name is a Java encoding valid for java.lang and java.io
35   Cp437(new int[]{0,2}),
36   ISO8859_1(new int[]{1,3}, "ISO-8859-1"),
37   ISO8859_2(4, "ISO-8859-2"),
38   ISO8859_3(5, "ISO-8859-3"),
39   ISO8859_4(6, "ISO-8859-4"),
40   ISO8859_5(7, "ISO-8859-5"),
41   // ISO8859_6(8, "ISO-8859-6"),
42   ISO8859_7(9, "ISO-8859-7"),
43   // ISO8859_8(10, "ISO-8859-8"),
44   ISO8859_9(11, "ISO-8859-9"),
45   // ISO8859_10(12, "ISO-8859-10"),
46   // ISO8859_11(13, "ISO-8859-11"),
47   ISO8859_13(15, "ISO-8859-13"),
48   // ISO8859_14(16, "ISO-8859-14"),
49   ISO8859_15(17, "ISO-8859-15"),
50   ISO8859_16(18, "ISO-8859-16"),
51   SJIS(20, "Shift_JIS"),
52   Cp1250(21, "windows-1250"),
53   Cp1251(22, "windows-1251"),
54   Cp1252(23, "windows-1252"),
55   Cp1256(24, "windows-1256"),
56   UnicodeBigUnmarked(25, "UTF-16BE""UnicodeBig"),
57   UTF8(26, "UTF-8"),
58   ASCII(new int[] {27, 170}, "US-ASCII"),
59   Big5(28),
60   GB18030(29, "GB2312""EUC_CN""GBK"),
61   EUC_KR(30, "EUC-KR");
62
63   private static final Map<Integer,CharacterSetECI> VALUE_TO_ECI = new HashMap<>();
64   private static final Map<String,CharacterSetECI> NAME_TO_ECI = new HashMap<>();
65   static {
66     for (CharacterSetECI eci : values()) {
67       for (int value : eci.values) {
68         VALUE_TO_ECI.put(value, eci);
69       }
70       NAME_TO_ECI.put(eci.name(), eci);
71       for (String name : eci.otherEncodingNames) {
72         NAME_TO_ECI.put(name, eci);
73       }
74     }
75   }
76
77   private final int[] values;
78   private final String[] otherEncodingNames;
79
80   CharacterSetECI(int value) {
81     this(new int[] {value});
82   }
83
84   CharacterSetECI(int value, String... otherEncodingNames) {
85     this.values = new int[] {value};
86     this.otherEncodingNames = otherEncodingNames;
87   }
88
89   CharacterSetECI(int[] values, String... otherEncodingNames) {
90     this.values = values;
91     this.otherEncodingNames = otherEncodingNames;
92   }
93
94   public int getValue() {
95     return values[0];
96   }
97
98   public Charset getCharset() {
99     return Charset.forName(name());
100   }
101
102   /**
103    * @param charset Java character set object
104    * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
105    *   but unsupported
106    */

107   public static CharacterSetECI getCharacterSetECI(Charset charset) {
108     return NAME_TO_ECI.get(charset.name());
109   }
110
111   /**
112    * @param value character set ECI value
113    * @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but
114    *   unsupported
115    * @throws FormatException if ECI value is invalid
116    */

117   public static CharacterSetECI getCharacterSetECIByValue(int value) throws FormatException {
118     if (value < 0 || value >= 900) {
119       throw FormatException.getFormatInstance();
120     }
121     return VALUE_TO_ECI.get(value);
122   }
123
124   /**
125    * @param name character set ECI encoding name
126    * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
127    *   but unsupported
128    */

129   public static CharacterSetECI getCharacterSetECIByName(String name) {
130     return NAME_TO_ECI.get(name);
131   }
132
133 }
134