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.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 cached typecode value, computed once.
  70:    */
  71:   private static TypeCode typecode;
  72: 
  73:   /**
  74:    * The Wide String typecode.
  75:    */
  76:   private static final TypeCode twString =
  77:     OrbRestricted.Singleton.create_wstring_tc(0);
  78: 
  79:   /**
  80:    * Returns the String Value repository Id.
  81:    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
  82:    */
  83:   public String get_id()
  84:   {
  85:     return id;
  86:   }
  87: 
  88:   /**
  89:    * Returns the String Value repository Id.
  90:    * @return "IDL:omg.org/CORBA/WStringValue:1.0", always.
  91:    */
  92:   public static String id()
  93:   {
  94:     return id;
  95:   }
  96: 
  97:   /**
  98:    * Read the wide string value from the input stream.
  99:    *
 100:    * @param istream a stream to read from.
 101:    *
 102:    * @return a string (delegates to read_wstring()).
 103:    */
 104:   public Serializable read_value(InputStream istream)
 105:   {
 106:     return istream.read_wstring();
 107:   }
 108: 
 109:   /**
 110:    * Write the given wide string value into the output stream.
 111:    *
 112:    * @param ostream a stream to write into.
 113:    * @param a_string a string to write.
 114:    */
 115:   public void write_value(OutputStream ostream, Serializable a_string)
 116:   {
 117:     try
 118:       {
 119:         ostream.write_wstring((String) a_string);
 120:       }
 121:     catch (ClassCastException ex)
 122:       {
 123:         MARSHAL m = new MARSHAL("String expected");
 124:         m.minor = Minor.ClassCast;
 125:         throw m;
 126:       }
 127:   }
 128: 
 129:   /**
 130:    * Extract the wide string from the given Any. The operation
 131:    * requires Any to hold a String value and not a String.
 132:    *
 133:    * @param an_any an Any to extract from.
 134:    *
 135:    * @return the extracted string.
 136:    */
 137:   public static String extract(Any an_any)
 138:   {
 139:     if (an_any.type().equal(type()))
 140:       {
 141:         an_any.type(twString);
 142:         return an_any.extract_wstring();
 143:       }
 144:     else
 145:       {
 146:         BAD_OPERATION bad = new BAD_OPERATION("WString value type expected");
 147:         bad.minor = Minor.Any;
 148:         throw bad;        
 149:       }
 150:   }
 151: 
 152:   /**
 153:    * Insert the wide string into the given Any. After the operation,
 154:    * the Any will have a Wide String Value typecode and not a
 155:    * String or WString typecode.
 156:    *
 157:    * @param an_any an Any to insert into.
 158:    *
 159:    * @param that a string to insert.
 160:    */
 161:   public static void insert(Any an_any, String that)
 162:   {
 163:     an_any.insert_wstring(that);
 164:     an_any.type(type());
 165:   }
 166: 
 167:   /**
 168:    * Reads a wide string as a value type.
 169:    *
 170:    * @param in a stream to read value from.
 171:    */
 172:   public static String read(InputStream in)
 173:   {
 174:     return in.read_wstring();
 175:   }
 176: 
 177:   /**
 178:    * Create and return the value box typecode, named "WStringValue",
 179:    * with the content typecode being unbounded string.
 180:    */
 181:   public static TypeCode type()
 182:   {
 183:     if (typecode == null)
 184:       {
 185:         ORB orb = OrbRestricted.Singleton;
 186:         typecode =
 187:           orb.create_value_box_tc(id(), "WStringValue", twString);
 188:       }
 189:     return typecode;
 190:   }
 191: 
 192:   /**
 193:    * Writes a wide string as a value type.
 194:    *
 195:    * @param out a stream to write value into.
 196:    *
 197:    * @param a_string a string to write.
 198:    */
 199:   public static void write(OutputStream out, String a_string)
 200:   {
 201:     out.write_wstring(a_string);
 202:   }