KickJava   Java API By Example, From Geeks To Geeks.

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


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.DynamicAny.*;
35 import org.omg.CORBA.ORB JavaDoc;
36 import org.omg.CORBA.TypeCode JavaDoc;
37 import org.omg.CORBA.Any JavaDoc;
38 import org.omg.CORBA.TCKind JavaDoc;
39
40 /**
41  * The class IdlArray represents an CORBA IDL array value. Instances are created through one
42  * of the create() factory methods in IdlNode.
43  * An IdlAray object has child nodes representing the content of the IDL array it represents. The type and
44  * value of the child node correspond with the type and value of the elements in the array it represents.
45  *
46  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
47  */

48 public class IdlArray extends IdlNode implements IdlWritable {
49     protected int size;
50     protected transient TypeCode JavaDoc content;
51     
52     protected IdlArray() {
53         isLeaf = false;
54         isMutable = false;
55         setUserObject(this);
56     }
57           
58     protected IdlArray(Any any) {
59         this();
60         setNode(any);
61     }
62         
63     protected IdlArray(TypeCode JavaDoc tc) {
64         this();
65         setNode(tc);
66     }
67     
68     protected void setNode(TypeCode JavaDoc tc) {
69         try {
70             this.tc = tc;
71             if (tc.kind().value() == TCKind._tk_array) {
72                 id = "";
73                 try {
74                     id = tc.id();
75                     type = "array " + id;
76                 } catch (Exception JavaDoc ex) {
77                     // anonymous arrays will cause an exception
78
type = "array";
79                 }
80                 size = tc.length();
81                 value = "" + size;
82                 content = tc.content_type();
83                 removeAllChildren();
84                 for (int i = 0; i < tc.length(); i++) {
85                     IdlNode m = create(content);
86                     m.field = "[" + i + "]";
87                     add(m);
88                 }
89             } else if (tc.kind().value() == TCKind._tk_alias) {
90                 id = tc.id();
91                 type = id;
92                 TypeCode JavaDoc stc = tc.content_type();
93                 while (stc.kind().value() == TCKind._tk_alias) {
94                     stc = stc.content_type();
95                 }
96                 if (stc.kind().value() != TCKind._tk_array) {
97                     throw new RuntimeException JavaDoc("Array type expected");
98                 }
99                 size = stc.length();
100                 value = "" + size;
101                 content = stc.content_type();
102                 removeAllChildren();
103                 for (int i = 0; i < stc.length(); i++) {
104                     IdlNode m = create(content);
105                     m.field = "[" + i + "]";
106                     add(m);
107                 }
108             } else {
109                 throw new RuntimeException JavaDoc("Union type expected");
110             }
111         } catch (Exception JavaDoc e) {
112             e.printStackTrace();
113         }
114     }
115
116     protected void setNode(Any any) {
117         try {
118             setNode(any.type());
119             org.omg.CORBA.portable.InputStream JavaDoc in = any.create_input_stream();
120             removeAllChildren();
121             for (int i = 0; i < size; i++) {
122                 Any memberAny = orb.create_any();
123                 memberAny.read_value(in, content);
124                 IdlNode m = create(memberAny);
125                 m.field = "[" + i + "]";
126                 add(m);
127             }
128         } catch (Exception JavaDoc e) {
129             e.printStackTrace();
130         }
131     }
132
133     /**
134      * Return the number of array elements.
135      *
136      * @return The array size.
137      */

138     public int getSize() {
139         return size;
140     }
141
142     /**
143      * Returns the current value as a CORBA Any value.
144      *
145      * @return The current value as a CORBA Any.
146      */

147     public Any toAny() {
148         try {
149             Any any = orb.create_any();
150             any.type(tc);
151             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
152             for (int i = 0; i < size; i++) {
153                 ((IdlNode)getChildAt(i)).toAny().write_value(out);
154             }
155             any.read_value(out.create_input_stream(), tc);
156             return any;
157         } catch (Exception JavaDoc e) {
158             e.printStackTrace();
159         }
160         return null;
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         try {
170             for (int i = 0; i < size; i++) {
171                 ((IdlNode)getChildAt(i)).write(os);
172             }
173         } catch (Exception JavaDoc e) {
174             e.printStackTrace();
175         }
176     }
177
178     /**
179      * Reads the value from a CORBA inputstream.
180      *
181      * @param is The inputstream to read from.
182      */

183     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
184         try {
185             for (int i = 0; i < size; i++) {
186                 ((IdlNode)getChildAt(i)).read(is);
187             }
188         } catch (Exception JavaDoc e) {
189             e.printStackTrace();
190         }
191     }
192     
193     // XML section
194

195     /**
196      * Creates new IdlArray object from an XML string.
197      * XML format example:
198      *
199      * <pre>
200      * &lt;array id="IDL:examples/MyArray:1.0" length=1&gt;
201      * &lt;double&gt;3.2&lt;/double&gt;
202      * &lt;/array&gt;
203      * </pre>
204      *
205      * @param xml The XML string from which to create a ne IdlArray instance.
206      */

207     public IdlArray(String JavaDoc xml) {
208         this(XmlNode.getNode(xml));
209     }
210
211     IdlArray(Node JavaDoc n) {
212         this(null, n);
213     }
214         
215     IdlArray(org.omg.CORBA.TypeCode JavaDoc t, Node JavaDoc n) {
216         this();
217         try {
218             id = XmlNode.getId(n);
219             if (!id.equals("")) {
220                 t = XmlNode.type(id);
221             }
222             setNode(t);
223             org.omg.CORBA.TypeCode JavaDoc content = tc.content_type();
224
225             if (XmlNode.getLength(n) != size) {
226                 throw new RuntimeException JavaDoc("Array length does not match actual length " + size + ".");
227             }
228             Node JavaDoc[] nodes = XmlNode.childElements(n);
229             
230             if (nodes.length != size) {
231                 throw new RuntimeException JavaDoc("Number of array elements does not match " + size + ".");
232             }
233             removeAllChildren();
234             for (int i = 0; i < size; i++) {
235                 IdlNode m = XmlNode.getIdlNode(content, nodes[i]);
236                 m.setField("[" + i + "]");
237                 add(m);
238             }
239             value = "" + size;
240         } catch (Exception JavaDoc e) {
241             throw new RuntimeException JavaDoc(e.toString());
242         }
243     }
244
245     /**
246      * Write the current value to an IdlWriter object.
247      *
248      * @param w The IdlWriter object to write the current value to.
249      */

250     public void write(IdlWriter w) {
251         write(this, w);
252     }
253
254     public static void write(IdlArray n, IdlWriter w) {
255         w.write_start_array(n.getId(), n.getSize());
256         for(int i = 0; i < n.getChildCount(); i++) {
257             XmlNode.write((IdlNode)n.getChildAt(i), w);
258         }
259         w.write_end_array();
260     }
261 }
Popular Tags