OSDN Git Service

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