KickJava   Java API By Example, From Geeks To Geeks.

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


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 IdlFixed represents an CORBA IDL fixed value. Instances are created through one
34  * of the create() factory methods in IdlNode.
35  * An IdFixed 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 IdlFixed extends IdlNode implements IdlWritable {
40     protected IdlFixed() {
41         setUserObject(this);
42         type = "fixed";
43         value = "0";
44         tc = orb.get_primitive_tc(TCKind.tk_fixed);
45     }
46
47     protected IdlFixed(Any JavaDoc any) {
48         this();
49         setNode(any);
50     }
51         
52     protected IdlFixed(TypeCode JavaDoc tc) {
53         this();
54         setNode(tc);
55     }
56     
57     protected void setNode(Any JavaDoc any) {
58         try {
59             setNode(any.type());
60             java.math.BigDecimal JavaDoc d = any.create_input_stream().read_fixed();
61             value = d.toString();
62         } catch (Exception JavaDoc e) {
63             e.printStackTrace();
64         }
65     }
66
67     /**
68      * Writes the value to a CORBA outputstream.
69      *
70      * @param is The outputstream to write to.
71      */

72     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
73         try {
74             os.write_fixed(new java.math.BigDecimal JavaDoc(value));
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77         }
78     }
79
80     /**
81      * Reads the value from a CORBA inputstream.
82      *
83      * @param is The inputstream to read from.
84      */

85     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
86         try {
87             java.math.BigDecimal JavaDoc d = is.read_fixed();
88             value = d.toString();
89         } catch (Exception JavaDoc e) {
90             e.printStackTrace();
91         }
92     }
93     
94     /**
95      * Returns the current value as a CORBA Any value.
96      *
97      * @return The current value as a CORBA Any.
98      */

99     public Any JavaDoc toAny() {
100         try {
101             Any JavaDoc any = orb.create_any();
102             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
103             any.type(tc);
104             out.write_fixed(new java.math.BigDecimal JavaDoc(value));
105             any.read_value(out.create_input_stream(), tc);
106             return any;
107         } catch (Exception JavaDoc e) {
108             e.printStackTrace();
109         }
110         return null;
111     }
112
113     /**
114      * Assigns the node value from a string.
115      *
116      * @param v The string from which to convert the value.
117      */

118     public void setValue(String JavaDoc v) {
119         try {
120             java.math.BigDecimal JavaDoc bd = new java.math.BigDecimal JavaDoc(v);
121             value = v;
122         } catch (Exception JavaDoc e) {
123             throw new RuntimeException JavaDoc(e.toString());
124         }
125     }
126     
127     // XML section
128

129     /**
130      * Create an IdlFixed node from an Xml representation.
131      * XML format example:
132      * <pre>
133      * &lt;fixed&gt;3.2&lt;/fixed&gt;
134      * </pre>
135      */

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

145     IdlFixed(Node JavaDoc n) {
146         this();
147         if (n == null || !n.getNodeName().toUpperCase().equals("FIXED")) {
148             throw new RuntimeException JavaDoc("fixed expected");
149         }
150         setValue(XmlNode.getText(n));
151     }
152  
153     /**
154      * Writes an XML representation of this node.
155      *
156      * @param w The xml writer used to write the node value.
157      */

158     /**
159      * Write the current value to an IdlWriter object.
160      *
161      * @param w The IdlWriter object to write the current value to.
162      */

163     public void write(IdlWriter w) {
164         w.write_fixed(value);
165     }
166 }
Popular Tags