KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ast > lib > UnionDeclImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Mathieu Vadet.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.ast.lib;
28
29 /** To access AST TypeKind. */
30 import org.objectweb.openccm.ast.api.TypeKind;
31
32 /** To access AST TypeRef. */
33 import org.objectweb.openccm.ast.api.TypeRef;
34
35 /** To access AST DeclarationKind. */
36 import org.objectweb.openccm.ast.api.DeclarationKind;
37
38 /** To access AST Declaration. */
39 import org.objectweb.openccm.ast.api.Declaration;
40
41 /** To access AST UnionMemberList. */
42 import org.objectweb.openccm.ast.api.UnionMemberList;
43
44 /** To use CORBA::TypeCode. */
45 import org.omg.CORBA.TypeCode JavaDoc;
46
47 /** To use CORBA::UnionDef. */
48 import org.omg.CORBA.UnionDef JavaDoc;
49 import org.omg.CORBA.UnionDefHelper;
50
51 /** To use CORBA::UnionMember. */
52 import org.omg.CORBA.UnionMember JavaDoc;
53
54 /**
55  * UnionDeclImpl is a wrapper class for IDL union declarations.
56  *
57  * Inherits from:
58  * - ForwardScopeIDLImpl as unions are also IDL forward scopes,
59  * - TypeRef as unions are IDL types.
60  *
61  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
62  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
63  *
64  * @version 0.3
65  */

66
67 public class UnionDeclImpl
68      extends ForwardScopeIDLImpl
69   implements org.objectweb.openccm.ast.api.UnionDecl,
70              IDLTypeWrapper
71 {
72     // ==================================================================
73
//
74
// Internal state.
75
//
76
// ==================================================================
77

78     /** Reference to the CORBA 3.0 UnionDef. */
79     private UnionDef JavaDoc union_def_;
80
81     /** The default index. */
82     private int default_index_;
83
84     /** The discriminator. */
85     private TypeRef discriminator_;
86
87     /** The discriminator. */
88     private TypeCode JavaDoc tc_discriminator_;
89
90     /** List of members. */
91     private UnionMemberListImpl members_;
92
93     // ==================================================================
94
//
95
// Constructor.
96
//
97
// ==================================================================
98

99     /**
100      * The constructor with the parent scope.
101      *
102      * @param rep The repository of the declaration.
103      * @param parent The parent scope of the declaration.
104      */

105     protected
106     UnionDeclImpl(Repository rep, ScopeImpl parent)
107     {
108         // Call the ForwardScopeIDLImpl constructor.
109
super(rep, parent);
110
111         // Init internal state.
112
union_def_ = null;
113         default_index_ = -1;
114         discriminator_ = null;
115         tc_discriminator_ = null;
116         members_ = new UnionMemberListImpl();
117     }
118
119     // ==================================================================
120
//
121
// Internal methods.
122
//
123
// ==================================================================
124

125     /**
126      * Obtain the associated CORBA::IDLType of the discriminator.
127      *
128      * @return The associated CORBA::IDLType of the discriminator.
129      */

130     protected org.omg.CORBA.IDLType JavaDoc
131     getDiscriminatorIDLType()
132     {
133       return ( discriminator_ != null ) ?
134              (((IDLTypeWrapper)discriminator_).getIDLType()) : null;
135     }
136
137     /**
138      * Obtain the associated CORBA::TypeCode of the discriminator.
139      *
140      * @return The associated CORBA::TypeCode of the discriminator.
141      */

142     protected TypeCode JavaDoc
143     getDiscriminatorTypeCode()
144     {
145         if(tc_discriminator_ == null)
146     {
147             org.omg.CORBA.IDLType JavaDoc type = getDiscriminatorIDLType();
148             if(type != null)
149                 tc_discriminator_ = type.type();
150     }
151         return tc_discriminator_;
152     }
153
154     /**
155      * Obtain the associated sequence of union members.
156      *
157      * @return The associated sequence of union members.
158      */

159     protected org.omg.CORBA.UnionMember JavaDoc[]
160     getUnionMemberSeq()
161     {
162         TypeCode JavaDoc tc = getDiscriminatorTypeCode();
163     return (tc != null) ? members_.getUnionMemberSeq(tc)
164                         : new org.omg.CORBA.UnionMember JavaDoc[0];
165     }
166
167     // ==================================================================
168
//
169
// Internal methods for DeclarationImpl.
170
//
171
// ==================================================================
172

173     /**
174      * Loads infos of the CORBA 3.0 UnionDef.
175      *
176      * @param contained The UnionDef to load.
177      */

178     protected void
179     load(org.omg.CORBA.Contained JavaDoc contained)
180     {
181         union_def_ = UnionDefHelper.narrow(contained);
182         setDiscriminator(getRepository().
183                          getAsTypeRef(union_def_.discriminator_type_def()));
184         try
185         {
186             default_index_ = union_def_.type().default_index();
187         }
188         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
189         {
190             // Wrap the CORBA::TypeCode::BadKind exception.
191
throw new org.objectweb.openccm.corba.UserExceptionWrapper(ex);
192         }
193
194         // load as scope now to prevent infinite loop in the case
195
// of recursive union.
196
super.load(contained);
197
198         UnionMember JavaDoc[] members = union_def_.members();
199         for (int i=0; i<members.length; i++)
200         {
201             AnyValueImpl any = new AnyValueImpl();
202             any.loadAny(members[i].label);
203             members_.addMember(members[i].name,
204                       getRepository().getAsTypeRef(members[i].type_def),
205                       any);
206         }
207     }
208
209     /**
210      * Loads infos of the CORBA 3.0 UnionDef.
211      *
212      * @param unionDef The UnionDef to load.
213      */

214     protected void
215     loadAsMapping(org.omg.CORBA.Contained JavaDoc contained)
216     {
217         union_def_ = UnionDefHelper.narrow(contained);
218         setDiscriminator(getRepository().
219               getAsMappedTypeRef(union_def_.discriminator_type_def()));
220         try
221         {
222             default_index_ = union_def_.type().default_index();
223         }
224         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc ex)
225         {
226             // Wrap the CORBA::TypeCode::BadKind exception.
227
throw new org.objectweb.openccm.corba.UserExceptionWrapper(ex);
228         }
229
230         // load as scope now to prevent infinite loop in the case
231
// of recursive struct.
232
super.loadAsMapping(contained);
233
234         UnionMember JavaDoc[] members = union_def_.members();
235         for (int i=0; i<members.length; i++)
236         {
237             AnyValueImpl any = new AnyValueImpl();
238             any.loadAny(members[i].label);
239             members_.addMember(members[i].name,
240                       getRepository().getAsMappedTypeRef(members[i].type_def),
241                       any);
242         }
243     }
244
245     /**
246      * Obtain its CORBA 3.0 Contained reference.
247      *
248      * @return The Contained object associated with the union declaration.
249      */

