001/* StringValueHelper.java -- 002 Copyright (C) 2005 Free Software Foundation, Inc. 003 004This file is part of GNU Classpath. 005 006GNU Classpath is free software; you can redistribute it and/or modify 007it under the terms of the GNU General Public License as published by 008the Free Software Foundation; either version 2, or (at your option) 009any later version. 010 011GNU Classpath is distributed in the hope that it will be useful, but 012WITHOUT ANY WARRANTY; without even the implied warranty of 013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014General Public License for more details. 015 016You should have received a copy of the GNU General Public License 017along with GNU Classpath; see the file COPYING. If not, write to the 018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 01902110-1301 USA. 020 021Linking this library statically or dynamically with other modules is 022making a combined work based on this library. Thus, the terms and 023conditions of the GNU General Public License cover the whole 024combination. 025 026As a special exception, the copyright holders of this library give you 027permission to link this library with independent modules to produce an 028executable, regardless of the license terms of these independent 029modules, and to copy and distribute the resulting executable under 030terms of your choice, provided that you also meet, for each linked 031independent module, the terms and conditions of the license of that 032module. An independent module is a module which is not derived from 033or based on this library. If you modify this library, you may extend 034this exception to your version of the library, but you are not 035obligated to do so. If you do not wish to do so, delete this 036exception statement from your version. */ 037 038 039package org.omg.CORBA; 040 041import gnu.CORBA.Minor; 042import gnu.CORBA.OrbRestricted; 043 044import org.omg.CORBA.portable.BoxedValueHelper; 045import org.omg.CORBA.portable.InputStream; 046import org.omg.CORBA.portable.OutputStream; 047 048import java.io.Serializable; 049 050/** 051 * Provides helper operations for the String value type, treating a 052 * 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 string. This is 056 * that Sun's IDL compiler (v1.4) would generate. 057 * 058 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) 059 */ 060public class StringValueHelper 061 implements BoxedValueHelper 062{ 063 /** 064 * The String value helper repository Id. 065 */ 066 private static final String id = "IDL:omg.org/CORBA/StringValue:1.0"; 067 068 /** 069 * The String typecode. 070 */ 071 private static final TypeCode tString = 072 OrbRestricted.Singleton.create_string_tc(0); 073 074 /** 075 * Returns the String Value repository Id. 076 * @return "IDL:omg.org/CORBA/StringValue: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/StringValue:1.0", always. 086 */ 087 public static String id() 088 { 089 return id; 090 } 091 092 /** 093 * Read the string value from the input stream. 094 * 095 * @param istream a stream to read from. 096 * 097 * @return a string (delegates to read_string()). 098 */ 099 public Serializable read_value(InputStream istream) 100 { 101 return istream.read_string(); 102 } 103 104 /** 105 * Write the given 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_string((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 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(tString); 137 return an_any.extract_string(); 138 } 139 else 140 { 141 BAD_OPERATION bad = new BAD_OPERATION("String value type expected"); 142 bad.minor = Minor.Any; 143 throw bad; 144 } 145 } 146 147 /** 148 * Insert the string into the given Any. After the operation, 149 * the Any will have a String Value typecode and not a 150 * String 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_string(that); 159 an_any.type(type()); 160 } 161 162 /** 163 * Reads a 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_string(); 170 } 171 172 /** 173 * Create and return the value box typecode, named "StringValue", 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(), "StringValue", tString); 180 } 181 182 /** 183 * Writes a 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_string(a_string); 192 } 193}