1 |
/* |
2 |
openisis - an open implementation of the CDS/ISIS database |
3 |
Version 0.8.x (patchlevel see file Version) |
4 |
Copyright (C) 2001-2003 by Erik Grziwotz, erik@openisis.org |
5 |
|
6 |
This library is free software; you can redistribute it and/or |
7 |
modify it under the terms of the GNU Lesser General Public |
8 |
License as published by the Free Software Foundation; either |
9 |
version 2.1 of the License, or (at your option) any later version. |
10 |
|
11 |
This library is distributed in the hope that it will be useful, |
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
Lesser General Public License for more details. |
15 |
|
16 |
You should have received a copy of the GNU Lesser General Public |
17 |
License along with this library; if not, write to the Free Software |
18 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
20 |
see README for more information |
21 |
EOH */ |
22 |
|
23 |
package org.openisis; |
24 |
|
25 |
import java.util.*; |
26 |
|
27 |
/** |
28 |
Various utilities. |
29 |
<p> |
30 |
$Id: Utils.java,v 1.2 2003/04/08 00:20:53 kripke Exp $ |
31 |
@version $Revision: 1.2 $ |
32 |
@author $Author: kripke $ |
33 |
*/ |
34 |
|
35 |
public abstract class Utils { |
36 |
|
37 |
private Utils () {} |
38 |
|
39 |
/** Build field array from map. Map keys must be valid field description |
40 |
names (if fdt is given) or objects whose string representation |
41 |
build tag numbers. |
42 |
@param ign if false, method will return null on an occurence of a |
43 |
map entry that cannot be transformed in a field |
44 |
@return field array or null, if ign is false and map contains |
45 |
illegal entries |
46 |
*/ |
47 |
public static Field[] ToFields (Map map, Fdt fdt, boolean ign) { |
48 |
ArrayList lst = new ArrayList (); |
49 |
Iterator it; |
50 |
Fdt.Fd fd; |
51 |
Map.Entry me; |
52 |
String str; |
53 |
Object key, val; |
54 |
int tag; |
55 |
|
56 |
if (null != map) { |
57 |
it = map.entrySet ().iterator (); |
58 |
while (it.hasNext ()) { |
59 |
me = (Map.Entry) it.next (); |
60 |
key = me.getKey (); |
61 |
if (null == key) { |
62 |
if (ign) { |
63 |
continue; |
64 |
} |
65 |
return null; |
66 |
} |
67 |
if (key instanceof Number) { |
68 |
tag = ((Number)key).intValue (); |
69 |
} |
70 |
else { |
71 |
str = key.toString (); |
72 |
try { |
73 |
tag = Integer.parseInt (str); |
74 |
} |
75 |
catch (Exception ex) { |
76 |
fd = null; |
77 |
if (null != fdt) { |
78 |
fd = fdt.fdByName (str); |
79 |
} |
80 |
if (null == fd) { |
81 |
if (ign) { |
82 |
continue; |
83 |
} |
84 |
return null; |
85 |
} |
86 |
tag = fd.id; |
87 |
} |
88 |
} |
89 |
val = me.getValue (); |
90 |
str = null == val ? "" : val.toString (); |
91 |
lst.add (new Field (tag, str)); |
92 |
} |
93 |
} |
94 |
|
95 |
return (Field[]) lst.toArray (new Field [lst.size ()]); |
96 |
} |
97 |
|
98 |
/** Fill tgt with field tag-val-entries. |
99 |
<br/> |
100 |
If fdt is given, only fields |
101 |
having a field decription are put in tgt with string keys. |
102 |
If fdt is null, entry keys are set as integers. |
103 |
<br/> |
104 |
If tgt is null, a new HashMap will be allocated. |
105 |
*/ |
106 |
public static Map ToMap (Field[] flds, Fdt fdt, Map tgt) { |
107 |
Fdt.Fd fd; |
108 |
|
109 |
if (null != flds) { |
110 |
if (null == tgt) { |
111 |
tgt = new HashMap (flds.length); |
112 |
} |
113 |
|
114 |
for (int j = flds.length; 0 <= --j; ) { |
115 |
if (null != fdt) { |
116 |
if (null != (fd = fdt.fdById (flds[j].tag))) { |
117 |
tgt.put (fd.name, flds[j].val); |
118 |
} |
119 |
} |
120 |
else { |
121 |
tgt.put (new Integer (flds[j].tag), flds[j].val); |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
return tgt; |
127 |
} |
128 |
} |
129 |
|