annotate src/org/tn5250j/encoding/BuiltInCodePageFactory.java @ 14:51f34b9de232 tn5250

adding tn5250 files
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 19:00:59 -0700
parents cbdff98c45ea
children 5949eb469a79
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
1 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
2 * $Id$
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
3 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
4 * Title: tn5250J
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
5 * Copyright: Copyright (c) 2001,2012
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
6 * Company:
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
7 * @author: master_jaf
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
8 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
9 * Description:
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
10 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
11 * This program is free software; you can redistribute it and/or modify
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
12 * it under the terms of the GNU General Public License as published by
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
13 * the Free Software Foundation; either version 2, or (at your option)
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
14 * any later version.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
15 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
16 * This program is distributed in the hope that it will be useful,
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
19 * GNU General Public License for more details.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
20 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
21 * You should have received a copy of the GNU General Public License
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
22 * along with this software; see the file COPYING. If not, write to
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
23 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
24 * Boston, MA 02111-1307 USA
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
25 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
26 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
27 package org.tn5250j.encoding;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
28
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
29 import java.lang.reflect.Constructor;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
30 import java.util.ArrayList;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
31 import java.util.HashSet;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
32 import java.util.List;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
33
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
34 import org.tn5250j.encoding.builtin.CCSID1025;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
35 import org.tn5250j.encoding.builtin.CCSID1026;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
36 import org.tn5250j.encoding.builtin.CCSID1112;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
37 import org.tn5250j.encoding.builtin.CCSID1140;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
38 import org.tn5250j.encoding.builtin.CCSID1141;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
39 import org.tn5250j.encoding.builtin.CCSID1147;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
40 import org.tn5250j.encoding.builtin.CCSID1148;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
41 import org.tn5250j.encoding.builtin.CCSID273;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
42 import org.tn5250j.encoding.builtin.CCSID277;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
43 import org.tn5250j.encoding.builtin.CCSID278;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
44 import org.tn5250j.encoding.builtin.CCSID280;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
45 import org.tn5250j.encoding.builtin.CCSID284;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
46 import org.tn5250j.encoding.builtin.CCSID285;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
47 import org.tn5250j.encoding.builtin.CCSID297;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
48 import org.tn5250j.encoding.builtin.CCSID37;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
49 import org.tn5250j.encoding.builtin.CCSID424;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
50 import org.tn5250j.encoding.builtin.CCSID500;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
51 import org.tn5250j.encoding.builtin.CCSID870;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
52 import org.tn5250j.encoding.builtin.CCSID871;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
53 import org.tn5250j.encoding.builtin.CCSID875;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
54 import org.tn5250j.encoding.builtin.ICodepageConverter;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
55 import org.tn5250j.tools.logging.TN5250jLogFactory;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
56 import org.tn5250j.tools.logging.TN5250jLogger;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
57
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
58 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
59 * Methods for built-in code page support.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
60 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
61 /* package */ class BuiltInCodePageFactory {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
62
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
63 private static BuiltInCodePageFactory singleton;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
64
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
65 private final List<Class<?>> clazzes = new ArrayList<Class<?>>();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
66 private final TN5250jLogger log = TN5250jLogFactory.getLogger(this.getClass());
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
67
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
68 private BuiltInCodePageFactory() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
69 register();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
70 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
71
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
72 public static synchronized final BuiltInCodePageFactory getInstance() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
73 if (singleton == null) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
74 singleton = new BuiltInCodePageFactory();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
75 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
76 return singleton;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
77 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
78
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
79 private void register() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
80 clazzes.add(CCSID37.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
81 clazzes.add(CCSID273.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
82 clazzes.add(CCSID277.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
83 clazzes.add(CCSID278.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
84 clazzes.add(CCSID280.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
85 clazzes.add(CCSID284.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
86 clazzes.add(CCSID285.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
87 clazzes.add(CCSID297.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
88 clazzes.add(CCSID424.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
89 clazzes.add(CCSID500.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
90 clazzes.add(CCSID870.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
91 clazzes.add(CCSID871.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
92 clazzes.add(CCSID875.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
93 clazzes.add(CCSID1025.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
94 clazzes.add(CCSID1026.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
95 clazzes.add(CCSID1112.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
96 clazzes.add(CCSID1140.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
97 clazzes.add(CCSID1141.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
98 clazzes.add(CCSID1147.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
99 clazzes.add(CCSID1148.class);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
100 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
101
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
102 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
103 * @return unsorted list of available code pages
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
104 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
105 public String[] getAvailableCodePages() {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
106 HashSet<String> cpset = new HashSet<String>();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
107 for (Class<?> clazz : clazzes) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
108 final ICodepageConverter converter = getConverterFromClassName(clazz);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
109 if (converter != null) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
110 cpset.add(converter.getName());
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
111 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
112 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
113 return cpset.toArray(new String[cpset.size()]);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
114 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
115
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
116 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
117 * @param encoding
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
118 * @return an {@link ICodePage} object OR null, of not found
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
119 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
120 public ICodePage getCodePage(String encoding) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
121 for (Class<?> clazz : clazzes) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
122 final ICodepageConverter converter = getConverterFromClassName(clazz);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
123 if (converter != null && converter.getName().equals(encoding)) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
124 return converter;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
125 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
126 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
127 return null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
128 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
129
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
130 /**
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
131 * Lazy loading converters takes time,
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
132 * but doesn't happen so often and saves memory.
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
133 *
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
134 * @param clazz {@link ICodepageConverter}
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
135 * @return
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
136 */
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
137 private ICodepageConverter getConverterFromClassName(Class<?> clazz) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
138 try {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
139 final Constructor<?> constructor = clazz.getConstructor(new Class[0]);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
140 final ICodepageConverter converter = (ICodepageConverter) constructor.newInstance();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
141 converter.init();
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
142 return converter;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
143 } catch (Exception e) {
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
144 log.error("Couldn't load code page converter class:" + clazz.getCanonicalName(), e);
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
145 return null;
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
146 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
147 }
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
148
cbdff98c45ea adding tn5250 files
Carl Byington <carl@five-ten-sg.com>
parents:
diff changeset
149 }