Mercurial > 510Connectbot
annotate src/org/tn5250j/encoding/ToolboxCodePageFactory.java @ 54:a7e660661e08 tn5250
start tn5250 integration
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Wed, 11 Jun 2014 14:01:43 -0700 |
parents | b29b39f386a4 |
children | ab8d2f6c5847 |
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"; |
5 | 37 private final static String[] CODEPAGES = { "Big5", "Cp037", "Cp273", "Cp277", "Cp278", |
38 "Cp280", "Cp284", "Cp285", "Cp297", "Cp420", "Cp424", "Cp437", | |
39 "Cp500", "Cp737", "Cp775", "Cp838", "Cp850", "Cp852", "Cp855", | |
40 "Cp856", "Cp857", "Cp858", "Cp860", "Cp861", "Cp862", "Cp863", | |
41 "Cp864", "Cp865", "Cp866", "Cp868", "Cp869", "Cp870", | |
42 "Cp871", "Cp874", "Cp875", "Cp918", "Cp921", "Cp922", | |
43 "Cp923", // IBM Latin-9. | |
44 "Cp930", "Cp933", "Cp935", "Cp937", "Cp939", "Cp942", "Cp943", | |
45 "Cp948", "Cp949", "Cp950", "Cp964", "Cp970", "Cp1006", "Cp1025", | |
46 "Cp1026", "Cp1046", "Cp1097", "Cp1098", "Cp1112", "Cp1122", | |
47 "Cp1123", "Cp1124", "Cp1140", "Cp1141", "Cp1142", "Cp1143", | |
48 "Cp1144", "Cp1145", "Cp1146", "Cp1147", "Cp1148", "Cp1149", | |
49 "Cp1252", "Cp1250", "Cp1251", "Cp1253", "Cp1254", "Cp1255", | |
50 "Cp1256", "Cp1257", "Cp1258", "Cp1381", "Cp1383", "Cp33722" }; | |
51 | |
52 private static final String CONVERTER_NAME = "com.ibm.as400.access.CharConverter"; | |
53 private static final String TOBYTES_NAME = "stringToByteArray"; | |
54 private static final String TOSTRING_NAME = "byteArrayToString"; | |
55 | |
56 private static ToolboxCodePageFactory singleton; | |
57 | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
58 |
5 | 59 |
60 private ToolboxCodePageFactory() { | |
61 /* private for singleton */ | |
62 } | |
63 | |
64 public static synchronized ToolboxCodePageFactory getInstance() { | |
65 if (singleton == null) { | |
66 singleton = new ToolboxCodePageFactory(); | |
67 } | |
68 return singleton; | |
69 } | |
70 | |
71 /** | |
72 * @return | |
73 */ | |
74 public String[] getAvailableCodePages() { | |
75 try { | |
76 final ClassLoader loader = getClassLoader(); | |
77 Class.forName(CONVERTER_NAME, false, loader); | |
78 return CODEPAGES; | |
79 } catch (Exception e) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
80 Log.i(TAG,"Couldn't locate JT400 Toolbox in classpath. Charset converters can't be used."); |
5 | 81 return new String[0]; |
82 } | |
83 } | |
84 | |
85 /** | |
86 * @param encoding | |
87 * @return | |
88 */ | |
89 public ICodePage getCodePage(String encoding) { | |
90 try { | |
91 ClassLoader loader = getClassLoader(); | |
92 Class<?> conv_class = Class.forName(CONVERTER_NAME, true, loader); | |
93 Constructor<?> conv_constructor = conv_class.getConstructor(new Class[] { String.class }); | |
94 Method toBytes_method = conv_class.getMethod(TOBYTES_NAME, new Class[] { String.class }); | |
95 Method toString_method = conv_class.getMethod(TOSTRING_NAME, new Class[] { byte[].class }); | |
96 Object convobj = conv_constructor.newInstance(new Object[] { encoding }); | |
97 return new ToolboxConverterProxy(convobj, toBytes_method, toString_method); | |
98 } catch (Exception e) { | |
25
5949eb469a79
adding tn5250 files, native android logging
Carl Byington <carl@five-ten-sg.com>
parents:
5
diff
changeset
|
99 Log.w(TAG,"Can't load charset converter from JT400 Toolbox for code page " + encoding, e); |
5 | 100 return null; |
101 } | |
102 } | |
103 | |
104 private static final ClassLoader getClassLoader() { | |
105 ClassLoader loader = ToolboxCodePageFactory.class.getClassLoader(); | |
106 if (loader == null) { | |
107 loader = ClassLoader.getSystemClassLoader(); | |
108 } | |
109 return loader; | |
110 } | |
111 | |
112 private static class ToolboxConverterProxy implements ICodePage { | |
113 | |
114 private final Object converter; | |
115 private final Method tobytesMethod; | |
116 private final Method tostringMethod; | |
117 | |
118 private ToolboxConverterProxy(Object converterObject, Method tobytesMethod, Method tostringMethod) { | |
119 super(); | |
120 this.converter = converterObject; | |
121 this.tobytesMethod = tobytesMethod; | |
122 this.tostringMethod = tostringMethod; | |
123 } | |
124 | |
125 @Override | |
126 public char ebcdic2uni(int index) { | |
127 Object result; | |
128 try { | |
129 result = tostringMethod.invoke(converter, new Object[] { new byte[] { (byte) (index & 0xFF) } }); | |
130 } catch (Throwable t) { | |
131 result = null; | |
132 } | |
133 | |
134 if (result == null) | |
135 return 0x00; | |
136 | |
137 return ((String) result).charAt(0); | |
138 } | |
139 | |
140 @Override | |
141 public byte uni2ebcdic(char index) { | |
142 Object result; | |
143 try { | |
144 result = tobytesMethod.invoke(converter, new Object[] { new String(new char[] { index }) }); | |
145 } catch (Throwable t) { | |
146 result = null; | |
147 } | |
148 | |
149 if (result == null) | |
150 return 0x00; | |
151 | |
152 return ((byte[]) result)[0]; | |
153 } | |
154 } | |
155 | |
156 } |