KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > IDL3 > UnionDeclImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@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): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.IDL3;
28
29 /**
30  * This class manages IDL union declarations.
31  *
32  * @author <a=href="Philippe.Merle@lifl.fr">Philippe Merle</a>
33  * <a=href="Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
34  *
35  * @version 0.3
36  */

37
38 public class UnionDeclImpl
39        extends ForwardScopeImpl
40        implements TypeRef, UnionDecl
41 {
42     // ==================================================================
43
//
44
// Internal state.
45
//
46
// ==================================================================
47

48     /**
49      ** Reference to the CORBA 3.0 UnionDef.
50      **/

51     private org.omg.CORBA.UnionDef JavaDoc union_def_;
52
53     /**
54      **
55      **/

56     private int default_index_;
57
58     /**
59      ** List of members.
60      **/

61     private UnionMembersImpl members_;
62
63     // ==================================================================
64
//
65
// Constructor.
66
//
67
// ==================================================================
68

69     /**
70      ** The constructor with the parent scope.
71      **
72      ** @param parent The parent scope of the declaration.
73      **/

74     protected
75     UnionDeclImpl(Repository rep, ScopeImpl parent)
76     {
77         // Call the ScopeImpl constructor.
78
super(rep, parent);
79
80         // Init internal state.
81
union_def_ = null;
82         members_ = new UnionMembersImpl();
83         default_index_ = -1;
84         the_declaration_kind_ = DeclarationKind._dk_union;
85     }
86
87     // ==================================================================
88
//
89
// Internal methods.
90
//
91
// ==================================================================
92

93     /**
94      ** Loads infos of the CORBA 3.0 UnionDef.
95      **
96      ** @param unionDef The UnionDef to load.
97      **/

98     protected void
99     load(org.omg.CORBA.Contained JavaDoc contained)
100     {
101         union_def_ = org.omg.CORBA.UnionDefHelper.narrow(contained);
102         setDiscriminator(getRepository().getAsTypeRef(union_def_.discriminator_type_def()));
103         try
104         {
105             default_index_ = union_def_.type().default_index();
106         }
107         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {}
108
109         org.omg.CORBA.UnionMember JavaDoc[] members = union_def_.members();
110
111         // load as scope now to prevent infinite loop in the case
112
// of recursive union.
113
super.load(contained);
114
115         for (int i=0;i<members.length;i++)
116         {
117             AnyValue any = new AnyValue();
118             any.loadAny(members[i].label);
119             addMember(members[i].name,
120                       getRepository().getAsTypeRef(members[i].type_def),
121                       any);
122         }
123     }
124
125     /**
126      ** Loads infos of the CORBA 3.0 UnionDef.
127      **
128      ** @param unionDef The UnionDef to load.
129      **/

130     protected void
131     loadAsMapping(org.omg.CORBA.Contained JavaDoc contained)
132     {
133         union_def_ = org.omg.CORBA.UnionDefHelper.narrow(contained);
134         setDiscriminator(getRepository().getAsMappedTypeRef(union_def_.discriminator_type_def()));
135         try
136         {
137             default_index_ = union_def_.type().default_index();
138         }
139         catch (org.omg.CORBA.TypeCodePackage.BadKind JavaDoc e) {}
140         org.omg.CORBA.UnionMember JavaDoc[] members = union_def_.members();
141
142         // load as scope now to prevent infinite loop in the case
143
// of recursive struct.
144
super.loadAsMapping(contained);
145
146         for (int i=0;i<members.length;i++)
147         {
148             AnyValue any = new AnyValue();
149             any.loadAny(members[i].label);
150             addMember(members[i].name,
151                       getRepository().getAsMappedTypeRef(members[i].type_def),
152                       any);
153         }
154     }
155
156     // ==================================================================
157
//
158
// Methods for the Declaration interface.
159
//
160
// ==================================================================
161

162     /**
163      ** Obtain the declaration external dependencies.
164      ** Note: for scopes, contained objects are not considered
165      ** as dependencies.
166      **
167      ** @return The list of dependencies as an array of Declaration.
168      **/

169     public Declaration[]
170     getDependencies()
171     {
172         if (dependencies_!=null)
173             return dependencies_;
174
175         dependencies_ = new Declaration[0];
176         org.objectweb.ccm.util.Vector union_depend = new org.objectweb.ccm.util.Vector();
177         Declaration[] depend = null;
178
179         // discriminator
180
if (getType().isDeclaration())
181             union_depend.add(getType());
182
183         depend = getType().getDependencies();
184         for (int i=0;i<depend.length;i++)
185             union_depend.add(depend[i]);
186
187
188         // members
189
TypeRef[] member_types = getMembers().getMemberTypes();
190
191         for (int i=0;i<member_types.length;i++)
192         {
193             if (member_types[i].isDeclaration())
194             {
195                 if ((!containsDecl((Declaration)member_types[i])) &&
196                     (union_depend.indexOf(member_types[i])==-1))
197                     union_depend.add(member_types[i]);
198             }
199
200             depend = member_types[i].getDependencies();
201             for (int j=0;j<depend.length;j++)
202             {
203                 if ((union_depend.indexOf(depend[j])==-1) &&
204                     (!containsDecl(depend[j])) &&
205                     (depend[j]!=this))
206                     union_depend.add(depend[j]);
207             }
208         }
209
210         dependencies_ = (Declaration[])union_depend.toArray(new Declaration[0]);
211         return dependencies_;
212     }
213
214     // ==================================================================
215
//
216
// Methods for the Scope interface.
217
//
218
// ==================================================================
219

220     // ==================================================================
221
//
222
// Methods for the TypeRef interface.
223
//
224
// ==================================================================
225

226     /**
227      ** Obtain its IDLType reference.
228      **
229      ** @return The IDLType associated with the union declaration.
230      **/

231     public org.omg.CORBA.IDLType JavaDoc
232     getIDLType()
233     {
234         return union_def_;
235     }
236
237     /**
238      **
239      **/

240     public int
241     getTypeKind()
242     {
243         return TypeKind._tk_union;
244     }
245
246     // ==================================================================
247
//
248
// Methods for the UnionDecl interface.
249
//
250
// ==================================================================
251

252     /**
253      ** Set the discriminator.
254      **
255      ** @param type The TypeRef of the discriminator.
256      **/

257     public void
258     setDiscriminator(TypeRef type)
259     {
260         if(type!=null)
261         {
262             type.addRef();
263             members_.setDiscriminator(type);
264         }
265     }
266
267     /**
268      ** Add a member.
269      **
270      ** @param name The name of the member.
271      ** @param type The TypeRef of the member.
272      ** @param value The value of the member.
273      **/

274     public void
275     addMember(String JavaDoc name,
276               TypeRef type,
277               AnyValue value)
278     {
279         members_.addMember(name, type, value);
280     }
281
282     /**
283      **
284      **/

285     public TypeRef
286     getType()
287     {
288         return members_.getDiscriminator();
289     }
290
291     /**
292      **
293      **/

294     public UnionMembers
295     getMembers()
296     {
297         return members_;
298     }
299
300     /**
301      **
302      **/

303     public int
304     getDefaultIndex()
305     {
306         return default_index_;
307     }
308
309     // ==================================================================
310
//
311
// Methods for the inherited DeclarationImpl class.
312
//
313
// ==================================================================
314

315     /**
316      **
317      **/

318     public void
319     destroy()
320     {
321         members_.destroy();
322         super.destroy();
323     }
324
325     /**
326      ** Obtain its CORBA 3.0 Contained reference.
327      **
328      ** @return The Contained object associated with the union declaration.
329      **/

330     protected org.omg.CORBA.Contained JavaDoc
331     getContained()
332     {
333         return union_def_;
334     }
335
336     // ==================================================================
337
//
338
// Methods for the inherited ScopeImpl class.
339
//
340
// ==================================================================
341

342     /**
343      ** Obtain its CORBA 3.0 Container reference.
344      **
345      ** @return The Container object associated with the union declaration.
346      **/

347     protected org.omg.CORBA.Container JavaDoc
348     getContainer()
349     {
350         return union_def_;
351     }
352
353     // ==================================================================
354
//
355
// Methods for the inherited ForwardScopeImpl class.
356
//
357
// ==================================================================
358

359     /**
360      ** Create the container object.
361      **/

362     protected void
363     createContainer()
364     {
365         // Create an UnionDef into the IFR.
366
org.omg.CORBA.IDLType JavaDoc disc = null;
367         if (members_.getDiscriminator()!=null)
368             disc = members_.getDiscriminator().getIDLType();
369
370         union_def_ = the_parent_.getContainer().create_union(getId(), getName(), getVersion(),
371                                                              disc,
372                                                              members_.getMemberDefs());
373     }
374
375     /**
376      ** Complete the container object.
377      **/

378     protected void
379     completeContainer()
380     {
381         union_def_.discriminator_type_def(members_.getDiscriminator().getIDLType());
382         union_def_.members(members_.getMemberDefs());
383     }
384 }
385
Popular Tags