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