250     protected org.omg.CORBA.Contained JavaDoc
251     getContained()
252     {
253         return union_def_;
254     }
255
256     // ==================================================================
257
//
258
// Internal methods for ScopeImpl.
259
//
260
// ==================================================================
261

262     /**
263      * Obtain its CORBA 3.0 Container reference.
264      *
265      * @return The Container object associated with the union declaration.
266      */

267     protected org.omg.CORBA.Container JavaDoc
268     getContainer()
269     {
270         return union_def_;
271     }
272
273     // ==================================================================
274
//
275
// Internal methods for ForwardScopeImpl.
276
//
277
// ==================================================================
278

279     // ==================================================================
280
//
281
// Internal methods for ForwardScopeIDLImpl.
282
//
283
// ==================================================================
284

285     /**
286      * Create the container object.
287      */

288     protected void
289     createContainer()
290     {
291         // Create an UnionDef into the IFR.
292
union_def_ = the_parent_.getContainer().
293                      create_union(getId(), getName(), getVersion(),
294                                   getDiscriminatorIDLType(),
295                                   getUnionMemberSeq());
296     }
297
298     /**
299      * Complete the container object.
300      */

301     protected void
302     completeContainer()
303     {
304         union_def_.discriminator_type_def(getDiscriminatorIDLType());
305         union_def_.members(getUnionMemberSeq());
306     }
307
308     // ==================================================================
309
//
310
// Public methods.
311
//
312
// ==================================================================
313

314     // ==================================================================
315
//
316
// Methods for OMG IDL org.objectweb.openccm.ast.api.WithDependencies
317
//
318
// ==================================================================
319

