KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Base class for IDL3 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 abstract public class DeclarationImpl
39                 implements Declaration
40 {
41     // ==================================================================
42
//
43
// Internal state.
44
//
45
// ==================================================================
46

47     /**
48      ** The prefix.
49      **/

50     private java.lang.String JavaDoc prefix_;
51
52     /**
53      ** The name.
54      **/

55     private java.lang.String JavaDoc name_;
56
57     /**
58      ** The version.
59      **/

60     private java.lang.String JavaDoc version_;
61
62     /**
63      ** The repository ID.
64      **/

65     private java.lang.String JavaDoc id_;
66
67     /**
68      ** To protect imported declaration from being destroyed
69      **/

70     private boolean is_imported_;
71
72     /**
73      ** Number of references on this declaration.
74      **/

75     private int nb_refs_;
76
77     /**
78      ** The DeclarationKind.
79      **/

80     protected int the_declaration_kind_;
81
82     /**
83      ** The IDL3 repository
84      **/

85     protected Repository the_repository_;
86
87     /**
88      ** The parent scope.
89      **/

90     protected ScopeImpl the_parent_;
91
92     /**
93      ** To know if the declaration is a mapping declaration.
94      **/

95     protected boolean is_mapping_;
96
97     /**
98      **
99      **/

100     protected Declaration[] dependencies_;
101
102     // ==================================================================
103
//
104
// Constructor.
105
//
106
// ==================================================================
107

108     /**
109      ** The constructor with the parent scope.
110      **
111      ** @param parent The parent scope of the declaration.
112      **/

113     protected
114     DeclarationImpl(Repository rep, ScopeImpl parent)
115     {
116         // Init internal state.
117
the_repository_ = rep;
118         the_parent_ = parent;
119         prefix_ = null;
120         name_ = null;
121         version_ = "1.0";
122         id_ = null;
123         is_imported_ = false;
124         is_mapping_ = false;
125         the_declaration_kind_ = DeclarationKind._dk_null;
126         nb_refs_ = 0;
127         dependencies_ = null;
128     }
129
130     // ==================================================================
131
//
132
// Internal methods.
133
//
134
// ==================================================================
135

136     /**
137      ** To compute the OMG IDL prefix of the Declaration.
138      **
139      ** @return the prefix in OMG IDL or "" if there's no prefix.
140      **/

141     protected java.lang.String JavaDoc
142     computePrefix()
143     {
144         int idx1 = id_.indexOf(':');
145         int idx2 = id_.indexOf('/');
146
147         java.lang.String JavaDoc pname = the_parent_.getName();
148         if (pname==null)
149         // the parent is the repository itself
150
{
151             if (id_.indexOf(name_)==-1)
152             // there was a pragma id => no prefix
153
return null;
154             else if (idx2!=-1)
155             // id = IDL:prefix/name:version
156
return id_.substring(idx1+1,idx2);
157             else
158             // id = IDL:name:version
159
return null;
160         }
161         else
162         {
163             if (id_.indexOf(name_)==-1)
164             // there was a pragma id => no prefix
165
return null;
166             else if (id_.indexOf(the_parent_.computeBaseRID())==-1)
167             // there was a pragma prefix
168
// perhaps a pragma id in fact ????? but can't always know
169
// eg
170
// module M {
171
// #pragma id I "OpenCCM/M/I:1.0" <=> #pragma prefix "OpenCCM"
172
// interface I {};
173
// };
174
// id = IDL:prefix/par_1/.../par_n/name:version
175
// TODO : check all parents'name
176
return id_.substring(idx1+1, idx2);
177             else
178             // same prefix as it's parent
179
return null;
180         }
181     }
182
183     /**
184      ** Compute the base repository ID.
185      **
186      ** @return The RepositoryId of the declaration without the version and the IDL fields.
187      **/

188     protected java.lang.String JavaDoc
189     computeBaseRID()
190     {
191         java.lang.String JavaDoc result = "";
192
193         if (prefix_ != null)
194             result = prefix_;
195         else if (the_parent_ != null)
196             result = the_parent_.computeBaseRID();
197
198         if (name_ != null)
199         {
200             if (result.length() != 0)
201                 result = result + '/' + name_;
202             else
203                 result = name_;
204         }
205
206         return result;
207     }
208
209     /**
210      ** To set the declaration as an imported declaration.
211      ** Imported declarations can't be destroyed in the IR3.
212      **/

213     protected void
214     setImported()
215     {
216         is_imported_ = true;
217     }
218
219     /**
220      **
221      **/

222     protected Repository
223     getRepository()
224     {
225         return the_repository_;
226     }
227
228     /**
229      ** Loads infos of the CORBA 3.0 Contained.
230      **
231      ** @param contained The Contained to load.
232      **/

233     protected void
234     load(org.omg.CORBA.Contained JavaDoc contained)
235     {
236         version_ = contained.version();
237         id_ = contained.id();
238         prefix_ = computePrefix();
239         the_repository_.addDeclInRep(getId(), this);
240         if (the_parent_!=null)
241             the_parent_.addDecl(this);
242     }
243
244     /**
245      ** Loads infos of the CORBA 3.0 Contained.
246      **
247      ** @param contained The Contained to load.
248      **/

249     protected void
250     loadAsMapping(org.omg.CORBA.Contained JavaDoc contained)
251     {
252         version_ = contained.version();
253         id_ = contained.id();
254         prefix_ = computePrefix();
255         the_repository_.addMappedDeclInRep(getId(), this);
256         is_mapping_ = true;
257         // only add declaration if the parent is itself a mapped declaration !!!
258
if ((the_parent_!=null) &&
259             (the_parent_.is_mapping_))
260             the_parent_.addDecl(this);
261     }
262
263     /**
264      ** Obtain its CORBA 3.0 Contained reference.
265      **
266      ** @return The Contained object associated with the declaration.
267      **/

268     abstract protected org.omg.CORBA.Contained JavaDoc
269     getContained();
270
271     // ==================================================================
272
//
273
// Methods for the Declaration interface (creation view).
274
//
275
// ==================================================================
276

277     /**
278      ** Destroy the declaration in the IR3.
279      **/

280     public void
281     destroy()
282     {
283         if (nb_refs_!=0)
284             throw new Error JavaDoc("Declaration can\'t be destroyed : it\'s still referenced");
285
286         org.omg.CORBA.IRObject JavaDoc obj = (org.omg.CORBA.IRObject JavaDoc)getContained();
287         if (obj != null)
288         {
289             // destroy the IR3 Contained if it was not imported.
290
if (!is_imported_)
291                 obj.destroy();
292
293             the_repository_.removeDeclFromRep(getId());
294             if (the_parent_!=null)
295                 the_parent_.removeDecl(name_);
296         }
297     }
298
299     /**
300      ** Set the prefix. For use.
301      **
302      ** @param prefix The prefix to set.
303      ** Note that setting the prefix should only be done when
304      ** a new prefix (ie different from the declared-in scope prefix)
305      ** was explicitly declared.
306      **/

307     public void
308     setPrefix(java.lang.String JavaDoc prefix)
309     {
310         prefix_ = prefix;
311
312         org.omg.CORBA.Contained JavaDoc contained = getContained();
313         if (contained != null)
314             contained.id(getId());
315     }
316
317     /**
318      ** Set the name.
319      **
320      ** @param name The name to set.
321      **/

322     public void
323     setName(java.lang.String JavaDoc name)
324     {
325         name_ = name;
326     }
327
328     /**
329      ** Set the version.
330      **
331      ** @param version The version to set.
332      **/

333     public void
334     setVersion(java.lang.String JavaDoc version)
335     {
336         version_ = version;
337         org.omg.CORBA.Contained JavaDoc contained = getContained();
338         if (contained != null)
339             contained.version(version);
340     }
341
342     /**
343      ** Set the repository ID.
344      **
345      ** @param id The RepositoryId to set.
346      **/

347     public void
348     setId(java.lang.String JavaDoc id)
349     {
350         id_ = id;
351         the_repository_.addDeclInRep(id, this);
352         org.omg.CORBA.Contained JavaDoc contained = getContained();
353         if (contained != null)
354             contained.id(id_);
355     }
356
357     /**
358      ** Create it into the IR3.
359      **/

360     public void
361     create()
362     {
363         if (the_parent_!=null)
364             the_parent_.addDecl(this);
365     }
366
367     // ==================================================================
368
//
369
// Methods for the Declaration interface (load view).
370
//
371
// ==================================================================
372

373     /**
374      ** Obtain the prefix.
375      **
376      ** @return The prefix of the declaration
377      ** or null if there's no prefix.
378      **/

379     public java.lang.String JavaDoc
380     getPrefix()
381     {
382         if (prefix_!=null)
383             return prefix_;
384         if ((the_parent_!=null) && (the_parent_.getName()!=null))
385             return the_parent_.getPrefix();
386
387         return "";
388     }
389
390     /**
391      ** Obtain the name.
392      **
393      ** @return The name of the declaration.
394      **/

395     public java.lang.String JavaDoc
396     getName()
397     {
398         return name_;
399     }
400
401     /**
402      ** Obtain the absolute name.
403      **
404      ** @return The absolute name of the declaration.
405      **/

406     public java.lang.String JavaDoc
407     getAbsoluteName()
408     {
409         java.lang.String JavaDoc result = "";
410         if(the_parent_ != null)
411             result = the_parent_.getAbsoluteName();
412
413         if(name_ != null)
414             result = result + "::" + name_;
415
416         return result;
417     }
418
419     /**
420      ** Obtain the version.
421      **
422      ** @return The version of the declaration.
423      ** Note that version "1.0" is assumed is no version was set.
424      **/

425     public java.lang.String JavaDoc
426     getVersion()
427     {
428         return version_;
429     }
430
431     /**
432      ** Obtain the repository ID.
433      **
434      ** @return The RepositoryId of the declaration.
435      **/

436     public java.lang.String JavaDoc
437     getId()
438     {
439         if (id_ != null)
440             return id_;
441
442         return "IDL:" + computeBaseRID() + ':' + version_;
443     }
444
445     /**
446      ** Returns the kind of the Declaration object.
447      **
448      ** @return The DeclarationKind of the object.
449      **/

450     public int
451     getDeclKind()
452     {
453         return the_declaration_kind_;
454     }
455
456     /**
457      ** Obtain the parent scope.
458      **
459      ** @return The parent scope of the declaration
460      ** or null if the declaration has no parent.
461      **/

462     public Scope
463     getParent()
464     {
465         return the_parent_;
466     }
467
468     /**
469      ** Obtain the declaration external dependencies.
470      ** Note: for scopes, contained objects are not considered
471      ** as dependencies.
472      **
473      ** @return The list of dependencies as an array of Declaration.
474      **/

475     public Declaration[]
476     getDependencies()
477     {
478         return new Declaration[0];
479     }
480
481     // ==================================================================
482
//
483
// Public methods for TypeRef interface.
484
// Implemented here to do some code factorization.
485
//
486
// ==================================================================
487

488     /**
489      **
490      **/

491     public boolean
492     isDeclaration()
493     {
494         return true;
495     }
496
497     // ==================================================================
498
//
499
// Public methods for reference counting management.
500
//
501
// ==================================================================
502

503     /**
504      **
505      **/

506     public void
507     addRef()
508     {
509         nb_refs_++;
510     }
511
512     /**
513      **
514      **/

515     public void
516     removeRef()
517     {
518         nb_refs_--;
519     }
520 }
521
Popular Tags