KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > common > lib > GeneratorBase


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): Mathieu Vadet, Christophe Demarey.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.common.lib;
28
29
30 // Package dependencies.
31
import org.objectweb.openccm.ast.api.Declaration;
32 import org.objectweb.openccm.ast.api.DeclarationKind;
33 import org.objectweb.openccm.ast.api.AST;
34 import org.objectweb.openccm.ast.api.Scope;
35 import org.objectweb.openccm.ast.api.FileScope;
36 import org.objectweb.openccm.generator.common.lib.GenerationException;
37 import java.util.List JavaDoc;
38
39
40 /**
41  * This class allows naviguation in the OpenCCM AST
42  * and calls to map files.
43  **/

44 public class GeneratorBase
45      extends org.objectweb.openccm.generator.common.lib.Generator
46   implements org.objectweb.openccm.generator.common.api.GeneratorBase
47 {
48
49     // ===========================================================
50
//
51
// Internal State.
52
//
53
// ===========================================================
54

55     /**
56      * The Abstract Syntax Tree
57      **/

58     protected AST ast_;
59
60     /**
61      * Dependencies Tree of Declarations
62      **/

63     protected java.util.Hashtable JavaDoc dependencies_tree_;
64
65     /**
66      * The target declaration to generate
67      **/

68     protected Declaration target_decl_;
69
70
71     // ===========================================================
72
//
73
// Constructors.
74
//
75
// ===========================================================
76

77     /**
78      * The default constructor.
79      *
80      * @param ast - The Abstract Syntax Tree.
81      **/

82     public GeneratorBase(AST ast)
83     {
84         // Call the common Generator constructor
85
super();
86
87         // Init internal states
88
ast_ = ast;
89         disableLog();
90         initialize();
91     }
92
93     // ===========================================================
94
//
95
// Internal methods.
96
//
97
// ===========================================================
98

99     /**
100      * Compute dependencies for the scope to generate.
101      *
102      * @param decl - The declaration to visit.
103      *
104      * @return Declaration's dependencies.
105      **/

106     protected Declaration[]
107     computeDependencies(Declaration decl)
108     {
109         Declaration[] depend = decl.getDependencies();
110 /* System.err.println("### Found Dependencies : "+depend.length);
111                 for (int i=0;i<depend.length;i++)
112                     System.err.println(" "+depend[i].getAbsoluteName());
113 */

114         // build dependencies list in correct order
115
//
116
// TODO : inter module cycles are not taken into account
117
// intra module cycles will be resolved by forward declarations
118
//
119
List JavaDoc ordered_depend = new java.util.ArrayList JavaDoc();
120         for (int i=0;i<depend.length;i++)
121         {
122 // System.err.println("--+ : "+depend[i]);
123
// System.err.println("--+ : "+depend[i].getId());
124
// System.err.println("--+ : "+ordered_depend.indexOf(depend[i]));
125
if ( isBusinessDeclaration(depend[i]) && (ordered_depend.indexOf(depend[i])==-1) )
126                 ordered_depend.add(depend[i]);
127         }
128
129         ordered_depend.add(decl);
130         depend = (Declaration[]) ordered_depend.toArray(new Declaration[0]);
131
132 /* System.err.println("### Dependency list cleaned : "+depend.length);
133                 for (int i=0;i<depend.length;i++)
134                     System.err.println(" "+depend[i].getAbsoluteName());
135 */

136         java.util.Arrays.sort(depend, new org.objectweb.openccm.ast.lib.DependencyComparator());
137 /*
138                 System.err.println("### Dependency list sorted : "+depend.length);
139                 for (int i=0;i<depend.length;i++)
140                     System.err.println(" "+depend[i].getAbsoluteName());
141 */

142         return depend;
143     }
144
145     /**
146      * Build dependencies tree for declarations passed in parameter.
147      * For each declaration, the parent is added in the tree until
148      * the parent is the root repository or a file scope.
149      *
150      * @param decls - An array of declarations.
151      *
152      * @return A List containing root declarations.
153      **/

154     protected List JavaDoc
155     dependenciesTreeUpdate(Declaration[] decls)
156     {
157         Declaration decl = null;
158         Scope parent = null;
159         List JavaDoc childs = null;
160         String JavaDoc root = new String JavaDoc("repository");
161         List JavaDoc root_childs = new java.util.ArrayList JavaDoc();
162
163         // Create a new table
164
dependencies_tree_ = new java.util.Hashtable JavaDoc();
165
166         // Add the root key
167
dependencies_tree_.put(root, root_childs);
168
169         // Compute each declaration
170
for (int i=0;i<decls.length;i++)
171         {
172             decl = decls[i];
173             parent = decl.getParent();
174
175             while ( (parent.getDeclKind() != DeclarationKind.dk_repository) &&
176                     !(parent instanceof FileScope) )
177             {
178                 if (dependencies_tree_.containsKey(parent))
179                 {
180                     childs = (List JavaDoc)dependencies_tree_.get(parent);
181                     if (childs.indexOf(decl) == -1)
182                         childs.add(decl);
183                     // continue;
184
}
185                 else
186                 {
187                     childs = new java.util.ArrayList JavaDoc();
188                     childs.add(decl);
189                     dependencies_tree_.put(parent, childs);
190                 }
191
192                 decl = parent;
193                 parent = decl.getParent();
194             }// end while
195
// Add this declaration into the root key
196
if (root_childs.indexOf(decl) == -1)
197                 root_childs.add(decl);
198         }//end for
199

200 /* System.out.println("### Dependency tree : ");
201         for (java.util.Enumeration e = dependencies_tree_.keys() ; e.hasMoreElements() ;)
202         {
203             Object o = e.nextElement();
204             try{
205                System.out.println("Key :"+((Declaration)o).getAbsoluteName());
206             }catch(Exception ex){
207                System.out.println("Root :"+o);
208             }
209             List elements = (List)dependencies_tree_.get(o);
210             System.out.println("elements : "+elements);
211             for (java.util.Enumeration e2 = elements.elements() ; e2.hasMoreElements() ;)
212             {
213                 System.out.println(" + "+((Declaration)e2.nextElement()).getAbsoluteName());
214             }
215         }
216 */

217         return root_childs;
218     }
219
220     /**
221      * Method called after generation.
222      * Generally used to close files.
223      **/

224     private void
225     after_generation()
226     {
227         // To redefine if necessary
228
}
229
230     // ===========================================================
231
//
232
// Public methods.
233
//
234
// ===========================================================
235

236     /**
237      * Initialize the generator.
238      **/

239     public void
240     initialize()
241     {
242         dependencies_tree_ = null;
243         target_decl_ = null;
244     }
245
246     /**
247      * Obtains the OpenCCM's Abstract Syntax Tree.
248      *
249      * @return The OpenCCM's Abstract Syntax Tree.
250      */

251     public org.objectweb.openccm.ast.api.AST
252     getAST()
253     {
254         return ast_;
255     }
256
257     /**
258      * Is the declaration a business (i.e. a non system) declaration ?
259      *
260      * @param decl - The declaration.
261      *
262      * @return True if the declaration is business declaration, false otherwise.
263      **/

264     public boolean
265     isBusinessDeclaration(Declaration decl)
266     {
267         String JavaDoc abs_name = decl.getAbsoluteName();
268         if (abs_name.equals("::CORBA::TypeCode"))
269         {
270             return false;
271         }
272         else if (abs_name.startsWith("::CORBA"))
273         {
274             return false;
275         }
276         else if (abs_name.startsWith("::Components"))
277         {
278             return false;
279         }
280
281         return true;
282     }
283
284
285     /**
286      * Visit a declaration in the repository
287      *
288      * @param obj - The declaration to visit.
289      **/

290     public void
291     visit(Declaration obj)
292     {
293         Declaration[] depend = null;
294
295         // get the first non module scope that contains the target object
296
Declaration nmodule = obj;
297         Declaration current = obj;
298         while ((current.getDeclKind()!= DeclarationKind.dk_repository) &&
299                (current.getDeclKind()!= DeclarationKind.dk_module))
300         {
301             nmodule = current;
302             current = nmodule.getParent();
303         }
304
305         // set the first non module scope as target declaration
306
target_decl_ = nmodule;
307
308         // Compute declaration dependencies
309
// depend = computeDependencies(nmodule);
310
depend = new Declaration[1];
311         depend[0] = target_decl_;
312
313         // build dependencies tree (declaration tree)
314
List JavaDoc root_decls = null;
315
316         root_decls = dependenciesTreeUpdate(depend);
317         visit_dep_tree(root_decls);
318     }
319
320     /**
321      * Visit dependencies tree.
322      *
323      * @param childs - Declarations to visit.
324      **/

325     public void
326     visit_dep_tree(List JavaDoc childs)
327     {
328         if (childs != null)
329         {
330             forward(childs);
331             for (int i=0; i<childs.size(); i++)
332             {
333                 Declaration child = (Declaration)childs.get(i);
334
335                 if (child == target_decl_)
336                 {
337                     visitContained(child, "");
338                 }
339                 else if ( child.getDeclKind() == DeclarationKind.dk_module )
340                 {
341                     put("obj", child);
342                     if ( dependencies_tree_.get(child) != null )
343                         map("MODULE_WITH_DEPENDENCIES");
344                     else
345                         map("MODULE");
346                 }
347                 else
348                 {
349                     visitContained(child, "");
350                 }
351             }
352         }
353     }
354
355     /**
356      * Visit dependencies tree.
357      *
358      * @param decl - The declaration to visit.
359      **/

360     public void
361     visit_dep_tree(Declaration decl)
362     {
363         List JavaDoc childs = null;
364
365         childs = (List JavaDoc)dependencies_tree_.get(decl);
366         visit_dep_tree(childs);
367     }
368
369     /**
370      * Visit declarations.
371      *
372      * @param vect - A List containing Declarations to visit.
373      **/

374     public void
375     visit(List JavaDoc vect)
376     {
377         Declaration[] decls = (Declaration[]) vect.toArray(new Declaration[0]);
378         // build dependencies tree (declaration tree)
379
List JavaDoc root_decls = null;
380
381         root_decls = dependenciesTreeUpdate(decls);
382         visit_dep_tree(root_decls);
383     }
384
385     /**
386      * Visit a single contained declaration
387      *
388      * @param contained - The declaration to visit.
389      * @param extension - used to map id
390      **/

391     public void
392     visitContained(Declaration contained,
393                    String JavaDoc extension)
394     {
395         String JavaDoc id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(contained.getDeclKind()).toUpperCase()+ extension;
396
397         put("obj", contained);
398         map(id);
399     }
400
401     /**
402      * Utility method to check forward declarations.
403      *
404      * @return The associated search mask.
405      **/

406     public long
407     forward_check()
408     {
409         // Must be overrided in sub-classes
410
return 0;
411     }
412
413     /**
414      * Generate forwards declaration from a List of declarations.
415      *
416      * @param vect - The List of declarations.
417      **/

418     public void
419     forward(List JavaDoc vect)
420     {
421         Declaration[] decls = (Declaration[])vect.toArray(new Declaration[0]);
422
423         for (int i=0;i<decls.length;i++)
424         {
425             if ((decls[i].getDeclKind() & forward_check())!=0)
426                 visitContained(decls[i], "_FORWARD");
427         }
428     }
429
430     /**
431      * Generate forwards declaration from a scope.
432      *
433      * @param scope - The Scope containing declarations.
434      **/

435     public void
436     forward(org.objectweb.openccm.ast.api.Scope scope)
437     {
438         Declaration[] objs = null;
439         objs = scope.getContents(true, forward_check());
440
441         for (int i=0;i<objs.length;i++)
442             visitContained(objs[i], "_FORWARD");
443
444         put("obj", scope);
445     }
446
447     /**
448      * Generate contents of a declaration
449      *
450      * @param decl - The declaration.
451      **/

452     public void
453     contents(org.objectweb.openccm.ast.api.Scope scope,
454              long limited)
455     {
456         Declaration[] objs = null;
457         objs = scope.getContents(true, limited);
458
459         for (int i=0;i<objs.length;i++)
460         {
461             visitContained(objs[i], "");
462         }
463         put("obj", scope);
464    }
465
466     /**
467      * Get a declaration.
468      * name must be a valid declaration name.
469      *
470      * @param name - The name of the declaration to get.
471      *
472      * @return The matching declaration.
473      **/

474     public Declaration
475     getDeclaration(String JavaDoc name)
476     throws GenerationException
477     {
478         Declaration decl = null;
479
480         if (getAST() == null)
481         {
482             String JavaDoc msg = "[Error]: Interface Repository has not been fed!";
483             throw new GenerationException(msg);
484         }
485
486         // Search the object to generate
487
decl = getAST().lookup(name);
488         if (decl == null)
489         {
490             String JavaDoc msg = "[Error]: object " + name + " not found : aborting generation !";
491             throw new GenerationException(msg);
492         }
493         return decl;
494     }
495
496     /**
497      * Get a declaration from a scope.
498      * name must be a valid declaration name.
499      *
500      * @param scope - The scope to visit.
501      * @param name - The name of the declaration to get.
502      *
503      * @return The matching declaration.
504      **/

505     public Declaration
506     getDeclaration(Scope scope, String JavaDoc name)
507     throws GenerationException
508     {
509         Declaration[] decls = null;
510
511         if (scope == null)
512         {
513             String JavaDoc msg = "[Error]: Scope is not valid!";
514             throw new GenerationException(msg);
515         }
516
517         decls = scope.getContents(true, DeclarationKind.dk_all);
518         for (int i=0;i<decls.length;i++)
519         {
520             if( decls[i].getName().equalsIgnoreCase(name) )
521                 return decls[i];
522         }
523         return null;
524     }
525
526     /**
527      * Get contents of the scope matching limited_types.
528      *
529      * @param scope - The scope to visit.
530      * @param limited_types - A logical combination of DeclarationKind.
531      *
532      * @return The list of contained declarations.
533      **/

534     public List JavaDoc
535     getDeclarations(Scope scope, long limited_types)
536     throws GenerationException
537     {
538         Declaration[] decls = null;
539         List JavaDoc list = new java.util.ArrayList JavaDoc();
540
541         if (scope == null)
542         {
543             String JavaDoc msg = "[Error]: Scope is not valid!";
544             throw new GenerationException(msg);
545         }
546
547         decls = scope.getContents(true, limited_types);
548         for (int i=0;i<decls.length;i++)
549         {
550             if( isBusinessDeclaration(decls[i]) )
551             {
552                 // System.out.println("## find decl <"+decls[i].getAbsoluteName()+">");
553
list.add(decls[i]);
554             }
555         }
556         return list;
557     }
558
559     /**
560      * Get all declarations matching with limited_types contained in the file scope,
561      * including sub-scopes.
562      *
563      * @param limited_types - A logical combination of DeclarationKind.
564      *
565      * @return The list of founded declarations.
566      **/

567     public List JavaDoc
568     getAllDeclarations(Scope scope, long limited_types)
569     throws GenerationException
570     {
571         Declaration[] decls = null;
572         List JavaDoc result = new java.util.ArrayList JavaDoc();
573
574         if (scope == null)
575         {
576             String JavaDoc msg = "[Error]: Scope is not valid!";
577             throw new GenerationException(msg);
578         }
579
580         decls = scope.getContents(true, limited_types+DeclarationKind.dk_module);
581
582         for (int i=0; i<decls.length; i++)
583         {
584             if ((decls[i].getDeclKind()&limited_types) != 0)
585             {
586                 result.add(decls[i]);
587             }
588             else
589             {
590                 // This is a module
591
if ( isBusinessDeclaration(decls[i]))
592                     result.addAll(getAllDeclarations((Scope)decls[i], limited_types));
593             }
594         }
595         return result;
596     }
597 }
598
Popular Tags