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