320     /**
321      * Obtain the declaration external dependencies.
322      *
323      * Note: for scopes, contained objects are not considered
324      * as dependencies.
325      *
326      * @return The list of dependencies as an array of Declaration.
327      */

328     public Declaration[]
329     getDependencies()
330     {
331         if (dependencies_!=null)
332             return dependencies_;
333
334         dependencies_ = new Declaration[0];
335         java.util.List JavaDoc union_depend = new java.util.ArrayList JavaDoc();
336         Declaration[] depend = null;
337
338         // discriminator
339
if (getDiscriminator().isDeclaration())
340             union_depend.add(getDiscriminator());
341
342         depend = getDiscriminator().getDependencies();
343         for (int i=0;i<depend.length;i++)
344             union_depend.add(depend[i]);
345
346         // members
347
org.objectweb.openccm.ast.api.UnionMember[] members =
348             members_.getUnionMembers();
349         for (int i=0; i<members.length; i++)
350         {
351             TypeRef member_type = members[i].getType();
352             if (member_type.isDeclaration())
353             {
354                 if ((!containsDecl((Declaration)member_type)) &&
355                     (union_depend.indexOf(member_type)==-1))
356                     union_depend.add(member_type);
357             }
358
359             depend = member_type.getDependencies();
360             for (int j=0;j<depend.length;j++)
361             {
362                 if ((union_depend.indexOf(depend[j])==-1) &&
363                     (!containsDecl(depend[j])) &&
364                     (depend[j]!=this))
365                     union_depend.add(depend[j]);
366             }
367         }
368
369         dependencies_ = (Declaration[])union_depend.toArray(new Declaration[0]);
370         return dependencies_;
371     }
372
373     // ==================================================================
374
//
375
// Methods for OMG IDL org.objectweb.openccm.ast.api.Declaration
376
//
377
// ==================================================================
378

379     /**
380      * Obtain its DeclarationKind.
381      *
382      * @return The DeclarationKind of the object.
383      */

384     public long
385     getDeclKind()
386     {
387         return DeclarationKind.dk_union;
388     }
389
390     // ==================================================================
391
//
392
// Methods for OMG IDL org.objectweb.openccm.ast.api.Scope
393
//
394
// ==================================================================
395

396     // ==================================================================
397
//
398
// Methods for OMG IDL org.objectweb.openccm.ast.api.ForwardScope
399
//
400
// ==================================================================
401

402     // ==================================================================
403
//
404
// Methods for OMG IDL org.objectweb.openccm.ast.api.TypeRef
405
//
406
// ==================================================================
407

408     /**
409      * Obtain its associated TypeKind.
410      *
411      * @return The associated TypeKind.
412      */

413     public TypeKind
414     getTypeKind()
415     {
416         return TypeKind.tk_union;
417     }
418
419     // ==================================================================
420
//
421
// Methods for OMG IDL org.objectweb.openccm.ast.api.UnionDecl
422
//
423
// ==================================================================
424

425     /**
426      * Set the associated discriminator type.
427      *
428      * @param type The TypeRef of the associated discriminator.
429      */

430     public void
431     setDiscriminator(TypeRef type)
432     {
433         if(type!=null)
434         {
435             discriminator_ = type;
436         }
437     }
438
439     /**
440      * Obtain the associated discriminator type.
441      *
442      * @return The associated discriminator type.
443      */

444     public TypeRef
445     getDiscriminator()
446     {
447         return discriminator_;
448     }
449
450     /**
451      * Obtain the union member list.
452      *
453      * @return The union member list.
454      */

455     public UnionMemberList
456     getMemberList()
457     {
458         return members_;
459     }
460
461     /**
462      * Obtain the default index.
463      *
464      * @return The default index.
465      */

466     public int
467     getDefaultIndex()
468     {
469         return default_index_;
470     }
471
472     // ==================================================================
473
//
474
// Methods for interface IDLTypeWrapper
475
//
476
// ==================================================================
477

478     /**
479      * Obtain its IDLType reference.
480      *
481      * @return The IDLType associated with the union declaration.
482      */

483     public org.omg.CORBA.IDLType JavaDoc
484     getIDLType()
485     {
486         return union_def_;
487     }
488 }
489
Popular Tags