KickJava   Java API By Example, From Geeks To Geeks.

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


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 // Package dependencies.
30
import org.objectweb.openccm.ast.api.DeclarationCategory;
31 import org.objectweb.openccm.ast.api.Declaration;
32 import org.objectweb.openccm.ast.api.AST;
33 import org.objectweb.openccm.ast.api.Scope;
34
35
36 /**
37  * DeclarationImpl is a wrapper class for all IDL declarations.
38  *
39  * Inherits from:
40  * - WithName as IDL declarations have an associated name.
41  *
42  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
43  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
44  *
45  * @version 0.3
46  */

47
48 abstract public class DeclarationImpl
49                 extends WithNameImpl
50                 implements Declaration
51 {
52     // ==================================================================
53
//
54
// Internal state.
55
//
56
// ==================================================================
57

58     /** The prefix. */
59     protected java.lang.String JavaDoc prefix_;
60
61     /** The version. */
62     protected java.lang.String JavaDoc version_;
63
64     /** The repository ID. */
65     protected java.lang.String JavaDoc id_;
66
67     /** To protect imported declaration from being destroyed. */
68     protected boolean is_imported_;
69
70     /** The IDL3 repository. */
71     protected Repository the_repository_;
72
73     /** The parent scope. */
74     protected ScopeImpl the_parent_;
75
76     /** To know if the declaration is a mapping declaration. */
77     protected boolean is_mapping_;
78
79     /** Associated dependencies. */
80     protected Declaration[] dependencies_;
81
82     // ==================================================================
83
//
84
// Constructor.
85
//
86
// ==================================================================
87

88     /**
89      * The constructor with the parent scope.
90      *
91      * @param rep The repository of the declaration.
92      * @param parent The parent scope of the declaration.
93      */

94     protected
95     DeclarationImpl(Repository rep,
96                     ScopeImpl parent)
97     {
98         // Call the default WithNameImpl constructor.
99
super();
100
101         // Init internal state.
102
the_repository_ = rep;
103         the_parent_ = parent;
104         prefix_ = null;
105         version_ = "1.0";
106         id_ = null;
107         is_imported_ = false;
108         is_mapping_ = false;
109         dependencies_ = null;
110     }
111
112     // ==================================================================
113
//
114
// Internal methods.
115
//
116
// ==================================================================
117

118     /**
119      * To compute the OMG IDL prefix of the Declaration.
120      *
121      * @return the prefix in OMG IDL or "" if there's no prefix.
122      */

123     protected java.lang.String JavaDoc
124     computePrefix()
125     {
126         int idx1 = id_.indexOf(':');
127         int idx2 = id_.indexOf('/');
128
129         java.lang.String JavaDoc pname = the_parent_.getName();
130         if ( (pname==null) ||
131              (pname.equals("")) ||
132              (the_parent_ instanceof org.objectweb.openccm.ast.api.FileScope) )
133         // the parent is the repository (null) or the file scope ("") itself
134
{
135             // System.err.println("Computing prefix for: "+getId()+" with Repository as parent");
136
if (id_.indexOf(name_)==-1)
137             // there was a pragma id => no prefix
138
return null;
139             else if (idx2!=-1)
140             // id = IDL:prefix/name:version
141
return id_.substring(idx1+1,idx2);
142             else
143             // id = IDL:name:version
144
return null;
145         }
146         else
147         {
148             // System.err.println("Computing prefix for: "+getId()+" with: "+pname+" as parent");
149
if (id_.indexOf(name_)==-1)
150             // there was a pragma id => no prefix
151
return null;
152             else if (id_.indexOf(the_parent_.computeBaseRID())==-1)
153             // there was a pragma prefix
154
// perhaps a pragma id in fact ????? but can't always know
155
// eg
156
// module M {
157
// #pragma id I "OpenCCM/M/I:1.0" <=> #pragma prefix "OpenCCM"
158
// interface I {};
159
// };
160
// id = IDL:prefix/par_1/.../par_n/name:version
161
// TODO : check all parents'name
162
return id_.substring(idx1+1, idx2);
163             else
164             // same prefix as it's parent
165
return null;
166         }
167     }
168
169     /**
170      * Compute the base repository ID.
171      *
172      * @return The RepositoryId of the declaration without the version
173      * and the IDL fields.
174      */

175     protected java.lang.String JavaDoc
176     computeBaseRID()
177     {
178         java.lang.String JavaDoc result = "";
179
180         if (prefix_ != null)
181             result = prefix_;
182         if( (the_parent_ != null) &&
183             (the_parent_.getName()!=null) &&
184             (!the_parent_.getName().equals("")) &&
185             (!(the_parent_ instanceof org.objectweb.openccm.ast.api.FileScope)) )
186         {
187             // the parent is the repository (null) or the file scope ("") itself
188
result = the_parent_.computeBaseRID();
189         }
190
191         if (name_ != null)
192         {
193             if (result.length() != 0)
194                 result = result + '/' + name_;
195             else
196                 result = name_;
197         }
198
199         return result;
200     }
201
202     /**
203      * To set the declaration as an imported declaration.
204      * Imported declarations can't be destroyed in the IR3.
205      */

206     protected void
207     setImported()
208     {
209         is_imported_ = true;
210     }
211
212     /**
213      * Get the associated Repository.
214      */

215     protected Repository
216     getRepository()
217     {
218         return the_repository_;
219     }
220
221     /**
222      * Loads infos of the CORBA 3.0 Contained.
223      *
224      * @param contained The Contained to load.
225      */

226     protected void
227     load(org.omg.CORBA.Contained JavaDoc contained)
228     {
229         version_ = contained.version();
230         id_ = contained.id();
231         prefix_ = computePrefix();
232         the_repository_.addDeclInRep(getId(), this);
233         if (the_parent_!=null)
234             the_parent_.addDecl(this);
235     }
236
237     /**
238      * Loads infos of the CORBA 3.0 Contained.
239      *
240      * @param contained The Contained to load.
241      */

242     protected void
243     loadAsMapping(org.omg.CORBA.Contained JavaDoc contained)
244     {
245       // System.out.println("loadAsMapping IS MAPPING TRUE " + this);
246

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

263     abstract protected org.omg.CORBA.Contained JavaDoc
264     getContained();
265
266     // ==================================================================
267
//
268
// Public methods.
269
//
270
// ==================================================================
271

272     /**
273      * Obtain the ID prefix, i.e. "IDL", "PSDL", or "CIDL".
274      *
275      * @return The ID prefix.
276      */

277     public String JavaDoc
278     getIdPrefix()
279     {
280         if (getCategory() == DeclarationCategory.dc_cidl)
281             return "CIDL";
282         if (getCategory() == DeclarationCategory.dc_psdl)
283             return "PSDL";
284         return "IDL";
285     }
286
287     /**
288      * isDeclaration is implemented here to do some code factorization.
289      */

290     public boolean
291     isDeclaration()
292     {
293         return true;
294     }
295
296     // ==================================================================
297
//
298
// Methods for OMG IDL org.objectweb.openccm.ast.api.WithDependencies
299
//
300
// ==================================================================
301

302     /**
303      * Obtain the external Declaration dependencies.
304      * Note: For scopes, contained objects are not considered
305      * as dependencies.
306      *
307      * @return The list of Declaration dependencies.
308      */

309     public Declaration[]
310     getDependencies()
311     {
312         return new Declaration[0];
313     }
314
315     // ==================================================================
316
//
317
// Methods for OMG IDL org.objectweb.openccm.ast.api.Declaration
318
//
319
// ==================================================================
320

321     /**
322      * Obtain its DeclarationKind.
323      *
324      * This method is implemented into DeclarationImpl subclasses.
325      *
326      * @return The DeclarationKind of the object.
327      */

328     abstract public long
329     getDeclKind();
330
331     /**
332      * Obtain its absolute name.
333      *
334      * @return Its absolute name.
335      */

336     public java.lang.String JavaDoc
337     getAbsoluteName()
338     {
339         java.lang.String JavaDoc result = "";
340         if(the_parent_ != null)
341             result = the_parent_.getAbsoluteName();
342
343         if(name_ != null)
344             result = result + "::" + name_;
345
346         return result;
347     }
348
349     /**
350      * Set its repository ID.
351      *
352      * @param id The RepositoryId to set.
353      */

354     public void
355     setId(java.lang.String JavaDoc id)
356     {
357         id_ = id;
358         the_repository_.addDeclInRep(id, this);
359         org.omg.CORBA.Contained JavaDoc contained = getContained();
360         if (contained != null)
361             contained.id(id_);
362     }
363
364    /**
365     * Obtain its repository ID.
366     *
367     * @return Its RepositoryId.
368     */

369     public java.lang.String JavaDoc
370     getId()
371     {
372         if (id_ != null)
373             return id_;
374
375         return getIdPrefix() + ':' + computeBaseRID() + ':' + version_;
376     }
377
378     /**
379      * Set its prefix.
380      *
381      * @param prefix The prefix to set.
382      * Note that setting the prefix should only be done
383      * when a new prefix (ie different from the
384      * declared-in scope prefix) was explicitly declared.
385      */

386     public void
387     setPrefix(java.lang.String JavaDoc prefix)
388     {
389         prefix_ = prefix;
390
391         org.omg.CORBA.Contained JavaDoc contained = getContained();
392         if (contained != null)
393             contained.id(getId());
394     }
395
396     /**
397      * Obtain its prefix.
398      *
399      * @return The prefix of the declaration or
400      * null if there's no prefix.
401      */

402     public java.lang.String JavaDoc
403     getPrefix()
404     {
405         if (prefix_!=null)
406             return prefix_;
407         if ( (the_parent_!=null) &&
408              (the_parent_.getName()!=null) &&
409              (!the_parent_.getName().equals("")) )
410             return the_parent_.getPrefix();
411
412         return "";
413     }
414
415     /**
416      * Set its version.
417      *
418      * @param version The version to set.
419      */

420     public void
421     setVersion(java.lang.String JavaDoc version)
422     {
423         version_ = version;
424         org.omg.CORBA.Contained JavaDoc contained = getContained();
425         if (contained != null)
426             contained.version(version);
427     }
428
429     /**
430      * Obtain its version.
431      *
432      * @return The version of the declaration.
433      * Note that version "1.0" is assumed as default.
434      */

435     public java.lang.String JavaDoc
436     getVersion()
437     {
438         return version_;
439     }
440
441     /**
442      * Obtain its parent Scope.
443      *
444      * @return The parent Scope of the declaration
445      * or null if the declaration has no parent.
446      */

447     public Scope
448     getParent()
449     {
450         return the_parent_;
451     }
452
453     /**
454      * Create this Declaration into the Interface Repository.
455      */

456     public void
457     create()
458     {
459         if (the_parent_!=null)
460             the_parent_.addDecl(this);
461     }
462
463     /**
464      * Destroy this Declaration from the Interface Repository.
465      */

466     public void
467     destroy()
468     {
469         org.omg.CORBA.IRObject JavaDoc obj = (org.omg.CORBA.IRObject JavaDoc)getContained();
470         if (obj != null)
471         {
472             // destroy the IR3 Contained if it was not imported.
473
if (!is_imported_)
474                 obj.destroy();
475
476             the_repository_.removeDeclFromRep(getId());
477             if (the_parent_!=null)
478                 the_parent_.removeDecl(name_);
479         }
480     }
481
482     /**
483      * Get the associated AST.
484      */

485     public AST
486     getAST()
487     {
488         return the_repository_;
489     }
490
491     /**
492       * Obtain its category (IDL, PSDL, CIDL).
493       *
494       * @return The category of the declaration.
495       */

496     public DeclarationCategory
497     getCategory()
498     {
499         return DeclarationCategory.dc_idl;
500     }
501
502     /**
503      * Check if the Declaration is correct.
504      */

505     public void
506     check()
507     {
508         // To be overrided in sub-classes
509
}
510
511     /**
512      * Print the Declaration.
513      */

514     public void
515     print()
516     {
517         System.out.println(getAbsoluteName());
518     }
519 }
520
Popular Tags