KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > client > util > AnyUtil


1 // Copyright (C) 1998-1999
2
// Object Oriented Concepts, Inc.
3

4 // **********************************************************************
5
//
6
// Copyright (c) 1997
7
// Mark Spruiell (mark@intellisoft.com)
8
//
9
// See the COPYING file for more information
10
//
11
// **********************************************************************
12

13 package org.jacorb.trading.client.util;
14
15 import java.io.*;
16 import org.omg.CORBA.*;
17 import org.omg.DynamicAny.*;
18
19 public class AnyUtil
20 {
21     private AnyUtil()
22     {
23     }
24
25
26     public static void print(org.omg.CORBA.ORB JavaDoc _orb, PrintWriter pw, Any val)
27     {
28     org.omg.CORBA.ORB JavaDoc orb = _orb;
29     try {
30         TypeCode tc = val.type();
31
32         while (tc.kind() == TCKind.tk_alias)
33         tc = tc.content_type();
34
35         TCKind kind = tc.kind();
36         if (kind == TCKind.tk_sequence)
37         printSequence(_orb,pw, val);
38
39         switch (kind.value()) {
40         case TCKind._tk_short: {
41         int s = val.extract_short();
42         pw.print(s);
43         }
44         break;
45
46         case TCKind._tk_long: {
47         int l = val.extract_long();
48         pw.print(l);
49         }
50         break;
51
52         case TCKind._tk_ushort: {
53         int i = val.extract_ushort();
54         pw.print(i);
55         }
56         break;
57
58         case TCKind._tk_ulong: {
59         long l = val.extract_ulong();
60         pw.print(l);
61         }
62         break;
63
64         case TCKind._tk_float: {
65         float f = val.extract_float();
66         pw.print(f);
67         }
68         break;
69
70         case TCKind._tk_double: {
71         double d = val.extract_double();
72         pw.print(d);
73         }
74         break;
75
76         case TCKind._tk_boolean: {
77         boolean b = val.extract_boolean();
78         pw.print(b);
79         }
80         break;
81
82         case TCKind._tk_char: {
83         char c = val.extract_char();
84         pw.print(c);
85         }
86         break;
87
88         case TCKind._tk_string: {
89         String JavaDoc s = val.extract_string();
90         pw.print("'" + s + "'");
91         }
92         break;
93
94         case TCKind._tk_octet: {
95         byte b = val.extract_octet();
96         pw.print((int)b);
97         }
98         break;
99
100         case TCKind._tk_objref: {
101         org.omg.CORBA.Object JavaDoc obj = val.extract_Object();
102         if (obj == null)
103             pw.print("nil");
104         else
105         {
106             _orb.object_to_string(obj);
107         }
108         }
109         break;
110         }
111     }
112     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
113         e.printStackTrace();
114     }
115     catch (BAD_OPERATION e) {
116         e.printStackTrace();
117     }
118     }
119
120
121     public static void print(PrintWriter pw, TypeCode tc)
122     {
123     try {
124         TCKind kind = tc.kind();
125
126         switch (kind.value()) {
127         case TCKind._tk_objref:
128         pw.print("interface " + tc.name());
129         break;
130
131         case TCKind._tk_alias: {
132         pw.print("typedef ");
133         TypeCode content = tc.content_type();
134         printName(pw, content);
135         pw.print(" " + tc.name());
136         }
137         break;
138
139         case TCKind._tk_string: {
140         pw.print("string");
141         int len = tc.length();
142         if (len != 0)
143             pw.print("<" + len + ">");
144         }
145         break;
146
147         case TCKind._tk_sequence: {
148         pw.print("sequence<");
149         TypeCode content = tc.content_type();
150         printName(pw, content);
151         int len = tc.length();
152         if (len != 0)
153             pw.print(", " + len);
154         pw.print(">");
155         }
156         break;
157
158         case TCKind._tk_array: {
159         TypeCode content = tc.content_type();
160         printName(pw, content);
161         int len = tc.length();
162         pw.print("[" + len + "]");
163         }
164         break;
165
166         case TCKind._tk_union:
167         case TCKind._tk_enum:
168         case TCKind._tk_struct:
169         case TCKind._tk_except:
170         default:
171         printKind(pw, kind);
172         }
173     }
174     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
175         e.printStackTrace();
176     }
177     }
178
179
180     protected static void printSequence(org.omg.CORBA.ORB JavaDoc orb, PrintWriter pw, Any val)
181     {
182     try {
183         TypeCode tc = val.type();
184
185         while (tc.kind() == TCKind.tk_alias)
186         tc = tc.content_type();
187
188         TypeCode contentTC = tc.content_type();
189         TCKind kind = contentTC.kind();
190
191         DynAnyFactory factory = DynAnyFactoryHelper.narrow(orb.resolve_initial_references("DynAnyFactory"));
192         org.omg.DynamicAny.DynAny JavaDoc da = factory.create_dyn_any(val);
193         org.omg.DynamicAny.DynSequence JavaDoc ds = org.omg.DynamicAny.DynSequenceHelper.narrow(da);
194         int len = ds.get_length();
195
196         switch (kind.value()) {
197         case TCKind._tk_short: {
198         for (int i = 0; i < len; i++, ds.next()) {
199             pw.print(ds.current_component().get_short());
200             if (i < len - 1)
201             pw.print(", ");
202         }
203         }
204         break;
205
206         case TCKind._tk_long: {
207         for (int i = 0; i < len; i++, ds.next()) {
208             pw.print(ds.current_component().get_long());
209             if (i < len - 1)
210             pw.print(", ");
211         }
212         }
213         break;
214
215         case TCKind._tk_ushort: {
216         for (int i = 0; i < len; i++, ds.next()) {
217             pw.print(ds.current_component().get_ushort());
218             if (i < len - 1)
219             pw.print(", ");
220         }
221         }
222         break;
223
224         case TCKind._tk_ulong: {
225         for (int i = 0; i < len; i++, ds.next()) {
226             pw.print(ds.current_component().get_ulong());
227             if (i < len - 1)
228             pw.print(", ");
229         }
230         }
231         break;
232
233         case TCKind._tk_float: {
234         for (int i = 0; i < len; i++, ds.next()) {
235             pw.print(ds.current_component().get_float());
236             if (i < len - 1)
237             pw.print(", ");
238         }
239         }
240         break;
241
242         case TCKind._tk_double: {
243         for (int i = 0; i < len; i++, ds.next()) {
244             pw.print(ds.current_component().get_double());
245             if (i < len - 1)
246             pw.print(", ");
247         }
248         }
249         break;
250
251         case TCKind._tk_boolean: {
252         for (int i = 0; i < len; i++, ds.next()) {
253             pw.print(ds.current_component().get_boolean());
254             if (i < len - 1)
255             pw.print(", ");
256         }
257         }
258         break;
259
260         case TCKind._tk_char: {
261         for (int i = 0; i < len; i++, ds.next()) {
262             pw.print(ds.current_component().get_char());
263             if (i < len - 1)
264             pw.print(", ");
265         }
266         }
267         break;
268
269         case TCKind._tk_string: {
270         for (int i = 0; i < len; i++, ds.next()) {
271             pw.print("'" + ds.current_component().get_string() + "'");
272             if (i < len - 1)
273             pw.print(", ");
274         }
275         }
276         break;
277
278         case TCKind._tk_octet: {
279         for (int i = 0; i < len; i++, ds.next()) {
280             pw.print((int)ds.current_component().get_octet());
281             if (i < len - 1)
282             pw.print(", ");
283         }
284         }
285         break;
286
287         case TCKind._tk_objref: {
288         for (int i = 0; i < len; i++, ds.next()) {
289             org.omg.CORBA.Object JavaDoc obj = ds.current_component().get_reference();
290             if (obj == null)
291             pw.println("nil");
292             else
293             {
294             orb.object_to_string(obj);
295             }
296
297             if (i < len - 1)
298             pw.println();
299         }
300         }
301         break;
302         }
303
304         da.destroy();
305     }
306     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
307         e.printStackTrace();
308     }
309     // GB:
310
catch (org.omg.CORBA.ORBPackage.InvalidName JavaDoc e) {
311         e.printStackTrace();
312     }
313     catch (org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc e) {
314         e.printStackTrace();
315     }
316     catch (org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc e) {
317         e.printStackTrace();
318     }
319     catch (org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode JavaDoc e) {
320         e.printStackTrace();
321     }
322     catch (BAD_OPERATION e) {
323         e.printStackTrace();
324     }
325     }
326
327
328     protected static void printName(PrintWriter pw, TypeCode tc)
329     {
330     try {
331         TCKind kind = tc.kind();
332         switch (kind.value()) {
333         case TCKind._tk_objref:
334         case TCKind._tk_union:
335         case TCKind._tk_enum:
336         case TCKind._tk_struct:
337         case TCKind._tk_except:
338         case TCKind._tk_alias:
339         pw.print(tc.name());
340         break;
341
342         case TCKind._tk_string: {
343         pw.print("string");
344         int len = tc.length();
345         if (len != 0)
346             pw.print("<" + len + ">");
347         }
348         break;
349
350         case TCKind._tk_sequence: {
351         pw.print("sequence<");
352         TypeCode content = tc.content_type();
353         printName(pw, content);
354         int len = tc.length();
355         if (len != 0)
356             pw.print(", " + len);
357         pw.print(">");
358         }
359         break;
360
361         case TCKind._tk_array: {
362         TypeCode content = tc.content_type();
363         printName(pw, content);
364         int len = tc.length();
365         pw.print("[" + len + "]");
366         }
367
368         default:
369         printKind(pw, kind);
370         }
371     }
372     catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {
373         e.printStackTrace();
374     }
375     }
376
377
378     protected static void printKind(PrintWriter pw, TCKind kind)
379     {
380     switch (kind.value()) {
381     case TCKind._tk_null:
382         pw.print("null");
383         break;
384
385     case TCKind._tk_void:
386         pw.print("void");
387         break;
388
389     case TCKind._tk_short:
390         pw.print("short");
391         break;
392
393     case TCKind._tk_long:
394         pw.print("long");
395         break;
396
397     case TCKind._tk_ushort:
398         pw.print("unsigned short");
399         break;
400
401     case TCKind._tk_ulong:
402         pw.print("unsigned long");
403         break;
404
405     case TCKind._tk_float:
406         pw.print("float");
407         break;
408
409     case TCKind._tk_double:
410         pw.print("double");
411         break;
412
413     case TCKind._tk_boolean:
414         pw.print("boolean");
415         break;
416
417     case TCKind._tk_char:
418         pw.print("char");
419         break;
420
421     case TCKind._tk_octet:
422         pw.print("octet");
423         break;
424
425     case TCKind._tk_any:
426         pw.print("any");
427         break;
428
429     case TCKind._tk_TypeCode:
430         pw.print("TypeCode");
431         break;
432
433     case TCKind._tk_Principal:
434         pw.print("Principal");
435         break;
436
437     case TCKind._tk_objref:
438         pw.print("Object");
439         break;
440
441     case TCKind._tk_struct:
442         pw.print("struct");
443         break;
444
445     case TCKind._tk_union:
446         pw.print("union");
447         break;
448
449     case TCKind._tk_enum:
450         pw.print("enum");
451         break;
452
453     case TCKind._tk_string:
454         pw.print("string");
455         break;
456
457     case TCKind._tk_sequence:
458         pw.print("sequence");
459         break;
460
461     case TCKind._tk_array:
462         pw.print("array");
463         break;
464
465     case TCKind._tk_alias:
466         pw.print("alias");
467         break;
468
469     case TCKind._tk_except:
470         pw.print("exception");
471         break;
472
473     default:
474         pw.print("(unknown)");
475         break;
476     }
477     }
478 }
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
Popular Tags