Source for org.omg.CORBA.WStringValueHelper

   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.Restricted_ORB;
  42: import gnu.CORBA.gnuAny;
  43: 
  44: import org.omg.CORBA.TypeCodePackage.BadKind;
  45: import org.omg.CORBA.portable.BoxedValueHelper;
  46: import org.omg.CORBA.portable.InputStream;
  47: import org.omg.CORBA.portable.OutputStream;
  48: 
  49: import java.io.Serializable;
  50: 
  51: /**
  52:  * Provides helper operations for the Wide String value type, treating a
  53:  * Wide String as a CORBA value type rather than as a primitive type. The OMG
  54:  * specification states this may be convenient in some specific
  55:  * cases. The typecode is different, but the reading/writing format in
  56:  * this implementation is the same as for the ordinary wide string. This is
  57:  * that Sun's IDL compiler (v1.4) would generate.
  58:  *
  59:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  60:  */
  61: public class WStringValueHelper
  62:   implements BoxedValueHelper
  63: {
  64:   /**
  65:    * The Wide String value helper repository Id.
  66:    */
  67:   private static final String id = "IDL:omg.org/CORBA/WStringValue:1.0";
  68: 
  69:   /**
  70:    * The cached typecode value, computed once.
  71:    */
  72:   private static TypeCode typecode;
  73: 
  74:   /**
  75:    * The Wide String typecode.
  76:    */
  77:   private static final TypeCode twString =
  78:     Restricted_ORB.Singleton.create_wstring_tc(0);
  79: 
  80:   /**
  81:    * Returns the String Value repository Id.
  82:    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
  83:    */
  84:   public String get_id()
  85:   {
  86:     return id;
  87:   }
  88: 
  89:   /**
  90:    * Returns the String Value repository Id.
  91:    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
  92:    */
  93:   public static String id()
  94:   {
  95:     return id;
  96:   }
  97: 
  98:   /**
  99:    * Read the wide string value from the input stream.
 100:    *
 101:    * @param istream a stream to read from.
 102:    *
 103:    * @return a string (delegates to read_wstring()).
 104:    */
 105:   public Serializable read_value(InputStream istream)
 106:   {
 107:     return istream.read_wstring();
 108:   }
 109: 
 110:   /**
 111:    * Write the given wide string value into the output stream.
 112:    *
 113:    * @param ostream a stream to write into.
 114:    * @param a_string a string to write.
 115:    */
 116:   public void write_value(OutputStream ostream, Serializable a_string)
 117:   {
 118:     try
 119:       {
 120:         ostream.write_wstring((String) a_string);
 121:       }
 122:     catch (ClassCastException ex)
 123:       {
 124:         throw new MARSHAL("String expected");
 125:       }
 126:   }
 127: 
 128:   /**
 129:    * Extract the wide string from the given Any. The operation
 130:    * requires Any to hold a String value and not a String.
 131:    *
 132:    * @param an_any an Any to extract from.
 133:    *
 134:    * @return the extracted string.
 135:    */
 136:   public static String extract(Any an_any)
 137:   {
 138:     if (an_any.type().equal(type()))
 139:       {
 140:         an_any.type(twString);
 141:         return an_any.extract_wstring();
 142:       }
 143:     else
 144:       throw new BAD_OPERATION("Contains not a wide string value type");
 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",
 174:    * with the content typecode being unbounded string.
 175:    */
 176:   public static TypeCode type()
 177:   {
 178:     if (typecode == null)
 179:       {
 180:         ORB orb = Restricted_ORB.Singleton;
 181:         typecode =
 182:           orb.create_value_box_tc(id(), "WStringValue", twString);
 183:       }
 184:     return typecode;
 185:   }
 186: 
 187:   /**
 188:    * Writes a wide string as a value type.
 189:    *
 190:    * @param out a stream to write value into.
 191:    *
 192:    * @param a_string a string to write.
 193:    */
 194:   public static void write(OutputStream out, String a_string)
 195:   {
 196:     out.write_wstring(a_string);
 197:   }