KickJava   Java API By Example, From Geeks To Geeks.

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


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 IdlChar represents an CORBA IDL char value. Instances are created through one
34  * of the create() factory methods in IdlNode.
35  * An IdlChar 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 IdlChar extends IdlNode implements IdlWritable {
40     protected IdlChar() {
41         setUserObject(this);
42         type = "char";
43         value = " ";
44         tc = orb.get_primitive_tc(TCKind.tk_char);
45     }
46
47     protected IdlChar(Any JavaDoc any) {
48         this();
49         setNode(any);
50     }
51         
52     protected IdlChar(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_char();
61         } catch (Exception JavaDoc e) {
62             e.printStackTrace();
63         }
64     }
65
66     public void setValue(String JavaDoc v) {
67         if (v == null || v.equals("")) {
68             value = " ";
69         } else {
70             value = v;
71         }
72     }
73     
74     /**
75      * Writes the value to a CORBA outputstream.
76      *
77      * @param is The outputstream to write to.
78      */

79     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
80         try {
81             os.write_char(value.charAt(0));
82         } catch (Throwable JavaDoc e) {
83             e.printStackTrace();
84         }
85     }
86
87     /**
88      * Reads the value from a CORBA inputstream.
89      *
90      * @param is The inputstream to read from.
91      */

92     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
93         value = "" + is.read_char();
94     }
95     
96     /**
97      * Returns the current value as a CORBA Any value.
98      *
99      * @return The current value as a CORBA Any.
100      */

101     public Any JavaDoc toAny() {
102         try {
103             Any JavaDoc any = orb.create_any();
104             org.omg.CORBA.portable.OutputStream JavaDoc out = any.create_output_stream();
105             any.type(tc);
106             out.write_char(value.charAt(0));
107             any.read_value(out.create_input_stream(), tc);
108             return any;
109         } catch (Throwable JavaDoc e) {
110             e.printStackTrace();
111         }
112         return null;
113     }
114     
115     // XML section
116

117     /**
118      * Create an IdlChar from an Xml representation.
119      *
120      * XML format example:
121      * <pre>
122      * &lt;char&gt;C&lt;/char&gt;
123      * </pre>
124      *
125      * @param xml The XML string from which to create a ne IdlChar instance.
126      */

127     public IdlChar(String JavaDoc xml) {
128         this(XmlNode.getNode(xml));
129     }
130         
131     /**
132      * Construct an XmlNode from an Xml node.
133      * Package access only. Called from the Xml parser to contruct
134      * an Xml type tree from an Xml representation.
135      */

136     IdlChar(Node JavaDoc n) {
137         this();
138
139         if (n == null || !n.getNodeName().toUpperCase().equals("CHAR")) {
140             throw new RuntimeException JavaDoc("char expected");
141         }
142         setValue(XmlNode.getText(n));
143     }
144
145    
146     /**
147      * Write the current value to an IdlWriter object.
148      *
149      * @param w The IdlWriter object to write the current value to.
150      */

151     public void write(IdlWriter w) {
152         w.write_char(value);
153     }
154 }
Popular Tags