1 26 27 package org.objectweb.openccm.generator.idl.lib; 28 29 30 31 import org.objectweb.openccm.ast.api.Declaration; 32 33 34 import org.objectweb.openccm.ast.api.DeclarationKind; 35 36 37 import org.objectweb.openccm.ast.api.Scope; 38 39 40 import org.objectweb.openccm.ast.api.FileScope; 41 42 43 import org.objectweb.openccm.generator.common.lib.GenerationException; 44 45 46 import java.util.ArrayList ; 47 48 49 import java.util.List ; 50 51 52 import java.util.Hashtable ; 53 54 55 public class IDL2Generator 56 extends IDL3Generator 57 implements org.objectweb.openccm.generator.idl.api.IDL2Generator 58 { 59 60 66 71 protected Hashtable local_decls_; 72 73 76 protected boolean single_file_; 77 78 79 85 90 91 public IDL2Generator(org.objectweb.openccm.ast.api.AST ast) 92 { 93 super(ast); 95 96 single_file_ = false; 97 } 98 99 105 111 private void 112 putTargetDecl(Declaration target_decl) 113 { 114 Declaration current = null, 115 parent = null; 116 ArrayList 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 (); 126 childs.add(current); 127 local_decls_.put(parent.getId(), childs); 128 current = parent; 129 parent = current.getParent(); 130 } 131 132 childs = new ArrayList (); 133 childs.add(current); 134 local_decls_.put("root", childs); 135 } 136 137 140 private void 141 putDecl(Declaration child) 142 { 143 Declaration parent = child.getParent(); 144 String parent_id = parent.getId(); 145 ArrayList childs = null; 146 147 childs = (ArrayList )local_decls_.get(parent_id); 149 if (childs == null) 150 { 151 childs = new ArrayList (); 152 childs.add(child); 153 local_decls_.put(parent_id, childs); 154 if ( child.getAbsoluteName().startsWith(target_decl_.getAbsoluteName()) 156 && (parent.getId() != target_decl_.getId()) ) 157 putDecl(parent); 158 } 159 else 160 { 161 if (!childs.contains(child)) 163 childs.add(child); 164 } 165 } 166 167 172 private void 173 generate_local_idl2(String id) 174 { 175 ArrayList decls = (ArrayList )local_decls_.get(id); 176 if (decls == null) return ; 178 179 java.util.Iterator 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 (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 String decl_id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(dk).toUpperCase(); 202 put("obj", decl); 203 map(decl_id); 204 } 205 } 206 } 207 208 212 protected void 213 after_generation() 214 { 215 super.after_generation(); 216 217 if ( !single_file_ ) 219 { 220 switchToFile("local"); 221 last_prefix_ = ""; 222 generate_local_idl2("root"); 223 close("local"); 224 } 225 } 226 227 233 238 public long 239 forward_check() 240 { 241 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_event ); 252 } 253 254 260 261 267 279 public void 280 initialize( String outputfile, 281 String outputfile_local, 282 ArrayList includes, 283 ArrayList userIncludes, 284 Scope target_decl, 285 String app_name ) 286 { 287 String file_macro = null; 288 289 super.initialize( outputfile, 290 new ArrayList (), 291 app_name ); 292 293 294 single_file_ = outputfile.equals(outputfile_local); 295 296 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 f = new java.io.File (outputfile_local); 307 file_macro = "__FILE_" + f.getName().replace('.', '_') + "__"; 308 } 309 put("FILE_MACRO", file_macro); 310 311 if ( !single_file_ ) 312 { 313 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 java.io.File out = new java.io.File (outputfile); 320 print("#include \"" + out.getName() + "\"\n\n", "local"); 321 322 323 switchToFile("out"); 324 } 325 } 326 327 334 public void 335 generate(Scope target_decl) 336 throws GenerationException 337 { 338 local_decls_ = new Hashtable (); 340 putTargetDecl(target_decl); 341 342 super.generate(target_decl); 343 } 344 345 350 public void 351 generate(List 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 366 public void 367 visitContained(Declaration contained, 368 String extension) 369 { 370 if ( single_file_ ) 371 { 372 super.visitContained(contained, extension); 373 } 374 else 375 { 376 long dk = contained.getDeclKind(); 377 String id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(dk).toUpperCase()+ extension; 378 379 put("obj", contained); 380 if ( dk == DeclarationKind.dk_local_interface) 382 { 383 putDecl(contained); 384 } 385 else 386 { 387 map(id); 388 } 389 } 390 } 391 392 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 |