KickJava   Java API By Example, From Geeks To Geeks.

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


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

47 public class IdlSequence extends IdlNode implements IdlWritable {
48     protected boolean isBounded;
49     protected int size;
50     protected transient TypeCode JavaDoc content;
51     
52     protected IdlSequence() {
53         isLeaf = false;
54         setUserObject(this);
55     }
56    
57     protected IdlSequence(Any JavaDoc any) {
58         this();
59         setNode(any);
60     }
61         
62     protected IdlSequence(TypeCode JavaDoc tc) {
63         this();
64         setNode(tc);
65     }
66     
67     protected void setNode(TypeCode JavaDoc tc) {
68         try {
69             this.tc = tc;
70             if (tc.kind().value() == TCKind._tk_sequence) {
71                 id = "";
72                 try {
73                     id = tc.id();
74                     type = "sequence " + id;
75                 } catch (Exception JavaDoc ex) {
76                     // anonymous sequences will cause an exception
77
type = "sequence";
78                 }
79                 if (tc.length() == 0) {
80                     isBounded = false;
81                 } else {
82                     isBounded = true;
83                     size = tc.length();
84                 }
85                 content = tc.content_type();
86             } else if (tc.kind().value() == TCKind._tk_alias) {
87                 id = tc.id();
88                 type = id;
89                 TypeCode JavaDoc stc = tc.content_type();
90                 while (stc.kind().value() == TCKind._tk_alias) {
91                     stc = stc.content_type();
92                 }
93                 if (stc.kind().value() != TCKind._tk_sequence) {
94                     throw new RuntimeException JavaDoc("Sequence type expected");
95                 }
96                 if (stc.length() == 0) {
97                     isBounded = false;
98                 } else {
99                     isBounded = true;
100                     size = stc.length();
101                 }
102                 content = stc.content_type();
103             } else {
104                 throw new RuntimeException JavaDoc("Sequence type expected");
105             }
106             removeAllChildren();
107             if (size == 0) {
108                 isBounded = false;
109                 size = 1;
110                 value = "1";
111                 // for unbound sequences create one default element
112
IdlNode m = create(content);
113                 m.field = "[0]";
114                 add(m);
115             } else {
116                 isBounded = true;
117                 value = "" + size;
118                 for (int i = 0; i < size; i++) {
119                     IdlNode m = create(content);
120                     m.field = "[" + i + "]";
121                     add(m);
122                 }
123             }
124         } catch (Exception JavaDoc e) {
125             e.printStackTrace();
126         }
127     }
128
129     protected void setNode(Any JavaDoc any) {
130         try {
131             setNode(any.type());
132             org.omg.CORBA.portable.InputStream JavaDoc in = any.create_input_stream();
133             if (isBounded) {
134                 int receivedSize = in.read_ulong();
135                 if (receivedSize > size) {
136                     throw new RuntimeException JavaDoc("Unexpected sequence size " + receivedSize + ". " + id + " has a maximum size of " + size + ".");
137                 }
138             } else {
139                 size = in.read_ulong();
140             }
141             removeAllChildren();
142             for (int i = 0; i < size; i++) {
143                 Any JavaDoc memberAny = orb.create_any();
144                 memberAny.read_value(in, content);
145                 IdlNode m = create(memberAny);
146                 m.field = "[" + i + "]";
147                 add(m);
148             }
149             value = "" + size;
150         } catch (Exception JavaDoc e) {
151             e.printStackTrace();
152         }
153     }
154     
155     /**
156      * Tests if the sequence length is bound to a maximum value.
157      *
158      * @return True if the sequence length is bound to a maximum value, false otherwise.
159      */

160     public boolean isBounded() {
161         return isBounded;
162     }
163     
164     /**
165      * Returns the number of sequence elements.
166      *
167      * @return The number of sequence elements.
168      */

169     public int getSize() {
170         return size;
171     }
172     
173     /**
174      * Sets the number of sequence elements.
175      *
176      * @param v The number of sequence elements.
177      */

178      public void setValue(String JavaDoc v) {
179         try {
180             setSize(Integer.parseInt(v));
181         } catch (Exception JavaDoc e) {
182             e.printStackTrace();
183         }
184     }
185
186     /**
187      * Sets the number of sequence elements.
188      *
189      * @param s The number of sequence elements.
190      */

191     public void setSize(int s) {
192         try {
193             if (isBounded && s > tc.length()) {
194                 throw new RuntimeException JavaDoc("Unexpected sequence size " + s + ". " + id + " has a maximum size of " + tc.length() + ".");
195             }
196             removeAllChildren();
197             size = s;
198             value = "" + size;
199             for (int i = 0; i < size; i++) {
200                 IdlNode m = create(content);
201                 m.field = "[" + i + "]";
202                 add(m);
203             }
204         } catch (Exception JavaDoc e) {
205             e.printStackTrace();
206         }
207     }
208
209     /**
210      * Returns the current value as a CORBA Any value.
211      *
212      * @return The current value as a CORBA Any.
213      */

214     public Any JavaDoc toAny() {
215         try {
216             Any JavaDoc any = orb.create_any();
217             any.type(tc);
218             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
219             out.write_ulong(size);
220             for (int i = 0; i < size; i++) {
221                 ((IdlNode)getChildAt(i)).toAny().write_value(out);
222             }
223             any.read_value(out.create_input_stream(), tc);
224             return any;
225         } catch (Exception JavaDoc e) {
226             e.printStackTrace();
227         }
228         return null;
229     }
230
231     /**
232      * Writes the value to a CORBA outputstream.
233      *
234      * @param is The outputstream to write to.
235      */

236     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
237         try {
238             for (int i = 0; i < size; i++) {
239                 ((IdlNode)getChildAt(i)).write(os);
240             }
241         } catch (Exception JavaDoc e) {
242             e.printStackTrace();
243         }
244     }
245
246     /**
247      * Reads the value from a CORBA inputstream.
248      *
249      * @param is The inputstream to read from.
250      */

