KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
29 import java.io.*;
30 import java.lang.reflect.*;
31 import org.omg.CORBA.ORB JavaDoc;
32 import org.omg.CORBA.TypeCode JavaDoc;
33 import org.omg.CORBA.Any JavaDoc;
34 import org.omg.CORBA.TCKind JavaDoc;
35
36 /**
37  * The class IdlStruct represents an CORBA IDL struct value. Instances are created through one
38  * of the create() factory methods in IdlNode.
39  * An IdlStruct object has child nodes representing each struct member field.
40  *
41  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
42  */

43 public class IdlStruct extends IdlNode implements IdlWritable {
44     protected String JavaDoc[] members;
45     protected transient TypeCode JavaDoc[] types;
46     
47     protected IdlStruct() {
48         isLeaf = false;
49         isMutable = false;
50         setUserObject(this);
51     }
52     
53     protected IdlStruct(Any JavaDoc any) {
54         this();
55         setNode(any);
56     }
57         
58     protected IdlStruct(TypeCode JavaDoc tc) {
59         this();
60         setNode(tc);
61     }
62     
63     protected void setNode(TypeCode JavaDoc tc) {
64         try {
65             this.tc = tc;
66             if (tc.kind().value() == TCKind._tk_struct) {
67                 id = tc.id();
68                 type = "struct " + id;
69                 members = new String JavaDoc[tc.member_count()];
70                 types = new TypeCode JavaDoc[tc.member_count()];
71                 removeAllChildren();
72                 for (int i = 0; i < tc.member_count(); i++) {
73                     types[i] = tc.member_type(i);
74                     members[i] = tc.member_name(i);
75                     IdlNode m = IdlNode.create(types[i]);
76                     m.field = members[i];
77                     add(m);
78                 }
79             } else if (tc.kind().value() == TCKind._tk_alias) {
80                 id = tc.id();
81                 type = id;
82                 TypeCode JavaDoc stc = tc.content_type();
83                 while (stc.kind().value() == TCKind._tk_alias) {
84                     stc = stc.content_type();
85                 }
86                 if (stc.kind().value() != TCKind._tk_struct) {
87                     throw new RuntimeException JavaDoc("Struct type expected");
88                 }
89                 members = new String JavaDoc[tc.member_count()];
90                 types = new TypeCode JavaDoc[tc.member_count()];
91                 removeAllChildren();
92                 for (int i = 0; i < stc.member_count(); i++) {
93                     types[i] = stc.member_type(i);
94                     members[i] = stc.member_name(i);
95                     IdlNode m = IdlNode.create(types[i]);
96                     m.field = members[i];
97                     add(m);
98                 }
99             } else {
100                 throw new RuntimeException JavaDoc("Struct type expected");
101             }
102         } catch (Exception JavaDoc e) {
103             e.printStackTrace();
104         }
105     }
106
107     protected void setNode(Any JavaDoc any) {
108         try {
109             setNode(any.type());
110             org.omg.CORBA.portable.InputStream JavaDoc in = any.create_input_stream();
111             removeAllChildren();
112             for (int i = 0; i < members.length; i++) {
113                 Any JavaDoc memberAny = orb.create_any();
114                 memberAny.read_value(in, types[i]);
115                 IdlNode m = create(memberAny);
116                 m.field = members[i];
117                 add(m);
118             }
119         } catch (Throwable JavaDoc e) {
120             e.printStackTrace();
121         }
122     }
123     
124     /**
125      * Return the list of member names for this struct.
126      *
127      * @return The list of member names for this struct.
128      */

129     public String JavaDoc[] getMembers() {
130         return members;
131     }
132
133     /**
134      * Returns the current value as a CORBA Any value.
135      *
136      * @return The current value as a CORBA Any.
137      */

138     public Any JavaDoc toAny() {
139         try {
140             Any JavaDoc any = orb.create_any();
141             any.type(tc);
142             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
143             for (int i = 0; i < members.length; i++) {
144                 Any JavaDoc memberAny = ((IdlNode)getChildAt(i)).toAny();
145                 memberAny.write_value(out);
146             }
147             any.read_value(out.create_input_stream(), tc);
148             return any;
149         } catch (Throwable JavaDoc e) {
150             e.printStackTrace();
151         }
152         return null;
153     }
154
155     /**
156      * Writes the value to a CORBA outputstream.
157      *
158      * @param is The outputstream to write to.
159      */

160     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
161         try {
162             for (int i = 0; i < members.length; i++) {
163                 ((IdlNode)getChildAt(i)).write(os);
164             }
165         } catch (Exception JavaDoc e) {
166             e.printStackTrace();
167         }
168     }
169
170     /**
171      * Reads the value from a CORBA inputstream.
172      *
173      * @param is The inputstream to read from.
174      */

