1 /* WStringValueHelper.java --
2    Copyright (C) 2005 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package org.omg.CORBA;
40 
41 import gnu.CORBA.Minor;
42 import gnu.CORBA.OrbRestricted;
43 
44 import org.omg.CORBA.portable.BoxedValueHelper;
45 import org.omg.CORBA.portable.InputStream;
46 import org.omg.CORBA.portable.OutputStream;
47 
48 import java.io.Serializable;
49 
50 /**
51  * Provides helper operations for the Wide String value type, treating a
52  * Wide String as a CORBA value type rather than as a primitive type. The OMG
53  * specification states this may be convenient in some specific
54  * cases. The typecode is different, but the reading/writing format in
55  * this implementation is the same as for the ordinary wide string. This is
56  * that Sun's IDL compiler (v1.4) would generate.
57  *
58  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59  */
60 public class WStringValueHelper
61   implements BoxedValueHelper
62 {
63   /**
64    * The Wide String value helper repository Id.
65    */
66   private static final String id = "IDL:omg.org/CORBA/WStringValue:1.0";
67 
68   /**
69    * The Wide String typecode.
70    */
71   private static final TypeCode twString =
72     OrbRestricted.Singleton.create_wstring_tc(0);
73 
74   /**
75    * Returns the String Value repository Id.
76    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
77    */
get_id()78   public String get_id()
79   {
80     return id;
81   }
82 
83   /**
84    * Returns the String Value repository Id.
85    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
86    */
id()87   public static String id()
88   {
89     return id;
90   }
91 
92   /**
93    * Read the wide string value from the input stream.
94    *
95    * @param istream a stream to read from.
96    *
97    * @return a string (delegates to read_wstring()).
98    */
read_value(InputStream istream)99   public Serializable read_value(InputStream istream)
100   {
101     return istream.read_wstring();
102   }
103 
104   /**
105    * Write the given wide string value into the output stream.
106    *
107    * @param ostream a stream to write into.
108    * @param a_string a string to write.
109    */
write_value(OutputStream ostream, Serializable a_string)110   public void write_value(OutputStream ostream, Serializable a_string)
111   {
112     try
113       {
114         ostream.write_wstring((String) a_string);
115       }
116     catch (ClassCastException ex)
117       {
118         MARSHAL m = new MARSHAL("String expected");
119         m.minor = Minor.ClassCast;
120         throw m;
121       }
122   }
123 
124   /**
125    * Extract the wide string from the given Any. The operation
126    * requires Any to hold a String value and not a String.
127    *
128    * @param an_any an Any to extract from.
129    *
130    * @return the extracted string.
131    */
extract(Any an_any)132   public static String extract(Any an_any)
133   {
134     if (an_any.type().equal(type()))
135       {
136         an_any.type(twString);
137         return an_any.extract_wstring();
138       }
139     else
140       {
141         BAD_OPERATION bad = new BAD_OPERATION("WString value type expected");
142         bad.minor = Minor.Any;
143         throw bad;
144       }
145   }
146 
147   /**
148    * Insert the wide string into the given Any. After the operation,
149    * the Any will have a Wide String Value typecode and not a
150    * String or WString typecode.
151    *
152    * @param an_any an Any to insert into.
153    *
154    * @param that a string to insert.
155    */
insert(Any an_any, String that)156   public static void insert(Any an_any, String that)
157   {
158     an_any.insert_wstring(that);
159     an_any.type(type());
160   }
161 
162   /**
163    * Reads a wide string as a value type.
164    *
165    * @param in a stream to read value from.
166    */
read(InputStream in)167   public static String read(InputStream in)
168   {
169     return in.read_wstring();
170   }
171 
172   /**
173    * Create and return the value box typecode, named "WStringValue", with the
174    * content typecode being unbounded string.
175    */
type()176   public static TypeCode type()
177   {
178     ORB orb = OrbRestricted.Singleton;
179     return orb.create_value_box_tc(id(), "WStringValue", twString);
180   }
181 
182   /**
183    * Writes a wide string as a value type.
184    *
185    * @param out a stream to write value into.
186    *
187    * @param a_string a string to write.
188    */
write(OutputStream out, String a_string)189   public static void write(OutputStream out, String a_string)
190   {
191     out.write_wstring(a_string);
192   }
193 }
194