KickJava   Java API By Example, From Geeks To Geeks.

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


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 IdlDouble represents an CORBA IDL double value. Instances are created through one
34  * of the create() factory methods in IdlNode.
35  * An IdDouble object has no child nodes.
36  *
37  * @author <a HREF="mailto:batteram@lucent.com">Harold Batteram</a> <b>Lucent Technologies</b>
38  */

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

71     public Any JavaDoc toAny() {
72         try {
73             Any JavaDoc any = orb.create_any();
74             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
75             any.type(tc);
76             out.write_double(Double.parseDouble(value));
77             any.read_value(out.create_input_stream(), tc);
78             return any;
79         } catch (Exception JavaDoc e) {
80             e.printStackTrace();
81         }
82         return null;
83     }
84
85     /**
86      * Assigns the node value from a string.
87      *
88      * @param v The string from which to convert the value.
89      */

90     public void setValue(String JavaDoc v) {
91         try {
92             Double.parseDouble(v);
93             value = v;
94         } catch (NumberFormatException JavaDoc e) {
95             throw new RuntimeException JavaDoc(e.toString());
96         }
97     }
98
99     /**
100      * Writes the value to a CORBA outputstream.
101      *
102      * @param is The outputstream to write to.
103      */

104     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
105         try {
106             os.write_double(Double.parseDouble(value));
107         } catch (Exception JavaDoc e) {
108             e.printStackTrace();
109         }
110     }
111
112     /**
113      * Reads the value from a CORBA inputstream.
114      *
115      * @param is The inputstream to read from.
116      */

117     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
118         value = "" + is.read_double();
119     }
120         
121     // XML section
122

123     /**
124      * Create an IdlDouble from an Xml representation.
125      *
126      * XML format example:
127      * <pre>
128      * &lt;double&gt;3.2&lt;/double&gt;
129      * </pre>
130      *
131      * @param xml The XML string from which to create an IdlDouble instance.
132      */

133     public IdlDouble(String JavaDoc xml) {
134         this(XmlNode.getNode(xml));
135     }
136     
137     /**
138      * Construct an XmlNode from an Xml node.
139      * Package access only. Called from the Xml parser to contruct
140      * an Xml type tree from an Xml representation.
141      */

142     IdlDouble(Node JavaDoc n) {
143         this();
144
145         if (n == null || !n.getNodeName().toUpperCase().equals("DOUBLE")) {
146             throw new RuntimeException JavaDoc("double expected");
147         }
148         setValue(XmlNode.getText(n));
149     }
150     
151     /**
152      * Write the current value to an IdlWriter object.
153      *
154      * @param w The IdlWriter object to write the current value to.
155      */

156     public void write(IdlWriter w) {
157         w.write_double(value);
158     }
159 }
Popular Tags