KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.coach.idltree;
2
3 import org.w3c.dom.Node JavaDoc;
4 import org.omg.CORBA.TypeCode JavaDoc;
5 import org.omg.CORBA.Any JavaDoc;
6 import org.omg.CORBA.TCKind JavaDoc;
7 import org.coach.util.IorPrinter;
8
9 /**
10  * The class IdlObject represents an CORBA IDL Object value. Instances are created through one
11  * of the create() factory methods in IdlNode.
12  * The value is maintained as a stringified ior value.
13  * An IdlObject object has no child nodes.
14  *
15  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
16  */

17 public class IdlObject extends IdlNode implements IdlWritable {
18     protected IdlObject() {
19         setUserObject(this);
20         type = "Object";
21         value = "null";
22         tc = orb.get_primitive_tc(TCKind.tk_objref);
23     }
24
25     protected IdlObject(Any JavaDoc any) {
26         this();
27         setNode(any);
28     }
29         
30     protected IdlObject(TypeCode JavaDoc tc) {
31         this();
32         setNode(tc);
33     }
34     
35     protected void setNode(Any JavaDoc any) {
36         try {
37             setNode(any.type());
38 // org.omg.CORBA.Object obj = any.create_input_stream().read_Object();
39
org.omg.CORBA.Object JavaDoc obj = any.extract_Object();
40             try {
41                 value = orb.object_to_string(obj);
42                 IorPrinter iorPrinter = new IorPrinter(value);
43                 id = iorPrinter.getTypeId();
44             } catch (Exception JavaDoc e) {
45                 value = "null";
46             }
47         } catch (Exception JavaDoc e) {
48             e.printStackTrace();
49         }
50     }
51
52     /**
53      * Returns the current value as a CORBA Any value.
54      *
55      * @return The current value as a CORBA Any.
56      */

57     public Any JavaDoc toAny() {
58         try {
59             Any JavaDoc any = orb.create_any();
60             any.type(tc);
61             if (!value.equals("null")) {
62                 any.insert_Object(orb.string_to_object(value));
63             }
64             return any;
65         } catch (Exception JavaDoc e) {
66             e.printStackTrace();
67         }
68         return null;
69     }
70
71     /**
72      * Assigns the node value from a string.
73      *
74      * @param v The string from which to convert the value.
75      */

76     public void setValue(String JavaDoc v) {
77         if (v.toUpperCase().equals("NULL")) {
78             value = "null";
79             id = "";
80         } else {
81             int idx = v.indexOf("IOR:");
82             if (idx < 0) {
83                 throw new RuntimeException JavaDoc("value is not an IOR: " + v);
84             }
85             value = v.substring(idx);
86             try {
87                 IorPrinter iorPrinter = new IorPrinter(value);
88                 id = iorPrinter.getTypeId();
89             } catch (Exception JavaDoc e) {
90                 id = "";
91             }
92         }
93     }
94
95     /**
96      * Writes the value to a CORBA outputstream.
97      *
98      * @param is The outputstream to write to.
99      */

100     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
101         try {
102             os.write_Object(orb.string_to_object(value));
103         } catch (Exception JavaDoc e) {
104             e.printStackTrace();
105         }
106     }
107
108     /**
109      * Reads the value from a CORBA inputstream.
110      *
111      * @param is The inputstream to read from.
112      */

113     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
114         value = orb.object_to_string(is.read_Object());
115     }
116     
117     // XML section
118

119     /**
120      * Create an IdlObject node from an Xml representation.
121      *
122      * XML format example:
123      * <pre>
124      * &lt;object&gt;id="IDL:examples/MyInterface:1.0"&gt;
125      * IOR:000....
126      * &lt;/object&gt;
127      * </pre>
128      *
129      * @param xml The XML string from which to create an IdlObject instance.
130      */

131     public IdlObject(String JavaDoc xml) {
132         this(XmlNode.getNode(xml));
133     }
134     
135     /**
136      * Construct an XmlNode from an Xml node.
137      * Package access only. Called from the Xml parser to contruct
138      * an Xml type tree from an Xml representation.
139      */

140     IdlObject(Node JavaDoc n) {
141         this();
142         if (n == null || !n.getNodeName().toUpperCase().equals("OBJECT")) {
143             throw new RuntimeException JavaDoc("Object expected");
144         }
145         setValue(XmlNode.getText(n));
146     }
147   
148     /**
149      * Write the current value to an IdlWriter object.
150      *
151      * @param w The IdlWriter object to write the current value to.
152      */

153     public void write(IdlWriter w) {
154         w.write_Object(value);
155     }
156 }
Popular Tags