KickJava   Java API By Example, From Geeks To Geeks.

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


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.TypeCode JavaDoc;
29 import org.omg.CORBA.Any JavaDoc;
30 import org.omg.CORBA.TCKind JavaDoc;
31
32 /**
33  * The class IdlBoolean represents an CORBA IDL boolean value. Instances are created through one
34  * of the create() factory methods in IdlNode.
35  * An IdlBoolean object has no child nodes.
36  * Valid values are TRUE and FALSE.
37  *
38  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
39  */

40 public class IdlBoolean extends IdlNode implements IdlWritable {
41     protected IdlBoolean() {
42         setUserObject(this);
43         type = "boolean";
44         value = "FALSE";
45         tc = orb.get_primitive_tc(TCKind.tk_boolean);
46     }
47
48     protected IdlBoolean(Any JavaDoc any) {
49         this();
50         setNode(any);
51     }
52         
53     protected IdlBoolean(TypeCode JavaDoc tc) {
54         this();
55         setNode(tc);
56     }
57     
58     protected void setNode(Any JavaDoc any) {
59         try {
60             setNode(any.type());
61             value = ("" + any.create_input_stream().read_boolean()).toUpperCase();
62         } catch (Exception JavaDoc e) {
63             e.printStackTrace();
64         }
65     }
66
67     /**
68      * Returns the current value as a CORBA Any value.
69      *
70      * @return The current value as a CORBA Any.
71      */

72     public Any JavaDoc toAny() {
73         try {
74             Any JavaDoc any = orb.create_any();
75             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
76             any.type(tc);
77             out.write_boolean(value.equals("TRUE") ? true : false);
78             any.read_value(out.create_input_stream(), tc);
79             return any;
80         } catch (Exception JavaDoc e) {
81             e.printStackTrace();
82         }
83         return null;
84     }
85
86     /**
87      * Writes the value to a CORBA outputstream.
88      *
89      * @param is The outputstream to write to.
90      */

91     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
92         try {
93             os.write_boolean(value.equals("TRUE") ? true : false);
94         } catch (Exception JavaDoc e) {
95             e.printStackTrace();
96         }
97     }
98
99     /**
100      * Reads the value from a CORBA inputstream.
101      *
102      * @param is The inputstream to read from.
103      */

104     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
105         value = ("" + is.read_boolean()).toUpperCase();
106     }
107
108     // XML section
109

110     /**
111      * Creates new IdlBoolean object from an XML string.
112      *
113      * XML format example:
114      * <pre>
115      * &lt;boolean&gt;TRUE&lt;/boolean&gt;
116      * </pre>
117      *
118      * @param xml The XML string from which to create a ne IdlBoolean instance.
119      */

120     public IdlBoolean(String JavaDoc xml) {
121         this(XmlNode.getNode(xml));
122     }
123         
124     IdlBoolean(Node JavaDoc n) {
125         this();
126                        
127         if (n == null || !n.getNodeName().toUpperCase().equals("BOOLEAN")) {
128             throw new RuntimeException JavaDoc("boolean expected");
129         }
130         setValue(XmlNode.getText(n).toUpperCase());
131     }
132     
133     /**
134      * Write the current value to an IdlWriter object.
135      *
136      * @param w The IdlWriter object to write the current value to.
137      */

138     public void write(IdlWriter w) {
139         w.write_boolean(value);
140     }
141 }
Popular Tags