StringValueHelper.java 5.3 KB

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