KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 import org.omg.CORBA.ORB JavaDoc;
33 import org.omg.CORBA.TypeCode JavaDoc;
34 import org.omg.CORBA.Any JavaDoc;
35 import org.omg.CORBA.TCKind JavaDoc;
36
37 /**
38  * The class IdlEnum represents an CORBA IDL enum value. Instances are created through one
39  * of the create() factory methods in IdlNode.
40  * An IdlEnum object has no child nodes.
41  * The valid enum values are derived from the typecode.
42  *
43  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
44  */

45 public class IdlEnum extends IdlNode implements IdlWritable {
46     protected String JavaDoc[] members;
47     protected int index;
48
49     protected IdlEnum() {
50         setUserObject(this);
51     }
52    
53     protected IdlEnum(Any JavaDoc any) {
54         this();
55         setNode(any);
56     }
57         
58     protected IdlEnum(TypeCode JavaDoc tc) {
59         this();
60         setNode(tc);
61     }
62
63     /**
64      * Assigns the node value from a string.
65      *
66      * @param v The string from which to convert the value.
67      */

68     public void setValue(String JavaDoc v) {
69         boolean found = false;
70         for (int i = 0; i < members.length; i++) {
71             if (members[i].equals(v)) {
72                 found = true;
73                 index = i;
74                 break;
75             }
76         }
77         if (!found) {
78             throw new RuntimeException JavaDoc(v + " is not a valid enumeration in " + id);
79         }
80         value = v;
81     }
82
83     protected void setNode(TypeCode JavaDoc tc) {
84         try {
85             this.tc = tc;
86             if (tc.kind().value() == TCKind._tk_enum) {
87                 id = tc.id();
88                 type = "enum " + id;
89                 value = tc.member_name(0);
90                 members = getMembers(tc);
91                 index = 0;
92             } else if (tc.kind().value() == TCKind._tk_alias) {
93                 id = tc.id();
94                 type = id;
95                 TypeCode JavaDoc stc = tc.content_type();
96                 while (stc.kind().value() == TCKind._tk_alias) {
97                     stc = stc.content_type();
98                 }
99                 if (stc.kind().value() != TCKind._tk_enum) {
100                     throw new RuntimeException JavaDoc("Enum type expected");
101                 }
102                 value = stc.member_name(0);
103                 members = getMembers(stc);
104                 index = 0;
105             } else {
106                 throw new RuntimeException JavaDoc("Enum type expected");
107             }
108         } catch (Exception JavaDoc e) {
109             e.printStackTrace();
110         }
111     }
112
113     protected void setNode(Any JavaDoc any) {
114         try {
115             setNode(any.type());
116             org.omg.CORBA.portable.InputStream JavaDoc in = any.create_input_stream();
117             index = in.read_long();
118             value = tc.member_name(index);
119         } catch (Exception JavaDoc e) {
120             e.printStackTrace();
121         }
122     }
123
124     /**
125      * Writes the value to a CORBA outputstream.
126      *
127      * @param is The outputstream to write to.
128      */

129     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
130         try {
131             os.write_long(index);
132         } catch (Exception JavaDoc e) {
133             e.printStackTrace();
134         }
135     }
136
137     /**
138      * Reads the value from a CORBA inputstream.
139      *
140      * @param is The inputstream to read from.
141      */

142     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
143         try {
144             index = is.read_long();
145             value = tc.member_name(index);
146         } catch (Exception JavaDoc e) {
147             e.printStackTrace();
148         }
149     }
150     
151     /**
152      * Returns the list of valid enumeration values as a String array.
153      *
154      * @return A String array with all valid enumeration values.
155      */

156     public String JavaDoc[] getEnums() {
157         return members;
158     }
159
160     /**
161      * Returns the index number of the current enum value.
162      *
163      * @return The index number of the current enum value.
164      */

165     public int getIndex() {
166         return index;
167     }
168
169     /**
170      * Sets the current enum value from the integer index number.
171      * Throws a RuntimeException if the given index number is out of range.
172      */

173     public void setIndex(int i) {
174         if (i < 0 || i >= members.length) {
175             throw new RuntimeException JavaDoc("Enum index out of range");
176         }
177         value = members[i];
178         index = i;
179     }
180
181     /**
182      * Returns the current value as a CORBA Any value.
183      *
184      * @return The current value as a CORBA Any.
185      */

186     public Any JavaDoc toAny() {
187         try {
188             Any JavaDoc any = orb.create_any();
189             any.type(tc);
190             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
191             out.write_long(index);
192             any.read_value(out.create_input_stream(), tc);
193             return any;
194         } catch (Exception JavaDoc e) {
195             e.printStackTrace();
196         }
197         return null;
198     }
199
200     private String JavaDoc[] getMembers(TypeCode JavaDoc tc) {
201         try {
202             String JavaDoc[] members = new String JavaDoc[tc.member_count()];
203             for (int i = 0; i < members.length; i++) {
204                 members[i] = tc.member_name(i);
205             }
206             return members;
207         } catch (Exception JavaDoc e) {
208             e.printStackTrace();
209         }
210         return null;
211     }
212     
213     // XML section
214

215     /**
216      * Create an IdlEnum from an Xml representation.
217      *
218      * XML format example:
219      * <pre>
220      * &lt;enum&gt; id="IDL:example/MyEnum" value="blue" &lt;/enum&gt;
221      * </pre>
222      *
223      * @param xml The XML string from which to create an IdlEnum instance.
224      */

225     public IdlEnum(String JavaDoc xml) {
226         this(XmlNode.getNode(xml));
227     }
228             
229     IdlEnum(Node JavaDoc n) {
230         this();
231         id = XmlNode.getId(n);
232         setNode(XmlNode.type(id));
233         setValue(XmlNode.getText(n));
234     }
235
236     /**
237      * Write the current value to an IdlWriter object.
238      *
239      * @param w The IdlWriter object to write the current value to.
240      */

241     public void write(IdlWriter w) {
242         w.write_enum(id, value);
243     }
244 }
Popular Tags