Issue 521, avoid an NPE
[zxing.git] / csharp / common / CharacterSetECI.cs
1 /*\r
2 * Copyright 2008 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 using System;\r
17 namespace com.google.zxing.common\r
18 {\r
19         \r
20         /// <summary> Encapsulates a Character Set ECI, according to "Extended Channel Interpretations" 5.3.1.1\r
21         /// of ISO 18004.\r
22         /// \r
23         /// </summary>\r
24         /// <author>  Sean Owen\r
25         /// </author>\r
26         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
27         /// </author>\r
28         public sealed class CharacterSetECI:ECI\r
29         {\r
30                 public System.String EncodingName\r
31                 {\r
32                         get\r
33                         {\r
34                                 return encodingName;\r
35                         }\r
36                         \r
37                 }\r
38                 \r
39                 private static System.Collections.Hashtable VALUE_TO_ECI;\r
40                 private static System.Collections.Hashtable NAME_TO_ECI;\r
41                 \r
42                 private static void  initialize()\r
43                 {\r
44                         VALUE_TO_ECI = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable(29));\r
45                         NAME_TO_ECI = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable(29));\r
46                         // TODO figure out if these values are even right!\r
47                         addCharacterSet(0, "Cp437");\r
48                         addCharacterSet(1, new System.String[]{"ISO8859_1", "ISO-8859-1"});\r
49                         addCharacterSet(2, "Cp437");\r
50                         addCharacterSet(3, new System.String[]{"ISO8859_1", "ISO-8859-1"});\r
51                         addCharacterSet(4, "ISO8859_2");\r
52                         addCharacterSet(5, "ISO8859_3");\r
53                         addCharacterSet(6, "ISO8859_4");\r
54                         addCharacterSet(7, "ISO8859_5");\r
55                         addCharacterSet(8, "ISO8859_6");\r
56                         addCharacterSet(9, "ISO8859_7");\r
57                         addCharacterSet(10, "ISO8859_8");\r
58                         addCharacterSet(11, "ISO8859_9");\r
59                         addCharacterSet(12, "ISO8859_10");\r
60                         addCharacterSet(13, "ISO8859_11");\r
61                         addCharacterSet(15, "ISO8859_13");\r
62                         addCharacterSet(16, "ISO8859_14");\r
63                         addCharacterSet(17, "ISO8859_15");\r
64                         addCharacterSet(18, "ISO8859_16");\r
65                         addCharacterSet(20, new System.String[]{"SJIS", "Shift_JIS"});\r
66                 }\r
67                 \r
68                 //UPGRADE_NOTE: Final was removed from the declaration of 'encodingName '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
69                 private System.String encodingName;\r
70                 \r
71                 private CharacterSetECI(int value_Renamed, System.String encodingName):base(value_Renamed)\r
72                 {\r
73                         this.encodingName = encodingName;\r
74                 }\r
75                 \r
76                 private static void  addCharacterSet(int value_Renamed, System.String encodingName)\r
77                 {\r
78                         CharacterSetECI eci = new CharacterSetECI(value_Renamed, encodingName);\r
79                         VALUE_TO_ECI[(System.Int32) value_Renamed] = eci; // can't use valueOf\r
80                         NAME_TO_ECI[encodingName] = eci;\r
81                 }\r
82                 \r
83                 private static void  addCharacterSet(int value_Renamed, System.String[] encodingNames)\r
84                 {\r
85                         CharacterSetECI eci = new CharacterSetECI(value_Renamed, encodingNames[0]);\r
86                         VALUE_TO_ECI[(System.Int32) value_Renamed] = eci; // can't use valueOf\r
87                         for (int i = 0; i < encodingNames.Length; i++)\r
88                         {\r
89                                 NAME_TO_ECI[encodingNames[i]] = eci;\r
90                         }\r
91                 }\r
92                 \r
93                 /// <param name="value">character set ECI value\r
94                 /// </param>\r
95                 /// <returns> {@link CharacterSetECI} representing ECI of given value, or null if it is legal but\r
96                 /// unsupported\r
97                 /// </returns>\r
98                 /// <throws>  IllegalArgumentException if ECI value is invalid </throws>\r
99                 public static CharacterSetECI getCharacterSetECIByValue(int value_Renamed)\r
100                 {\r
101                         if (VALUE_TO_ECI == null)\r
102                         {\r
103                                 initialize();\r
104                         }\r
105                         if (value_Renamed < 0 || value_Renamed >= 900)\r
106                         {\r
107                                 throw new System.ArgumentException("Bad ECI value: " + value_Renamed);\r
108                         }\r
109                         return (CharacterSetECI) VALUE_TO_ECI[(System.Int32) value_Renamed];\r
110                 }\r
111                 \r
112                 /// <param name="name">character set ECI encoding name\r
113                 /// </param>\r
114                 /// <returns> {@link CharacterSetECI} representing ECI for character encoding, or null if it is legal\r
115                 /// but unsupported\r
116                 /// </returns>\r
117                 public static CharacterSetECI getCharacterSetECIByName(System.String name)\r
118                 {\r
119                         if (NAME_TO_ECI == null)\r
120                         {\r
121                                 initialize();\r
122                         }\r
123                         return (CharacterSetECI) NAME_TO_ECI[name];\r
124                 }\r
125         }\r
126 }