1 26 27 package org.objectweb.openccm.generator.common.lib; 28 29 30 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 ; 38 39 40 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 55 58 protected AST ast_; 59 60 63 protected java.util.Hashtable dependencies_tree_; 64 65 68 protected Declaration target_decl_; 69 70 71 77 82 public GeneratorBase(AST ast) 83 { 84 super(); 86 87 ast_ = ast; 89 disableLog(); 90 initialize(); 91 } 92 93 99 106 protected Declaration[] 107 computeDependencies(Declaration decl) 108 { 109 Declaration[] depend = decl.getDependencies(); 110 114 List ordered_depend = new java.util.ArrayList (); 120 for (int i=0;i<depend.length;i++) 121 { 122 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 136 java.util.Arrays.sort(depend, new org.objectweb.openccm.ast.lib.DependencyComparator()); 137 142 return depend; 143 } 144 145 154 protected List 155 dependenciesTreeUpdate(Declaration[] decls) 156 { 157 Declaration decl = null; 158 Scope parent = null; 159 List childs = null; 160 String root = new String ("repository"); 161 List root_childs = new java.util.ArrayList (); 162 163 dependencies_tree_ = new java.util.Hashtable (); 165 166 dependencies_tree_.put(root, root_childs); 168 169 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 )dependencies_tree_.get(parent); 181 if (childs.indexOf(decl) == -1) 182 childs.add(decl); 183 } 185 else 186 { 187 childs = new java.util.ArrayList (); 188 childs.add(decl); 189 dependencies_tree_.put(parent, childs); 190 } 191 192 decl = parent; 193 parent = decl.getParent(); 194 } if (root_childs.indexOf(decl) == -1) 197 root_childs.add(decl); 198 } 200 217 return root_childs; 218 } 219 220 224 private void 225 after_generation() 226 { 227 } 229 230 236 239 public void 240 initialize() 241 { 242 dependencies_tree_ = null; 243 target_decl_ = null; 244 } 245 246 251 public org.objectweb.openccm.ast.api.AST 252 getAST() 253 { 254 return ast_; 255 } 256 257 264 public boolean 265 isBusinessDeclaration(Declaration decl) 266 { 267 String 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 290 public void 291 visit(Declaration obj) 292 { 293 Declaration[] depend = null; 294 295 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 target_decl_ = nmodule; 307 308 depend = new Declaration[1]; 311 depend[0] = target_decl_; 312 313 List root_decls = null; 315 316 root_decls = dependenciesTreeUpdate(depend); 317 visit_dep_tree(root_decls); 318 } 319 320 325 public void 326 visit_dep_tree(List 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 360 public void 361 visit_dep_tree(Declaration decl) 362 { 363 List childs = null; 364 365 childs = (List )dependencies_tree_.get(decl); 366 visit_dep_tree(childs); 367 } 368 369 374 public void 375 visit(List vect) 376 { 377 Declaration[] decls = (Declaration[]) vect.toArray(new Declaration[0]); 378 List root_decls = null; 380 381 root_decls = dependenciesTreeUpdate(decls); 382 visit_dep_tree(root_decls); 383 } 384 385 391 public void 392 visitContained(Declaration contained, 393 String extension) 394 { 395 String id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(contained.getDeclKind()).toUpperCase()+ extension; 396 397 put("obj", contained); 398 map(id); 399 } 400 401 406 public long 407 forward_check() 408 { 409 return 0; 411 } 412 413 418 public void 419 forward(List 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 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 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 474 public Declaration 475 getDeclaration(String name) 476 throws GenerationException 477 { 478 Declaration decl = null; 479 480 if (getAST() == null) 481 { 482 String msg = "[Error]: Interface Repository has not been fed!"; 483 throw new GenerationException(msg); 484 } 485 486 decl = getAST().lookup(name); 488 if (decl == null) 489 { 490 String msg = "[Error]: object " + name + " not found : aborting generation !"; 491 throw new GenerationException(msg); 492 } 493 return decl; 494 } 495 496 505 public Declaration 506 getDeclaration(Scope scope, String name) 507 throws GenerationException 508 { 509 Declaration[] decls = null; 510 511 if (scope == null) 512 { 513 String 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 534 public List 535 getDeclarations(Scope scope, long limited_types) 536 throws GenerationException 537 { 538 Declaration[] decls = null; 539 List list = new java.util.ArrayList (); 540 541 if (scope == null) 542 { 543 String 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 list.add(decls[i]); 554 } 555 } 556 return list; 557 } 558 559 567 public List 568 getAllDeclarations(Scope scope, long limited_types) 569 throws GenerationException 570 { 571 Declaration[] decls = null; 572 List result = new java.util.ArrayList (); 573 574 if (scope == null) 575 { 576 String 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 if ( isBusinessDeclaration(decls[i])) 592 result.addAll(getAllDeclarations((Scope)decls[i], limited_types)); 593 } 594 } 595 return result; 596 } 597 } 598 | Popular Tags |