251     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
252         try {
253             for (int i = 0; i < size; i++) {
254                 ((IdlNode)getChildAt(i)).read(is);
255             }
256         } catch (Exception JavaDoc e) {
257             e.printStackTrace();
258         }
259     }
260     
261     // XML section
262

263     public IdlSequence(String JavaDoc xml) {
264         this(XmlNode.getNode(xml));
265     }
266
267     IdlSequence(Node JavaDoc n) {
268         this(null, n);
269     }
270     
271     IdlSequence(org.omg.CORBA.TypeCode JavaDoc t, Node JavaDoc n) {
272         this();
273         try {
274             id = XmlNode.getId(n);
275             if (!id.equals("")) {
276                 t = XmlNode.type(id);
277             }
278             setNode(t);
279             
280             if (tc.kind().value() == TCKind._tk_alias) {
281                 org.omg.CORBA.TypeCode JavaDoc content = tc.content_type().content_type();
282                 size = tc.content_type().length();
283             } else {
284                 org.omg.CORBA.TypeCode JavaDoc content = tc.content_type();
285                 size = tc.length();
286             }
287             int length = XmlNode.getLength(n);
288
289             if (size != 0 && size < length) {
290                 throw new RuntimeException JavaDoc("This sequence length has a maximum of " + size + " elements.");
291             }
292
293             Node JavaDoc[] nodes = XmlNode.childElements(n);
294     
295             if (nodes.length != length) {
296                 throw new RuntimeException JavaDoc("Number of sequence elements does not match " + length + ".");
297             }
298             removeAllChildren();
299             for (int i = 0; i < length; i++) {
300                 IdlNode m = XmlNode.getIdlNode(content, nodes[i]);
301                 m.setField(field + "[" + i + "]");
302                 add(m);
303             }
304             value = "" + length;
305         } catch (Exception JavaDoc e) {
306 e.printStackTrace();
307 // throw new RuntimeException(e.toString());
308
}
309     }
310
311     /**
312      * Write the current value to an IdlWriter object.
313      *
314      * @param w The IdlWriter object to write the current value to.
315      */

316     public void write(IdlWriter w) {
317         write(this, w);
318     }
319
320     public static void write(IdlSequence n, IdlWriter w) {
321         w.write_start_sequence(n.getId(), n.getSize());
322         for(int i = 0; i < n.getChildCount(); i++) {
323             XmlNode.write((IdlNode)n.getChildAt(i), w);
324         }
325         w.write_end_sequence();
326     }
327 }
Popular Tags