Mercurial > 510Connectbot
annotate app/src/main/java/org/tn5250j/encoding/ToolboxCodePageFactory.java @ 516:3407f4741240
update copyrights
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 31 May 2024 11:17:53 -0600 |
parents | d29cce60f393 |
children |
rev | line source |
---|---|
5 | 1 /** |
2 * Title: ToolboxCodePage | |
3 * Copyright: Copyright (c) 2001, 2002, 2003 | |
4 * Company: | |
5 * @author Kenneth J. Pouncey | |
6 * rewritten by LDC, WVL, Luc | |
7 * @version 0.4 | |
8 * | |
9 * Description: | |
10 * | |
11 * This program is free software; you can redistribute it and/or modify | |
12 * it under the terms of the GNU General Public License as published by | |
13 * the Free Software Foundation; either version 2, or (at your option) | |
14 * any later version. | |
15 * | |
16 * This program is distributed in the hope that it will be useful, | |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 * GNU General Public License for more details. | |
20 * | |
21 * You should have received a copy of the GNU General Public License | |
22 * along with this software; see the file COPYING. If not, write to | |
23 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, | |
24 * Boston, MA 02111-1307 USA | |
25 * | |
26 */ | |
27 package org.tn5250j.encoding; | |
28 | |
29 import java.lang.reflect.Constructor; | |
30 import java.lang.reflect.Method; | |
31 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
32 import android.util.Log; |
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
33 |
5 | 34 |
27
b29b39f386a4
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
35 class ToolboxCodePageFactory { |
b29b39f386a4
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
25
diff
changeset
|
36 private static final String TAG = "ToolboxCodePageFactory"; |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
37 private final static String[] CODEPAGES = { "Big5", "Cp037", "Cp273", "Cp277", "Cp278", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
38 "Cp280", "Cp284", "Cp285", "Cp297", "Cp420", "Cp424", "Cp437", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
39 "Cp500", "Cp737", "Cp775", "Cp838", "Cp850", "Cp852", "Cp855", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
40 "Cp856", "Cp857", "Cp858", "Cp860", "Cp861", "Cp862", "Cp863", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
41 "Cp864", "Cp865", "Cp866", "Cp868", "Cp869", "Cp870", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
42 "Cp871", "Cp874", "Cp875", "Cp918", "Cp921", "Cp922", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
43 "Cp923", // IBM Latin-9. |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
44 "Cp930", "Cp933", "Cp935", "Cp937", "Cp939", "Cp942", "Cp943", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
45 "Cp948", "Cp949", "Cp950", "Cp964", "Cp970", "Cp1006", "Cp1025", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
46 "Cp1026", "Cp1046", "Cp1097", "Cp1098", "Cp1112", "Cp1122", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
47 "Cp1123", "Cp1124", "Cp1140", "Cp1141", "Cp1142", "Cp1143", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
48 "Cp1144", "Cp1145", "Cp1146", "Cp1147", "Cp1148", "Cp1149", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
49 "Cp1252", "Cp1250", "Cp1251", "Cp1253", "Cp1254", "Cp1255", |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
50 "Cp1256", "Cp1257", "Cp1258", "Cp1381", "Cp1383", "Cp33722" |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
51 }; |
5 | 52 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
53 private static final String CONVERTER_NAME = "com.ibm.as400.access.CharConverter"; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
54 private static final String TOBYTES_NAME = "stringToByteArray"; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
55 private static final String TOSTRING_NAME = "byteArrayToString"; |
5 | 56 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
57 private static ToolboxCodePageFactory singleton; |
5 | 58 |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
59 |
5 | 60 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
61 private ToolboxCodePageFactory() { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
62 /* private for singleton */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
63 } |
5 | 64 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
65 public static synchronized ToolboxCodePageFactory getInstance() { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
66 if (singleton == null) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
67 singleton = new ToolboxCodePageFactory(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
68 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
69 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
70 return singleton; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
71 } |
5 | 72 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
73 /** |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
74 * @return |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
75 */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
76 public String[] getAvailableCodePages() { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
77 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
78 final ClassLoader loader = getClassLoader(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
79 Class.forName(CONVERTER_NAME, false, loader); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
80 return CODEPAGES; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
81 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
82 catch (Exception e) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
83 Log.i(TAG, "Couldn't locate JT400 Toolbox in classpath. Charset converters can't be used."); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
84 return new String[0]; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
85 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
86 } |
5 | 87 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
88 /** |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
89 * @param encoding |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
90 * @return |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
91 */ |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
92 public ICodePage getCodePage(String encoding) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
93 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
94 ClassLoader loader = getClassLoader(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
95 Class<?> conv_class = Class.forName(CONVERTER_NAME, true, loader); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
96 Constructor<?> conv_constructor = conv_class.getConstructor(new Class[] { String.class }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
97 Method toBytes_method = conv_class.getMethod(TOBYTES_NAME, new Class[] { String.class }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
98 Method toString_method = conv_class.getMethod(TOSTRING_NAME, new Class[] { byte[].class }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
99 Object convobj = conv_constructor.newInstance(new Object[] { encoding }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
100 return new ToolboxConverterProxy(convobj, toBytes_method, toString_method); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
101 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
102 catch (Exception e) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
103 Log.w(TAG, "Can't load charset converter from JT400 Toolbox for code page " + encoding, e); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
104 return null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
105 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
106 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
107 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
108 private static final ClassLoader getClassLoader() { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
109 ClassLoader loader = ToolboxCodePageFactory.class.getClassLoader(); |
5 | 110 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
111 if (loader == null) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
112 loader = ClassLoader.getSystemClassLoader(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
113 } |
5 | 114 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
115 return loader; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
116 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
117 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
118 private static class ToolboxConverterProxy implements ICodePage { |
5 | 119 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
120 private final Object converter; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
121 private final Method tobytesMethod; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
122 private final Method tostringMethod; |
5 | 123 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
124 private ToolboxConverterProxy(Object converterObject, Method tobytesMethod, Method tostringMethod) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
125 super(); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
126 this.converter = converterObject; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
127 this.tobytesMethod = tobytesMethod; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
128 this.tostringMethod = tostringMethod; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
129 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
130 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
131 public char ebcdic2uni(int index) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
132 Object result; |
5 | 133 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
134 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
135 result = tostringMethod.invoke(converter, new Object[] { new byte[] { (byte)(index & 0xFF) } }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
136 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
137 catch (Throwable t) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
138 result = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
139 } |
5 | 140 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
141 if (result == null) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
142 return 0x00; |
5 | 143 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
144 return ((String) result).charAt(0); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
145 } |
5 | 146 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
147 public byte uni2ebcdic(char index) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
148 Object result; |
5 | 149 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
150 try { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
151 result = tobytesMethod.invoke(converter, new Object[] { new String(new char[] { index }) }); |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
152 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
153 catch (Throwable t) { |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
154 result = null; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
155 } |
5 | 156 |
112
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
157 if (result == null) |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
158 return 0x00; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
159 |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
160 return ((byte[]) result)[0]; |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
161 } |
77ac18bc1b2f
cleanup java formatting
Carl Byington <carl@five-ten-sg.com>
parents:
80
diff
changeset
|
162 } |
5 | 163 |
164 } |