comparison src/org/tn5250j/encoding/BuiltInCodePageFactory.java @ 5:cbdff98c45ea tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 12:38:09 -0700
parents
children 5949eb469a79
comparison
equal deleted inserted replaced
4:1f5d9b76a183 5:cbdff98c45ea
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 org.tn5250j.tools.logging.TN5250jLogFactory;
56 import org.tn5250j.tools.logging.TN5250jLogger;
57
58 /**
59 * Methods for built-in code page support.
60 */
61 /* package */ class BuiltInCodePageFactory {
62
63 private static BuiltInCodePageFactory singleton;
64
65 private final List<Class<?>> clazzes = new ArrayList<Class<?>>();
66 private final TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
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 return singleton;
77 }
78
79 private void register() {
80 clazzes.add(CCSID37.class);
81 clazzes.add(CCSID273.class);
82 clazzes.add(CCSID277.class);
83 clazzes.add(CCSID278.class);
84 clazzes.add(CCSID280.class);
85 clazzes.add(CCSID284.class);
86 clazzes.add(CCSID285.class);
87 clazzes.add(CCSID297.class);
88 clazzes.add(CCSID424.class);
89 clazzes.add(CCSID500.class);
90 clazzes.add(CCSID870.class);
91 clazzes.add(CCSID871.class);
92 clazzes.add(CCSID875.class);
93 clazzes.add(CCSID1025.class);
94 clazzes.add(CCSID1026.class);
95 clazzes.add(CCSID1112.class);
96 clazzes.add(CCSID1140.class);
97 clazzes.add(CCSID1141.class);
98 clazzes.add(CCSID1147.class);
99 clazzes.add(CCSID1148.class);
100 }
101
102 /**
103 * @return unsorted list of available code pages
104 */
105 public String[] getAvailableCodePages() {
106 HashSet<String> cpset = new HashSet<String>();
107 for (Class<?> clazz : clazzes) {
108 final ICodepageConverter converter = getConverterFromClassName(clazz);
109 if (converter != null) {
110 cpset.add(converter.getName());
111 }
112 }
113 return cpset.toArray(new String[cpset.size()]);
114 }
115
116 /**
117 * @param encoding
118 * @return an {@link ICodePage} object OR null, of not found
119 */
120 public ICodePage getCodePage(String encoding) {
121 for (Class<?> clazz : clazzes) {
122 final ICodepageConverter converter = getConverterFromClassName(clazz);
123 if (converter != null && converter.getName().equals(encoding)) {
124 return converter;
125 }
126 }
127 return null;
128 }
129
130 /**
131 * Lazy loading converters takes time,
132 * but doesn't happen so often and saves memory.
133 *
134 * @param clazz {@link ICodepageConverter}
135 * @return
136 */
137 private ICodepageConverter getConverterFromClassName(Class<?> clazz) {
138 try {
139 final Constructor<?> constructor = clazz.getConstructor(new Class[0]);
140 final ICodepageConverter converter = (ICodepageConverter) constructor.newInstance();
141 converter.init();
142 return converter;
143 } catch (Exception e) {
144 log.error("Couldn't load code page converter class:" + clazz.getCanonicalName(), e);
145 return null;
146 }
147 }
148
149 }