KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > idl > lib > IDL2Generator


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

26
27 package org.objectweb.openccm.generator.idl.lib;
28
29
30 /** To access all the AST Declaration. */
31 import org.objectweb.openccm.ast.api.Declaration;
32
33 /** To access AST DeclarationKind. */
34 import org.objectweb.openccm.ast.api.DeclarationKind;
35
36 /** To access all the AST Scope. */
37 import org.objectweb.openccm.ast.api.Scope;
38
39 /** To access AST FileScope. */
40 import org.objectweb.openccm.ast.api.FileScope;
41
42 /** To access Generation Exception. */
43 import org.objectweb.openccm.generator.common.lib.GenerationException;
44
45 /** To use ArrayList. */
46 import java.util.ArrayList JavaDoc;
47
48 /** To access List. */
49 import java.util.List JavaDoc;
50
51 /** To use Hashtable. */
52 import java.util.Hashtable JavaDoc;
53
54
55 public class IDL2Generator
56      extends IDL3Generator
57   implements org.objectweb.openccm.generator.idl.api.IDL2Generator
58 {
59
60     // ===========================================================
61
//
62
// Internal State.
63
//
64
// ===========================================================
65

66     /**
67      * All local declarations.
68      * key = parent_id
69      * value = an array list of childs
70      **/

71     protected Hashtable JavaDoc local_decls_;
72
73     /**
74      * True if declarations are mapped into a single file.
75      **/

76     protected boolean single_file_;
77
78
79     // ===========================================================
80
//
81
// Constructors.
82
//
83
// ===========================================================
84

85     /**
86      * The default constructor.
87      *
88      * @param ast - The Abstract Syntax Tree.
89      */

90
91     public IDL2Generator(org.objectweb.openccm.ast.api.AST ast)
92     {
93         // Call the IDL3Generator constructor
94
super(ast);
95
96         single_file_ = false;
97     }
98
99     // ===========================================================
100
//
101
// Internal methods.
102
//
103
// ===========================================================
104

105     /**
106      * Put the target declaration in the Hashtable,
107      * i.e. add all parents declarations.
108      *
109      * @param target_decl - The target declaration to add.
110      **/

111     private void
112     putTargetDecl(Declaration target_decl)
113     {
114         Declaration current = null,
115                     parent = null;
116         ArrayList JavaDoc childs = null;
117
118         current = target_decl;
119         parent = current.getParent();
120
121         while ( (parent != null) &&
122                 (parent.getDeclKind() != DeclarationKind.dk_repository) &&
123                 !(parent instanceof FileScope) )
124         {
125             childs = new ArrayList JavaDoc();
126             childs.add(current);
127             local_decls_.put(parent.getId(), childs);
128             current = parent;
129             parent = current.getParent();
130         }
131
132         childs = new ArrayList JavaDoc();
133         childs.add(current);
134         local_decls_.put("root", childs);
135     }
136
137     /**
138      * Put a declaration in the Hashtable.
139      **/

140     private void
141     putDecl(Declaration child)
142     {
143         Declaration parent = child.getParent();
144         String JavaDoc parent_id = parent.getId();
145         ArrayList JavaDoc childs = null;
146
147         // Is the parent in the Hashtable ?
148
childs = (ArrayList JavaDoc)local_decls_.get(parent_id);
149         if (childs == null)
150         {
151             childs = new ArrayList JavaDoc();
152             childs.add(child);
153             local_decls_.put(parent_id, childs);
154             // Add the parent if necessary
155
if ( child.getAbsoluteName().startsWith(target_decl_.getAbsoluteName())
156                  && (parent.getId() != target_decl_.getId()) )
157                 putDecl(parent);
158         }
159         else
160         {
161             // Only add the declaration to childs
162
if (!childs.contains(child))
163                 childs.add(child);
164         }
165     }
166
167     /**
168      * Generate local idl2.
169      *
170      * @param id - The file identifier for local declarations generation.
171      **/

172     private void
173     generate_local_idl2(String JavaDoc id)
174     {
175         ArrayList JavaDoc decls = (ArrayList JavaDoc)local_decls_.get(id);
176         // If there is no declarations in scope, just return.
177
if (decls == null) return ;
178
179         java.util.Iterator JavaDoc it = decls.iterator();
180
181         while (it.hasNext())
182         {
183             Declaration decl = (Declaration)it.next();
184             long dk = decl.getDeclKind();
185
186             if ( dk == DeclarationKind.dk_module)
187             {
188                 // If the module is empty, just return.
189
if (local_decls_.get(decl.getId()) == null) return;
190                 print(macro(decl));
191                 print("module " + checkName(decl.getName()) + "\n");
192                 print("{/inc\n");
193                 local_forward( (org.objectweb.openccm.ast.api.Scope)decl );
194                 map("END_LINE");
195                 generate_local_idl2(decl.getId());
196                 print("/dec};\n");
197             }
198             else
199             {
200                 // Map local interfaces
201
String JavaDoc decl_id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(dk).toUpperCase();
202                 put("obj", decl);
203                 map(decl_id);
204             }
205         }
206     }
207
208     /**
209      * Method called after generation.
210      * Generally used to close files.
211      **/

212     protected void
213     after_generation()
214     {
215         super.after_generation();
216
217         // Generate local idl2
218
if ( !single_file_ )
219         {
220             switchToFile("local");
221             last_prefix_ = "";
222             generate_local_idl2("root");
223             close("local");
224         }
225     }
226
227     // ===========================================================
228
//
229
// Useful methods for IDL2 Mapping.
230
//
231
// ===========================================================
232

233     /**
234      * Utility method to check forward declarations.
235      *
236      * @return The associated search mask.
237      **/

238     public long
239     forward_check()
240     {
241         //
242
// TODO : do not forward struct and unions because no orb supports it yet !
243
//
244
return ( DeclarationKind.dk_interface +
245                  DeclarationKind.dk_local_interface +
246                  DeclarationKind.dk_abstract_interface +
247                  DeclarationKind.dk_component +
248                  DeclarationKind.dk_value +
249                  // DeclarationKind.dk_struct +
250
// DeclarationKind.dk_union +
251
DeclarationKind.dk_event );
252     }
253
254     // ===========================================================
255
//
256
// Methods for IDL2 Mapping.
257
//
258
// ===========================================================
259

260
261     // ===========================================================
262
//
263
// Public methods.
264
//
265
// ===========================================================
266

267     /**
268      * Initialize the generator.
269      * If outputfile equals to outputfile_local, declarations will be
270      * genarated in the same file.
271      *
272      * @param outputfile - The name where declarations will be generated.
273      * @param outputfile_loacal - The file name to write local declarations in.
274      * @param includes - files to include (with <file>).
275      * @param userIncludes - user files to include (with "file").
276      * @param target_decl - The scope to visit.
277      * @param app_name - The application's name.
278      */

279     public void
280     initialize( String JavaDoc outputfile,
281                 String JavaDoc outputfile_local,
282                 ArrayList JavaDoc includes,
283                 ArrayList JavaDoc userIncludes,
284                 Scope target_decl,
285                 String JavaDoc app_name )
286     {
287         String JavaDoc file_macro = null;
288
289         super.initialize( outputfile,
290                           new ArrayList JavaDoc(),
291                           app_name );
292
293         /** Compare input and output file **/
294         single_file_ = outputfile.equals(outputfile_local);
295
296         /** Initialize velocity context **/
297         put("userIncludes", userIncludes);
298         put("includes", includes);
299
300         if (target_decl != null)
301         {
302             file_macro = get_macro(target_decl);
303         }
304         else
305         {
306             java.io.File JavaDoc f = new java.io.File JavaDoc(outputfile_local);
307             file_macro = "__FILE_" + f.getName().replace('.', '_') + "__";
308         }
309         put("FILE_MACRO", file_macro);
310
311         if ( !single_file_ )
312         {
313             /** The output file for local declarations **/
314             open(outputfile_local, "local");
315             map("LICENSE", "local");
316             print("\n#ifndef " + file_macro + "LOCAL", "local");
317             print("\n#define " + file_macro + "LOCAL\n", "local");
318             // Import non local IDL2 declarations
319
java.io.File JavaDoc out = new java.io.File JavaDoc(outputfile);
320             print("#include \"" + out.getName() + "\"\n\n", "local");
321
322             /** Set the default output **/
323             switchToFile("out");
324         }
325     }
326
327     /**
328      * Generates contents of a scope.
329      *
330      * @param target_decl - The scope to visit.
331      *
332      * @throws GenerationException
333      **/

334     public void
335     generate(Scope target_decl)
336     throws GenerationException
337     {
338         // Init local declarations hashtable
339
local_decls_ = new Hashtable JavaDoc();
340         putTargetDecl(target_decl);
341
342         super.generate(target_decl);
343     }
344
345     /**
346      * Generates a List of declarations.
347      *
348      * @param decls - Declarations to visit.
349      **/

350     public void
351     generate(List JavaDoc decls)
352     throws org.objectweb.openccm.generator.common.lib.GenerationException
353     {
354         put("obj", decls);
355         map("FILE_HEADER");
356         visit(decls);
357         after_generation();
358     }
359
360     /**
361      * Visit a single contained declaration
362      *
363      * @param contained - The declaration to visit.
364      * @param extension - used to map id
365      **/

366     public void
367     visitContained(Declaration contained,
368                    String JavaDoc extension)
369     {
370         if ( single_file_ )
371         {
372             super.visitContained(contained, extension);
373         }
374         else
375         {
376             long dk = contained.getDeclKind();
377             String JavaDoc id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(dk).toUpperCase()+ extension;
378
379             put("obj", contained);
380             // System.err.println("Visit contained :"+contained.getAbsoluteName()+" | "+id+" | "+contained.getPrefix() );
381
if ( dk == DeclarationKind.dk_local_interface)
382             {
383                 putDecl(contained);
384             }
385             else
386             {
387                 map(id);
388             }
389         }
390     }
391
392     /**
393      * Generate forwards declaration from a scope for local declarations.
394      *
395      * @param scope - The Scope containing declarations.
396      **/

397     public void
398     local_forward(org.objectweb.openccm.ast.api.Scope scope)
399     {
400         Declaration[] objs = null;
401         objs = scope.getContents(true, DeclarationKind.dk_local_interface);
402
403         for (int i=0;i<objs.length;i++)
404             super.visitContained(objs[i], "_FORWARD");
405
406         put("obj", scope);
407     }
408
409 }
410
Popular Tags