KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > idltree > IdlAny


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.idltree;
26
27 import org.w3c.dom.Node JavaDoc;
28 import javax.swing.tree.*;
29
30 import java.util.*;
31 import java.io.*;
32 import java.lang.reflect.*;
33
34 import org.omg.CORBA.ORB JavaDoc;
35 import org.omg.CORBA.TypeCode JavaDoc;
36 import org.omg.CORBA.Any JavaDoc;
37 import org.omg.CORBA.TCKind JavaDoc;
38
39 /**
40  * The class IdlAny represents an CORBA IDL Any value. Instances are created through one
41  * of the create() factory methods in IdlNode.
42  * An IdlAny object can one or zero other IdlNode objects as a child node. The type and
43  * value of the child node correspond with the type and value contained in the Any it represents.
44  * If the CORBA Any is empty, the IdlAny object has no children.
45  * By default IdlAny represents an empy CORBA Any object.
46  *
47  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
48  */

49 public class IdlAny extends IdlNode implements IdlWritable {
50     protected IdlAny() {
51         setUserObject(this);
52         type = "any";
53         isLeaf = false;
54         tc = orb.get_primitive_tc(TCKind.tk_any);
55     }
56
57     protected IdlAny(Any JavaDoc any) {
58         this();
59         setNode(any);
60     }
61         
62     protected IdlAny(TypeCode JavaDoc tc) {
63         this();
64         setNode(tc);
65     }
66
67     protected void setNode(Any JavaDoc any) {
68         try {
69             setNode(any.type());
70             removeAllChildren();
71             Any JavaDoc content = any.extract_any();
72             // If the any is empty, its kind value will be tk_null
73
if (content.type().kind().value() != TCKind._tk_null) {
74                 add(IdlNode.create(content));
75             }
76         } catch (Exception JavaDoc e) {
77             e.printStackTrace();
78         }
79     }
80
81     /**
82      * Returns the current value as a CORBA Any value.
83      *
84      * @return The current value as a CORBA Any.
85      */

86     public Any JavaDoc toAny() {
87         try {
88             Any JavaDoc any = orb.create_any();
89             any.type(tc);
90             if (getChildCount() == 1) {
91                 IdlNode n = (IdlNode)getChildAt(0);
92                 any.insert_any(n.toAny());
93             } else {
94                 Any JavaDoc content = orb.create_any();
95                 content.type(orb.get_primitive_tc(TCKind.tk_null));
96                 any.insert_any(content);
97             }
98             return any;
99         } catch (Exception JavaDoc e) {
100             e.printStackTrace();
101         }
102         return null;
103     }
104         
105     /**
106      * Creates a child IdlNode value representing the content of the CORBA Any.
107      *
108      * Creates an initialized child node representing the content of the
109      * CORBA Any value based on the repository id, or the type name of a CORBA base type.
110      *
111      * @param id The CORBA repository id for the content type or the type name of a CORBA base type.
112      */

113     public void setContent(String JavaDoc id) {
114         try {
115             removeAllChildren();
116             TypeCode JavaDoc t = type(id);
117             add(IdlNode.create(t));
118         } catch (Exception JavaDoc e) {
119             e.printStackTrace();
120         }
121     }
122
123     protected static org.omg.CORBA.TypeCode JavaDoc type(String JavaDoc id) {
124         String JavaDoc type = idToType(id);
125         Integer JavaDoc index = (Integer JavaDoc)table.get(type.toLowerCase());
126
127         if (index == null) {
128             // The type is not a CORBA primitive
129
try {
130                 Class JavaDoc hc = Class.forName(type + "Helper");
131                 Method hm = hc.getMethod("type", null);
132                 return (org.omg.CORBA.TypeCode JavaDoc)hm.invoke(null, null);
133             } catch (Exception JavaDoc e) {
134 e.printStackTrace();
135                 return null;
136             }
137         } else {
138 // org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init();
139
switch (index.intValue()) {
140                 case 0: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_boolean);
141                 case 1: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_octet);
142                 case 2: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_short);
143                 case 3: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ushort);
144                 case 4: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_long);
145                 case 5: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulong);
146                 case 6: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longlong);
147                 case 7: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_ulonglong);
148                 case 8: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_float);
149                 case 9: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_double);
150                 case 10: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_longdouble);
151                 case 11: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_char);
152                 case 12: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wchar);
153                 case 13: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_string);
154                 case 14: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_wstring);
155                 case 15: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_objref);
156                 case 17: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_fixed);
157                 case 18: return orb.get_primitive_tc(org.omg.CORBA.TCKind.tk_any);
158                 default: return null;
159             }
160         }
161     }
162
163     /**
164      * Writes the value to a CORBA outputstream.
165      *
166      * @param is The outputstream to write to.
167      */

