1 28 29 30 package org.objectweb.ccm.runtime; 31 32 import org.objectweb.corba.runtime.*; 33 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 48 public class CIDLIDLGenerator 49 extends org.objectweb.corba.generator.common.lib.GeneratorBase 50 { 51 private IDLTranslator _translator; 53 private String _last_prefix; 54 private java.util.Stack _module_stack; 55 56 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 71 private IDLTranslator 72 getTranslator() 73 { 74 return _translator; 75 } 76 77 private java.util.Stack 78 computeContentStack(Declaration decl) 79 { 80 86 java.util.Stack stack = new java.util.Stack (); 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 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 145 StringBuffer list = new StringBuffer (); 147 Declaration[] decls = null; 148 149 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 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 172 final public String 173 getMacro(Declaration decl) 174 { 175 String res = ""; 176 String version = decl.getVersion(); 177 String id = decl.getId(); 178 String prefix = decl.getPrefix(); 179 180 if (!prefix.equals(_last_prefix)) 182 { 183 res = res + "#pragma prefix \""+prefix+"\"\n"; 184 _last_prefix = prefix; 185 } 186 else 187 { 188 String 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 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 213 generateWithContents(java.util.Stack stack) 214 throws GenerationException 215 { 216 Object 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 231 pushModule(ModuleDecl module) 232 { 233 _module_stack.push(module); 234 return ""; 235 } 236 237 final public String 238 popModule() 239 { 240 Object obj = _module_stack.pop(); 241 put("obj", obj); 242 return ""; 243 } 244 245 249 final public void 250 initialize(org.objectweb.util.cmdline.api.Console console) 251 { 252 java.util.Vector rpath = new java.util.Vector (); 254 rpath.add(System.getProperty("ecm.templates")); 255 setRessourcePath(rpath); 256 257 java.util.Vector libs = new java.util.Vector (); 259 libs.add("cidl_idl.vm"); 260 setLibrary(libs); 261 262 super.init(); 265 266 put("which", "cidl_idl"); 268 put("generator", this); 269 } 270 271 final public void 272 generate(String 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 String absname = decl.getAbsoluteName().substring(2); 282 String basename = absname.replaceAll("::", "_"); 283 String filename = basedir+java.io.File.separator+basename+"_seg.idl"; 284 open(filename, "out"); 285 286 _last_prefix = ""; 288 _module_stack = new java.util.Stack (); 289 put("FILE_IFDEF_MACRO", "__"+basename.toUpperCase()+"_SEG__"); 290 291 java.util.ArrayList includes = new java.util.ArrayList (); 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 |