1 33 34 35 package com.icesoft.metadata.generators; 36 37 import com.icesoft.jsfmeta.util.AbstractGenerator; 38 import com.icesoft.jsfmeta.util.InternalConfig; 39 import com.icesoft.jsfmeta.util.JavaSourceWriter; 40 import java.io.BufferedWriter ; 41 import java.io.File ; 42 import java.io.FileWriter ; 43 import java.io.IOException ; 44 45 import com.sun.rave.jsfmeta.beans.AttributeBean; 46 import com.sun.rave.jsfmeta.beans.ComponentBean; 47 import com.sun.rave.jsfmeta.beans.DescriptionBean; 48 import com.sun.rave.jsfmeta.beans.PropertyBean; 49 import com.sun.rave.jsfmeta.beans.RendererBean; 50 51 public class BaseComponentGenerator extends AbstractGenerator { 52 53 private boolean base; 54 55 private boolean override; 56 57 public BaseComponentGenerator(InternalConfig internalConfig) { 58 59 super(internalConfig); 60 base = true; 61 override = false; 62 } 63 64 public boolean getBase() { 65 return base; 66 } 67 68 public void setBase(boolean base) { 69 this.base = base; 70 } 71 72 public boolean isOverride() { 73 return override; 74 } 75 76 public void setOverride(boolean override) { 77 this.override = override; 78 } 79 80 public void generate() throws IOException { 81 82 ComponentBean cbs[] = getConfig().getComponents(); 83 for (int i = 0; i < cbs.length; i++){ 84 if (generated(cbs[i].getComponentClass())) 85 generate(cbs[i]); 86 } 87 } 88 89 private boolean isAliasFor(PropertyBean[] pbs){ 90 91 for(int i=0; i< pbs.length; i++){ 92 93 if(pbs[i].getAliasFor() != null){ 94 return true; 95 } 96 } 97 98 return false; 99 } 100 101 private void bindings(ComponentBean cb) throws IOException { 102 103 PropertyBean pbs[] = cb.getProperties(); 104 105 if (!isAliasFor(pbs)){ 106 return; 107 } 108 JavaSourceWriter writer = getWriter(); 109 String propertyName = null; 110 String aliasName = null; 111 writer.startJavaDoc(); 112 writer 113 .emitJavaDoc("<p>Return the <code>ValueBinding</code> stored for the"); 114 writer 115 .emitJavaDoc("specified name (if any), respecting any property aliases.</p>"); 116 writer.emitJavaDoc(); 117 writer.emitJavaDoc("@param name Name of value binding to retrieve"); 118 writer.endJavaDoc(); 119 writer.startMethod("getValueBinding", "ValueBinding", 120 new String [] { "String" }, new String [] { "name" }); 121 for (int k = 0; k < pbs.length; k++) { 122 aliasName = pbs[k].getAliasFor(); 123 if (aliasName != null) { 124 propertyName = pbs[k].getPropertyName(); 125 writer.emitExpression("if (name.equals(" 126 + writer.toJavaString(propertyName) + ")) {", true); 127 writer.indent(); 128 writer.emitExpression("return super.getValueBinding(" 129 + writer.toJavaString(aliasName) + ");", true); 130 writer.outdent(); 131 writer.emitExpression("}", true); 132 } 133 } 134 135 writer.emitExpression("return super.getValueBinding(name);", true); 136 writer.endMethod(); 137 writer.emitNewline(); 138 writer.startJavaDoc(); 139 writer 140 .emitJavaDoc("<p>Set the <code>ValueBinding</code> stored for the"); 141 writer.emitJavaDoc("specified name (if any), respecting any property"); 142 writer.emitJavaDoc("aliases.</p>"); 143 writer.emitJavaDoc(); 144 writer.emitJavaDoc("@param name Name of value binding to set"); 145 writer 146 .emitJavaDoc("@param binding ValueBinding to set, or null to remove"); 147 writer.endJavaDoc(); 148 writer.startMethod("setValueBinding", "void", new String [] { "String", 149 "ValueBinding" }, new String [] { "name", "binding" }); 150 for (int j = 0; j < pbs.length; j++) { 151 aliasName = pbs[j].getAliasFor(); 152 if (aliasName != null) { 153 propertyName = pbs[j].getPropertyName(); 154 writer.emitExpression("if (name.equals(" 155 + writer.toJavaString(propertyName) + ")) {", true); 156 writer.indent(); 157 writer.emitExpression("super.setValueBinding(" 158 + writer.toJavaString(aliasName) + ", binding);", true); 159 writer.emitExpression("return;", true); 160 writer.outdent(); 161 writer.emitExpression("}", true); 162 } 163 } 164 165 writer.emitExpression("super.setValueBinding(name, binding);", true); 166 writer.endMethod(); 167 writer.emitNewline(); 168 } 169 170 private void constructor(ComponentBean cb) throws IOException { 171 JavaSourceWriter writer = getWriter(); 172 String simple = simpleClassName(cb.getComponentClass()); 173 if (getBase()) 174 simple = simple + "Base"; 175 String rendererType = rendererType(cb); 176 writer.startJavaDoc(); 177 writer.emitJavaDoc("<p>Construct a new <code>" + simple 178 + "</code>.</p>"); 179 writer.endJavaDoc(); 180 writer.startMethod(simple, null, null, null); 181 writer.emitExpression("super();", true); 182 if (rendererType != null) { 183 writer.emitExpression("setRendererType(\"" + rendererType + "\");", 184 true); 185 writer.endMethod(); 186 } 187 writer.emitNewline(); 188 } 189 190 private void family(ComponentBean cb) throws IOException { 191 JavaSourceWriter writer = getWriter(); 192 writer.startJavaDoc(); 193 writer.emitJavaDoc("<p>Return the family for this component.</p>"); 194 writer.endJavaDoc(); 195 writer.startMethod("getFamily", "String", null, null); 196 writer.emitExpression("return \"" + componentFamily(cb) + "\";", true); 197 writer.endMethod(); 198 writer.emitNewline(); 199 } 200 201 private void footer(ComponentBean cb) throws IOException { 202 JavaSourceWriter writer = getWriter(); 203 writer.endClass(); 204 } 205 206 private void generate(ComponentBean cb) throws IOException { 207 if (cb.getComponentClass().startsWith("javax.faces.component.")) 208 return; 209 if (cb.isSuppressed()) { 210 return; 211 } else { 212 File outputFile = outputFile(cb.getComponentClass() 213 + (getBase() ? "Base" : "")); 214 outputFile.mkdirs(); 215 outputFile.delete(); 216 getWriter().setOutputWriter( 217 new BufferedWriter (new FileWriter (outputFile))); 218 license(cb); 219 header(cb); 220 constructor(cb); 221 family(cb); 222 bindings(cb); 223 properties(cb); 224 restore(cb); 225 save(cb); 226 footer(cb); 227 getWriter().flush(); 228 getWriter().close(); 229 return; 230 } 231 } 232 233 private void header(ComponentBean cb) throws IOException { 234 235 ComponentBean bcb = null; 236 String baseComponentType = cb.getBaseComponentType(); 237 if (baseComponentType != null) 238 bcb = getConfig().getComponent(baseComponentType); 239 RendererBean rb = renderer(cb); 240 JavaSourceWriter writer = getWriter(); 241 String simple = simpleClassName(cb.getComponentClass()); 242 if (getBase()) 243 simple = simple + "Base"; 244 String componentClass = cb.getComponentClass(); 245 int last = componentClass.lastIndexOf('.'); 246 if (last >= 0) { 247 writer.emitPackage(componentClass.substring(0, last)); 248 writer.emitNewline(); 249 } 250 writer.emitImport("java.io.IOException"); 251 writer.emitImport("javax.faces.component.UIComponent"); 252 writer.emitImport("javax.faces.context.FacesContext"); 253 writer.emitImport("javax.faces.el.MethodBinding"); 254 writer.emitImport("javax.faces.el.ValueBinding"); 255 writer.emitNewline(); 256 writer.startJavaDoc(); 257 DescriptionBean db = null; 258 if (isOverride() && rb != null){ 259 db = rb.getDescription(""); 260 } 261 if (db == null){ 262 db = cb.getDescription(""); 263 } 264 if (db != null) { 265 String description = db.getDescription(); 266 if (description != null && description.length() > 0) 267 writer.emitJavaDocMultiLine(description.trim()); 268 } 269 writer.emitJavaDoc("<p>Auto-generated component class."); 270 writer.emitJavaDoc("Do <strong>NOT</strong> modify; all changes"); 271 writer.emitJavaDoc("<strong>will</strong> be lost!</p>"); 272 writer.endJavaDoc(); 273 writer.emitNewline(); 274 if (bcb != null){ 275 writer.startClass(simple, bcb.getComponentClass(), null, !getBase(), 276 getBase()); 277 }else{ 278 writer.startClass(simple, "javax.faces.component.UIComponentBase", 279 null, true, getBase()); 280 } 281 writer.emitNewline(); 282 } 283 284 private void license(ComponentBean componentbean) throws IOException { 285 286 JavaSourceWriter writer = getWriter(); 287 writer.emitLicense(); 288 } 289 290 private void properties(ComponentBean cb) throws IOException { 291 PropertyBean pbs[] = cb.getProperties(); 292 if (pbs == null){ 293 return; 294 } 295 for (int i = 0; i < pbs.length; i++){ 296 if (!pbs[i].isSuppressed()){ 297 property(cb, pbs[i]); 298 } 299 } 300 301 } 302 303 private void property(ComponentBean cb, PropertyBean pb) throws IOException { 304 String name = pb.getPropertyName(); 305 String type = pb.getPropertyClass(); 306 if (type == null){ 307 type = "String"; 308 } else if (type.startsWith("java.lang.")){ 309 type = type.substring(10); 310 } 311 RendererBean rb = renderer(cb); 312 AttributeBean ab = null; 313 if (rb != null){ 314 ab = rb.getAttribute(pb.getPropertyName()); 315 } 316 String var = mangle(name); 317 JavaSourceWriter writer = getWriter(); 318 String aliasFor = pb.getAliasFor(); 319 if (aliasFor != null) { 320 PropertyBean pb1 = cb.getProperty(aliasFor); 321 if (pb1 == null) { 322 for (ComponentBean cb1 = cb; pb1 == null && cb1 != null; pb1 = cb1 323 .getProperty(aliasFor)) { 324 String bct = cb1.getBaseComponentType(); 325 if (bct == null) 326 throw new IllegalArgumentException (aliasFor); 327 cb1 = getConfig().getComponent(bct); 328 if (cb1 == null) 329 throw new IllegalArgumentException (bct); 330 } 331 332 if (pb1 == null) 333 throw new IllegalArgumentException (aliasFor); 334 } 335 String aliasType = pb1.getPropertyClass(); 336 337 if (aliasType == null){ 338 aliasType = "String"; 339 }else if (aliasType.startsWith("java.lang.")){ 340 aliasType = aliasType.substring(10); 341 } 342 writer.emitExpression("// " + name, true); 343 String readMethod = null; 344 if (!pb.isWriteOnly()) { 345 DescriptionBean db = null; 346 if (isOverride() && ab != null){ 347 db = ab.getDescription(""); 348 } 349 if (db == null){ 350 db = pb.getDescription(""); 351 } 352 if (db != null) { 353 String description = db.getDescription(); 354 if (description != null && description.length() > 0) { 355 writer.startJavaDoc(); 356 description = description.trim(); 357 if (description.startsWith("<")){ 358 writer.emitJavaDocMultiLine(description); 359 }else{ 360 writer.emitJavaDocMultiLine("<p>" + description 361 + "</p>"); 362 } 363 writer.endJavaDoc(); 364 } 365 } 366 String method = pb.getReadMethod(); 367 if (method == null) 368 if ("boolean".equals(type)) 369 method = "is" + capitalize(name); 370 else 371 method = "get" + capitalize(name); 372 readMethod = method; 373 writer.startMethod(method, type, null, null); 374 StringBuffer sb = new StringBuffer ("return "); 375 if (!type.equals(aliasType)) { 376 sb.append("("); 377 sb.append(type); 378 sb.append(") "); 379 } 380 String aliasMethod = pb1.getReadMethod(); 381 if (aliasMethod == null){ 382 if ("boolean".equals(aliasType)){ 383 aliasMethod = "is" + capitalize(aliasFor); 384 }else{ 385 aliasMethod = "get" + capitalize(aliasFor); 386 } 387 } 388 sb.append(aliasMethod); 389 sb.append("();"); 390 writer.emitExpression(sb.toString(), true); 391 writer.endMethod(); 392 writer.emitNewline(); 393 } 394 if (!pb.isReadOnly()) { 395 DescriptionBean db = null; 396 if (isOverride() && ab != null) 397 db = ab.getDescription(""); 398 if (db == null) 399 db = pb.getDescription(""); 400 if (db != null) { 401 String description = db.getDescription(); 402 if (description != null && description.length() > 0) { 403 writer.startJavaDoc(); 404 description = description.trim(); 405 if (description.startsWith("<")) 406 writer.emitJavaDocMultiLine(description); 407 else 408 writer.emitJavaDocMultiLine("<p>" + description 409 + "</p>"); 410 if (readMethod != null) 411 writer.emitJavaDoc("@see #" + readMethod + "()"); 412 writer.endJavaDoc(); 413 } 414 } 415 String method = pb.getWriteMethod(); 416 if (method == null) 417 method = "set" + capitalize(name); 418 writer.startMethod(method, "void", new String [] { type }, 419 new String [] { var }); 420 String aliasMethod = pb1.getWriteMethod(); 421 if (aliasMethod == null) 422 aliasMethod = "set" + capitalize(aliasFor); 423 StringBuffer sb = new StringBuffer (aliasMethod); 424 sb.append("("); 425 if (!type.equals(aliasType)) { 426 sb.append("("); 427 sb.append(aliasType); 428 sb.append(") "); 429 } 430 sb.append(var); 431 sb.append(");"); 432 writer.emitExpression(sb.toString(), true); 433 writer.endMethod(); 434 writer.emitNewline(); 435 } 436 return; 437 } 438 writer.emitExpression("// " + name, true); 439 StringBuffer sb = new StringBuffer ("private "); 440 if ("java.lang.String".equals(type)) 441 sb.append("String"); 442 else 443 sb.append(type); 444 sb.append(" "); 445 sb.append(var); 446 sb.append(" = "); 447 if (primitive(type)) 448 sb.append((String ) defaults.get(type)); 449 else 450 sb.append("null"); 451 sb.append(";"); 452 writer.emitExpression(sb.toString(), true); 453 if (primitive(type) && pb.isBindable()) { 454 sb = new StringBuffer ("private boolean "); 455 sb.append(var); 456 sb.append("_set = false;"); 457 writer.emitExpression(sb.toString(), true); 458 } 459 writer.emitNewline(); 460 String readMethod = null; 461 if (!pb.isWriteOnly()) { 462 DescriptionBean db = null; 463 if (isOverride() && ab != null) 464 db = ab.getDescription(""); 465 if (db == null) 466 db = pb.getDescription(""); 467 if (db != null) { 468 String description = db.getDescription(); 469 if (description != null && description.length() > 0) { 470 writer.startJavaDoc(); 471 description = description.trim(); 472 if (description.startsWith("<")) 473 writer.emitJavaDocMultiLine(description); 474 else 475 writer.emitJavaDocMultiLine("<p>" + description 476 + "</p>"); 477 writer.endJavaDoc(); 478 } 479 } 480 String method = pb.getReadMethod(); 481 if (method == null) 482 if ("boolean".equals(type)) 483 method = "is" + capitalize(name); 484 else 485 method = "get" + capitalize(name); 486 readMethod = method; 487 writer.startMethod(method, type, null, null); 488 if (!pb.isBindable()) { 489 writer.emitExpression("return this." + var + ";", true); 490 } else { 491 if (primitive(type)) 492 writer.emitExpression("if (this." + var + "_set) {", true); 493 else 494 writer.emitExpression("if (this." + var + " != null) {", 495 true); 496 writer.indent(); 497 writer.emitExpression("return this." + var + ";", true); 498 writer.outdent(); 499 writer.emitExpression("}", true); 500 writer.emitExpression("ValueBinding _vb = getValueBinding(\"" 501 + name + "\");", true); 502 writer.emitExpression("if (_vb != null) {", true); 503 writer.indent(); 504 if (primitive(type)) { 505 writer 506 .emitExpression( 507 "Object _result = _vb.getValue(getFacesContext());", 508 true); 509 writer.emitExpression("if (_result == null) {", true); 510 writer.indent(); 511 writer.emitExpression("return " + defaults.get(type) + ";", 512 true); 513 writer.outdent(); 514 writer.emitExpression("} else {", true); 515 writer.indent(); 516 writer.emitExpression("return ((" + wrappers.get(type) 517 + ") _result)." + unwrappers.get(type) + "();", 518 true); 519 writer.outdent(); 520 writer.emitExpression("}", true); 521 } else { 522 writer.emitExpression("return (" + type + ") " 523 + "_vb.getValue(getFacesContext());", true); 524 } 525 writer.outdent(); 526 writer.emitExpression("}", true); 527 sb = new StringBuffer ("return "); 528 if (pb.getDefaultValue() != null) 529 sb.append(pb.getDefaultValue()); 530 else if (primitive(type)) 531 sb.append((String ) defaults.get(type)); 532 else 533 sb.append("null"); 534 sb.append(";"); 535 writer.emitExpression(sb.toString(), true); 536 } 537 writer.endMethod(); 538 writer.emitNewline(); 539 } 540 if (!pb.isReadOnly()) { 541 DescriptionBean db = null; 542 if (isOverride() && ab != null) 543 db = ab.getDescription(""); 544 if (db == null) 545 db = pb.getDescription(""); 546 if (db != null) { 547 String description = db.getDescription(); 548 if (description != null && description.length() > 0) { 549 writer.startJavaDoc(); 550 description = description.trim(); 551 if (description.startsWith("<")) 552 writer.emitJavaDocMultiLine(description); 553 else 554 writer.emitJavaDocMultiLine("<p>" + description 555 + "</p>"); 556 if (readMethod != null) 557 writer.emitJavaDoc("@see #" + readMethod + "()"); 558 writer.endJavaDoc(); 559 } 560 } 561 String method = pb.getWriteMethod(); 562 if (method == null) 563 method = "set" + capitalize(name); 564 writer.startMethod(method, "void", new String [] { type }, 565 new String [] { var }); 566 writer.emitExpression("this." + var + " = " + var + ";", true); 567 if (primitive(type) && pb.isBindable()) 568 writer.emitExpression("this." + var + "_set = true;", true); 569 writer.endMethod(); 570 } 571 writer.emitNewline(); 572 } 573 574 private void restore(ComponentBean cb) throws IOException { 575 PropertyBean pbs[] = cb.getProperties(); 576 if (pbs == null) 577 pbs = new PropertyBean[0]; 578 JavaSourceWriter writer = getWriter(); 579 writer.startJavaDoc(); 580 writer.emitJavaDoc("<p>Restore the state of this component.</p>"); 581 writer.endJavaDoc(); 582 writer.startMethod("restoreState", "void", new String [] { 583 "FacesContext", "Object" }, 584 new String [] { "_context", "_state" }); 585 writer.emitExpression("Object _values[] = (Object[]) _state;", true); 586 writer 587 .emitExpression("super.restoreState(_context, _values[0]);", 588 true); 589 int index = 1; 590 for (int i = 0; i < pbs.length; i++) { 591 if (pbs[i].getAliasFor() != null) 592 continue; 593 String name = pbs[i].getPropertyName(); 594 String type = pbs[i].getPropertyClass(); 595 if (type == null) 596 type = "String"; 597 else if (type.startsWith("java.lang.")) 598 type = type.substring(10); 599 String var = mangle(name); 600 if (primitive(type)) { 601 writer.emitExpression("this." + var + " = ((" 602 + wrappers.get(type) + ") _values[" + index++ + "])." 603 + unwrappers.get(type) + "();", true); 604 if (pbs[i].isBindable()) 605 writer.emitExpression("this." + var 606 + "_set = ((Boolean) _values[" + index++ 607 + "]).booleanValue();", true); 608 continue; 609 } 610 if (type.equals("javax.faces.el.MethodBinding")) 611 writer.emitExpression("this." + var + " = (" + type + ") " 612 + "restoreAttachedState(_context, _values[" + index++ 613 + "]);", true); 614 else 615 writer.emitExpression("this." + var + " = (" + type 616 + ") _values[" + index++ + "];", true); 617 } 618 619 writer.endMethod(); 620 writer.emitNewline(); 621 } 622 623 private void save(ComponentBean cb) throws IOException { 624 PropertyBean pbs[] = cb.getProperties(); 625 if (pbs == null) 626 pbs = new PropertyBean[0]; 627 JavaSourceWriter writer = getWriter(); 628 writer.startJavaDoc(); 629 writer.emitJavaDoc("<p>Save the state of this component.</p>"); 630 writer.endJavaDoc(); 631 writer.startMethod("saveState", "Object", 632 new String [] { "FacesContext" }, new String [] { "_context" }); 633 int n = pbs.length + 1; 634 for (int i = 0; i < pbs.length; i++) { 635 if (pbs[i].getAliasFor() != null) { 636 n--; 637 continue; 638 } 639 if (primitive(pbs[i].getPropertyClass()) && pbs[i].isBindable()) 640 n++; 641 } 642 643 writer 644 .emitExpression("Object _values[] = new Object[" + n + "];", 645 true); 646 writer.emitExpression("_values[0] = super.saveState(_context);", true); 647 int index = 1; 648 for (int i = 0; i < pbs.length; i++) { 649 if (pbs[i].getAliasFor() != null) 650 continue; 651 String name = pbs[i].getPropertyName(); 652 String type = pbs[i].getPropertyClass(); 653 if (type == null) 654 type = "String"; 655 else if (type.startsWith("java.lang.")) 656 type = type.substring(10); 657 String var = mangle(name); 658 if (primitive(type)) { 659 if (type.equals("boolean")) 660 writer.emitExpression("_values[" + index++ + "] = " 661 + "this." + var 662 + " ? Boolean.TRUE : Boolean.FALSE;", true); 663 else 664 writer.emitExpression("_values[" + index++ + "] = new " 665 + wrappers.get(type) + "(this." + var + ");", true); 666 if (pbs[i].isBindable()) 667 writer.emitExpression("_values[" + index++ + "] = " 668 + "this." + var 669 + "_set ? Boolean.TRUE : Boolean.FALSE;", true); 670 continue; 671 } 672 if (type.equals("javax.faces.el.MethodBinding")) 673 writer 674 .emitExpression("_values[" + index++ 675 + "] = saveAttachedState(_context, " + var 676 + ");", true); 677 else 678 writer.emitExpression("_values[" + index++ + "] = this." + var 679 + ";", true); 680 } 681 682 writer.emitExpression("return _values;", true); 683 writer.endMethod(); 684 writer.emitNewline(); 685 } 686 687 } 688 | Popular Tags |