168     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
169         if (getChildCount() == 1) {
170             IdlNode n = (IdlNode)getChildAt(0);
171             n.write(os);
172         }
173     }
174
175     /**
176      * Reads the value from a CORBA inputstream.
177      *
178      * @param is The inputstream to read from.
179      */

180     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
181         if (getChildCount() == 1) {
182             IdlNode n = (IdlNode)getChildAt(0);
183             n.read(is);
184         }
185     }
186
187     /**
188      * Convert IDL:omg.org/CosNotification/StructuredEvent:1.0
189      * into org.omg.CosNotification.StructuredEvent
190      */

191     static private String JavaDoc idToType(String JavaDoc id) {
192         String JavaDoc javaName = "";
193         // strip off IDL: en version :1.0 sections
194
if (id.indexOf(":") > 0) {
195             id = id.substring(id.indexOf(":") + 1, id.lastIndexOf(":"));
196         }
197         if (id.indexOf(".") > 0) {
198             // there is a pragma prefix
199
String JavaDoc pragma = id.substring(0, id.indexOf("/"));
200             String JavaDoc[] el = splitName(pragma, ".");
201             String JavaDoc prefix = el[el.length - 1];
202             for (int i = el.length - 2; i >= 0; i--) {
203                 prefix += "." + el[i];
204             }
205             javaName = prefix + id.substring(id.indexOf("/")).replace('/', '.');
206         } else {
207             javaName = id.replace('/', '.');
208         }
209         return javaName;
210     }
211         
212     static private final java.util.Hashtable JavaDoc table = new java.util.Hashtable JavaDoc();
213     static {
214         table.put("boolean", new Integer JavaDoc(0));
215         table.put("octet", new Integer JavaDoc(1));
216         table.put("short", new Integer JavaDoc(2));
217         table.put("ushort", new Integer JavaDoc(3));
218         table.put("long", new Integer JavaDoc(4));
219         table.put("ulong", new Integer JavaDoc(5));
220         table.put("longlong", new Integer JavaDoc(6));
221         table.put("ulonglong", new Integer JavaDoc(7));
222         table.put("float", new Integer JavaDoc(8));
223         table.put("double", new Integer JavaDoc(9));
224         table.put("longdouble", new Integer JavaDoc(10));
225         table.put("char", new Integer JavaDoc(11));
226         table.put("wchar", new Integer JavaDoc(12));
227         table.put("string", new Integer JavaDoc(13));
228         table.put("wstring", new Integer JavaDoc(14));
229         table.put("object", new Integer JavaDoc(15));
230         table.put("interface", new Integer JavaDoc(16));
231         table.put("fixed", new Integer JavaDoc(17));
232         table.put("any", new Integer JavaDoc(18));
233         table.put("enum", new Integer JavaDoc(19));
234         table.put("struct", new Integer JavaDoc(20));
235         table.put("union", new Integer JavaDoc(21));
236         table.put("sequence", new Integer JavaDoc(22));
237         table.put("array", new Integer JavaDoc(23));
238     }
239
240     // XML section
241

242     /**
243      * Creates new IdlAny object from an XML string.
244      *
245      * XML format example:
246      * <pre>
247      * &lt;any&gt;
248      * &lt;double&gt;3.2&lt;/double&gt;
249      * &lt;/any&gt;
250      * </pre>
251      * @param xml The XML string from which to create a ne IdlAny instance.
252      */

253     public IdlAny(String JavaDoc xml) {
254         this(XmlNode.getNode(xml));
255     }
256             
257     IdlAny(Node JavaDoc node) {
258         this();
259
260         if (node == null || !node.getNodeName().toUpperCase().equals("ANY")) {
261             throw new RuntimeException JavaDoc("any expected");
262         }
263                 
264         Node JavaDoc child = XmlNode.firstChildElement(node);
265
266         if (child != null) {
267             add(XmlNode.getIdlNode(child));
268         }
269     }
270
271     /**
272      * Write the current value to an IdlWriter object.
273      *
274      * @param w The IdlWriter object to write the current value to.
275      */

276     public void write(IdlWriter w) {
277         write(this, w);
278     }
279
280     public static void write(IdlAny n, IdlWriter w) {
281         w.write_start_any();
282         if (n.getChildCount() == 1) {
283             XmlNode.write((IdlNode)n.getChildAt(0), w);
284         }
285         w.write_end_any();
286     }
287 }
Popular Tags