1 33 34 package com.icesoft.metadata.generators; 35 36 import com.icesoft.jsfmeta.util.AbstractGenerator; 37 import com.icesoft.jsfmeta.util.InternalConfig; 38 import com.icesoft.jsfmeta.util.JavaSourceWriter; 39 40 41 import java.io.BufferedWriter ; 42 import java.io.File ; 43 import java.io.FileWriter ; 44 import java.io.IOException ; 45 import java.util.HashSet ; 46 import java.util.Set ; 47 48 import com.sun.rave.jsfmeta.beans.ComponentBean; 49 import com.sun.rave.jsfmeta.beans.PropertyBean; 50 import com.sun.rave.jsfmeta.beans.RendererBean; 51 import java.util.logging.Level ; 52 53 58 59 public class TagLibraryGenerator extends AbstractGenerator { 60 61 62 private boolean base; 63 64 private String constantMethodBindingPackage; 65 66 private String tagClassPackage; 67 68 public TagLibraryGenerator(InternalConfig internalConfig) { 69 super(internalConfig); 70 71 base = Boolean.getBoolean(internalConfig.getProperty("project.base.beaninfo")); 72 constantMethodBindingPackage = internalConfig.getProperty("constantMethodBinding"); 73 tagClassPackage = internalConfig.getProperty("project.taglib.package"); 74 } 75 76 public boolean getBase() { 77 return base; 78 } 79 80 public void setBase(boolean base) { 81 82 this.base = base; 83 } 84 85 public String getConstantMethodBindingPackage() { 86 return constantMethodBindingPackage; 87 } 88 89 public void setConstantMethodBindingPackage( 90 String constantMethodBindingPackage) { 91 this.constantMethodBindingPackage = constantMethodBindingPackage; 92 } 93 94 public String getTagClassPackage() { 95 return tagClassPackage; 96 } 97 98 public void setTagClassPackage(String tagClassPackage) { 99 this.tagClassPackage = tagClassPackage; 100 } 101 102 public void generate() throws IOException { 103 ComponentBean cbs[] = getConfig().getComponents(); 104 for (int i = 0; i < cbs.length; i++) 105 if (generated(cbs[i].getComponentClass())) 106 generate(cbs[i]); 107 108 } 109 110 private boolean actionListener(String name) { 111 if (name.equals("actionListener")) 112 return true; 113 return name.endsWith("ActionListener"); 114 } 115 116 private void attribute(ComponentBean cb, PropertyBean pb) 117 throws IOException { 118 String name = pb.getPropertyName(); 119 String var = mangle(name); 120 JavaSourceWriter writer = getWriter(); 121 writer.emitExpression("// " + name, true); 122 writer.emitExpression("private String " + var + " = null;", true); 123 writer.startMethod("set" + capitalize(name), "void", 124 new String [] { "String" }, new String [] { var }); 125 writer.emitExpression("this." + var + " = " + var + ";", true); 126 writer.endMethod(); 127 writer.emitNewline(); 128 } 129 130 private void attributes(ComponentBean cb, RendererBean rb) 131 throws IOException { 132 attributes(cb, rb, ((Set ) (new HashSet ()))); 133 } 134 135 private void attributes(ComponentBean cb, RendererBean rb, Set set) 136 throws IOException { 137 PropertyBean pbs[] = cb.getProperties(); 138 if (pbs == null) 139 pbs = new PropertyBean[0]; 140 for (int i = 0; i < pbs.length; i++) { 141 if ("id".equals(pbs[i].getPropertyName()) 142 || "rendered".equals(pbs[i].getPropertyName()) 143 || set.contains(pbs[i].getPropertyName()) 144 ) 145 continue; 146 set.add(pbs[i].getPropertyName()); 147 PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i] 148 .getPropertyName())); 149 if ("com.icesoft.faces.component.menubar.MenuBar".equals(cb.getComponentClass()) && 150 (pb.getPropertyName().equals("action") || 151 pb.getPropertyName().equals("actionListener"))) 152 continue; 153 if (pb.isTagAttribute()) 154 attribute(cb, pb); 155 } 156 157 String baseComponentType = cb.getBaseComponentType(); 158 if (baseComponentType != null) { 159 ComponentBean bcb = getConfig().getComponent(baseComponentType); 160 attributes(bcb, rb, set); 161 } 162 } 163 164 private void footer(ComponentBean cb) throws IOException { 165 JavaSourceWriter writer = getWriter(); 166 writer.endClass(); 167 } 168 169 private String stripHtmlName(String s, String word) { 170 171 String tmp = ""; 172 if (!s.startsWith(word)) { 173 tmp = s; 174 } else { 175 tmp = s.substring(word.length()); 176 } 177 return tmp; 178 } 179 180 private void generate(ComponentBean cb) throws IOException { 181 if(cb.getComponentClass() == null){ 182 System.err.println("Please check the following component metadata:" ); 183 System.err.println("component type :"+cb.getComponentType()+" does not have component class assigned"); 184 } 185 186 if (cb.getComponentClass().startsWith("javax.faces.component.")) 187 return; 188 if (cb.isSuppressed()) 189 return; 190 RendererBean rb = renderer(cb); 191 192 if(rb != null && rb.getInstanceName() == null && rb.getTagName() == null){ 193 194 System.err.println("Please check the following component metadata:"); 195 System.err.println(" component class="+cb.getComponentClass()+" is not correct"); 196 System.exit(1); 197 } 198 if (rb == null){ 199 200 System.err.println("Please check the following component metadata:"); 201 System.err.println(" component class="+ cb.getComponentClass()+" is not correct"); 202 System.exit(1); 203 } 204 205 if (rb.getTagName() == null){ 206 return; 207 } 208 String tagClass = null; 209 if (tagClassPackage == null) { 210 tagClass = cb.getComponentClass() + (getBase() ? "TagBase" : "Tag"); 211 } else { 212 String componentClass = cb.getComponentClass(); 213 if (componentClass.indexOf(".ext.") > 0) { 214 logger.log(Level.FINEST, "tag class name="+simpleClassName(cb.getComponentClass())); 215 216 tagClass = tagClassPackage 217 + "." 218 + stripHtmlName( 219 simpleClassName(cb.getComponentClass()), "Html") 220 + (getBase() ? "TagBase" : "Tag"); 221 222 } else { 223 224 tagClass = cb.getComponentClass() 225 + (getBase() ? "TagBase" : "Tag"); 226 } 227 } 228 File outputFile = outputFile(tagClass); 229 outputFile.mkdirs(); 230 outputFile.delete(); 231 getWriter().setOutputWriter( 232 new BufferedWriter (new FileWriter (outputFile))); 233 logger.log(Level.FINEST, "Generate " + outputFile.getAbsoluteFile()); 234 license(cb); 235 header(cb); 236 required(cb); 237 release(cb, rb); 238 properties(cb, rb); 239 attributes(cb, rb); 240 signatures(cb); 241 footer(cb); 242 getWriter().flush(); 243 getWriter().close(); 244 } 245 246 private void header(ComponentBean cb) throws IOException { 247 ComponentBean bcb = null; 248 String baseComponentType = cb.getBaseComponentType(); 249 if (baseComponentType != null) { 250 bcb = getConfig().getComponent(baseComponentType); 251 if (bcb == null) { 252 253 logger.log(Level.SEVERE, "invalid baseComponentType"); 254 throw new IllegalArgumentException ("invalid baseComponentType"); 255 } 256 } 257 JavaSourceWriter writer = getWriter(); 258 259 String simple = stripHtmlName(simpleClassName(cb.getComponentClass()), 261 "Html") 262 + "Tag"; 263 if (getBase()) 264 simple = simple + "Base"; 265 if (tagClassPackage == null) { 266 String componentClass = cb.getComponentClass(); 267 int last = componentClass.lastIndexOf('.'); 268 if (last >= 0){ 269 writer.emitPackage(componentClass.substring(0, last)); 270 } 271 } else { 272 String componentClass = cb.getComponentClass(); 273 if (componentClass.indexOf(".ext.") > 0) { 274 writer.emitPackage(tagClassPackage); 275 } else { 276 int last = componentClass.lastIndexOf('.'); 277 if (last >= 0) 278 writer.emitPackage(componentClass.substring(0, last)); 279 } 280 } 281 writer.emitNewline(); 282 writer.emitImport("java.io.IOException"); 283 writer.emitImport("javax.servlet.jsp.JspException"); 284 writer.emitImport("javax.faces.component.UIComponent"); 285 writer.emitImport("javax.faces.context.FacesContext"); 286 writer.emitImport("javax.faces.convert.Converter"); 287 writer.emitImport("javax.faces.el.MethodBinding"); 288 writer.emitImport("javax.faces.el.ValueBinding"); 289 writer.emitImport("javax.faces.event.ActionEvent"); 290 writer.emitImport("javax.faces.event.ValueChangeEvent"); 291 writer.emitImport("javax.faces.webapp.UIComponentTag"); 292 writer.emitImport("com.icesoft.faces.component.dragdrop.DragEvent"); 293 writer.emitImport("com.icesoft.faces.component.dragdrop.DropEvent"); 294 295 writer.emitImport("com.icesoft.faces.component.outputchart.*"); 296 writer.emitImport("com.icesoft.faces.component.ext.*"); 297 writer.emitImport("com.icesoft.faces.component.panelpositioned.*"); 298 writer.emitImport("com.icesoft.faces.component.paneltabset.*"); 299 300 301 302 if (constantMethodBindingPackage != null) 303 writer.emitImport(constantMethodBindingPackage 304 + ".MethodBindingString"); 305 writer.emitNewline(); 306 writer.startJavaDoc(); 307 writer.emitJavaDoc("<p>Auto-generated component tag class."); 308 writer.emitJavaDoc("Do <strong>NOT</strong> modify; all changes"); 309 writer.emitJavaDoc("<strong>will</strong> be lost!</p>"); 310 writer.endJavaDoc(); 311 writer.emitNewline(); 312 writer.startClass(simple, "UIComponentTag", null); 313 writer.emitNewline(); 314 } 315 316 private void license(ComponentBean componentbean) throws IOException { 317 318 JavaSourceWriter writer = getWriter(); 319 writer.emitLicense(); 320 321 } 322 323 private void properties(ComponentBean cb, RendererBean rb) 324 throws IOException { 325 JavaSourceWriter writer = getWriter(); 326 writer.startJavaDoc(); 327 writer 328 .emitJavaDoc("<p>Transfer tag attributes to component properties.</p>"); 329 writer.endJavaDoc(); 330 writer.startMethod("setProperties", "void", 331 new String [] { "UIComponent" }, new String [] { "_component" }, 332 "protected"); 333 writer.emitExpression("try{", true); 335 writer.indent(); 336 writer.emitExpression("super.setProperties(_component);", true); 337 properties(cb, rb, ((Set ) (new HashSet ()))); 338 writer.outdent(); 339 writer.emitExpression("}catch(Exception e1){e1.printStackTrace();throw new RuntimeException(e1);}", true); 340 writer.endMethod(); 341 writer.emitNewline(); 342 } 343 344 private void properties(ComponentBean cb, RendererBean rb, Set set) 345 throws IOException { 346 JavaSourceWriter writer = getWriter(); 347 PropertyBean pbs[] = cb.getProperties(); 348 if (pbs == null) 349 pbs = new PropertyBean[0]; 350 for (int i = 0; i < pbs.length; i++) { 351 if ("id".equals(pbs[i].getPropertyName()) 352 || "rendered".equals(pbs[i].getPropertyName()) 353 || set.contains(pbs[i].getPropertyName())) 354 continue; 355 set.add(pbs[i].getPropertyName()); 356 PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i] 357 .getPropertyName())); 358 if (pb.isSuppressed() || !pb.isTagAttribute()) 359 continue; 360 361 if ("com.icesoft.faces.component.menubar.MenuBar".equals(cb.getComponentClass()) && 362 (pb.getPropertyName().equals("action") || 363 pb.getPropertyName().equals("actionListener"))) { 364 continue; 365 } 366 String name = pb.getPropertyName(); 367 String type = pb.getPropertyClass(); 368 String var = mangle(name); 369 writer.emitExpression("if (" + var + " != null) {", true); 370 writer.indent(); 371 if (!pb.isBindable()) { 372 if (primitive(type)) { 373 if ("char".equals(type) 374 || "java.lang.Character".equals(type)) 375 writer.emitExpression( 376 "_component.getAttributes().put(\"" + name 377 + "\", new Character(" + var 378 + ".charAt(0)));", true); 379 else 380 writer.emitExpression( 381 "_component.getAttributes().put(\"" + name 382 + "\", " + wrappers.get(type) 383 + ".valueOf(" + var + "));", true); 384 } else { 385 if (name.equalsIgnoreCase("var") 386 && cb.getComponentClass().indexOf(".icesoft.") > 0) { 387 388 String className = simpleClassName(cb 389 .getComponentClass()); 390 391 String [] hasVarClassArray = new String [] { 392 "HtmlDataTable", "PanelTabSet", "PanelSeries", "UIColumns"}; 393 String [] castClassArray = new String [] { 394 "javax.faces.component.UIData", 395 "com.icesoft.faces.component.panelseries.UISeries", 396 "com.icesoft.faces.component.panelseries.UISeries", 397 "com.icesoft.faces.component.ext.UIColumns" 398 }; logger.log(Level.FINEST, "var class name="+ className); 399 for (int v = 0; v < hasVarClassArray.length; v++) { 400 401 if (className.equalsIgnoreCase(hasVarClassArray[v])) { 402 403 String tmp = castClassArray[v] + " " 404 + hasVarClassArray[v].toLowerCase() 405 + " = " + "(" + castClassArray[v] 406 + ")_component;"; 407 408 writer.emitExpression("try{", true); 409 writer.indent(); 410 writer.emitExpression(tmp, true); 411 writer.emitExpression(hasVarClassArray[v] 412 .toLowerCase() 413 + ".setVar(" + var + ");", true); 414 writer.outdent(); 415 writer.emitExpression( 416 "}catch (ClassCastException cce) {", 417 true); 418 writer.indent(); 419 writer 420 .emitExpression( 421 "throw new IllegalStateException(_component.toString() + \" not expected type. Expected: " 422 + castClassArray[v] 423 + ". Perhaps you're missing a tag?\");", 424 true); 425 writer.outdent(); 426 writer.emitExpression("}", true); 427 } 428 } 429 430 } else { 431 writer.emitExpression( 432 "_component.getAttributes().put(\"" + name 433 + "\", " + var + ");", true); 434 } 435 } 436 } else if ("com.icesoft.faces.context.effects.Effect".equals(type) 437 && !"effect".equalsIgnoreCase(name)) { 438 writer.emitExpression( 439 "com.icesoft.faces.component.ext.taglib.Util.addLocalEffect(" 440 + var + ", \"" + name + "\", _component);", 441 true); 442 } else if ("com.icesoft.faces.context.effects.Effect".equals(type) 443 && "effect".equalsIgnoreCase(name)) { 444 writer.emitExpression( 445 "com.icesoft.faces.component.ext.taglib.Util.addEffect(" 446 + var + ", _component);", true); 447 } else if ("javax.faces.convert.Converter".equals(type)) { 448 writer.emitExpression("if (isValueReference(" + var + ")) {", 449 true); 450 writer.indent(); 451 writer.emitExpression( 452 "ValueBinding _vb = getFacesContext().getApplication().createValueBinding(" 453 + var + ");", true); 454 writer.emitExpression("_component.setValueBinding(\"" + name 455 + "\", _vb);", true); 456 writer.outdent(); 457 writer.emitExpression("} else {", true); 458 writer.indent(); 459 writer 460 .emitExpression( 461 "Converter _converter = FacesContext.getCurrentInstance().", 462 true); 463 writer.emitExpression(" getApplication().createConverter(" 464 + var + ");", true); 465 writer 466 .emitExpression( 467 "_component.getAttributes().put(\"converter\", _converter);", 468 true); 469 writer.outdent(); 470 writer.emitExpression("}", true); 471 } else if ("javax.faces.el.MethodBinding".equals(type)) { 472 if ("action".equals(name) || "linkAction".equals(name)) { 473 writer.emitExpression("if (isValueReference(" + var 474 + ")) {", true); 475 writer.indent(); 476 writer.emitExpression( 477 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 478 + var + ", actionArgs);", true); 479 writer.emitExpression("_component.getAttributes().put(\"" 480 + name + "\", _mb);", true); 481 writer.outdent(); 482 writer.emitExpression("} else {", true); 483 writer.indent(); 484 writer 485 .emitExpression( 486 "MethodBinding _mb = new com.icesoft.faces.component.ext.taglib.MethodBindingString(" 487 + var + ");", true); 488 writer.emitExpression("_component.getAttributes().put(\"" 489 + name + "\", _mb);", true); 490 writer.outdent(); 491 writer.emitExpression("}", true); 492 } else if (actionListener(name)) { 493 494 writer.emitExpression("if (isValueReference(" + var 495 + ")) {", true); 496 writer.indent(); 497 writer.emitExpression( 498 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 499 + var + ", actionListenerArgs);", true); 500 501 writer.emitExpression("_component.getAttributes().put(\"" 502 + name + "\", _mb);", true); 503 writer.outdent(); 504 writer.emitExpression("} else {", true); 505 writer.indent(); 506 writer.emitExpression("throw new IllegalArgumentException(" 507 + var + ");", true); 508 writer.outdent(); 509 writer.emitExpression("}", true); 510 } else if (validator(name)) { 511 writer.emitExpression("if (isValueReference(" + var 512 + ")) {", true); 513 writer.indent(); 514 writer.emitExpression( 515 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 516 + var + ", validatorArgs);", true); 517 writer.emitExpression("_component.getAttributes().put(\"" 518 + name + "\", _mb);", true); 519 writer.outdent(); 520 writer.emitExpression("} else {", true); 521 writer.indent(); 522 writer.emitExpression("throw new IllegalArgumentException(" 523 + var + ");", true); 524 writer.outdent(); 525 writer.emitExpression("}", true); 526 } else if ("valueChangeListener".equals(name)) { 527 writer.emitExpression("if (isValueReference(" + var 528 + ")) {", true); 529 writer.indent(); 530 writer 531 .emitExpression( 532 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 533 + var 534 + ", valueChangeListenerArgs);", 535 true); 536 writer.emitExpression("_component.getAttributes().put(\"" 537 + name + "\", _mb);", true); 538 writer.outdent(); 539 writer.emitExpression("} else {", true); 540 writer.indent(); 541 writer.emitExpression("throw new IllegalArgumentException(" 542 + var + ");", true); 543 writer.outdent(); 544 writer.emitExpression("}", true); 545 } else if ("progressListener".equals(name)) { 546 writer.emitExpression("if (isValueReference(" + var 547 + ")) {", true); 548 writer.indent(); 549 writer 550 .emitExpression( 551 "Class progressListenerArgs[] = { java.util.EventObject.class };", 552 true); 553 writer.emitExpression( 554 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 555 + var + ", progressListenerArgs);", true); 556 writer.emitExpression("_component.getAttributes().put(\"" 557 + name + "\", _mb);", true); 558 writer.outdent(); 559 writer.emitExpression("} else {", true); 560 writer.indent(); 561 writer.emitExpression("throw new IllegalArgumentException(" 562 + var + ");", true); 563 writer.outdent(); 564 writer.emitExpression("}", true); 565 } else if("listener".equalsIgnoreCase(name)){ 566 writer.emitExpression("if (isValueReference(" + var 571 + ")) {", true); 572 writer.indent(); 573 writer 574 .emitExpression( 575 "Class[] panelPositionedArgs = new Class[]{PanelPositionedEvent.class};", 576 true); 577 writer.emitExpression( 578 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 579 + var + ", panelPositionedArgs);", true); 580 writer.emitExpression("_component.getAttributes().put(\"" 581 + name + "\", _mb);", true); 582 writer.outdent(); 583 writer.emitExpression("} else {", true); 584 writer.indent(); 585 writer.emitExpression("throw new IllegalArgumentException(" 586 + var + ");", true); 587 writer.outdent(); 588 writer.emitExpression("}", true); 589 590 591 } else if("dragListener".equalsIgnoreCase(name)){ 592 logger.log(Level.FINEST, "Generating Method Binding for drag Listener"); 593 594 writer.emitExpression("if (isValueReference(" + var 595 + ")) {", true); 596 writer.indent(); 597 writer 598 .emitExpression( 599 "Class[] dragListenerArgs = new Class[]{DragEvent.class};", 600 true); 601 writer.emitExpression( 602 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 603 + var + ", dragListenerArgs);", true); 604 String className = cb.getComponentClass(); 605 607 String setterName = "set" + firstCharUpper(name); 608 String s = "((" + className + ")_component)." + setterName + "(_mb);"; 609 writer.emitExpression(s,true); 610 writer.outdent(); 614 writer.emitExpression("} else {", true); 615 writer.indent(); 616 writer.emitExpression("throw new IllegalArgumentException(" 617 + var + ");", true); 618 writer.outdent(); 619 writer.emitExpression("}", true); 620 621 } 622 623 else if("dropListener".equalsIgnoreCase(name)){ 624 625 writer.emitExpression("if (isValueReference(" + var 626 + ")) {", true); 627 writer.indent(); 628 writer 629 .emitExpression( 630 "Class[] dropListenerArgs= new Class[]{DropEvent.class};", 631 true); 632 writer.emitExpression( 633 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 634 + var + ", dropListenerArgs );", true); 635 writer.emitExpression("_component.getAttributes().put(\"" 636 + name + "\", _mb);", true); 637 writer.outdent(); 638 writer.emitExpression("} else {", true); 639 writer.indent(); 640 writer.emitExpression("throw new IllegalArgumentException(" 641 + var + ");", true); 642 writer.outdent(); 643 writer.emitExpression("}", true); 644 645 } else if ("selectionListener".equalsIgnoreCase(name)) { 646 writer.emitExpression("if (isValueReference(" + var 647 + ")) {", true); 648 writer.indent(); 649 writer 650 .emitExpression( 651 "Class[] selectionListenerArgs= new Class[]{RowSelectorEvent.class};", 652 true); 653 writer.emitExpression( 654 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 655 + var + ", selectionListenerArgs );", true); 656 writer.emitExpression("_component.getAttributes().put(\"" 657 + name + "\", _mb);", true); 658 writer.outdent(); 659 writer.emitExpression("} else {", true); 660 writer.indent(); 661 writer.emitExpression("throw new IllegalArgumentException(" 662 + var + ");", true); 663 writer.outdent(); 664 writer.emitExpression("}", true); 665 666 } 667 else if ("selectionAction".equalsIgnoreCase(name)) { 668 writer.emitExpression("if (isValueReference(" + var 669 + ")) {", true); 670 writer.indent(); 671 672 writer.emitExpression( 673 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 674 + var + ", null );", true); 675 writer.emitExpression("_component.getAttributes().put(\"" 676 + name + "\", _mb);", true); 677 writer.outdent(); 678 writer.emitExpression("} else {", true); 679 writer.indent(); 680 writer.emitExpression("throw new IllegalArgumentException(" 681 + var + ");", true); 682 writer.outdent(); 683 writer.emitExpression("}", true); 684 685 } 686 else if ("tabChangeListener".equalsIgnoreCase(name)) { 687 writer.emitExpression("if (isValueReference(" + var 688 + ")) {", true); 689 writer.indent(); 690 writer 691 .emitExpression( 692 "Class[] selectionListenerArgs= new Class[]{TabChangeEvent.class};", 693 true); 694 writer.emitExpression( 695 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 696 + var + ", selectionListenerArgs );", true); 697 writer.emitExpression("_component.getAttributes().put(\"" 698 + name + "\", _mb);", true); 699 writer.outdent(); 700 writer.emitExpression("} else {", true); 701 writer.indent(); 702 writer.emitExpression("throw new IllegalArgumentException(" 703 + var + ");", true); 704 writer.outdent(); 705 writer.emitExpression("}", true); 706 707 } 708 else if ("renderOnSubmit".equalsIgnoreCase(name)) { 709 writer.emitExpression("if (isValueReference(" + var 710 + ")) {", true); 711 writer.indent(); 712 writer 713 .emitExpression( 714 "Class[] selectionListenerArgs= new Class[]{OutputChart.class};", 715 true); 716 writer.emitExpression( 717 "MethodBinding _mb = getFacesContext().getApplication().createMethodBinding(" 718 + var + ", selectionListenerArgs);", true); 719 writer.emitExpression("_component.getAttributes().put(\"" 720 + name + "\", _mb);", true); 721 writer.outdent(); 722 writer.emitExpression("} else {", true); 723 writer.indent(); 724 writer.emitExpression("MethodBinding _mb = new " 725 +"com.icesoft.faces.el.LiteralBooleanMethodBinding" 726 +"("+var+");", true); 727 writer.emitExpression("_component.getAttributes().put(\"" 728 + name + "\", _mb);", true); 729 writer.outdent(); 730 writer.emitExpression("}", true); 731 732 } 733 else { 734 735 throw new IllegalArgumentException (name); 736 } 737 } else if (primitive(type)) { 738 writer.emitExpression("if (isValueReference(" + var + ")) {", 739 true); 740 writer.indent(); 741 writer.emitExpression( 742 "ValueBinding _vb = getFacesContext().getApplication().createValueBinding(" 743 + var + ");", true); 744 writer.emitExpression("_component.setValueBinding(\"" + name 745 + "\", _vb);", true); 746 writer.outdent(); 747 writer.emitExpression("} else {", true); 748 writer.indent(); 749 if ("char".equals(type) || "java.lang.Character".equals(type)) 750 writer.emitExpression("_component.getAttributes().put(\"" 751 + name + "\", new Character(" + var 752 + ".charAt(0)));", true); 753 else 754 writer.emitExpression("_component.getAttributes().put(\"" 755 + name + "\", " + wrappers.get(type) + ".valueOf(" 756 + var + "));", true); 757 writer.outdent(); 758 writer.emitExpression("}", true); 759 } else { 760 writer.emitExpression("if (isValueReference(" + var + ")) {", 761 true); 762 writer.indent(); 763 writer.emitExpression( 764 "ValueBinding _vb = getFacesContext().getApplication().createValueBinding(" 765 + var + ");", true); 766 writer.emitExpression("_component.setValueBinding(\"" + name 767 + "\", _vb);", true); 768 writer.outdent(); 769 writer.emitExpression("} else {", true); 770 writer.indent(); 771 772 if (name.equalsIgnoreCase("listValue") 773 && rb.getRendererType().equalsIgnoreCase( 774 "com.icesoft.faces.SelectInputText")) { 775 writer 776 .emitExpression( 777 "throw new IllegalArgumentException(\"Not a valid value bind expression\");", 778 true); 779 } else { 780 writer.emitExpression("_component.getAttributes().put(\"" 781 + name + "\", " + var + ");", true); 782 } 783 writer.outdent(); 784 writer.emitExpression("}", true); 785 } 786 writer.outdent(); 787 writer.emitExpression("}", true); 788 } 789 790 String baseComponentType = cb.getBaseComponentType(); 791 if (baseComponentType != null) { 792 ComponentBean bcb = getConfig().getComponent(baseComponentType); 793 properties(bcb, rb, set); 794 } 795 } 796 797 private void release(ComponentBean cb, RendererBean rb) throws IOException { 798 JavaSourceWriter writer = getWriter(); 799 writer.startJavaDoc(); 800 writer 801 .emitJavaDoc("<p>Release any allocated tag handler attributes.</p>"); 802 writer.endJavaDoc(); 803 writer.startMethod("release", "void", null, null); 804 writer.emitExpression("super.release();", true); 805 release(cb, rb, ((Set ) (new HashSet ()))); 806 writer.endMethod(); 807 writer.emitNewline(); 808 } 809 810 private void release(ComponentBean cb, RendererBean rb, Set set) 811 throws IOException { 812 JavaSourceWriter writer = getWriter(); 813 PropertyBean pbs[] = cb.getProperties(); 814 if (pbs == null) 815 pbs = new PropertyBean[0]; 816 for (int i = 0; i < pbs.length; i++) { 817 if ("id".equals(pbs[i].getPropertyName()) 818 || "rendered".equals(pbs[i].getPropertyName()) 819 || set.contains(pbs[i].getPropertyName())) 820 continue; 821 set.add(pbs[i].getPropertyName()); 822 PropertyBean pb = merge(pbs[i], rb.getAttribute(pbs[i] 823 .getPropertyName())); 824 if ("com.icesoft.faces.component.menubar.MenuBar".equals(cb.getComponentClass()) && 825 (pb.getPropertyName().equals("action") || 826 pb.getPropertyName().equals("actionListener"))) { 827 continue; 828 } 829 if (pb.isTagAttribute()) 830 writer.emitExpression( 831 mangle(pb.getPropertyName()) + " = null;", true); 832 } 833 834 835 String baseComponentType = cb.getBaseComponentType(); 836 if (baseComponentType != null) { 837 ComponentBean bcb = getConfig().getComponent(baseComponentType); 838 release(bcb, rb, set); 839 } 840 } 841 842 private void required(ComponentBean cb) throws IOException { 843 String componentType = cb.getComponentType(); 844 String rendererType = rendererType(cb); 845 JavaSourceWriter writer = getWriter(); 846 writer.startJavaDoc(); 847 writer.emitJavaDoc("<p>Return the requested component type.</p>"); 848 writer.endJavaDoc(); 849 writer.startMethod("getComponentType", "String", null, null); 850 writer.emitExpression("return \"" + componentType + "\";", true); 851 writer.endMethod(); 852 writer.emitNewline(); 853 writer.startJavaDoc(); 854 writer.emitJavaDoc("<p>Return the requested renderer type.</p>"); 855 writer.endJavaDoc(); 856 writer.startMethod("getRendererType", "String", null, null); 857 858 if (rendererType.equalsIgnoreCase("null")) { 860 writer.emitExpression("return null;", true); 861 } else { 862 writer.emitExpression("return \"" + rendererType + "\";", true); 863 } 864 writer.endMethod(); 865 writer.emitNewline(); 866 } 867 868 private void signatures(ComponentBean cb) throws IOException { 869 JavaSourceWriter writer = getWriter(); 870 writer.emitExpression( 871 "private static Class actionArgs[] = new Class[0];", true); 872 writer 873 .emitExpression( 874 "private static Class actionListenerArgs[] = { ActionEvent.class };", 875 true); 876 writer 877 .emitExpression( 878 "private static Class validatorArgs[] = { FacesContext.class, UIComponent.class, Object.class };", 879 true); 880 writer 881 .emitExpression( 882 "private static Class valueChangeListenerArgs[] = { ValueChangeEvent.class };", 883 true); 884 writer.emitNewline(); 885 appendDoTagMethods(); 886 } 887 888 private void appendDoTagMethods() throws IOException { 889 JavaSourceWriter writer = getWriter(); 890 writer.emitNewline(); 891 writer.emitExpression("// ", true); 892 writer.emitExpression("// Methods From TagSupport", true); 893 writer.emitExpression("// ", true); 894 doTagMethod("Start"); 895 doTagMethod("End"); 896 } 897 898 private void doTagMethod(String name) throws IOException { 899 JavaSourceWriter writer = getWriter(); 900 writer.emitNewline(); 901 writer.emitExpression("public int do" + name 902 + "Tag() throws JspException {", true); 903 writer.indent(); 904 writer.emitExpression("int rc = 0;", true); 905 writer.emitExpression("try {", true); 906 writer.indent(); 907 writer.emitExpression("rc = super.do" + name + "Tag();", true); 908 writer.outdent(); 909 writer.emitExpression("} catch (JspException e) {", true); 910 writer.indent(); 911 writer.emitExpression("throw e;", true); 912 writer.outdent(); 913 writer.emitExpression("} catch (Throwable t) {", true); 914 writer.indent(); 915 writer.emitExpression("throw new JspException(t);", true); 916 writer.outdent(); 917 writer.emitExpression("}", true); 918 writer.emitExpression("return rc;", true); 919 writer.outdent(); 920 writer.emitExpression("}", true); 921 writer.emitNewline(); 922 } 923 924 private boolean validator(String name) { 925 if (name.equals("validator")) 926 return true; 927 return name.endsWith("Validator"); 928 } 929 930 private String firstCharUpper(String s){ 931 if(s == null)return null; 932 char[] ca = s.toCharArray(); 933 if(ca.length < 1)return s; 934 ca[0] = Character.toUpperCase(ca[0]); 935 return new String (ca); 936 } 937 938 } 939 | Popular Tags |