• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // only character sets supported by the current JVM are registered here
64   private static final Map<Integer,CharacterSetECI> VALUE_TO_ECI = new HashMap<>();
65   private static final Map<String,CharacterSetECI> NAME_TO_ECI = new HashMap<>();
66   static {
67     for (CharacterSetECI eci : values()) {
68       if (Charset.isSupported(eci.name())) {
69         for (int value : eci.values) {
VALUE_TO_ECI.put(value, eci)70           VALUE_TO_ECI.put(value, eci);
71         }
eci.name()72         NAME_TO_ECI.put(eci.name(), eci);
73         for (String name : eci.otherEncodingNames) {
NAME_TO_ECI.put(name, eci)74           NAME_TO_ECI.put(name, eci);
75         }
76       }
77     }
78   }
79 
80   private final int[] values;
81   private final String[] otherEncodingNames;
82 
CharacterSetECI(int value)83   CharacterSetECI(int value) {
84     this(new int[] {value});
85   }
86 
CharacterSetECI(int value, String... otherEncodingNames)87   CharacterSetECI(int value, String... otherEncodingNames) {
88     this.values = new int[] {value};
89     this.otherEncodingNames = otherEncodingNames;
90   }
91 
CharacterSetECI(int[] values, String... otherEncodingNames)92   CharacterSetECI(int[] values, String... otherEncodingNames) {
93     this.values = values;
94     this.otherEncodingNames = otherEncodingNames;
95   }
96 
getValue()97   public int getValue() {
98     return values[0];
99   }
100 
getCharset()101   public Charset getCharset() {
102     return Charset.forName(name());
103   }
104 
105   /**
106    * @param charset Java character set object
107    * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
108    *   but unsupported
109    */
getCharacterSetECI(Charset charset)110   public static CharacterSetECI getCharacterSetECI(Charset charset) {
111     return NAME_TO_ECI.get(charset.name());
112   }
113 
114   /**
115    * @param value character set ECI value
116    * @return {@code CharacterSetECI} representing ECI of given value, or null if it is legal but
117    *   unsupported
118    * @throws FormatException if ECI value is invalid
119    */
getCharacterSetECIByValue(int value)120   public static CharacterSetECI getCharacterSetECIByValue(int value) throws FormatException {
121     if (value < 0 || value >= 900) {
122       throw FormatException.getFormatInstance();
123     }
124     return VALUE_TO_ECI.get(value);
125   }
126 
127   /**
128    * @param name character set ECI encoding name
129    * @return CharacterSetECI representing ECI for character encoding, or null if it is legal
130    *   but unsupported
131    */
getCharacterSetECIByName(String name)132   public static CharacterSetECI getCharacterSetECIByName(String name) {
133     return NAME_TO_ECI.get(name);
134   }
135 
136 }
137