175     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
176         try {
177             for (int i = 0; i < members.length; i++) {
178                 ((IdlNode)getChildAt(i)).read(is);
179             }
180         } catch (Exception JavaDoc e) {
181             e.printStackTrace();
182         }
183     }
184     
185     // XML section
186

187     /**
188      * Create an IdlStruct from an Xml representation.
189      *
190      * The XML format example
191      * <pre>
192      * &lt;struct id="IDL:example/MyStruct:1.0"&gt;
193      * &lt;member name="m1"&gt;
194      * &lt;string&gt;my member value&lt;/string&gt;
195      * &lt;/member&gt;
196      * &lt;member name="m2"&gt;
197      * &lt;long&gt;100&lt;/long&gt;
198      * &lt;/member&gt;
199      * &lt;/struct&gt;
200      * </pre>
201      *
202      * @param xml The XML string from which to create an IdlStruct instance.
203      */

204     public IdlStruct(String JavaDoc xml) {
205         this(XmlNode.getNode(xml));
206     }
207     
208     IdlStruct(Node JavaDoc n) {
209         this();
210         try {
211             id = XmlNode.getId(n);
212             setNode(XmlNode.type(id));
213              
214             Node JavaDoc[] nodes = XmlNode.childElements(n);
215             String JavaDoc[] xmlmembers = new String JavaDoc[nodes.length];
216     
217             removeAllChildren();
218             for (int i = 0; i < nodes.length; i++) {
219                 if (nodes[i].getNodeName().toUpperCase().equals("MEMBER")) {
220                     xmlmembers[i] = XmlNode.getName(nodes[i]);
221                     IdlNode m = XmlNode.getIdlNode(tc.member_type(i), XmlNode.firstChildElement(nodes[i]));
222                     m.setField(xmlmembers[i]);
223                     add(m);
224                 } else {
225                     throw new RuntimeException JavaDoc("Member expected.");
226                 }
227             }
228             if (members.length >= xmlmembers.length) {
229                 for (int i = 0; i < members.length; i++) {
230                     if (i >= xmlmembers.length || !members[i].equals(xmlmembers[i])) {
231                         throw new RuntimeException JavaDoc("exception member " + members[i] + " is missing for " + id);
232                     }
233                 }
234             } else {
235                 for (int i = 0; i < xmlmembers.length; i++) {
236                     if (i >= members.length || !members[i].equals(xmlmembers[i])) {
237                         throw new RuntimeException JavaDoc(xmlmembers[i] + " is not a valid exception member in " + id);
238                     }
239                 }
240             }
241         } catch (Exception JavaDoc e) {
242             e.printStackTrace();
243         }
244     }
245
246     /**
247      * Write the current value to an IdlWriter object.
248      *
249      * @param w The IdlWriter object to write the current value to.
250      */

251     public void write(IdlWriter w) {
252         write(this, w);
253     }
254
255     public static void write(IdlStruct n, IdlWriter w) {
256         w.write_start_struct(n.getId());
257         String JavaDoc[] mbrs = n.getMembers();
258         for(int i = 0; i < n.getChildCount(); i++) {
259             w.write_start_member(mbrs[i]);
260             XmlNode.write((IdlNode)n.getChildAt(i), w);
261             w.write_end_member();
262         }
263         w.write_end_struct();
264     }
265 }
Popular Tags