KickJava   Java API By Example, From Geeks To Geeks.

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


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

40 public class IdlString extends IdlNode implements IdlWritable {
41     protected IdlString() {
42         setUserObject(this);
43         type = "string";
44         value = "";
45         tc = orb.get_primitive_tc(TCKind.tk_string);
46     }
47
48     protected IdlString(Any JavaDoc any) {
49         this();
50         setNode(any);
51     }
52         
53     protected IdlString(TypeCode JavaDoc tc) {
54         this();
55         setNode(tc);
56     }
57     
58     protected void setNode(Any JavaDoc any) {
59         try {
60             setNode(any.type());
61             setValue(any.create_input_stream().read_string());
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_string(value);
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      * Assigns the node value from a string.
88      *
89      * @param v The string from which to convert the value.
90      */

91     public void setValue(String JavaDoc v) {
92         if (v == null) {
93             v = "";
94         }
95         value = v;
96     }
97
98     /**
99      * Writes the value to a CORBA outputstream.
100      *
101      * @param is The outputstream to write to.
102      */

103     public void write(org.omg.CORBA.portable.OutputStream JavaDoc os) {
104         os.write_string(value);
105     }
106
107     /**
108      * Reads the value from a CORBA inputstream.
109      *
110      * @param is The inputstream to read from.
111      */

112     public void read(org.omg.CORBA.portable.InputStream JavaDoc is) {
113         value = is.read_string();
114     }
115     
116     // XML section
117

118     /**
119      * Create an IdlString node from an Xml representation.
120      * XML format example:
121      * <pre>
122      * &lt;string&gt;3&lt;/string&gt;
123      * </pre>
124      */

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

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

148     public void write(IdlWriter w) {
149         w.write_string(value);
150     }
151 }
Popular Tags