Source code: joelib/data/JOETypeTable.java
1 ///////////////////////////////////////////////////////////////////////////////
2 // Filename: $RCSfile: JOETypeTable.java,v $
3 // Purpose: Type table.
4 // Language: Java
5 // Compiler: JDK 1.4
6 // Authors: Joerg K. Wegner
7 // Version: $Revision: 1.15 $
8 // $Date: 2003/08/22 15:56:16 $
9 // $Author: wegner $
10 // Original Author: ???, OpenEye Scientific Software
11 // Original Version: babel 2.0a1
12 //
13 // Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 ///////////////////////////////////////////////////////////////////////////////
24 package joelib.data;
25
26 import joelib.util.JHM;
27
28 import wsi.ra.tool.PropertyHolder;
29
30 /*==========================================================================*
31 * IMPORTS
32 *========================================================================== */
33 import java.util.Properties;
34 import java.util.Vector;
35
36 import org.apache.log4j.Category;
37
38
39 /*==========================================================================*
40 * CLASS DECLARATION
41 *========================================================================== */
42
43 /**
44 * Type table to map internal atom types to atom types of other file formats.
45 * The definition file can be defined in the
46 * <tt>joelib.data.JOETypeTable.resourceFile</tt> property in the {@link wsi.ra.tool.PropertyHolder}.
47 * The {@link wsi.ra.tool.ResourceLoader} loads the <tt>joelib.properties</tt> file for default.
48 *
49 * <p>
50 * Default:<br>
51 * joelib.data.JOETypeTable.resourceFile=<a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/joelib/joelib/src/joelib/data/plain/types.txt?rev=HEAD&content-type=text/vnd.viewcvs-markup">joelib/data/plain/types.txt</a>
52 *
53 * @author wegnerj
54 * @license GPL
55 * @cvsversion $Revision: 1.15 $, $Date: 2003/08/22 15:56:16 $
56 */
57 public class JOETypeTable extends JOEGlobalDataBase
58 {
59 //~ Static fields/initializers /////////////////////////////////////////////
60
61 /*-------------------------------------------------------------------------*
62 * public static member variables
63 *------------------------------------------------------------------------- */
64
65 // Obtain a suitable logger.
66 private static Category logger = Category.getInstance(
67 "joelib.data.JOETypeTable");
68 private static JOETypeTable typeTable;
69 protected static final String DEFAULT_RESOURCE = "joelib/data/plain/types.txt";
70
71 //~ Instance fields ////////////////////////////////////////////////////////
72
73 /**
74 * of type <tt>String</tt>
75 */
76 private Vector _colnames;
77
78 /**
79 * of type <tt>String</tt>-{@link java.util.Vector}
80 */
81 private Vector _table;
82
83 /*-------------------------------------------------------------------------*
84 * public member variables
85 *------------------------------------------------------------------------- */
86
87 // private int _linecount;
88 private int _from;
89
90 /*-------------------------------------------------------------------------*
91 * public member variables
92 *------------------------------------------------------------------------- */
93
94 // private int _linecount;
95 private int _ncols;
96
97 /*-------------------------------------------------------------------------*
98 * public member variables
99 *------------------------------------------------------------------------- */
100
101 // private int _linecount;
102 private int _nrows;
103
104 /*-------------------------------------------------------------------------*
105 * public member variables
106 *------------------------------------------------------------------------- */
107
108 // private int _linecount;
109 private int _to;
110
111 //~ Constructors ///////////////////////////////////////////////////////////
112
113 /*-------------------------------------------------------------------------*
114 * constructor
115 *------------------------------------------------------------------------- */
116
117 /**
118 * Constructor for the JOETypeTable object
119 */
120 private JOETypeTable()
121 {
122 initialized = false;
123
124 Properties prop = PropertyHolder.instance().getProperties();
125 resourceFile = prop.getProperty(this.getClass().getName() +
126 ".resourceFile", DEFAULT_RESOURCE);
127
128 // _linecount = 0;
129 _from = _to = -1;
130
131 _colnames = new Vector();
132 _table = new Vector();
133 }
134
135 //~ Methods ////////////////////////////////////////////////////////////////
136
137 /**
138 * Sets the fromType attribute of the JOETypeTable object
139 *
140 * @param from The new fromType value
141 * @return Description of the Return Value
142 */
143 public boolean setFromType(String from)
144 {
145 if (!initialized)
146 {
147 init();
148 }
149
150 String tmp = from;
151
152 for (int i = 0; i < _colnames.size(); i++)
153 {
154 if (tmp.equals((String) _colnames.get(i)))
155 {
156 _from = i;
157
158 return true;
159 }
160 }
161
162 //throw new Exception("Requested type column not found");
163 logger.error("Requested type column not found");
164
165 return false;
166 }
167
168 /**
169 * Sets the toType attribute of the JOETypeTable object
170 *
171 * @param to The new toType value
172 * @return Description of the Return Value
173 */
174 public boolean setToType(String to)
175 {
176 if (!initialized)
177 {
178 init();
179 }
180
181 String tmp = to;
182
183 for (int i = 0; i < _colnames.size(); i++)
184 {
185 if (tmp.equals((String) _colnames.get(i)))
186 {
187 _to = i;
188
189 return true;
190 }
191 }
192
193 //throw new Exception("Requested type column not found");
194 logger.error("Requested type column not found");
195
196 return false;
197 }
198
199 /**
200 * Description of the Method
201 */
202 public void finalize()
203 {
204 }
205
206 /*-------------------------------------------------------------------------*
207 * public methods
208 *------------------------------------------------------------------------- */
209
210 /**
211 * Description of the Method
212 *
213 * @return Description of the Return Value
214 */
215 public static synchronized JOETypeTable instance()
216 {
217 if (typeTable == null)
218 {
219 typeTable = new JOETypeTable();
220 }
221
222 return typeTable;
223 }
224
225 /**
226 * Description of the Method
227 *
228 * @param buffer Description of the Parameter
229 */
230 public void parseLine(String buffer)
231 {
232 //if (_linecount == 0)
233 // System.out.println("line "+getLineCounter()+": "+buffer);
234 if (getLineCounter() == 1)
235 {
236 Vector vs = new Vector();
237
238 // of type String
239 JHM.instance().tokenize(vs, buffer);
240
241 _ncols = Integer.parseInt((String) vs.get(0));
242 _nrows = Integer.parseInt((String) vs.get(1));
243 }
244 else if (getLineCounter() == 2)
245 {
246 JHM.instance().tokenize(_colnames, buffer);
247 }
248 else
249 {
250 // skip empty lines
251 if (buffer.trim().equals(""))
252 {
253 return;
254 }
255
256 // store all types
257 Vector vc = new Vector();
258 JHM.instance().tokenize(vc, buffer);
259
260 // System.out.println("vc.size "+vc.size() +"_nrows"+_nrows);
261 if (vc.size() == _nrows)
262 {
263 _table.add(vc);
264 }
265 else
266 {
267 logger.error("Wrong number of rows " + vc.size() + " (" +
268 _nrows + " expected) in " + resourceFile + " in line " +
269 getLineCounter() + ":\n" + buffer);
270 }
271 }
272
273 // _linecount++;
274 }
275
276 /**
277 * Description of the Method
278 *
279 * @param from Description of the Parameter
280 * @return Description of the Return Value
281 */
282 public String translate(char[] from)
283 {
284 if (!initialized)
285 {
286 init();
287 }
288
289 String to = translate(new String(from));
290
291 return to;
292 }
293
294 /**
295 * Description of the Method
296 *
297 * @param from Description of the Parameter
298 * @return Description of the Return Value
299 */
300 public String translate(String from)
301 {
302 // System.out.println("from:"+from);
303 if (!initialized)
304 {
305 init();
306 }
307
308 if ((from == null) || from.equals(""))
309 {
310 logger.error("Empty atom type can not be translated !");
311
312 return null;
313 }
314
315 // String to=null;
316 for (int i = 0; i < _table.size(); i++)
317 {
318 Vector v = (Vector) _table.get(i);
319
320 // System.out.println("check: "+((String) v.get(_from)));
321 if ((v.size() > _from) && ((String) v.get(_from)).equals(from))
322 {
323 return (String) v.get(_to);
324 }
325 }
326
327 logger.error("Atom type (" + from + ") can not be found! Check '" +
328 PropertyHolder.instance().getProperties().getProperty(JOETypeTable.class.getName() +
329 ".resourceFile", DEFAULT_RESOURCE) + "' and '" +
330 PropertyHolder.instance().getProperties().getProperty(JOEAtomTyper.class.getName() +
331 ".resourceFile", JOEAtomTyper.DEFAULT_RESOURCE) +
332 "' definition files.");
333
334 return null;
335 }
336 }
337 ///////////////////////////////////////////////////////////////////////////////
338 // END OF FILE.
339 ///////////////////////////////////////////////////////////////////////////////