comparison app/src/main/java/org/tn5250j/encoding/BuiltInCodePageFactory.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/org/tn5250j/encoding/BuiltInCodePageFactory.java@77ac18bc1b2f
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /**
2 * $Id$
3 *
4 * Title: tn5250J
5 * Copyright: Copyright (c) 2001,2012
6 * Company:
7 * @author: master_jaf
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.util.ArrayList;
31 import java.util.HashSet;
32 import java.util.List;
33
34 import org.tn5250j.encoding.builtin.CCSID1025;
35 import org.tn5250j.encoding.builtin.CCSID1026;
36 import org.tn5250j.encoding.builtin.CCSID1112;
37 import org.tn5250j.encoding.builtin.CCSID1140;
38 import org.tn5250j.encoding.builtin.CCSID1141;
39 import org.tn5250j.encoding.builtin.CCSID1147;
40 import org.tn5250j.encoding.builtin.CCSID1148;
41 import org.tn5250j.encoding.builtin.CCSID273;
42 import org.tn5250j.encoding.builtin.CCSID277;
43 import org.tn5250j.encoding.builtin.CCSID278;
44 import org.tn5250j.encoding.builtin.CCSID280;
45 import org.tn5250j.encoding.builtin.CCSID284;
46 import org.tn5250j.encoding.builtin.CCSID285;
47 import org.tn5250j.encoding.builtin.CCSID297;
48 import org.tn5250j.encoding.builtin.CCSID37;
49 import org.tn5250j.encoding.builtin.CCSID424;
50 import org.tn5250j.encoding.builtin.CCSID500;
51 import org.tn5250j.encoding.builtin.CCSID870;
52 import org.tn5250j.encoding.builtin.CCSID871;
53 import org.tn5250j.encoding.builtin.CCSID875;
54 import org.tn5250j.encoding.builtin.ICodepageConverter;
55 import android.util.Log;
56
57
58 /**
59 * Methods for built-in code page support.
60 */
61 class BuiltInCodePageFactory {
62 private static final String TAG = "BuiltInCodePageFactory";
63 private static BuiltInCodePageFactory singleton;
64
65 private final List<Class<?>> clazzes = new ArrayList<Class<?>>();
66
67
68 private BuiltInCodePageFactory() {
69 register();
70 }
71
72 public static synchronized final BuiltInCodePageFactory getInstance() {
73 if (singleton == null) {
74 singleton = new BuiltInCodePageFactory();
75 }
76
77 return singleton;
78 }
79
80 private void register() {
81 clazzes.add(CCSID37.class);
82 clazzes.add(CCSID273.class);
83 clazzes.add(CCSID277.class);
84 clazzes.add(CCSID278.class);
85 clazzes.add(CCSID280.class);
86 clazzes.add(CCSID284.class);
87 clazzes.add(CCSID285.class);
88 clazzes.add(CCSID297.class);
89 clazzes.add(CCSID424.class);
90 clazzes.add(CCSID500.class);
91 clazzes.add(CCSID870.class);
92 clazzes.add(CCSID871.class);
93 clazzes.add(CCSID875.class);
94 clazzes.add(CCSID1025.class);
95 clazzes.add(CCSID1026.class);
96 clazzes.add(CCSID1112.class);
97 clazzes.add(CCSID1140.class);
98 clazzes.add(CCSID1141.class);
99 clazzes.add(CCSID1147.class);
100 clazzes.add(CCSID1148.class);
101 }
102
103 /**
104 * @return unsorted list of available code pages
105 */
106 public String[] getAvailableCodePages() {
107 HashSet<String> cpset = new HashSet<String>();
108
109 for (Class<?> clazz : clazzes) {
110 final ICodepageConverter converter = getConverterFromClassName(clazz);
111
112 if (converter != null) {
113 cpset.add(converter.getName());
114 }
115 }
116
117 return cpset.toArray(new String[cpset.size()]);
118 }
119
120 /**
121 * @param encoding
122 * @return an {@link ICodePage} object OR null, of not found
123 */
124 public ICodePage getCodePage(String encoding) {
125 for (Class<?> clazz : clazzes) {
126 final ICodepageConverter converter = getConverterFromClassName(clazz);
127
128 if (converter != null && converter.getName().equals(encoding)) {
129 return converter;
130 }
131 }
132
133 return null;
134 }
135
136 /**
137 * Lazy loading converters takes time,
138 * but doesn't happen so often and saves memory.
139 *
140 * @param clazz {@link ICodepageConverter}
141 * @return
142 */
143 private ICodepageConverter getConverterFromClassName(Class<?> clazz) {
144 try {
145 final Constructor<?> constructor = clazz.getConstructor(new Class[0]);
146 final ICodepageConverter converter = (ICodepageConverter) constructor.newInstance();
147 converter.init();
148 return converter;
149 }
150 catch (Exception e) {
151 Log.e(TAG, "Couldn't load code page converter class:" + clazz.getCanonicalName(), e);
152 return null;
153 }
154 }
155
156 }