Source for gnu.CORBA.TypeCodeHelper

   1: /* TypeCodeHelper.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 gnu.CORBA;
  40: 
  41: import gnu.CORBA.typecodes.FixedTypeCode;
  42: import gnu.CORBA.typecodes.GeneralTypeCode;
  43: import gnu.CORBA.typecodes.ArrayTypeCode;
  44: import gnu.CORBA.typecodes.PrimitiveTypeCode;
  45: import gnu.CORBA.typecodes.RecordTypeCode;
  46: import gnu.CORBA.typecodes.StringTypeCode;
  47: 
  48: import org.omg.CORBA.TCKind;
  49: import org.omg.CORBA.TypeCode;
  50: import org.omg.CORBA.TypeCodePackage.BadKind;
  51: import org.omg.CORBA.TypeCodePackage.Bounds;
  52: 
  53: /**
  54:  * Reads and writes the TypeCodes usind common data representation.
  55:  *
  56:  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  57:  */
  58: public class TypeCodeHelper
  59: {
  60:   /**
  61:    * Read the CORBA {@link TypeCode}. First, the TypeCode kind
  62:    * is read as four byte long. Then, if needed, the additional
  63:    * parameters are loaded following CORBA specification.
  64:    *
  65:    * @param in a stream to read from.
  66:    */
  67:   public static TypeCode read(org.omg.CORBA.portable.InputStream in)
  68:                        throws BadKind, Bounds
  69:   {
  70:     TCKind kind = TCKind.from_int(in.read_long());
  71:     TypeCode rt;
  72:     GeneralTypeCode g;
  73:     RecordTypeCode r;
  74:     RecordTypeCode.Field f;
  75:     StringTypeCode s;
  76:     int n;
  77: 
  78:     switch (kind.value())
  79:       {
  80:         case TCKind._tk_sequence :
  81:         case TCKind._tk_array :
  82: 
  83:           ArrayTypeCode p = new ArrayTypeCode(kind);
  84:           p.setLength(in.read_long());
  85:           rt = p;
  86:           break;
  87: 
  88:         case TCKind._tk_string :
  89:         case TCKind._tk_wstring :
  90:           s = new StringTypeCode(kind);
  91:           s.setLength(in.read_long());
  92:           rt = s;
  93:           break;
  94: 
  95:         case TCKind._tk_fixed :
  96: 
  97:           FixedTypeCode fx = new FixedTypeCode();
  98:           fx.setDigits(in.read_short());
  99:           fx.setScale(in.read_short());
 100:           rt = fx;
 101:           break;
 102: 
 103:         case TCKind._tk_objref :
 104:         case TCKind._tk_native :
 105:         case TCKind._tk_abstract_interface :
 106:           g = new GeneralTypeCode(kind);
 107:           g.setId(in.read_string());
 108:           g.setName(in.read_string());
 109:           rt = g;
 110:           break;
 111: 
 112:         case TCKind._tk_alias :
 113:         case TCKind._tk_value_box :
 114:           g = new GeneralTypeCode(kind);
 115:           g.setId(in.read_string());
 116:           g.setName(in.read_string());
 117:           g.setContentType(in.read_TypeCode());
 118:           rt = g;
 119:           break;
 120: 
 121:         case TCKind._tk_struct :
 122:         case TCKind._tk_except :
 123:           r = new RecordTypeCode(kind);
 124:           r.setId(in.read_string());
 125:           r.setName(in.read_string());
 126: 
 127:           n = in.read_long();
 128: 
 129:           for (int i = 0; i < n; i++)
 130:             {
 131:               f = r.field();
 132:               f.name = in.read_string();
 133:               f.type = in.read_TypeCode();
 134:             }
 135:           rt = r;
 136:           break;
 137: 
 138:         case TCKind._tk_enum :
 139:           r = new RecordTypeCode(kind);
 140:           r.setId(in.read_string());
 141:           r.setName(in.read_string());
 142: 
 143:           n = in.read_long();
 144: 
 145:           for (int i = 0; i < n; i++)
 146:             {
 147:               f = r.field();
 148:               f.name = in.read_string();
 149:             }
 150:           rt = r;
 151:           break;
 152: 
 153:         case TCKind._tk_union :
 154:           r = new RecordTypeCode(kind);
 155:           r.setId(in.read_string());
 156:           r.setName(in.read_string());
 157:           r.setDiscriminator_type(in.read_TypeCode());
 158:           r.setDefaultIndex(in.read_long());
 159: 
 160:           n = in.read_long();
 161: 
 162:           for (int i = 0; i < n; i++)
 163:             {
 164:               f = r.field();
 165:               f.label = in.read_any();
 166:               f.name = in.read_string();
 167:               f.type = in.read_TypeCode();
 168:             }
 169:           rt = r;
 170: 
 171:           break;
 172: 
 173:         case TCKind._tk_value :
 174:           r = new RecordTypeCode(kind);
 175:           r.setId(in.read_string());
 176:           r.setName(in.read_string());
 177:           r.setTypeModifier(in.read_short());
 178:           r.setConcreteBase_type(in.read_TypeCode());
 179: 
 180:           n = in.read_long();
 181: 
 182:           for (int i = 0; i < n; i++)
 183:             {
 184:               f = r.field();
 185:               f.name = in.read_string();
 186:               f.type = in.read_TypeCode();
 187:               f.visibility = in.read_short();
 188:             }
 189:           rt = r;
 190:           break;
 191: 
 192:         default :
 193:           rt = new PrimitiveTypeCode(kind);
 194:       }
 195:     return rt;
 196:   }
 197: 
 198:   /**
 199:    * Write the CORBA {@link TypeCode}. First, the TypeCode kind
 200:    * is written as four byte long. Then, if needed, the additional
 201:    * parameters are stored following CORBA specification.
 202:    *
 203:    * @param out a stream to write into.
 204:    * @param x a {@link TypeCode} to write.
 205:    */
 206:   public static void write(org.omg.CORBA.portable.OutputStream out, TypeCode x)
 207:                     throws BadKind, Bounds
 208:   {
 209:     out.write_long(x.kind().value());
 210: 
 211:     switch (x.kind().value())
 212:       {
 213:         case TCKind._tk_string :
 214:         case TCKind._tk_wstring :
 215:           out.write_long(x.length());
 216:           break;
 217: 
 218:         case TCKind._tk_sequence :
 219:         case TCKind._tk_array :
 220:           write(out, x.content_type());
 221:           out.write_long(x.length());
 222:           break;
 223: 
 224:         case TCKind._tk_fixed :
 225:           out.write_short(x.fixed_digits());
 226:           out.write_short(x.fixed_scale());
 227:           break;
 228: 
 229:         case TCKind._tk_objref :
 230:         case TCKind._tk_native :
 231:         case TCKind._tk_abstract_interface :
 232:           out.write_string(x.id());
 233:           out.write_string(x.name());
 234:           break;
 235: 
 236:         case TCKind._tk_alias :
 237:         case TCKind._tk_value_box :
 238:           out.write_string(x.id());
 239:           out.write_string(x.name());
 240:           write(out, x.content_type());
 241:           break;
 242: 
 243:         case TCKind._tk_struct :
 244:         case TCKind._tk_except :
 245:           out.write_string(x.id());
 246:           out.write_string(x.name());
 247: 
 248:           out.write_long(x.member_count());
 249: 
 250:           for (int i = 0; i < x.member_count(); i++)
 251:             {
 252:               out.write_string(x.member_name(i));
 253:               write(out, x.member_type(i));
 254:             }
 255:           break;
 256: 
 257:         case TCKind._tk_enum :
 258:           out.write_string(x.id());
 259:           out.write_string(x.name());
 260: 
 261:           out.write_long(x.member_count());
 262: 
 263:           for (int i = 0; i < x.member_count(); i++)
 264:             {
 265:               out.write_string(x.member_name(i));
 266:             }
 267:           break;
 268: 
 269:         case TCKind._tk_union :
 270:           out.write_string(x.id());
 271:           out.write_string(x.name());
 272: 
 273:           write(out, x.discriminator_type());
 274:           out.write_long(x.default_index());
 275: 
 276:           out.write_long(x.member_count());
 277: 
 278:           for (int i = 0; i < x.member_count(); i++)
 279:             {
 280:               out.write_any(x.member_label(i));
 281:               out.write_string(x.member_name(i));
 282:               write(out, x.member_type(i));
 283:             }
 284:           break;
 285: 
 286:         case TCKind._tk_value :
 287:           out.write_string(x.id());
 288:           out.write_string(x.name());
 289:           out.write_short(x.type_modifier());
 290:           write(out, x.concrete_base_type());
 291: 
 292:           out.write_long(x.member_count());
 293: 
 294:           for (int i = 0; i < x.member_count(); i++)
 295:             {
 296:               out.write_string(x.member_name(i));
 297:               write(out, x.member_type(i));
 298:               out.write_short(x.member_visibility(i));
 299:             }
 300:           break;
 301: 
 302:         default :}
 303:   }
 304: }