KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.omg.CORBA.Any JavaDoc;
29 import org.omg.DynamicAny.*;
30
31 import java.util.*;
32 import java.lang.reflect.*;
33
34 /**
35  * The class IdlParameter represents a parameter of an operation on an CORBA IDL interface. Instances are created through one
36  * of the create() factory methods in IdlNode.
37  * An IdlParameter node has an direction attribute which has a value of "in", "inout", "out" or "return" according
38  * the parameter direction it represents.
39  * An IdlParameter node also has a name attribute corresponding to the name of the parameter.
40  * If the IdlParameter node respresnts a return value, the name attribute is "return".
41  * An IdlParameter object has one child corresponding to the parameter type.
42  *
43  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
44  */

45 public class IdlParameter extends IdlNode implements IdlWritable {
46     protected String JavaDoc name;
47     protected String JavaDoc dir;
48
49     protected IdlParameter() {
50         isLeaf = false;
51         isMutable = false;
52         setUserObject(this);
53         type = "parameter";
54     }
55     
56     protected IdlParameter(String JavaDoc dir, String JavaDoc name, Any JavaDoc any) {
57         this();
58         try {
59             this.dir = dir;
60             this.name = name;
61             IdlNode n = create(any);
62             n.field = name;
63             add(n);
64         } catch (Exception JavaDoc e) {
65             e.printStackTrace();
66         }
67     }
68
69     /**
70      * Returns the parameter name.
71      *
72      * @return The name of the parameter.
73      */

74     public String JavaDoc getName() {
75         return name;
76     }
77
78     /**
79      * Returns the parameter direction.
80      *
81      * Possible directions are in, inout, out and return.
82      *
83      * @return The direction of the parameter.
84      */

85     public String JavaDoc getDirection() {
86         return dir;
87     }
88
89     /**
90      * Returns a string representation of this parameter.
91      *
92      * @return A string representation of this parameter.
93      */

94     public String JavaDoc toString() {
95         if (dir.equals("return")) {
96             return dir;
97         } else {
98             return dir + " " + name;
99         }
100     }
101
102     /**
103      * Return the IdlNode child node for this parameter.
104      *
105      * @return The IdlNode child node for this parameter.
106      */

107     public IdlNode getParameter() {
108         return (IdlNode)getChildAt(0);
109     }
110     
111     /**
112      * Creates an IdlNode child parameter value from the content of the any.
113      *
114      * @param The any from which to create an IdlNode child.
115      */

116     public void setValue(Any JavaDoc any) {
117         removeAllChildren();
118         IdlNode n = create(any);
119         n.field = name;
120         add(n);
121     }
122
123     /**
124      * Writes the value to a CORBA outputstream.
125      *
126      * @param is The outputstream to write to.
127      */

128     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
129         ((IdlNode)getChildAt(0)).write(os);
130     }
131
132     /**
133      * Reads the value from a CORBA inputstream.
134      *
135      * @param is The inputstream to read from.
136      */

137     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
138         ((IdlNode)getChildAt(0)).read(is);
139     }
140         
141     // XML section
142

143     /**
144      * Create an IdlParameter from an Xml representation.
145      *
146      * The XML format example
147      * <pre>
148      * &lt;inout name="p2"&gt;
149      * &lt;long&gt;100&lt;/long&gt;
150      * &lt;/inout&gt;
151      * </pre>
152      *
153      * @param xml The XML string from which to create an IdlParameter instance.
154      */

155     public IdlParameter(String JavaDoc xml) {
156         this(XmlNode.getNode(xml));
157     }
158     
159     IdlParameter(Node JavaDoc n) {
160         this();
161         try {
162             if (n.getNodeName().toUpperCase().equals("IN")) {
163                 dir = "in";
164                 name = XmlNode.getName(n);
165             } else if (n.getNodeName().toUpperCase().equals("OUT")) {
166                 dir = "out";
167                 name = XmlNode.getName(n);
168             } else if (n.getNodeName().toUpperCase().equals("INOUT")) {
169                 dir = "inout";
170                 name = XmlNode.getName(n);
171             } else if (n.getNodeName().toUpperCase().equals("RETURN")) {
172                 name = "return";
173                 dir = "return";
174             }
175             IdlNode nn = XmlNode.getIdlNode(XmlNode.firstChildElement(n));
176             nn.setField(name);
177             add(nn);
178         } catch (Exception JavaDoc e) {
179             e.printStackTrace();
180             throw new RuntimeException JavaDoc(e.toString());
181         }
182     }
183
184     IdlParameter(String JavaDoc dir, String JavaDoc name, org.omg.CORBA.TypeCode JavaDoc tc) {
185         this();
186         try {
187             this.dir = dir;
188             this.name = name;
189             IdlNode n = create(tc);
190             n.setField(name);
191             add(n);
192          } catch (Exception JavaDoc e) {
193             e.printStackTrace();
194             throw new RuntimeException JavaDoc(e.toString());
195         }
196     }
197                 
198     /**
199      * Write the current value to an IdlWriter object.
200      *
201      * @param w The IdlWriter object to write the current value to.
202      */

203     public void write(IdlWriter w) {
204         write(this, w);
205     }
206
207     public static void write(IdlParameter n, IdlWriter w) {
208         if (n.getDirection().equals("in")) {
209             w.write_start_inpar(n.getName());
210             XmlNode.write((IdlNode)n.getParameter(), w);
211             w.write_end_inpar();
212         } else if (n.getDirection().equals("out")) {
213             w.write_start_outpar(n.getName());
214             XmlNode.write((IdlNode)n.getParameter(), w);
215             w.write_end_outpar();
216         } else if (n.getDirection().equals("inout")) {
217             w.write_start_inoutpar(n.getName());
218             XmlNode.write((IdlNode)n.getParameter(), w);
219             w.write_end_inoutpar();
220         } else if (n.getDirection().equals("return")) {
221             w.write_start_return();
222             XmlNode.write((IdlNode)n.getParameter(), w);
223             w.write_end_return();
224          }
225     }
226 }
Popular Tags