1 57 58 package org.apache.wsif.compiler.schema.tools; 59 60 import java.io.File ; 61 import java.io.IOException ; 62 import java.io.OutputStream ; 63 import java.io.OutputStreamWriter ; 64 import java.io.Writer ; 65 import java.util.Enumeration ; 66 import java.util.HashMap ; 67 import java.util.Iterator ; 68 import java.util.Map ; 69 import java.util.Vector ; 70 import java.security.AccessController ; 71 import java.security.PrivilegedAction ; 72 73 import javax.xml.namespace.QName ; 74 75 import com.ibm.wsdl.util.xml.DOMUtils; 76 import org.apache.wsif.WSIFConstants; 77 import org.apache.wsif.compiler.schema.SchemaException; 78 import org.apache.wsif.compiler.util.StreamFactory; 79 import org.apache.wsif.compiler.util.TypeMapping; 80 import org.apache.wsif.logging.Trc; 81 import org.w3c.dom.Element ; 82 import org.w3c.dom.Node ; 83 84 89 public class Schema2Java { 90 91 private HashMap registry = new HashMap (); 92 private String workingDirectory; 93 private HashMap namespaceRegistry = new HashMap (); 94 95 private HashMap undefinedRegistry = new HashMap (); 96 private Vector schemaElementList = new Vector (); 97 98 private String packageName = ""; 99 private boolean verbose = true; 100 private boolean overwrite = false; 101 private boolean javac = true; 102 103 107 public Schema2Java(String schemaURI) { 108 registry.put(new QName (schemaURI, "string"), "java.lang.String"); 109 registry.put(new QName (schemaURI, "integer"), "java.math.BigInteger"); 110 registry.put(new QName (schemaURI, "boolean"), "boolean"); 111 registry.put(new QName (schemaURI, "float"), "float"); 112 registry.put(new QName (schemaURI, "double"), "double"); 113 registry.put(new QName (schemaURI, "binary"), "byte[]"); 114 registry.put(new QName (schemaURI, "base64Binary"), "byte[]"); 115 registry.put(new QName (schemaURI, "hexBinary"), "byte[]"); 116 registry.put(new QName (schemaURI, "long"), "long"); 117 registry.put(new QName (schemaURI, "int"), "int"); 118 registry.put(new QName (schemaURI, "short"), "short"); 119 registry.put(new QName (schemaURI, "decimal"), "java.math.BigDecimal"); 120 registry.put( 121 new QName (schemaURI, "QName"), 122 "javax.xml.namespace.QName"); 123 if (schemaURI.equals(WSIFConstants.NS_URI_2001_SCHEMA_XSD)) { 125 registry.put( 126 new QName (schemaURI, "dateTime"), 127 "java.util.Calendar"); 128 } else { 129 registry.put( 130 new QName (schemaURI, "timeInstant"), 131 "java.util.Calendar"); 132 } 133 registry.put(new QName (schemaURI, "byte"), "byte"); 134 registry.put(new QName (schemaURI, "void"), "void"); 135 registry.put(new QName (schemaURI, "ur-type"), "java.lang.Object"); 136 137 registry.put( 139 new QName (WSIFConstants.NS_URI_SOAP_ENC, "string"), 140 "java.lang.String"); 141 registry.put( 142 new QName (WSIFConstants.NS_URI_SOAP_ENC, "boolean"), 143 "java.lang.Boolean"); 144 registry.put( 145 new QName (WSIFConstants.NS_URI_SOAP_ENC, "float"), 146 "java.lang.Float"); 147 registry.put( 148 new QName (WSIFConstants.NS_URI_SOAP_ENC, "double"), 149 "java.lang.Double"); 150 registry.put( 151 new QName (WSIFConstants.NS_URI_SOAP_ENC, "decimal"), 152 "java.math.BigDecimal"); 153 registry.put( 154 new QName (WSIFConstants.NS_URI_SOAP_ENC, "int"), 155 "java.lang.Integer"); 156 registry.put( 157 new QName (WSIFConstants.NS_URI_SOAP_ENC, "short"), 158 "java.lang.Short"); 159 registry.put( 160 new QName (WSIFConstants.NS_URI_SOAP_ENC, "byte"), 161 "java.lang.Byte"); 162 registry.put( 163 new QName (WSIFConstants.NS_URI_SOAP_ENC, "base64"), 164 "byte[]"); 165 } 166 167 174 private ClassFile any2Field(SchemaAny any, ClassFile file) { 175 176 if (any == null || file == null) 177 throw new IllegalArgumentException ( 178 "Argument to 'any2Field' cannot " + "be null."); 179 180 file.addField( 181 new ClassField( 182 "byte[]", 183 Conventions.schema2JavaName("any", null, false), 184 any.isArray())); 185 return file; 186 187 } 188 189 196 private ClassFile anyAttribute2Field( 197 SchemaAnyAttribute anyAttr, 198 ClassFile file) { 199 200 if (anyAttr == null || file == null) 201 throw new IllegalArgumentException ( 202 "Argument to 'anyAttribute2Field' " + "cannot be null."); 203 204 file.addField( 205 new ClassField( 206 "byte[]", 207 Conventions.schema2JavaName("anyAttribute", null, false), 208 false)); 209 return file; 210 } 211 212 219 private ClassFile attribute2Field(SchemaAttribute attr, ClassFile file) 220 throws SchemaException { 221 222 if (attr == null || file == null) 223 throw new IllegalArgumentException ( 224 "Argument to 'attribute2Field' " + "cannot be null."); 225 226 QName type = attr.getType(); 227 QName ref = attr.getRef(); 228 SchemaSimpleType simp = attr.getChild(); 229 String targetURI = attr.getTargetURI(); 230 231 ClassField field; 232 if (ref != null) { 233 field = 234 new ClassField( 235 getJavaType(ref, targetURI, "attribute"), 236 Conventions.schema2JavaName( 237 "attribute", 238 ref.getLocalPart(), 239 false), 240 false); 241 file.addField(field); 242 } else if (type != null) { 243 field = 244 new ClassField( 245 getJavaType(type, targetURI, "simpleType"), 246 Conventions.schema2JavaName( 247 "attribute", 248 attr.getName(), 249 false), 250 false); 251 file.addField(field); 252 } else if (simp != null) { 253 String name = 254 Conventions.schema2JavaName("attribute", attr.getName(), false); 255 file = simpleType2Field(name, false, simp, file); 256 } 257 258 return file; 259 } 260 261 269 private ClassFile attributeGroup2Class(SchemaAttributeGroup attrGp) 270 throws SchemaException { 271 272 if (attrGp == null) 273 throw new IllegalArgumentException ( 274 "Argument to 'attributeGroup2Class' " + "cannot be null."); 275 276 String className = 277 Conventions.schema2JavaName( 278 "attributeGroup", 279 attrGp.getName(), 280 true); 281 ClassFile file = new ClassFile(className); 282 283 Vector children = attrGp.getChildren(); 284 for (int i = 0; i < children.size(); i++) { 285 SchemaType child = (SchemaType) children.elementAt(i); 286 switch (child.getElementType()) { 287 case SchemaType.ATTRIBUTEGROUP : 288 file = 289 attributeGroup2Field( 290 (SchemaAttributeGroup) child, 291 file); 292 break; 293 case SchemaType.ATTRIBUTE : 294 file = attribute2Field((SchemaAttribute) child, file); 295 break; 296 case SchemaType.ANYATTRIBUTE : 297 } 298 } 299 300 return file; 301 } 302 303 309 private ClassFile attributeGroup2Field( 310 SchemaAttributeGroup attrGp, 311 ClassFile file) 312 throws SchemaException { 313 314 if (attrGp == null || file == null) 315 throw new IllegalArgumentException ( 316 "Argument to 'attributeGroup2Field' " + "cannot be null."); 317 318 ClassField field; 319 320 QName ref = attrGp.getRef(); 321 if (ref != null) 322 field = 323 new ClassField( 324 getJavaType(ref, attrGp.getTargetURI(), "attributeGroup"), 325 Conventions.schema2JavaName( 326 "attributeGroup", 327 ref.getLocalPart(), 328 false), 329 false); 330 else { 331 throw new SchemaException( 332 "Attribute group can only be defined at " + "the top level."); 333 } 334 file.addField(field); 335 336 return file; 337 338 } 339 340 347 private ClassFile complexType2Class( 348 String className, 349 SchemaComplexType elm) 350 throws SchemaException { 351 352 if (className == null || elm == null) 353 throw new IllegalArgumentException ( 354 "Argument to 'complexType2Class' " + "cannot be null."); 355 356 QName base = elm.getBase(); 357 ClassFile file = new ClassFile(className); 358 359 if (base != null) 360 file.superClassName = 361 getJavaType(base, elm.getTargetURI(), "complexType"); 362 365 Vector children = elm.getChildren(); 366 for (int i = 0; i < children.size(); i++) { 367 SchemaType child = (SchemaType) children.elementAt(i); 368 String name = child.getName(); 369 switch (child.getElementType()) { 370 case SchemaType.ELEMENT : 371 file = element2Field((SchemaElement) child, file); 372 break; 373 case SchemaType.GROUP : 374 file = group2Field((SchemaGroup) child, file); 375 break; 376 case SchemaType.ATTRIBUTE : 377 file = attribute2Field((SchemaAttribute) child, file); 378 break; 379 case SchemaType.ATTRIBUTEGROUP : 380 file = 381 attributeGroup2Field( 382 (SchemaAttributeGroup) child, 383 file); 384 break; 385 case SchemaType.ANY : 386 file = any2Field((SchemaAny) child, file); 387 break; 388 case SchemaType.ANYATTRIBUTE : 389 file = anyAttribute2Field((SchemaAnyAttribute) child, file); 390 break; 391 case SchemaType.ALL : 392 case SchemaType.CHOICE : 393 case SchemaType.SEQUENCE : 394 file = mgs2Field((SchemaMGS) child, file); 395 break; 396 } 397 } 398 399 return file; 400 } 401 402 416 public Map createJavaMapping(Element root, Map reg) 417 throws SchemaException { 418 419 if (root == null || reg == null) 420 throw new IllegalArgumentException ( 421 "Argument to 'createJavaMapping' " + "cannot be null."); 422 423 String targetURI = root.getAttribute("targetNamespace"); 424 Vector elements = parseSchemaRoot(root, targetURI); 426 427 for (int i = 0; i < elements.size(); i++) { 428 schemaElementList.addElement(elements.elementAt(i)); 429 } 430 431 updateRegistry(schemaElementList); 433 434 return getRegistry(reg); 435 436 } 437 438 445 private ClassFile element2Field(SchemaElement elm, ClassFile file) 446 throws SchemaException { 447 448 if (elm == null || file == null) 449 throw new IllegalArgumentException ( 450 "Argument to 'element2Field' " + "cannot be null."); 451 452 String name = elm.getName(); 453 QName ref = elm.getRef(); 454 QName type = elm.getType(); 455 SchemaType child = elm.getChild(); 456 String targetURI = elm.getTargetURI(); 457 458 if (name != null) 459 name = Conventions.schema2JavaName("element", name, false); 460 461 if (ref != null) { 462 String javaType = getJavaType(ref, targetURI, "element"); 463 file.addField( 464 new ClassField( 465 javaType, 466 Conventions.schema2JavaName( 467 "element", 468 ref.getLocalPart(), 469 false), 470 elm.isArray())); 471 } else if (type != null) { 472 476 String javaType = getJavaType(type, targetURI, "complexType"); 477 file.addField(new ClassField(javaType, name, elm.isArray())); 478 } else if (child == null) { 479 throw new SchemaException( 480 "The type of this element is unknown: '" + name + "'."); 481 } else if (child.getElementType() == SchemaType.SIMPLETYPE) { 482 file = 483 simpleType2Field( 484 name, 485 elm.isArray(), 486 (SchemaSimpleType) child, 487 file); 488 } else if (child.getElementType() == SchemaType.COMPLEXTYPE) { 489 throw new SchemaException( 490 "Inner complex-types are not supported: '" + name + "'."); 491 492 } 496 497 return file; 498 } 499 500 507 private QName getAttributeQName(Element element, String attr) { 508 509 if (element == null || attr == null) 510 throw new IllegalArgumentException ( 511 "Argument to 'getAttrQName' " + "cannot be null."); 512 513 String name = DOMUtils.getAttribute(element, attr); 514 515 if (name == null) 516 return null; 517 518 int index = name.lastIndexOf(":"); 519 String prefix = null; 520 521 if (index != -1) { 522 prefix = name.substring(0, index); 523 name = name.substring(index + 1); 524 } 525 String uri = DOMUtils.getNamespaceURIFromPrefix(element, prefix); 526 527 return new QName (uri, name); 528 529 } 530 531 537 private String getGenericJavaName(QName qName, String elementType) { 538 539 if (qName == null) 540 throw new IllegalArgumentException ( 541 "Argument to 'getGenericJavaName' " + "cannot be null."); 542 543 String javaName = (String ) undefinedRegistry.get(qName); 544 if (javaName != null) 545 return javaName; 546 547 String javaPath = getNamespaceURIMapping(qName.getNamespaceURI()); 548 549 String name = qName.getLocalPart(); 550 name = Conventions.schema2JavaName(elementType, name, true); 551 552 if (javaPath.compareTo("") != 0) 553 javaName = javaPath + "." + name; 554 else 555 javaName = name; 556 557 undefinedRegistry.put(qName, javaName); 558 559 Trc.event( 560 this, 561 "Schema name ", 562 qName, 563 " is undefined, and so generic Java name ", 564 javaName, 565 " is used instead."); 566 return javaName; 567 } 568 569 576 private String getJavaType( 577 QName name, 578 String targetURI, 579 String elementType) 580 throws SchemaException { 581 582 if (name == null) 583 throw new IllegalArgumentException ( 584 "Argument to 'getJavaType' " + "cannot be null."); 585 586 String type = (String ) registry.get(name); 587 588 if (type == null) { 589 throw new SchemaException( 590 "Basic type '" + name + "' not supported."); 591 592 } 594 595 String targetNS = getNamespaceURIMapping(targetURI); 596 597 if (type.startsWith(targetNS) 598 599 ) 600 type = type.substring(type.lastIndexOf(".") + 1); 601 602 return type; 603 } 604 605 609 public void setPackageName(String packageName) { 610 this.packageName = packageName; 611 } 612 613 619 public String getNamespaceURIMapping(String namespaceURI) { 620 621 if (namespaceURI == null) 622 throw new IllegalArgumentException ( 623 "Argument to " 624 + "'getNamespaceURIMapping' " 625 + "cannot be null."); 626 627 if (namespaceURI.compareTo("") == 0) 628 return ""; 629 630 if (packageName != null && !packageName.equals("")) { 632 return packageName; 633 } 634 636 String javaPath = (String ) namespaceRegistry.get(namespaceURI); 637 if (javaPath == null) { 638 javaPath = Conventions.namespaceURI2JavaPath(namespaceURI); 639 namespaceRegistry.put(namespaceURI, javaPath); 640 } 641 return javaPath; 642 643 } 644 645 651 public Map getRegistry(Map reg) { 652 653 for (Iterator i = registry.keySet().iterator(); i.hasNext();) { 654 QName key = (QName ) i.next(); 655 reg.put(key, new TypeMapping(key, (String ) registry.get(key))); 656 } 657 return reg; 658 659 } 660 661 667 public String getWorkingDirectory() { 668 if (workingDirectory == null) { 669 workingDirectory = 670 (String ) AccessController.doPrivileged(new PrivilegedAction () { 671 public Object run() { 672 return System.getProperty("user.dir"); 673 } 674 }); 675 } 676 return workingDirectory; 677 } 678 679 private ClassFile group2Class(SchemaGroup gp) throws SchemaException { 680 681 if (gp == null) 682 throw new IllegalArgumentException ( 683 "Argument to 'group2Class' " + "cannot be null."); 684 685 SchemaMGS child = gp.getChild(); 686 String className = 687 Conventions.schema2JavaName("group", gp.getName(), true); 688 ClassFile file = new ClassFile(className); 689 file = mgs2Field(child, file); 690 691 return file; 692 } 693 694 701 private ClassFile group2Field(SchemaGroup gp, ClassFile file) 702 throws SchemaException { 703 704 if (gp == null || file == null) 705 throw new IllegalArgumentException ( 706 "Argument to 'group2Field' " + "cannot be null."); 707 708 QName ref = gp.getRef(); 709 710 if (ref != null) { 711 String javaType = getJavaType(ref, gp.getTargetURI(), "group"); 712 file.addField( 713 new ClassField( 714 javaType, 715 Conventions.schema2JavaName( 716 "group", 717 ref.getLocalPart(), 718 false), 719 gp.isArray())); 720 } 721 722 return file; 723 } 724 725 private ClassFile mgs2Class(String className, SchemaMGS elm) 726 throws SchemaException { 727 728 if (className == null || elm == null) 729 throw new IllegalArgumentException ( 730 "Argument to 'mgs2Class' " + "cannot be null."); 731 732 ClassFile file = new ClassFile(className); 733 734 Vector children = elm.getChildren(); 735 for (int i = 0; i < children.size(); i++) { 736 SchemaType child = (SchemaType) children.elementAt(i); 737 738 switch (child.getElementType()) { 739 case SchemaType.ELEMENT : 740 element2Field((SchemaElement) child, file); 741 break; 742 case SchemaType.GROUP : 743 group2Field((SchemaGroup) child, file); 744 break; 745 case SchemaType.ANY : 746 case SchemaType.CHOICE : 747 case SchemaType.SEQUENCE : 748 mgs2Field((SchemaMGS) child, file); 749 } 750 } 751 752 return file; 753 } 754 755 762 private ClassFile mgs2Field(SchemaMGS mgs, ClassFile file) 763 throws SchemaException { 764 765 if (mgs == null || file == null) 766 throw new IllegalArgumentException ( 767 "Argument to 'mgs2Field' " + "cannot be null."); 768 769 String mgsType; 770 switch (mgs.getElementType()) { 771 case SchemaType.ALL : 772 mgsType = "all"; 773 break; 774 case SchemaType.CHOICE : 775 mgsType = "choice"; 776 break; 777 case SchemaType.SEQUENCE : 778 mgsType = "sequence"; 779 break; 780 default : 781 return file; 782 } 783 784 String cname = Conventions.schema2JavaName(mgsType, null, true); 785 String fname = Conventions.schema2JavaName(mgsType, null, false); 786 file.addField(new ClassField(cname, fname, mgs.isArray())); 787 file.addInnerClass(mgs2Class(cname, mgs)); 788 return file; 789 790 } 791 792 799 public void outputJavaMapping() throws SchemaException, IOException { 800 801 Vector classFileList = new Vector (); 802 803 for (int i = 0; i < schemaElementList.size(); i++) { 804 SchemaType element = (SchemaType) schemaElementList.elementAt(i); 805 ClassFile file = null; 806 807 switch (element.getElementType()) { 808 case SchemaType.COMPLEXTYPE : 809 SchemaComplexType cmp = (SchemaComplexType) element; 810 String className = 811 Conventions.schema2JavaName( 812 "complexType", 813 cmp.getName(), 814 true); 815 file = complexType2Class(className, cmp); 816 break; 817 case SchemaType.GROUP : 818 file = group2Class((SchemaGroup) element); 819 break; 820 case SchemaType.ATTRIBUTEGROUP : 821 file = attributeGroup2Class((SchemaAttributeGroup) element); 822 break; 823 case SchemaType.ELEMENT : 824 SchemaElement el = (SchemaElement) element; 825 SchemaType child = el.getChild(); 826 if (child != null 827 && child.getElementType() == SchemaType.COMPLEXTYPE) { 828 className = 829 Conventions.schema2JavaName( 830 "element", 831 el.getName(), 832 true); 833 file = 834 complexType2Class( 835 className, 836 (SchemaComplexType) el.getChild()); 837 } 838 break; 839 } 840 if (file != null) { 841 file.packageName = 842 getNamespaceURIMapping(element.getTargetURI()); 843 classFileList.addElement(file); 844 } 845 } 846 847 int classCount = classFileList.size(); 848 String [] fileNames = new String [classCount]; 849 StreamFactory streamFactory = new StreamFactory(); 850 851 for (int classIndex = 0; classIndex < classCount; classIndex++) { 852 ClassFile classFile = 853 (ClassFile) classFileList.elementAt(classIndex); 854 String javaPathName = 855 Conventions.getJavaPathName( 856 getWorkingDirectory(), 857 classFile.packageName); 858 String javaFileName = classFile.className + ".java"; 859 OutputStream os = 860 streamFactory.getOutputStream( 861 javaPathName, 862 javaFileName, 863 overwrite); 864 Writer classWriter = new OutputStreamWriter (os); 865 866 classWriter.write( 867 "/*\n *This class is automatically generated by schema to Java program.\n"); 868 classWriter.write( 869 " *Only a subset of schema is handled and some of the schema information\n"); 870 classWriter.write(" *may be lost during translation\n */\n\n"); 871 872 if (classFile.packageName != null) 873 classWriter.write("package " + classFile.packageName + ";\n\n"); 874 printJavaFile(classWriter, classFile, null); 876 877 classWriter.flush(); 878 classWriter.close(); 879 880 fileNames[classIndex] = 881 new File (javaPathName, javaFileName).getAbsolutePath(); 882 } 883 884 if (javac) { 885 for (int i = 0; i < classCount; i++) { 886 if (Conventions.JDKcompile(fileNames[i], getWorkingDirectory()) 887 && verbose) { 888 System.out.println("Compiled file '" + fileNames[i] + "'."); 889 } 890 } 891 } 892 } 893 894 899 private Vector parseSchemaRoot(Element root, String targetURI) 900 throws SchemaException { 901 if (root == null) 902 throw new IllegalArgumentException ( 903 "Argument to 'parseSchemaRoot' " + "cannot be null."); 904 905 Node child = root.getFirstChild(); 906 Vector elements = new Vector (); 907 908 while (child != null) { 909 if (child.getNodeType() == Node.ELEMENT_NODE) { 910 Element element = (Element ) child; 911 912 String elementType = element.getLocalName(); 913 914 if (elementType == null) { 915 return null; 916 } 917 918 String name = DOMUtils.getAttribute(element, "name"); 919 QName type = getAttributeQName(element, "type"); 920 boolean isArray = false; 921 922 if (elementType.equals("complexType")) { 923 boolean isFinal = false; 924 boolean isAbstract = false; 925 QName base = null; 926 Vector children = parseSchemaRoot(element, targetURI); 927 elements.addElement( 928 new SchemaComplexType( 929 name, 930 base, 931 isAbstract, 932 isFinal, 933 children, 934 targetURI)); 935 } else if (elementType.equals("simpleType")) { 936 QName base = null; 937 boolean isList = false; 938 elements.addElement( 939 new SchemaSimpleType( 940 name, 941 base, 942 isList, 943 targetURI)); 944 } else if (elementType.equals("element")) { 945 QName ref = null; 946 elements.addElement( 947 new SchemaElement( 948 name, 949 ref, 950 type, 951 isArray, 952 null, 953 targetURI)); 954 } else { 955 } 957 } 958 959 child = child.getNextSibling(); 960 } 961 962 return elements; 963 } 964 965 973 private void printJavaFile( 974 Writer classWriter, 975 ClassFile classFile, 976 String tab) 977 throws java.io.IOException { 978 979 String className = classFile.className; 980 String superClassName = classFile.superClassName; 981 982 984 if (tab == null) { 985 tab = ""; 986 classWriter.write("public "); 987 } else 988 classWriter.write(tab); 989 if (classFile.isAbstract) 990 classWriter.write("abstract "); 991 else if (classFile.isFinal) 992 classWriter.write("final"); 993 if (superClassName != null) 994 classWriter.write( 995 "class " + className + " extends " + superClassName + "{\n"); 996 else 997 classWriter.write("class " + className + "{\n"); 998 999 classWriter.write("\n" + tab + "\t//instance variables\n"); 1000 1001 int fieldLength = classFile.getFieldCount(); 1002 for (int fieldIndex = 0; fieldIndex < fieldLength; fieldIndex++) { 1003 ClassField classField = classFile.getField(fieldIndex); 1004 classWriter.write(tab + "\tprivate " + classField.fieldType); 1005 if (classField.isArray) 1006 classWriter.write("[]"); 1007 classWriter.write("\t" + classField.fieldName + ";\n"); 1008 } 1009 1010 classWriter.write("\n" + tab + "\t//constructors\n"); 1011 if (fieldLength > 0) 1012 classWriter.write(tab + "\tpublic " + className + " () { }\n"); 1013 1014 classWriter.write("\n" + tab + "\tpublic " + className + " ("); 1015 for (int fieldIndex = 0; fieldIndex < fieldLength; fieldIndex++) { 1016 ClassField classField = classFile.getField(fieldIndex); 1017 classWriter.write(classField.fieldType); 1018 if (classField.isArray) 1019 classWriter.write("[]"); 1020 classWriter.write(" " + classField.fieldName); 1021 if (fieldIndex < fieldLength - 1) 1022 classWriter.write(", "); 1023 } 1024 classWriter.write(") {\n"); 1025 1026 for (int fieldIndex = 0; fieldIndex < fieldLength; fieldIndex++) { 1027 ClassField classField = classFile.getField(fieldIndex); 1028 classWriter.write( 1029 tab 1030 + "\t\tthis." 1031 + classField.fieldName 1032 + "\t= " 1033 + classField.fieldName 1034 + ";\n"); 1035 } 1036 classWriter.write(tab + "\t}\n"); 1037 1038 for (int fieldIndex = 0; fieldIndex < fieldLength; fieldIndex++) { 1042 ClassField classField = classFile.getField(fieldIndex); 1043 String propertyName = 1044 Character.toUpperCase(classField.fieldName.charAt(0)) 1045 + classField.fieldName.substring(1); 1046 classWriter.write("\n" + tab + "\tpublic "); 1047 if (classField.isArray) 1048 classWriter.write("[]"); 1049 classWriter.write( 1050 classField.fieldType + " get" + propertyName + "() {\n"); 1051 classWriter.write( 1052 tab + "\t\treturn " + classField.fieldName + ";\n"); 1053 classWriter.write(tab + "\t}\n"); 1054 classWriter.write("\n" + tab + "\tpublic "); 1055 classWriter.write( 1056 " void set" + propertyName + "(" + classField.fieldType); 1057 if (classField.isArray) 1058 classWriter.write("[]"); 1059 classWriter.write(" " + classField.fieldName + ") {\n"); 1060 classWriter.write( 1061 tab 1062 + "\t\tthis." 1063 + classField.fieldName 1064 + "\t= " 1065 + classField.fieldName 1066 + ";\n"); 1067 classWriter.write(tab + "\t}\n"); 1068 } 1069 1071 classWriter.write("\n\tpublic String toString() {\n"); 1072 classWriter.write("\t\treturn "); 1073 1074 for (int fieldIndex = 0; fieldIndex < fieldLength; fieldIndex++) { 1075 ClassField classField = classFile.getField(fieldIndex); 1076 1077 if (fieldIndex > 0) { 1078 classWriter.write(" + \"\\n\" + \n\t\t\t"); 1079 } 1080 1081 classWriter.write( 1082 "\"" + classField.fieldName + "=\" + " + classField.fieldName); 1083 } 1084 1085 classWriter.write(";\n\t}\n"); 1086 1087 int innerClassCount = classFile.getInnerClassCount(); 1088 if (innerClassCount > 0) { 1089 classWriter.write("\n" + tab + "\t//Inner classes\n"); 1090 for (int innerClassIndex = 0; 1091 innerClassIndex < innerClassCount; 1092 innerClassIndex++) { 1093 ClassFile innerClassFile = 1094 classFile.getInnerClass(innerClassIndex); 1095 printJavaFile(classWriter, innerClassFile, tab + "\t"); 1096 } 1097 } 1098 classWriter.write("\n" + tab + "}"); 1099 1100 } 1101 1102 1108 public void setNamespaceURIMapping(String namespaceURI, String javaPath) { 1109 1110 if (namespaceURI == null || javaPath == null) 1111 throw new IllegalArgumentException ( 1112 "Argument to " 1113 + "'setNamespaceURIMapping' " 1114 + "cannot be null."); 1115 1116 namespaceRegistry.put(namespaceURI, javaPath); 1117 } 1118 1119 1124 public void setWorkingDirectory(String dir) 1125 throws IllegalArgumentException { 1126 1127 if (dir == null) 1128 throw new IllegalArgumentException ( 1129 "Argument to " + "'setWorkingDirectory' " + "cannot be null."); 1130 1131 workingDirectory = dir; 1132 } 1133 1134 public void setVerbose(boolean verbose) { 1135 this.verbose = verbose; 1136 } 1137 1138 public boolean getVerbose() { 1139 return verbose; 1140 } 1141 1142 public void setOverwrite(boolean overwrite) { 1143 this.overwrite = overwrite; 1144 } 1145 1146 public boolean getOverwrite() { 1147 return overwrite; 1148 } 1149 1150 public void setJavac(boolean javac) { 1151 this.javac = javac; 1152 } 1153 1154 1161 private ClassFile simpleType2Field( 1162 String name, 1163 boolean isArray, 1164 SchemaSimpleType simp, 1165 ClassFile file) 1166 throws SchemaException { 1167 1168 if (name == null || simp == null || file == null) 1169 throw new IllegalArgumentException ( 1170 "Argument to 'simpleType2Field' " + "cannot be null."); 1171 1172 QName base = simp.getBase(); 1173 String type = getJavaType(base, simp.getTargetURI(), "simpleType"); 1174 1175 if (simp.isList()) 1176 type = type + "[]"; 1177 file.addField(new ClassField(type, name, isArray)); 1178 1179 return file; 1180 } 1181 1182 1191 private void updateRegistry(Vector elements) throws SchemaException { 1192 1193 if (elements == null) 1194 throw new IllegalArgumentException ( 1195 "Argument to 'updateRegistry' " + "cannot be null."); 1196 1197 HashMap simpleTypeTable = new HashMap (); 1198 1199 for (int i = 0; i < elements.size(); i++) { 1200 SchemaType element = (SchemaType) elements.elementAt(i); 1201 String name = element.getName(); 1202 if (name == null) 1203 name = ""; 1204 QName qName = new QName (element.getTargetURI(), name); 1205 1206 if (element.getElementType() == SchemaType.SIMPLETYPE) { 1207 simpleTypeTable.put(qName, element); 1208 } 1209 } 1210 1211 boolean changed = true; 1212 1213 while (changed) { 1214 changed = false; 1215 for (Iterator it = simpleTypeTable.keySet().iterator(); 1216 it.hasNext(); 1217 ) { 1218 QName qName = (QName ) it.next(); 1219 1220 SchemaSimpleType simpleType = 1221 (SchemaSimpleType) simpleTypeTable.get(qName); 1222 QName base = simpleType.getBase(); 1223 boolean isList = simpleType.isList(); 1224 1225 if (base != null) { 1226 String javaType = (String ) registry.get(base); 1227 if (javaType == null) 1228 if (simpleTypeTable.containsKey(base)) 1229 continue; 1230 else 1231 javaType = getGenericJavaName(base, "simpleType"); 1232 1233 if (isList) 1234 javaType = javaType + "[]"; 1235 registry.put(qName, javaType); 1236 changed = true; 1237 simpleTypeTable.remove(qName); 1238 } else { 1239 String javaType = getGenericJavaName(qName, "simpleType"); 1240 registry.put(qName, javaType); 1241 } 1244 } 1245 } 1246 1247 for (Iterator it2 = simpleTypeTable.keySet().iterator(); 1248 it2.hasNext(); 1249 ) { 1250 if (!registry.containsKey((QName ) it2.next())) 1251 throw new SchemaException( 1252 "'simpleType' definitions have cyclic " + "dependency."); 1253 } 1254 1255 for (int i = 0; i < elements.size(); i++) { 1256 SchemaType element = (SchemaType) elements.elementAt(i); 1257 String targetURI = element.getTargetURI(); 1258 String targetNSPrefix = getNamespaceURIMapping(targetURI); 1259 if (targetNSPrefix.compareTo("") != 0) 1260 targetNSPrefix = targetNSPrefix + "."; 1261 String name = element.getName(); 1262 if (name == null) 1263 name = ""; 1264 QName qName = new QName (targetURI, name); 1265 switch (element.getElementType()) { 1266 case SchemaType.COMPLEXTYPE : 1267 registry.put( 1268 qName, 1269 targetNSPrefix 1270 + Conventions.schema2JavaName( 1271 "complexType", 1272 name, 1273 true)); 1274 break; 1275 case SchemaType.GROUP : 1276 registry.put( 1277 qName, 1278 targetNSPrefix 1279 + Conventions.schema2JavaName("group", name, true)); 1280 break; 1281 case SchemaType.ATTRIBUTEGROUP : 1282 registry.put( 1283 qName, 1284 targetNSPrefix 1285 + Conventions.schema2JavaName( 1286 "attributeGroup", 1287 name, 1288 true)); 1289 break; 1290 } 1291 } 1292 1293 for (int i = 0; i < elements.size(); i++) { 1294 SchemaType element = (SchemaType) elements.elementAt(i); 1295 String name = element.getName(); 1296 String targetURI = element.getTargetURI(); 1297 String targetNSPrefix = getNamespaceURIMapping(targetURI); 1298 if (targetNSPrefix.compareTo("") != 0) 1299 targetNSPrefix = targetNSPrefix + "."; 1300 if (name == null) 1301 name = ""; 1302 QName qName = new QName (targetURI, name); 1303 switch (element.getElementType()) { 1304 case SchemaType.ELEMENT : 1305 SchemaElement schemaElement = (SchemaElement) element; 1306 QName type = schemaElement.getType(); 1307 SchemaType child = schemaElement.getChild(); 1308 if (type != null) { 1309 String javaType = (String ) registry.get(type); 1310 if (javaType != null) 1311 registry.put(qName, javaType); 1312 } else if (child != null) { 1313 if (child.getElementType() == SchemaType.SIMPLETYPE) { 1314 QName base = 1315 (QName ) ((SchemaSimpleType) child).getBase(); 1316 String javaType = (String ) registry.get(base); 1317 if (javaType != null) { 1318 if (((SchemaSimpleType) child).isList()) 1319 javaType = javaType + "[]"; 1320 registry.put(qName, javaType); 1321 } 1322 } else if ( 1323 child.getElementType() == SchemaType.COMPLEXTYPE) { 1324 registry.put( 1325 qName, 1326 targetNSPrefix 1327 + Conventions.schema2JavaName( 1328 "complexType", 1329 name, 1330 true)); 1331 } 1332 } 1333 break; 1334 case SchemaType.ATTRIBUTE : 1335 SchemaAttribute schemaAttribute = (SchemaAttribute) element; 1336 type = schemaAttribute.getType(); 1337 SchemaSimpleType simpleType = schemaAttribute.getChild(); 1338 if (type != null) { 1339 String javaType = (String ) registry.get(type); 1340 if (javaType != null) 1341 registry.put(qName, javaType); 1342 } else if (simpleType != null) { 1343 QName base = simpleType.getBase(); 1344 String javaType = (String ) registry.get(base); 1345 if (javaType != null) { 1346 if (simpleType.isList()) 1347 javaType = javaType + "[]"; 1348 registry.put(qName, javaType); 1349 } 1350 } 1351 break; 1352 } 1353 } 1354 } 1355} | Popular Tags |