1 package org.textensor.xml;
2
3 import java.util.ArrayList;
4 import java.util.StringTokenizer;
5
6 import org.textensor.report.E;
7
8
9
10 public final class Narrower {
11
12
13 static String destCache;
14
15
16 public static Object narrow(String fcln, Object arg) throws NumberFormatException {
17 Object ret = null;
18
19 destCache = fcln;
20
21
22 if (fcln.endsWith(".int")) {
23 ret = new Integer(makeInt(arg));
24
25 } else if (fcln.endsWith("boolean")) {
26 ret = new Boolean(makeBoolean(arg));
27
28 } else if (fcln.endsWith("double")) {
29 ret = new Double(makeDouble(arg));
30
31 } else if (fcln.startsWith("java.lang.String")) {
32 ret = arg;
33
34 } else if (fcln.startsWith("[D")) {
35 ret = makeDoubleArray(arg);
36
37 } else if (fcln.startsWith("[I")) {
38 ret = makeIntArray(arg);
39
40 } else if (fcln.startsWith("[Ljava.lang.String")) {
41 ret = makeStringArray(arg);
42
43 } else if (fcln.startsWith("[Z")) {
44 ret = makeBooleanArray(arg);
45
46 } else if (fcln.startsWith("[[D")) {
47 ret = makeDDArray(arg);
48
49 } else if (fcln.startsWith("[[I")) {
50 ret = makeIIArray(arg);
51
52 } else if (fcln.endsWith("ArrayList")) {
53 ret = makeArrayList(arg);
54
55
56
57
58
59 }
60
61 return ret;
62
63 }
64
65
66
67 public static void err(String s) {
68 E.error(s);
69 }
70
71
72
73 public static double parseDouble(String s) {
74 double dret = 0.;
75 int ii = s.indexOf("e");
76 if (ii < 0)
77 ii = s.indexOf("E");
78 if (ii < 0) {
79 dret = (new Double(s)).doubleValue();
80
81 } else {
82 String sa = s.substring(0, ii - 1);
83 String sp = s.substring(ii + 1, s.length());
84 int ppp = Integer.parseInt(sp);
85 dret = (new Double(sa)).doubleValue();
86 dret *= Math.pow(10., ppp);
87
88 }
89 return dret;
90 }
91
92
93 public static String[] makeStringArray(Object ob) {
94 String[] sret = null;
95 if (ob instanceof String[]) {
96 sret = (String[])ob;
97
98 } else if (ob instanceof String) {
99 sret = new String[1];
100 sret[0] = (String)ob;
101
102 } else if (ob instanceof ArrayList<?>) {
103 ArrayList v = (ArrayList)ob;
104 int n = v.size();
105 sret = new String[n];
106 int iout = 0;
107 for (Object sub : v) {
108 sret[iout++] = (String)sub;
109 }
110
111 } else {
112 err("ERROR - cant make string array from " + ob);
113 }
114 return sret;
115 }
116
117
118 public static double[] makeDoubleArray(Object ob) {
119 double[] dret = null;
120 if (ob instanceof double[]) {
121 dret = (double[])ob;
122
123 } else if (ob instanceof ArrayList) {
124 ArrayList v = (ArrayList)ob;
125 int n = v.size();
126 dret = new double[n];
127 int iout = 0;
128 for (Object sub : v) {
129 dret[iout++] = makeDouble(sub);
130 }
131
132 } else if (ob instanceof String) {
133 dret = readDoubleArray((String)ob);
134
135 } else if (ob != null) {
136 dret = new double[1];
137 dret[0] = makeDouble(ob);
138 }
139 return dret;
140 }
141
142
143 public static int[] makeIntArray(Object ob) {
144 int[] iret = null;
145 if (ob instanceof int[]) {
146 iret = (int[])ob;
147
148 } else if (ob instanceof ArrayList) {
149 ArrayList v = (ArrayList)ob;
150 int n = v.size();
151 iret = new int[n];
152 int iout = 0;
153 for (Object sub : v) {
154 iret[iout++] = makeInt(sub);
155 }
156
157
158 } else if (ob instanceof String) {
159 String sob = (String)ob;
160 StringTokenizer st = new StringTokenizer(sob, " ,\n");
161 int ntok = st.countTokens();
162
163 iret = new int[ntok];
164 for (int i = 0; i < iret.length; i++) {
165 iret[i] = Integer.parseInt(st.nextToken());
166 }
167
168 } else if (ob != null) {
169 iret = new int[1];
170 iret[0] = makeInt(ob);
171 }
172 return iret;
173 }
174
175
176
177 public static boolean[] makeBooleanArray(Object ob) {
178 boolean[] bret = null;
179 if (ob instanceof ArrayList) {
180 ArrayList v = (ArrayList)ob;
181 int n = v.size();
182 bret = new boolean[n];
183 int iout = 0;
184 for (Object sub : v) {
185 bret[iout++] = makeBoolean(sub);
186 }
187 } else if (ob != null) {
188 bret = new boolean[1];
189 bret[0] = makeBoolean(ob);
190 }
191 return bret;
192 }
193
194
195 public static int makeInt(Object arg) throws NumberFormatException {
196 int iret = 0;
197 if (arg instanceof Integer) {
198 iret = ((Integer)arg).intValue();
199
200 } else if (arg instanceof Double) {
201 iret = (int)(((Double)arg).doubleValue());
202
203 } else if (arg instanceof String) {
204 String s = (String)arg;
205 if (s.equals("false")) {
206 iret = 0;
207 } else if (s.equals("true")) {
208 iret = 1;
209 } else {
210 iret = parseInt((String)arg);
211 }
212 } else {
213 err("cant make an int from " + arg + " " + arg.getClass());
214 }
215 return iret;
216 }
217
218
219
220 public static int parseInt(String s) {
221 int iret = 0;
222 if (s.startsWith("0x")) {
223 s = s.substring(2, s.length());
224 iret = Integer.parseInt(s, 16);
225 } else {
226 iret = Integer.parseInt(s, 10);
227 }
228 return iret;
229 }
230
231
232
233 public static double makeDouble(Object arg) {
234 double dret = 0;
235 if (arg instanceof Double) {
236 dret = ((Double)arg).doubleValue();
237
238 } else if (arg instanceof String) {
239 dret = parseDouble((String)arg);
240
241 } else {
242 err(" cant make a double from " + arg + " " + arg.getClass());
243 (new Exception()).printStackTrace();
244 }
245 return dret;
246 }
247
248
249 public static boolean makeBoolean(Object arg) {
250 boolean bret = false;
251 if (arg instanceof Double) {
252 bret = ((((Double)arg).doubleValue()) > 0.5);
253
254 } else if (arg instanceof String) {
255 String sob = ((String)arg).trim();
256 bret = (sob.equals("1") || sob.equals("true"));
257 } else {
258 err(" instantiator cant make a boolean from " + arg);
259 }
260 return bret;
261 }
262
263
264
265 public static double[][] makeDDArray(Object ob) {
266 double[][] dret = null;
267 if (ob == null) {
268 dret = new double[0][0];
269
270 } else if (ob instanceof Double || ob instanceof String) {
271 dret = new double[1][1];
272 dret[0][0] = makeDouble(ob);
273
274 } else if (ob instanceof ArrayList) {
275 ArrayList v = (ArrayList)ob;
276 dret = new double[v.size()][];
277 int iout = 0;
278 for (Object sub : v) {
279 dret[iout++] = makeDoubleArray(sub);
280 }
281 } else {
282 err("cant make DD array from " + ob);
283 }
284 return dret;
285 }
286
287
288 public static int[][] makeIIArray(Object ob) {
289 int[][] iret = null;
290 if (ob == null) {
291 iret = new int[0][0];
292
293 } else if (ob instanceof Double || ob instanceof String) {
294 iret = new int[1][1];
295 iret[0][0] = makeInt(ob);
296
297 } else if (ob instanceof ArrayList) {
298 ArrayList v = (ArrayList)ob;
299 iret = new int[v.size()][];
300 int iout = 0;
301 for (Object sub : v) {
302 iret[iout++] = makeIntArray(sub);
303 }
304 } else {
305 err("cant make II array from " + ob);
306 }
307 return iret;
308 }
309
310
311 public static ArrayList<Object> makeArrayList(Object arg) {
312 ArrayList<Object> vret = new ArrayList<Object>();
313
314 if (arg instanceof ArrayList<?>) {
315 vret.addAll((ArrayList<?>)arg);
316
317 } else {
318 vret.add(arg);
319 }
320 return vret;
321 }
322
323
324
325 public static double[] readDoubleArray(String s) {
326 if (s.startsWith("{")) {
327 s = s.substring(1, s.indexOf("}"));
328 }
329 s = s.trim();
330
331 String[] sa = s.split("[ ,\t\n\r]+");
332
333
334
335
336
337
338 int nt = sa.length;
339 double[] value = new double[nt];
340
341 try {
342 for (int i = 0; i < nt; i++) {
343 value[i] = (new Double(sa[i])).doubleValue();
344 }
345 } catch (Exception ex) {
346 E.error("float reading cant extract " + nt + " doubles from " + s);
347 for (int i = 0; i < nt; i++) {
348 E.info("string " + i + "=xxx" + sa[i] + "xxx");
349 }
350 }
351
352 return value;
353 }
354
355 }