1 55 package org.jboss.axis.wsdl.toJava; 56 57 import org.jboss.axis.Constants; 58 import org.jboss.axis.message.SOAPElementAxisImpl; 59 import org.jboss.axis.utils.JavaUtils; 60 import org.jboss.axis.utils.Messages; 61 import org.jboss.axis.wsdl.symbolTable.ElementDecl; 62 import org.jboss.axis.wsdl.symbolTable.SchemaUtils; 63 import org.jboss.axis.wsdl.symbolTable.TypeEntry; 64 import org.w3c.dom.Node ; 65 66 import javax.xml.namespace.QName ; 67 import java.io.IOException ; 68 import java.io.PrintWriter ; 69 import java.util.Vector ; 70 71 74 public class JavaBeanWriter extends JavaClassWriter 75 { 76 private TypeEntry type; 77 private Vector elements; 78 private Vector attributes; 79 private TypeEntry extendType; 80 protected JavaBeanHelperWriter helper; 81 protected Vector names = new Vector (); protected String simpleValueType = null; protected PrintWriter pw; 84 85 protected boolean enableDefaultConstructor = true; 88 protected boolean enableFullConstructor = false; 89 protected boolean enableSimpleConstructors = false; 90 protected boolean enableToString = false; 91 protected boolean enableSetters = true; 92 protected boolean enableGetters = true; 93 protected boolean enableEquals = true; 94 protected boolean enableHashCode = true; 95 96 106 protected JavaBeanWriter(Emitter emitter, 107 TypeEntry type, 108 Vector elements, 109 TypeEntry extendType, 110 Vector attributes, 111 JavaWriter helper) 112 { 113 super(emitter, type.getName(), "complexType"); 114 this.type = type; 115 this.elements = elements; 116 this.attributes = attributes; 117 this.extendType = extendType; 118 this.helper = (JavaBeanHelperWriter)helper; 119 if (type.isSimpleType()) 120 { 121 enableSimpleConstructors = true; 122 enableToString = true; 123 } 124 } 126 129 protected void writeFileBody(PrintWriter pw) throws IOException 130 { 131 132 this.pw = pw; 133 134 preprocess(); 138 139 writeMemberFields(); 141 142 if (enableDefaultConstructor) 144 { 145 writeDefaultConstructor(); 146 } 147 148 if (enableFullConstructor) 150 { 151 writeFullConstructor(); 152 } 153 154 if (enableSimpleConstructors) 156 { 157 writeSimpleConstructors(); 158 } 159 160 if (enableToString) 162 { 163 writeToStringMethod(); 164 } 165 166 writeAccessMethods(); 168 169 if (enableEquals) 171 { 172 writeEqualsMethod(); 173 } 174 if (enableHashCode) 175 { 176 writeHashCodeMethod(); 177 } 178 179 if (!emitter.isHelperWanted()) 182 { 183 helper.setPrintWriter(pw); 185 } 186 helper.generate(); 187 } 189 198 protected void preprocess() 199 { 200 if (elements != null) 202 { 203 for (int i = 0; i < elements.size(); i++) 204 { 205 ElementDecl elem = (ElementDecl)elements.get(i); 206 String typeName = elem.getType().getName(); 207 String variableName; 208 if (elem.getAnyElement()) 209 { 210 typeName = SOAPElementAxisImpl.class.getName() + "[]"; 211 variableName = Constants.ANYCONTENT; 212 } 213 else 214 { 215 String elemName = elem.getName().getLocalPart(); 216 variableName = Utils.xmlNameToJava(elemName); 217 } 218 names.add(typeName); 219 names.add(variableName); 220 if (type.isSimpleType() && 221 variableName.equals("value")) 222 { 223 simpleValueType = typeName; 224 } 225 } 226 } 227 if (attributes != null) 229 { 230 for (int i = 0; i < attributes.size(); i += 2) 231 { 232 String typeName = ((TypeEntry)attributes.get(i)).getName(); 233 QName xmlName = (QName )attributes.get(i + 1); 234 String variableName = 235 Utils.xmlNameToJava(xmlName.getLocalPart()); 236 names.add(typeName); 237 names.add(variableName); 238 if (type.isSimpleType() && 239 variableName.equals("value")) 240 { 241 simpleValueType = typeName; 242 } 243 } 244 } 245 246 if (extendType != null && extendType.getDimensions().equals("[]")) 247 { 248 String typeName = extendType.getName(); 249 String elemName = extendType.getQName().getLocalPart(); 250 String variableName = Utils.xmlNameToJava(elemName); 251 names.add(typeName); 252 names.add(variableName); 253 } 254 } 255 256 261 protected String getClassModifiers() 262 { 263 Node node = type.getNode(); 264 if (node != null) 265 { 266 if (JavaUtils.isTrueExplicitly(Utils.getAttribute(node, "abstract"))) 267 { 268 return super.getClassModifiers() + "abstract "; 269 } 270 } 271 return super.getClassModifiers(); 272 } 274 279 protected String getExtendsText() 280 { 281 String extendsText = ""; 283 if (extendType != null && !type.isSimpleType() && extendType.getDimensions().length() == 0) 284 { 285 extendsText = " extends " + extendType.getName() + " "; 286 } 287 return extendsText; 288 } 289 290 295 protected String getImplementsText() 296 { 297 String implementsText = " implements java.io.Serializable"; 299 if (type.isSimpleType()) 300 { 301 implementsText += ", org.jboss.axis.encoding.SimpleType"; 302 } 303 implementsText += " "; 304 return implementsText; 305 } 306 307 310 protected void writeMemberFields() 311 { 312 for (int i = 0; i < names.size(); i += 2) 314 { 315 String typeName = (String )names.get(i); 316 String variable = (String )names.get(i + 1); 317 318 pw.print(" private " + typeName + " " + variable + ";"); 320 321 if (elements == null || i >= (elements.size() * 2)) 323 pw.println(" // attribute"); 324 else 325 pw.println(); 326 } 327 pw.println(); 328 } 329 330 333 protected void writeDefaultConstructor() 334 { 335 pw.println(" public " + className + "() {"); 337 pw.println(" }"); 338 pw.println(); 339 } 340 341 348 protected void writeFullConstructor() 349 { 350 Vector extendList = new Vector (); 352 extendList.add(type); 353 TypeEntry parent = extendType; 354 while (parent != null) 355 { 356 extendList.add(parent); 357 parent = SchemaUtils.getComplexElementExtensionBase(parent.getNode(), 358 emitter.getSymbolTable()); 359 } 360 361 Vector paramTypes = new Vector (); 364 Vector paramNames = new Vector (); 365 for (int i = extendList.size() - 1; i >= 0; i--) 366 { 367 TypeEntry te = (TypeEntry)extendList.elementAt(i); 368 369 String mangle = ""; 372 if (i > 0) 373 { 374 mangle = "_" + 375 Utils.xmlNameToJava(te.getQName().getLocalPart()) + 376 "_"; 377 } 378 Vector attributes = SchemaUtils.getContainedAttributeTypes(te.getNode(), emitter.getSymbolTable()); 380 if (attributes != null) 381 { 382 for (int j = 0; j < attributes.size(); j += 2) 383 { 384 paramTypes.add(((TypeEntry)attributes.get(j)).getName()); 385 String name = ((QName )attributes.get(j + 1)).getLocalPart(); 386 paramNames.add(mangle + Utils.xmlNameToJava(name)); 387 } 388 } 389 Vector elements = SchemaUtils.getContainedElementDeclarations(te.getNode(), emitter.getSymbolTable()); 391 if (elements != null) 392 { 393 for (int j = 0; j < elements.size(); j++) 394 { 395 ElementDecl elem = (ElementDecl)elements.get(j); 396 paramTypes.add(elem.getType().getName()); 397 paramNames.add(mangle + 398 Utils.xmlNameToJava(elem.getName().getLocalPart())); 399 } 400 } 401 } 402 int localParams = paramTypes.size() - names.size() / 2; 404 405 406 if (paramTypes.size() > 0) 408 { 409 pw.println(" public " + className + "("); 410 for (int i = 0; i < paramTypes.size(); i++) 411 { 412 pw.print(" " + paramTypes.elementAt(i) + 413 " " + paramNames.elementAt(i)); 414 if ((i + 1) < paramTypes.size()) 415 { 416 pw.println(","); 417 } 418 else 419 { 420 pw.println(") {"); 421 } 422 } 423 424 if (extendType != null && localParams > 0) 426 { 427 pw.println(" super("); 428 for (int j = 0; j < localParams; j++) 429 { 430 pw.print(" " + paramNames.elementAt(j)); 431 if ((j + 1) < localParams) 432 { 433 pw.println(","); 434 } 435 else 436 { 437 pw.println(");"); 438 } 439 } 440 } 441 for (int j = localParams; j < paramNames.size(); j++) 443 { 444 pw.println(" this." + paramNames.elementAt(j) + 445 " = " + paramNames.elementAt(j) + ";"); 446 } 447 pw.println(" }"); 448 pw.println(); 449 } 450 } 451 452 457 protected void writeSimpleConstructors() 458 { 459 if (type.isSimpleType() && simpleValueType != null) 462 { 463 if (!simpleValueType.equals("java.lang.String")) 464 { 465 pw.println(" public " + className + "(" + 466 simpleValueType + " value) {"); 467 pw.println(" this.value = value;"); 468 pw.println(" }"); 469 pw.println(); 470 } 471 472 pw.println(" // " + Messages.getMessage("needStringCtor")); 473 pw.println(" public " + className + "(java.lang.String value) {"); 474 String wrapper = JavaUtils.getWrapper(simpleValueType); 476 477 if (wrapper != null) 478 { 479 pw.println(" this.value = new " + wrapper + 480 "(value)." + simpleValueType + "Value();"); 481 } 482 else 483 { 484 if (simpleValueType.equals("byte[]")) 485 { 486 pw.println(" this.value = org.jboss.axis.types.HexBinary.decode(value);"); 487 } 488 else if (simpleValueType.equals("org.jboss.axis.types.URI")) 489 { 490 pw.println(" try {"); 491 pw.println(" this.value = new org.jboss.axis.types.URI(value);"); 492 pw.println(" }"); 493 pw.println(" catch (org.jboss.axis.types.URI.MalformedURIException mue) {"); 494 pw.println(" this.value = new org.jboss.axis.types.URI();"); 495 pw.println(" }"); 496 } 497 else if (simpleValueType.equals("java.util.Date")) 498 { 499 pw.println(" try {"); 500 pw.println(" this.value = (java.text.DateFormat.getDateTimeInstance()).parse(value);"); 501 pw.println(" }"); 502 pw.println(" catch (java.text.ParseException e){"); 503 pw.println(" throw new java.lang.RuntimeException(e.toString());"); 504 pw.println(" }"); 505 } 506 else if (simpleValueType.equals("java.util.Calendar")) 507 { 508 pw.println(" java.util.Calendar cal = java.util.Calendar.getInstance();"); 509 pw.println(" try {"); 510 pw.println(" java.util.Date dt = (java.text.DateFormat.getDateTimeInstance()).parse(value);"); 511 pw.println(" cal.setTime(dt);"); 512 pw.println(" this.value = cal;"); 513 pw.println(" }"); 514 pw.println(" catch (java.text.ParseException e){"); 515 pw.println(" throw new java.lang.RuntimeException(e.toString());"); 516 pw.println(" }"); 517 } 518 else 519 { 520 pw.println(" this.value = new " + 521 simpleValueType + "(value);"); 522 } 523 } 524 pw.println(" }"); 525 pw.println(); 526 } 527 } 528 529 534 protected void writeToStringMethod() 535 { 536 if (type.isSimpleType() && simpleValueType != null) 538 { 539 pw.println(" // " + Messages.getMessage("needToString")); 540 String wrapper = JavaUtils.getWrapper(simpleValueType); 541 pw.println(" public java.lang.String toString() {"); 542 if (wrapper != null) 543 { 544 pw.println(" return new " + wrapper + "(value).toString();"); 545 } 546 else 547 { 548 if (simpleValueType.equals("byte[]")) 549 { 550 pw.println(" return value == null ? null : org.jboss.axis.types.HexBinary.encode(value);"); 551 } 552 else 553 { 554 pw.println(" return value == null ? null : value.toString();"); 555 } 556 } 557 pw.println(" }"); 558 pw.println(); 559 } 560 } 561 562 565 protected void writeAccessMethods() 566 { 567 int j = 0; 568 for (int i = 0; i < names.size(); i += 2, j++) 570 { 571 String typeName = (String )names.get(i); 572 String name = (String )names.get(i + 1); 573 String capName = Utils.capitalizeFirstChar(name); 574 575 String get = "get"; 576 if (typeName.equals("boolean")) 577 get = "is"; 578 579 if (enableGetters) 580 { 581 pw.println(" public " + typeName + " " + 582 get + capName + "() {"); 583 pw.println(" return " + name + ";"); 584 pw.println(" }"); 585 pw.println(); 586 } 587 if (enableSetters) 588 { 589 pw.println(" public void set" + capName + "(" + 590 typeName + " " + name + ") {"); 591 pw.println(" this." + name + " = " + name + ";"); 592 pw.println(" }"); 593 pw.println(); 594 } 595 596 if (elements != null && j < elements.size()) 605 { 606 ElementDecl elem = (ElementDecl)elements.get(j); 607 if (elem.getType().getQName().getLocalPart().indexOf("[") > 0) 608 { 609 String compName = typeName.substring(0, typeName.lastIndexOf("[")); 610 if (enableGetters) 611 { 612 pw.println(" public " + compName + " " + get + capName + 613 "(int i) {"); 614 pw.println(" return " + name + "[i];"); 615 pw.println(" }"); 616 pw.println(); 617 } 618 if (enableSetters) 619 { 620 pw.println(" public void set" + capName + "(int i, " + 621 compName + " value) {"); 622 643 pw.println(" this." + name + "[i] = value;"); 644 pw.println(" }"); 645 pw.println(); 646 } 647 } 648 } 649 } 650 } 651 652 655 protected void writeEqualsMethod() 656 { 657 658 pw.println(" private java.lang.Object __equalsCalc = null;"); 661 pw.println(" public synchronized boolean equals(java.lang.Object obj) {"); 662 663 pw.println(" if (!(obj instanceof " + className + ")) return false;"); 665 pw.println(" " + className + " other = (" + className + ") obj;"); 666 pw.println(" if (obj == null) return false;"); 667 pw.println(" if (this == obj) return true;"); 668 669 pw.println(" if (__equalsCalc != null) {"); 671 pw.println(" return (__equalsCalc == obj);"); 672 pw.println(" }"); 673 pw.println(" __equalsCalc = obj;"); 674 675 String truth = "true"; 677 if (extendType != null && !type.isSimpleType()) 678 { 679 truth = "super.equals(obj)"; 680 } 681 pw.println(" boolean _equals;"); 682 if (names.size() == 0) 683 { 684 pw.println(" _equals = " + truth + ";"); 685 } 686 else 687 { 688 pw.println(" _equals = " + truth + " && "); 689 for (int i = 0; i < names.size(); i += 2) 690 { 691 String variableType = (String )names.get(i); 692 String variable = (String )names.get(i + 1); 693 String get = "get"; 694 695 if (variableType.equals("boolean")) 696 get = "is"; 697 698 if (variableType.equals("int") || 699 variableType.equals("long") || 700 variableType.equals("short") || 701 variableType.equals("float") || 702 variableType.equals("double") || 703 variableType.equals("boolean") || 704 variableType.equals("byte")) 705 { 706 pw.print(" this." + variable + " == other." + get + 707 Utils.capitalizeFirstChar(variable) + "()"); 708 } 709 else if (variableType.indexOf("[") >= 0) 710 { 711 pw.println(" ((this." + variable + 713 "==null && other." + get + 714 Utils.capitalizeFirstChar(variable) + "()==null) || "); 715 pw.println(" (this." + variable + "!=null &&"); 716 pw.print(" java.util.Arrays.equals(this." + variable + 717 ", other." + get + 718 Utils.capitalizeFirstChar(variable) + "())))"); 719 720 } 721 else 722 { 723 pw.println(" ((this." + variable + 724 "==null && other." + get + 725 Utils.capitalizeFirstChar(variable) + "()==null) || "); 726 pw.println(" (this." + variable + "!=null &&"); 727 pw.print(" this." + variable + 728 ".equals(other." + get + 729 Utils.capitalizeFirstChar(variable) + "())))"); 730 } 731 if (i == (names.size() - 2)) 732 pw.println(";"); 733 else 734 pw.println(" &&"); 735 } 736 } 737 pw.println(" __equalsCalc = null;"); 738 pw.println(" return _equals;"); 739 pw.println(" }"); 740 pw.println(""); 741 } 742 743 746 protected void writeHashCodeMethod() 747 { 748 pw.println(" private boolean __hashCodeCalc = false;"); 751 pw.println(" public synchronized int hashCode() {"); 752 pw.println(" if (__hashCodeCalc) {"); 753 pw.println(" return 0;"); 754 pw.println(" }"); 755 pw.println(" __hashCodeCalc = true;"); 756 757 String start = "1"; 759 if (extendType != null && !type.isSimpleType()) 760 { 761 start = "super.hashCode()"; 762 } 763 pw.println(" int _hashCode = " + start + ";"); 764 for (int i = 0; i < names.size(); i += 2) 765 { 766 String variableType = (String )names.get(i); 767 String variable = (String )names.get(i + 1); 768 String get = "get"; 769 770 if (variableType.equals("boolean")) 771 get = "is"; 772 773 if (variableType.equals("int") || 774 variableType.equals("short") || 775 variableType.equals("byte")) 776 { 777 pw.println(" _hashCode += " + get + 778 Utils.capitalizeFirstChar(variable) + "();"); 779 } 780 else if (variableType.equals("boolean")) 781 { 782 pw.println(" _hashCode += new Boolean(" + get + 783 Utils.capitalizeFirstChar(variable) + "()).hashCode();"); 784 } 785 else if (variableType.equals("long")) 786 { 787 pw.println(" _hashCode += new Long(" + get + 788 Utils.capitalizeFirstChar(variable) + "()).hashCode();"); 789 } 790 else if (variableType.equals("float")) 791 { 792 pw.println(" _hashCode += new Float(" + get + 793 Utils.capitalizeFirstChar(variable) + "()).hashCode();"); 794 } 795 else if (variableType.equals("double")) 796 { 797 pw.println(" _hashCode += new Double(" + get + 798 Utils.capitalizeFirstChar(variable) + "()).hashCode();"); 799 } 800 else if (variableType.indexOf("[") >= 0) 801 { 802 pw.println(" if (" + get + 806 Utils.capitalizeFirstChar(variable) + "() != null) {"); 807 pw.println(" for (int i=0;"); 808 pw.println(" i<java.lang.reflect.Array.getLength(" + get + 809 Utils.capitalizeFirstChar(variable) + "());"); 810 pw.println(" i++) {"); 811 pw.println(" java.lang.Object obj = java.lang.reflect.Array.get(" + 812 get + 813 Utils.capitalizeFirstChar(variable) + "(), i);"); 814 pw.println(" if (obj != null &&"); 815 pw.println(" !obj.getClass().isArray()) {"); 816 pw.println(" _hashCode += obj.hashCode();"); 817 pw.println(" }"); 818 pw.println(" }"); 819 pw.println(" }"); 820 } 821 else 822 { 823 pw.println(" if (" + get + 824 Utils.capitalizeFirstChar(variable) + "() != null) {"); 825 pw.println(" _hashCode += " + get + 826 Utils.capitalizeFirstChar(variable) + "().hashCode();"); 827 pw.println(" }"); 828 } 829 } 830 pw.println(" __hashCodeCalc = false;"); 832 pw.println(" return _hashCode;"); 833 pw.println(" }"); 834 pw.println(""); 835 } 836 } | Popular Tags |