KickJava   Java API By Example, From Geeks To Geeks.

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


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 IdlException represents an CORBA IDL exception value. Instances are created through one
39  * of the create() factory methods in IdlNode.
40  * An IdlException object can have child nodes representing exception member fields.
41  *
42  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
43  */

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

133     public String JavaDoc[] getMembers() {
134         return members;
135     }
136                 
137     /**
138      * Returns the current value as a CORBA Any value.
139      *
140      * @return The current value as a CORBA Any.
141      */

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

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

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

191     /**
192      * Create an IdlException from an Xml representation.
193      *
194      * The XML format example
195      * <pre>
196      * &lt; exception id="IDL:example/MyException:1.0"&gt;
197      * &lt;member name="message"&gt;
198      * &lt;string&gt; error message &lt;/string&gt;
199      * &lt;/member&gt;
200      * &lt;/exception&gt;
201      * </pre>
202      * @param xml The XML string from which to create an IdlEnum instance.
203      */

204     public IdlException(String JavaDoc xml) {
205         this(XmlNode.getNode(xml));
206     }
207     
208     IdlException(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             for (int i = 0; i < nodes.length; i++) {
218                 if (nodes[i].getNodeName().toUpperCase().equals("MEMBER")) {
219                     xmlmembers[i] = XmlNode.getName(nodes[i]);
220                     IdlNode m = XmlNode.getIdlNode(tc.member_type(i), XmlNode.firstChildElement(nodes[i]));
221                     m.setField(xmlmembers[i]);
222                     add(m);
223                 } else {
224                     throw new RuntimeException JavaDoc("Member expected.");
225                 }
226             }
227             if (members.length >= xmlmembers.length) {
228                 for (int i = 0; i < members.length; i++) {
229                     if (i >= xmlmembers.length || !members[i].equals(xmlmembers[i])) {
230                         throw new RuntimeException JavaDoc("exception member " + members[i] + " is missing for " + id);
231                     }
232                 }
233             } else {
234                 for (int i = 0; i < xmlmembers.length; i++) {
235                     if (i >= members.length || !members[i].equals(xmlmembers[i])) {
236                         throw new RuntimeException JavaDoc(xmlmembers[i] + " is not a valid exception member in " + id);
237                     }
238                 }
239             }
240         } catch (Exception JavaDoc e) {
241             e.printStackTrace();
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(IdlException n, IdlWriter w) {
255         w.write_start_exception(n.getId());
256         String JavaDoc[] mbrs = n.getMembers();
257         for(int i = 0; i < n.getChildCount(); i++) {
258             w.write_start_member(mbrs[i]);
259             XmlNode.write((IdlNode)n.getChildAt(i), w);
260             w.write_end_member();
261         }
262         w.write_end_exception();
263     }
264 }
Popular Tags