KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > CIDLIDLGenerator


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@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.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33 // import of OpenCCM modules
34
import org.objectweb.corba.ast.api.*;
35 import org.objectweb.corba.ast.lib.*;
36 import org.objectweb.corba.generator.java.lib.*;
37 import org.objectweb.corba.generator.java.ast.api.*;
38 import org.objectweb.corba.generator.java.ast.lib.*;
39 import org.objectweb.corba.generator.common.lib.GenerationException;
40
41 /**
42  ** <p>Generator class for cidl_idl tool.</p>
43  **
44  ** <p>NOTE: for each declaration in the root scope of the file, a file containing its IDL mapping is
45  ** generated. The filename is built using the IDL absolute name of the declaration and replacing
46  ** the "::" string by the "_" character (and removing the first "::") and appending "_seg.idl" string.</p>
47  **/

48 public class CIDLIDLGenerator
49 extends org.objectweb.corba.generator.common.lib.GeneratorBase
50 {
51     // generator
52
private IDLTranslator _translator;
53     private String JavaDoc _last_prefix;
54     private java.util.Stack JavaDoc _module_stack;
55
56     // default constructor
57
public
58     CIDLIDLGenerator(AST ast)
59     {
60         super(ast);
61
62         _translator = new IDLTranslator();
63         _last_prefix = null;
64         _module_stack = null;
65     }
66
67     //
68
// internal operations
69
//
70

71     private IDLTranslator
72     getTranslator()
73     {
74         return _translator;
75     }
76
77     private java.util.Stack JavaDoc
78     computeContentStack(Declaration decl)
79     {
80         // NOTE: compute a stack of scope starting at the root scope (ie the repository)
81
// and ending at the target declaration
82
// This allows generating only the required modules (without their full content)
83
// until reaching the target declaration
84
// NOTE2: the stack does not contain the target decl
85

86         // create tree as an array list
87
// NOTE: only one child per parent
88
java.util.Stack JavaDoc stack = new java.util.Stack JavaDoc();
89
90         Declaration curr = decl;
91         Declaration parent = decl.getParent();
92         while ((parent.getDeclKind()!=DeclarationKind.dk_repository) &&
93                !(parent instanceof FileScope)) {
94             stack.push(parent);
95             curr = parent;
96             parent = curr.getParent();
97         }
98
99         return stack;
100     }
101
102     private void
103     generateForDeclaration(Declaration decl)
104     throws GenerationException
105     {
106         // generation is done only for:
107
// - module
108
// - component
109
if (decl.getDeclKind()==DeclarationKind.dk_module) {
110             generateForModule((ModuleDecl)decl);
111         } else if (decl.getDeclKind()==DeclarationKind.dk_component) {
112             generateForComponent((ComponentDecl)decl);
113         }
114     }
115
116     private void
117     generateForModule(ModuleDecl module)
118     throws GenerationException
119     {
120         put("obj", module);
121         map("MODULE");
122     }
123
124     private void
125     generateForComponent(ComponentDecl comp)
126     throws GenerationException
127     {
128         // NOTE: no CIDL, assume monolithic
129
// for a component C1:
130
// component C1 {
131
// provides I1 f_i1;
132
// uses I2 r_i2;
133
// consumes E1 c_e1;
134
// emits E2 e_e2;
135
// };
136
// the following interface is generated:
137
// interface C1_MainSeg : C1, I1, E1Consumer {};
138
//
139
// for a component C2:
140
// component C2 : C1 {};
141
// the following interface is generated:
142
// interface C2_MainSeg : C2, I1, E1Consumer {};
143
//
144

145         // build the list of inherited interfaces
146
StringBuffer JavaDoc list = new StringBuffer JavaDoc();
147         Declaration[] decls = null;
148
149         // facets
150
decls = comp.getContents(false, DeclarationKind.dk_provides);
151         for (int i=0;i<decls.length;i++) {
152             ProvidesDecl facet = (ProvidesDecl)decls[i];
153             list.append(", "+facet.getInterface().getAbsoluteName());
154         }
155
156         // sinks
157
decls = comp.getContents(false, DeclarationKind.dk_consumes);
158         for (int i=0;i<decls.length;i++) {
159             ConsumesDecl sink = (ConsumesDecl)decls[i];
160             list.append(", "+sink.getEvent().getAbsoluteName()+"Consumer");
161         }
162
163         put("obj", comp);
164         put("segment_itf_list", list);
165         map("COMPONENT");
166     }
167
168     //
169
// public operations for .vm script (must return a string)
170
//
171

172     final public String JavaDoc
173     getMacro(Declaration decl)
174     {
175         String JavaDoc res = "";
176         String JavaDoc version = decl.getVersion();
177         String JavaDoc id = decl.getId();
178         String JavaDoc prefix = decl.getPrefix();
179
180         // System.err.println("Macro for " + decl.getAbsoluteName() + " : " + prefix);
181
if (!prefix.equals(_last_prefix))
182         {
183             res = res + "#pragma prefix \""+prefix+"\"\n";
184             _last_prefix = prefix;
185         }
186         else
187         {
188             String JavaDoc p_id = decl.getParent().getId();
189             int idx1 = p_id.indexOf(':');
190             int idx2 = p_id.lastIndexOf(':');
191             if (id.indexOf(p_id.substring(idx1, idx2))==-1)
192                 res = res + "#pragma id "+decl.getName()+" \""+id+"\"\n";
193         }
194         if (!version.equals("1.0"))
195             res = res + "#pragma version "+decl.getName()+" "+version+"\n";
196
197         return res;
198     }
199
200     final public String JavaDoc
201     generateContents(Scope scope)
202     throws GenerationException
203     {
204         Declaration[] decls = scope.getContents(false, DeclarationKind.dk_all);
205         for (int i=0;i<decls.length;i++) {
206             generateForDeclaration(decls[i]);
207         }
208
209         return "";
210     }
211
212     final public String JavaDoc
213     generateWithContents(java.util.Stack JavaDoc stack)
214     throws GenerationException
215     {
216         // get the top declaration
217
Object JavaDoc obj = stack.pop();
218         put("obj", obj);
219
220         if (stack.empty()) {
221             map("MODULE");
222         } else {
223             put("content", stack);
224             map("MODULE_WITH_CONTENT");
225         }
226
227         return "";
228     }
229
230     final public String JavaDoc
231     pushModule(ModuleDecl module)
232     {
233         _module_stack.push(module);
234         return "";
235     }
236
237     final public String JavaDoc
238     popModule()
239     {
240         Object JavaDoc obj = _module_stack.pop();
241         put("obj", obj);
242         return "";
243     }
244
245     //
246
// public operations
247
//
248

249     final public void
250     initialize(org.objectweb.util.cmdline.api.Console console)
251     {
252         // ressource path
253
java.util.Vector JavaDoc rpath = new java.util.Vector JavaDoc();
254         rpath.add(System.getProperty("ecm.templates"));
255         setRessourcePath(rpath);
256
257         // templates
258
java.util.Vector JavaDoc libs = new java.util.Vector JavaDoc();
259         libs.add("cidl_idl.vm");
260         setLibrary(libs);
261
262         // call the common generator init method
263
//log_enabled_ = true; // set to false by the constructor of GeneratorBase class
264
super.init();
265
266         // initialize velocity context
267
put("which", "cidl_idl");
268         put("generator", this);
269     }
270
271     final public void
272     generate(String JavaDoc basedir, Declaration decl,
273              org.objectweb.util.cmdline.api.Console console)
274     throws GenerationException
275     {
276         console.message("Will generate for: "+decl.getAbsoluteName());
277
278
279         // build output filename from declaration name by replacing "::" by "_" and appending "_seg.idl"
280
// remove the first "::"
281
String JavaDoc absname = decl.getAbsoluteName().substring(2);
282         String JavaDoc basename = absname.replaceAll("::", "_");
283         String JavaDoc filename = basedir+java.io.File.separator+basename+"_seg.idl";
284         open(filename, "out");
285
286         // general elements
287
_last_prefix = "";
288         _module_stack = new java.util.Stack JavaDoc();
289         put("FILE_IFDEF_MACRO", "__"+basename.toUpperCase()+"_SEG__");
290
291         // include file with name without "_seg" (as generated by OpenCCM ir3_idl2 generator)
292
java.util.ArrayList JavaDoc includes = new java.util.ArrayList JavaDoc();
293         includes.add(basename+".idl");
294         put("includes", includes);
295
296         map("FILE_HEADER");
297         generateWithContents(computeContentStack(decl));
298         map("FILE_FOOTER");
299
300         close("out");
301         console.message("File '"+filename+"' has been generated.");
302     }
303 }
304
305
306
Popular Tags