KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > TypeCode


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop;
20
21 import org.omg.CORBA.TCKind;
22 import org.omg.CORBA.TypeCodePackage.BadKind;
23 import org.omg.CORBA.TypeCodePackage.Bounds;
24
25 /**
26  ** An implementation of CORBA 'TypeCode' for use with RMI-IIOP.
27  **/

28 public class TypeCode extends org.omg.CORBA.TypeCode
29 {
30     // -----------------------------------------------------------------------
31
// public data
32
// -----------------------------------------------------------------------
33

34     public static final TypeCode NULL = new TypeCode(TCKind.tk_null);
35     public static final TypeCode VOID = new TypeCode(TCKind.tk_void);
36     public static final TypeCode ANY = new TypeCode(TCKind.tk_any);
37     public static final TypeCode BOOLEAN = new TypeCode(TCKind.tk_boolean);
38     public static final TypeCode CHAR = new TypeCode(TCKind.tk_char);
39     public static final TypeCode WCHAR = new TypeCode(TCKind.tk_wchar);
40     public static final TypeCode OCTET = new TypeCode(TCKind.tk_octet);
41     public static final TypeCode SHORT = new TypeCode(TCKind.tk_short);
42     public static final TypeCode USHORT = new TypeCode(TCKind.tk_ushort);
43     public static final TypeCode LONG = new TypeCode(TCKind.tk_long);
44     public static final TypeCode ULONG = new TypeCode(TCKind.tk_ulong);
45     public static final TypeCode LONGLONG = new TypeCode(TCKind.tk_longlong);
46     public static final TypeCode ULONGLONG = new TypeCode(TCKind.tk_ulonglong);
47     public static final TypeCode FLOAT = new TypeCode(TCKind.tk_float);
48     public static final TypeCode DOUBLE = new TypeCode(TCKind.tk_double);
49     public static final TypeCode LONGDOUBLE = new TypeCode(TCKind.tk_longdouble);
50     public static final TypeCode STRING = new TypeCode(TCKind.tk_string);
51     public static final TypeCode WSTRING = new TypeCode(TCKind.tk_wstring);
52     public static final TypeCode OBJREF = new TypeCode(TCKind.tk_objref);
53     public static final TypeCode TYPECODE = new TypeCode(TCKind.tk_TypeCode);
54
55     // -----------------------------------------------------------------------
56
// private data
57
// -----------------------------------------------------------------------
58

59     private TCKind _kind;
60
61     private String _name;
62
63     private String _type;
64
65     private String _id;
66
67     private String _label;
68
69     // content type, discriminator type, concrete base type,
70
// or other TypeCode for indirection.
71
private org.omg.CORBA.TypeCode _ref;
72
73     private String[] _member_name;
74
75     private org.omg.CORBA.TypeCode[] _member_type;
76
77     private org.omg.CORBA.Any[] _member_label;
78
79     private short[] _member_visibility;
80
81     private int _default;
82
83     private int _length;
84
85     private short _digits;
86
87     private short _scale;
88
89     private short _type_modifier;
90
91     private boolean _indirection;
92
93     // -----------------------------------------------------------------------
94
// public methods
95
// -----------------------------------------------------------------------
96
/**
97      *
98      * @param kind
99      */

100     public TypeCode(TCKind kind)
101     {
102         _kind = kind;
103         _default = -1;
104         if (kind.value() == TCKind._tk_objref)
105         {
106             _type = "Object";
107         }
108     }
109
110     /**
111      *
112      * @param tc
113      * @return
114      */

115     public boolean equal(org.omg.CORBA.TypeCode tc)
116     {
117         if (_indirection)
118         {
119             return _ref.equal(tc);
120         }
121         try
122         {
123             int tk = _kind.value();
124             if (tk != tc.kind().value())
125             {
126                 return false;
127             }
128             // TODO: compare id()
129
if (_member_name != null)
130             {
131                 int n = _member_name.length;
132                 if (n != tc.member_count())
133                 {
134                     return false;
135                 }
136                 for (int i = 0; i < n; i++)
137                 {
138                     if (! equalIfNotEmpty(member_name(i), tc.member_name(i)))
139                     {
140                         return false;
141                     }
142                     if (! member_type(i).equal(tc.member_type(i)))
143                     {
144                         return false;
145                     }
146                 }
147             }
148             if (tk == TCKind._tk_union)
149             {
150                 if (! discriminator_type().equal(tc.discriminator_type()))
151                 {
152                     return false;
153                 }
154                 int n = _member_name.length;
155                 for (int i = 0; i < n; i++)
156                 {
157                     if (! member_label(i).equal(tc.member_label(i)))
158                     {
159                         return false;
160                     }
161                 }
162             }
163             if (tk == TCKind._tk_array
164             || tk == TCKind._tk_sequence
165                 || tk == TCKind._tk_string
166                 || tk == TCKind._tk_wstring)
167             {
168                 if (length() != tc.length())
169                 {
170                     return false;
171                 }
172             }
173             if (tk == TCKind._tk_alias
174             || tk == TCKind._tk_array
175                 || tk == TCKind._tk_sequence)
176             {
177                 if (! content_type().equal(tc.content_type()))
178                 {
179                     return false;
180                 }
181             }
182             return true;
183         }
184         catch (org.omg.CORBA.TypeCodePackage.BadKind ex)
185         {
186             throw new org.omg.CORBA.UNKNOWN(ex.toString());
187         }
188         catch (org.omg.CORBA.TypeCodePackage.Bounds ex)
189         {
190             throw new org.omg.CORBA.UNKNOWN(ex.toString());
191         }
192     }
193
194     /**
195      *
196      * @param tc
197      * @return
198      */

199     public boolean equivalent
200         (org.omg.CORBA.TypeCode tc)
201     {
202         throw new org.omg.CORBA.NO_IMPLEMENT();
203     }
204
205     /**
206      *
207      * @return
208      */

209     public org.omg.CORBA.TypeCode get_compact_typecode()
210     {
211         throw new org.omg.CORBA.NO_IMPLEMENT();
212     }
213
214     /**
215      *
216      * @param a
217      * @param b
218      * @return
219      */

220     private boolean equalIfNotEmpty(String a, String b)
221     {
222         if (a.length() == 0 || b.length() == 0)
223         {
224             return true;
225         }
226         else
227         {
228             return a.equals(b);
229         }
230     }
231
232     /**
233      *
234      * @return
235      */

236     public TCKind kind()
237     {
238         if (_indirection)
239         {
240             return _ref.kind();
241         }
242         return _kind;
243     }
244
245     /**
246      *
247      * @return
248      * @throws BadKind
249      */

250     public String id()
251         throws BadKind
252     {
253         if (_indirection)
254         {
255             return _ref.id();
256         }
257         if (_id != null)
258         {
259             return _id;
260         }
261         if (_type != null && _type.equals("Object"))
262         {
263             return "";
264         }
265         return default_id();
266     }
267
268     // Sybase-internal
269
/**
270      *
271      * @param id
272      */

273     public void id(String id)
274     {
275         if (! id.equals(""))
276         {
277             _id = id;
278             if (id.startsWith("IDL:") && id.endsWith(":1.0"))
279             {
280                 // Infer _type field from standard IDL format _id
281
id = id.substring(4, id.length() - 4);
282                 if (id.startsWith("omg.org/"))
283                 {
284                     id = id.substring(8);
285                 }
286                 _type = "";
287                 for (;;)
288                 {
289                     int slash = id.indexOf('/');
290                     if (slash == -1)
291                     {
292                         break;
293                     }
294                     _type = _type + id.substring(0, slash) + "::";
295                     id = id.substring(slash + 1);
296                 }
297                 _type = _type + id;
298             }
299         }
300     }
301
302     /**
303      *
304      * @return
305      * @throws BadKind
306      */

307     public String name()
308         throws BadKind
309     {
310         if (_indirection)
311         {
312             return _ref.name();
313         }
314         /* TODO?
315         if (_name == null)
316         {
317             _name = (String)_names.get(new Integer(_kind.value()));
318         }
319         */

320         if (_name == null)
321         {
322             throw new BadKind();
323         }
324         return _name;
325     }
326
327     // Sybase-internal
328
/**
329      *
330      * @param name
331      */

332     public void name(String name)
333     {
334         _name = name;
335     }
336
337     /**
338      *
339      * @return
340      * @throws BadKind
341      */

342     public int member_count()
343         throws BadKind
344     {
345         if (_indirection)
346         {
347             return _ref.member_count();
348         }
349         if (_member_name == null)
350         {
351             throw new BadKind();
352         }
353         return _member_name.length;
354     }
355
356     // Sybase-internal
357
/**
358      *
359      * @param count
360      */

361     public void member_count(int count)
362     {
363         _member_name = new String[count];
364         _member_type = new org.omg.CORBA.TypeCode[count];
365         if (_kind.value() == TCKind._tk_union)
366         {
367             _member_label = new org.omg.CORBA.Any[count];
368         }
369         if (_kind.value() == TCKind._tk_value)
370         {
371             _member_visibility = new short[count];
372         }
373     }
374
375     /**
376      *
377      * @param index
378      * @return
379      * @throws BadKind
380      * @throws Bounds
381      */

382     public String member_name(int index)
383         throws BadKind, Bounds
384     {
385         if (_indirection)
386         {
387             return _ref.member_name(index);
388         }
389         if (_member_name == null)
390         {
391             throw new BadKind();
392         }
393         if (index < 0 || index >= _member_name.length)
394         {
395             throw new Bounds();
396         }
397         return _member_name[index];
398     }
399
400     // Sybase-internal
401
/**
402      *
403      * @param index
404      * @param name
405      */

406     public void member_name(int index, String name)
407     {
408         _member_name[index] = name;
409     }
410
411     /**
412      *
413      * @param index
414      * @return
415      * @throws BadKind
416      * @throws Bounds
417      */

418     public org.omg.CORBA.TypeCode member_type(int index)
419         throws BadKind, Bounds
420     {
421         if (_indirection)
422         {
423             return _ref.member_type(index);
424         }
425         if (_member_type == null)
426         {
427             throw new BadKind();
428         }
429         if (index < 0 || index >= _member_type.length)
430         {
431             throw new Bounds();
432         }
433         return _member_type[index];
434     }
435
436     // Sybase-internal
437
/**
438      *
439      * @param index
440      * @param type
441      */

442     public void member_type(int index, org.omg.CORBA.TypeCode type)
443     {
444         _member_type[index] = type;
445     }
446
447     /**
448      *
449      * @param index
450      * @return
451      * @throws BadKind
452      * @throws Bounds
453      */

454     public org.omg.CORBA.Any member_label(int index)
455         throws BadKind, Bounds
456     {
457         if (_indirection)
458         {
459             return _ref.member_label(index);
460         }
461         if (_member_label == null)
462         {
463             throw new BadKind();
464         }
465         if (index < 0 || index >= _member_label.length)
466         {
467             throw new Bounds();
468         }
469         return _member_label[index];
470     }
471
472     // Sybase-internal
473
/**
474      *
475      * @param index
476      * @param label
477      */

478     public void member_label(int index, org.omg.CORBA.Any label)
479     {
480         _member_label[index] = label;
481     }
482
483     /**
484      *
485      * @return
486      * @throws BadKind
487      */

488     public org.omg.CORBA.TypeCode discriminator_type()
489         throws BadKind
490     {
491         if (_indirection)
492         {
493             return _ref.discriminator_type();
494         }
495         if (_ref == null
496         || _kind.value() != TCKind._tk_union)
497         {
498             throw new BadKind();
499         }
500         return _ref;
501     }
502
503     // Sybase-internal
504
/**
505      *
506      * @param disc
507      */

508     public void discriminator_type(org.omg.CORBA.TypeCode disc)
509     {
510         _ref = disc;
511     }
512
513     /**
514      *
515      * @return
516      * @throws BadKind
517      */

518     public int default_index()
519         throws BadKind
520     {
521         if (_indirection)
522         {
523             return _ref.default_index();
524         }
525         if (_kind.value() != TCKind._tk_union)
526         {
527             throw new BadKind();
528         }
529         return _default;
530     }
531
532     // Sybase-internal
533
/**
534      *
535      * @param index
536      */

537     public void default_index(int index)
538     {
539         _default = index;
540         if (index != -1)
541         {
542             // We must store the member label for the discriminator type
543
// as some legal value, so we use zero.
544
_member_label[index] = new Any(_ref, "0");
545         }
546     }
547
548     /**
549      *
550      * @return
551      * @throws BadKind
552      */

553     public int length()
554         throws BadKind
555     {
556         if (_indirection)
557         {
558             return _ref.length();
559         }
560         int tk = _kind.value();
561         if (tk != TCKind._tk_string && tk != TCKind._tk_wstring
562         && tk != TCKind._tk_sequence && tk != TCKind._tk_array)
563         {
564             throw new BadKind();
565         }
566         return _length;
567     }
568
569     // Sybase-internal
570
/**
571      *
572      * @param length
573      */

574     public void length(int length)
575     {
576         _length = length;
577     }
578
579     /**
580      *
581      * @return
582      * @throws BadKind
583      */

584     public org.omg.CORBA.TypeCode content_type()
585         throws BadKind
586     {
587         if (_indirection)
588         {
589             return _ref.content_type();
590         }
591         int tk = _kind.value();
592         if (_ref == null
593         || (tk != TCKind._tk_alias
594             && tk != TCKind._tk_array
595             && tk != TCKind._tk_sequence
596             && tk != TCKind._tk_value_box))
597         {
598             throw new BadKind();
599         }
600         return _ref;
601     }
602
603     // Sybase-internal
604
/**
605      *
606      * @param type
607      */

608     public void content_type(org.omg.CORBA.TypeCode type)
609     {
610         _ref = type;
611     }
612
613     /**
614      *
615      * @return
616      * @throws BadKind
617      */

618     public short fixed_digits()
619         throws BadKind
620     {
621         if (_indirection)
622         {
623             return _ref.fixed_digits();
624         }
625         int tk = _kind.value();
626         if (tk != TCKind._tk_fixed)
627         {
628             throw new BadKind();
629         }
630         return _digits;
631     }
632
633     // Sybase-internal
634
/**
635      *
636      * @param digits
637      */

638     public void fixed_digits(short digits)
639     {
640         _digits = digits;
641     }
642
643     /**
644      *
645      * @return
646      * @throws BadKind
647      */

648     public short fixed_scale()
649         throws BadKind
650     {
651         if (_indirection)
652         {
653             return _ref.fixed_scale();
654         }
655         int tk = _kind.value();
656         if (tk != TCKind._tk_fixed)
657         {
658             throw new BadKind();
659         }
660         return _scale;
661     }
662
663     // Sybase-internal
664
/**
665      *
666      * @param scale
667      */

668     public void fixed_scale(short scale)
669     {
670         _scale = scale;
671     }
672
673     /**
674      *
675      * @param index
676      * @return
677      * @throws BadKind
678      * @throws Bounds
679      */

680     public short member_visibility
681         (int index)
682         throws BadKind, Bounds
683     {
684         if (_indirection)
685         {
686             return _ref.member_visibility(index);
687         }
688         if (_member_type == null)
689         {
690             throw new BadKind();
691         }
692         if (index < 0 || index >= _member_visibility.length)
693         {
694             throw new Bounds();
695         }
696         return _member_visibility[index];
697     }
698
699     // Sybase-internal
700
/**
701      *
702      * @param index
703      * @param visibility
704      */

705     public void member_visibility(int index, short visibility)
706     {
707         _member_visibility[index] = visibility;
708     }
709
710     /**
711      *
712      * @return
713      * @throws BadKind
714      */

715     public short type_modifier()
716         throws BadKind
717     {
718         if (_indirection)
719         {
720             return _ref.type_modifier();
721         }
722         int tk = _kind.value();
723         if (tk != TCKind._tk_value)
724         {
725             throw new BadKind();
726         }
727         return _type_modifier;
728     }
729
730     // Sybase-internal
731
/**
732      *
733      * @param modifier
734      */

735     public void type_modifier(short modifier)
736     {
737         _type_modifier = modifier;
738     }
739
740     /**
741      *
742      * @return
743      * @throws BadKind
744      */

745     public org.omg.CORBA.TypeCode concrete_base_type()
746         throws BadKind
747     {
748         if (_indirection)
749         {
750             return _ref.concrete_base_type();
751         }
752         int tk = _kind.value();
753         if (tk != TCKind._tk_value)
754         {
755             throw new BadKind();
756         }
757         return _ref;
758     }
759
760     // Sybase-internal
761
/**
762      *
763      * @param base
764      */

765     public void concrete_base_type(org.omg.CORBA.TypeCode base)
766     {
767         _ref = base;
768     }
769
770     // Sybase-internal
771
/**
772      *
773      * @param ref
774      */

775     public void indirection(org.omg.CORBA.TypeCode ref)
776     {
777         _ref = ref;
778         _indirection = true;
779     }
780
781     // Sybase-internal
782
/**
783      *
784      * @param id
785      */

786     public void recursive(String id)
787     {
788         _id = id;
789         _ref = null;
790         _indirection = true;
791     }
792
793     // Sybase-internal
794
/**
795      *
796      */

797     public void fix_recursive_members()
798     {
799         String id = _id == null ? default_id() : _id;
800         int n = _member_type.length;
801         for (int i = 0; i < n; i++)
802         {
803             TypeCode mt = (TypeCode)_member_type[i];
804             if (mt._kind.value() == TCKind._tk_sequence)
805             {
806                 TypeCode ct = (TypeCode)mt._ref;
807                 if (ct._indirection
808                 && ct._ref == null
809                     && ct._id.equals(id))
810                 {
811                     ct._ref = this;
812                 }
813             }
814         }
815     }
816
817     // -----------------------------------------------------------------------
818
// private methods
819
// -----------------------------------------------------------------------
820
/**
821      *
822      * @return
823      */

824     private String default_id()
825     {
826         // Take _type, and generate _id, e.g.
827
// if _type = "SessionManager::Manager",
828
// then _id = "IDL:SessionManager/Manager:1.0".
829
if (_type == null)
830         {
831             return "";
832         }
833         StringBuffer id = new StringBuffer(_type.length() + 10);
834         id.append("IDL:");
835         int n = _type.length();
836         for (int i = 0; i < n; i++)
837         {
838             char c = _type.charAt(i);
839             if (c == ':' && i + 1 < n && _type.charAt(i + 1) == ':')
840             {
841                 i++;
842             }
843             id.append(c == ':' ? '/' : c);
844         }
845         id.append(":1.0");
846         return id.toString();
847     }
848 }
849
Popular Tags