001    /* WStringValueHelper.java --
002       Copyright (C) 2005 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package org.omg.CORBA;
040    
041    import gnu.CORBA.Minor;
042    import gnu.CORBA.OrbRestricted;
043    
044    import org.omg.CORBA.portable.BoxedValueHelper;
045    import org.omg.CORBA.portable.InputStream;
046    import org.omg.CORBA.portable.OutputStream;
047    
048    import java.io.Serializable;
049    
050    /**
051     * Provides helper operations for the Wide String value type, treating a
052     * Wide String as a CORBA value type rather than as a primitive type. The OMG
053     * specification states this may be convenient in some specific
054     * cases. The typecode is different, but the reading/writing format in
055     * this implementation is the same as for the ordinary wide string. This is
056     * that Sun's IDL compiler (v1.4) would generate.
057     *
058     * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
059     */
060    public class WStringValueHelper
061      implements BoxedValueHelper
062    {
063      /**
064       * The Wide String value helper repository Id.
065       */
066      private static final String id = "IDL:omg.org/CORBA/WStringValue:1.0";
067    
068      /**
069       * The Wide String typecode.
070       */
071      private static final TypeCode twString =
072        OrbRestricted.Singleton.create_wstring_tc(0);
073    
074      /**
075       * Returns the String Value repository Id.
076       * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
077       */
078      public String get_id()
079      {
080        return id;
081      }
082    
083      /**
084       * Returns the String Value repository Id.
085       * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
086       */
087      public static String id()
088      {
089        return id;
090      }
091    
092      /**
093       * Read the wide string value from the input stream.
094       *
095       * @param istream a stream to read from.
096       *
097       * @return a string (delegates to read_wstring()).
098       */
099      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       */
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       */
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       */
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       */
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       */
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       */
189      public static void write(OutputStream out, String a_string)
190      {
191        out.write_wstring(a_string);
192      }
193    }