1 19 20 package org.netbeans.modules.schema2beansdev; 21 22 import org.netbeans.modules.schema2beansdev.gen.JavaUtil; 23 import org.netbeans.modules.schema2beansdev.gen.XMLWriter; 24 25 import java.io.*; 26 import java.util.*; 27 28 import org.w3c.dom.*; 29 import org.xml.sax.*; 30 31 import org.netbeans.modules.schema2beans.*; 32 33 40 public class SchemaRep implements PrefixGuesser { 41 public static boolean debug = false; 42 43 public static final String XSD_NS = "http://www.w3.org/2001/XMLSchema"; 44 45 49 public abstract class ElementExpr { 51 public abstract String getName(); 52 public abstract void writeDTD(StringBuffer out); 53 54 public abstract boolean writeDTDName(StringBuffer out); 56 public abstract void writeXMLSchema(XMLWriter out) throws IOException; 57 58 public abstract void validate(); 60 61 public abstract ElementExpr optimize(); 63 public abstract void readSchema(org.w3c.dom.Element node); 64 65 public abstract String getContentName(); 67 protected ElementExpr parentExpr; 68 protected void setParentExpr(ElementExpr ee) { 69 parentExpr = ee; 70 fullContentName = null; 71 } 72 public ElementExpr getParentExpr() { 73 return parentExpr; 74 } 75 private String fullContentName = null; 76 public String getFullContentName() { 77 if (fullContentName == null) { 78 String contentName = getContentName(); 79 if (parentExpr == null) { 80 if (contentName == null) 81 fullContentName = "/"; 82 else 83 fullContentName = ("/" + contentName).intern(); 84 } else { 85 String parentFullContentName = parentExpr.getFullContentName(); 86 if (contentName == null) 87 fullContentName = parentFullContentName; 88 else if (parentFullContentName == "/") 89 fullContentName = (parentFullContentName + contentName).intern(); 90 else 91 fullContentName = (parentFullContentName + "/" + contentName).intern(); 92 } 93 } 94 return fullContentName; 95 } 96 protected String uniquifyFullContentName() { 97 fullContentName = (getFullContentName()+"/#").intern(); 98 return fullContentName; 99 } 100 } 101 102 public abstract class ContainsSubElements extends ElementExpr { 106 protected List subElements; 108 public ContainsSubElements() { 109 subElements = new LinkedList(); 110 } 111 112 public void addSubElement(ElementExpr subElement) { 113 subElement.setParentExpr(this); 115 if (subElement instanceof Element) { 116 String subElementFullContentName; 117 boolean checkAgain; 118 do { 119 checkAgain = false; 120 subElementFullContentName = subElement.getFullContentName(); 121 Iterator it = subElements.iterator(); 122 while (it.hasNext()) { 123 ElementExpr otherElement = (ElementExpr) it.next(); 124 String otherElementFullContentName = otherElement.getFullContentName(); 125 if (subElementFullContentName == otherElementFullContentName) { 126 if (debug) 127 System.out.println("Found duplicate fullContentName for "+otherElement.getName()+" : "+subElementFullContentName); 128 subElement.uniquifyFullContentName(); 129 checkAgain = true; 130 } 131 } 132 } while (checkAgain); 133 } 134 subElements.add(subElement); 135 } 136 137 public void addSubElement(List se) { 138 Iterator it = se.iterator(); 140 while (it.hasNext()) { 141 addSubElement((ElementExpr) it.next()); 142 } 143 } 144 145 public Iterator subElementsIterator() { 146 return subElements.iterator(); 147 } 148 149 public ElementExpr findSubElement(String nodeName) { 150 Iterator it = subElements.iterator(); 152 while (it.hasNext()) { 153 ElementExpr el = (ElementExpr) it.next(); 154 if (el.getName().equals(nodeName)) 155 return el; 156 } 157 return null; 159 } 160 161 public ElementExpr findSubElement(Class type) { 162 Iterator it = subElements.iterator(); 163 while (it.hasNext()) { 164 ElementExpr el = (ElementExpr) it.next(); 165 if (type.isAssignableFrom(el.getClass())) 166 return el; 167 } 168 return null; 169 } 170 171 174 public ElementExpr findSubElement(String [] nodeNames) { 175 ContainsSubElements current = this; 176 for (int i = 0; i < nodeNames.length; ++i) { 177 ElementExpr ee = current.findSubElement(nodeNames[i]); 178 if (ee == null) 179 return null; 180 if (ee instanceof ContainsSubElements) 181 current = (ContainsSubElements) ee; 182 else if (i+1 != nodeNames.length) 183 return null; 184 } 185 return current; 186 } 187 188 191 public void findAllSubElements(String name, List lst) { 192 Iterator it = subElements.iterator(); 193 while (it.hasNext()) { 194 ElementExpr el = (ElementExpr) it.next(); 195 if (el.getName().equals(name)) 196 lst.add(el); 197 if (el instanceof ContainsSubElements) 198 ((ContainsSubElements)el).findAllSubElements(name, lst); 199 } 200 } 201 202 205 public void findAllSubElements(Class type, List lst) { 206 Iterator it = subElements.iterator(); 207 while (it.hasNext()) { 208 ElementExpr el = (ElementExpr) it.next(); 209 if (type.isAssignableFrom(el.getClass())) 210 lst.add(el); 211 if (el instanceof ContainsSubElements) 212 ((ContainsSubElements)el).findAllSubElements(type, lst); 213 } 214 } 215 216 public void validate() { 217 Map possibleSubElements = validSubElementTypeMap(); 219 Iterator it = subElements.iterator(); 220 while (it.hasNext()) { 221 ElementExpr ee = (ElementExpr) it.next(); 222 if (!possibleSubElements.containsKey(ee.getClass())) { 223 throw new IllegalStateException (Common.getMessage("MSG_InvalidContents", toString(), ee.getName(), getFullContentName())); 224 } 225 ee.validate(); 226 } 227 } 228 229 public abstract Map validSubElementTypeMap(); 230 231 public void writeDTD(StringBuffer out) { 232 Iterator it = subElements.iterator(); 233 while (it.hasNext()) { 234 ElementExpr el = (ElementExpr) it.next(); 235 el.writeDTD(out); 236 } 237 } 238 239 243 public String getAttributeString() { 244 return null; 245 } 246 247 250 public boolean compressWhiteSpaceOuter() { 251 return false; 252 } 253 254 257 public boolean compressWhiteSpaceInner() { 258 return false; 259 } 260 261 public void writeXMLSchema(XMLWriter out) throws IOException { 262 writeXMLSchema(out, true); 263 } 264 265 268 public void writeXMLSchema(XMLWriter out, boolean printMyElement) throws IOException { 269 if (printMyElement) { 270 out.startTag(getXSDNamespace(), getName(), false); 273 String attrs = getAttributeString(); 274 if (attrs != null) 275 out.write(attrs); 276 out.finishStartTag(subElements.size() > 0, !compressWhiteSpaceInner()); 277 if (subElements.size() == 0) 278 return; 279 } 280 Iterator it = subElements.iterator(); 281 while (it.hasNext()) { 282 ElementExpr el = (ElementExpr) it.next(); 283 el.writeXMLSchema(out); 284 } 285 if (printMyElement) { 286 out.endTag(!compressWhiteSpaceOuter()); 289 } 290 } 291 292 protected boolean writeDTDSubElementNames(StringBuffer out) { 293 return writeDTDSubElementNames(out, true); 294 } 295 protected boolean writeDTDSubElementNames(StringBuffer out, boolean writeParens) { 296 if (subElements.size() == 0) 297 return false; 298 boolean first = true; 299 Iterator it = subElements.iterator(); 300 boolean hasNamedSubElements = false; 301 StringBuffer subOut = new StringBuffer (); 303 StringBuffer freshOut = new StringBuffer (); 306 while (it.hasNext()) { 307 ElementExpr el = (ElementExpr) it.next(); 308 boolean anyNamed = el.writeDTDName(freshOut); 309 if (anyNamed) { 310 hasNamedSubElements = true; 311 if (first) 312 first = false; 313 else 314 subOut.append(", "); subOut.append(freshOut.toString()); 316 freshOut = new StringBuffer (); 317 } 318 } 319 if (!hasNamedSubElements) 320 return false; 321 if (writeParens && subElements.size() >= 2) 322 out.append("("); out.append(subOut); 324 if (writeParens && subElements.size() >= 2) 325 out.append(")"); return hasNamedSubElements; 327 } 328 329 public void findSubElementNames(List list) { 331 Iterator it = subElements.iterator(); 332 StringBuffer freshOut = new StringBuffer (); 333 while (it.hasNext()) { 334 ElementExpr el = (ElementExpr) it.next(); 335 boolean anyNamed = el.writeDTDName(freshOut); 336 } 337 } 338 339 public String toString() { 340 String attributeString = getAttributeString(); 341 if (attributeString == null) 342 return getName(); 343 else 344 return getName()+attributeString; 345 } 346 347 public boolean equals(Object o) { 348 if (!(o instanceof ContainsSubElements)) 349 return false; 350 ContainsSubElements e = (ContainsSubElements) o; 351 if (subElements.size() != e.subElements.size()) 353 return false; 354 Iterator it = subElements.iterator(); 355 Iterator ite = e.subElements.iterator(); 356 while (it.hasNext()) { 357 ElementExpr el = (ElementExpr) it.next(); 358 ElementExpr ele = (ElementExpr) ite.next(); 359 if (!el.equals(ele)) 362 return false; 363 } 364 return true; 365 } 366 367 public int hashCode() { 368 int result = 17; 369 Iterator it = subElements.iterator(); 370 while (it.hasNext()) { 371 ElementExpr el = (ElementExpr) it.next(); 372 result = 37*result + el.hashCode(); 373 } 374 return result; 375 } 376 377 public ElementExpr optimize() { 378 ListIterator it = subElements.listIterator(); 379 while (it.hasNext()) { 380 ElementExpr el = (ElementExpr) it.next(); 381 ElementExpr result = el.optimize(); 382 if (result == null) { 384 el.setParentExpr(null); 385 it.remove(); 386 } else if (el != result) 387 it.set(result); 388 } 389 return this; 390 } 391 392 public String getJavaTypeName() { 393 Iterator it = subElements.iterator(); 394 while (it.hasNext()) { 395 Object o = it.next(); 396 if (o instanceof HasJavaTypeName) { 397 return ((HasJavaTypeName)o).getJavaTypeName(); 399 } 400 } 401 return null; 402 } 403 } 404 405 public interface HasJavaTypeName { 406 public String getJavaTypeName(); 407 } 408 409 public interface MinMaxOccurs { 410 public String getMinOccurs(); 411 public String getMaxOccurs(); 412 } 413 414 public interface CanRef { 415 public boolean hasRef(); 416 419 public String getRef(); 420 public ElementExpr getRefElementExpr(); 421 } 422 423 public abstract class HasTypeName extends ContainsSubElements { 425 private String typeNameLocalPart = null; 426 private String typeNameNamespace = null; 427 428 protected String setTypeName(String typeName) { 429 if (typeName == null) { 430 typeNameLocalPart = null; 431 typeNameNamespace = null; 432 return null; 433 } 434 typeName = normalizeTargetNamespace(typeName).intern(); 435 typeNameLocalPart = removePrefix(typeName).intern(); 436 String prefix = prefixOf(typeName); 437 typeNameNamespace = getNamespaceURI(prefix); 438 if (typeNameNamespace != null) 439 typeNameNamespace = typeNameNamespace.intern(); 440 return typeName; 441 } 442 443 public String getTypeNameLocalPart() { 444 return typeNameLocalPart; 445 } 446 447 public String getTypeNameNamespace() { 448 return typeNameNamespace; 449 } 450 451 454 public String getTypeName() { 455 if (typeNameLocalPart == null) 456 return null; 457 String result = normalizeNamespace(typeNameNamespace, typeNameLocalPart); 458 return result; 459 } 460 461 } 462 463 public class SchemaNode extends ContainsSubElements { 466 protected String targetNamespace; 467 private Boolean elementFormQualifiedDefault = null; 468 private Boolean attributeFormQualifiedDefault = null; 469 470 public SchemaNode() { 471 } 472 473 public String getName() { 474 return "schema"; 475 } 476 477 public String getContentName() { 478 return null; 479 } 480 481 public void setTargetNamespace(String tn) { 482 targetNamespace = tn; 483 } 484 485 public String getTargetNamespace() { 486 return targetNamespace; 487 } 488 489 public boolean isElementFormQualified() { 490 if (elementFormQualifiedDefault == null) 491 return false; 492 return elementFormQualifiedDefault.booleanValue(); 493 } 494 495 public boolean isAttributeFormQualified() { 496 if (attributeFormQualifiedDefault == null) 497 return false; 498 return attributeFormQualifiedDefault.booleanValue(); 499 } 500 501 public void readSchema(org.w3c.dom.Element node) { 502 String tns = node.getAttribute("targetNamespace"); String efd = node.getAttribute("elementFormDefault"); String afd = node.getAttribute("attributeFormDefault"); if (tns != null && !"".equals(tns)) 506 targetNamespace = tns; 507 if (efd != null) 508 elementFormQualifiedDefault = Boolean.valueOf("qualified".equals(efd)); 509 SchemaRep.this.elementFormQualifiedDefault = isElementFormQualified(); 510 if (afd != null) 511 attributeFormQualifiedDefault = Boolean.valueOf("qualified".equals(afd)); 512 SchemaRep.this.attributeFormQualifiedDefault = isAttributeFormQualified(); 513 } 514 515 void merge(org.w3c.dom.Element node) { 516 String tns = node.getAttribute("targetNamespace"); String efd = node.getAttribute("elementFormDefault"); String afd = node.getAttribute("attributeFormDefault"); if (targetNamespace == null) 521 targetNamespace = tns; 522 523 boolean value = "qualified".equals(efd); 524 if (efd != null) { 525 if (elementFormQualifiedDefault == null || elementFormQualifiedDefault.booleanValue()) 526 elementFormQualifiedDefault = Boolean.valueOf(value); 527 } 528 SchemaRep.this.elementFormQualifiedDefault = value; 529 530 value = "qualified".equals(afd); 531 if (afd != null) { 532 if (attributeFormQualifiedDefault == null || attributeFormQualifiedDefault.booleanValue()) 533 attributeFormQualifiedDefault = Boolean.valueOf(value); 534 } 535 SchemaRep.this.attributeFormQualifiedDefault = value; 536 } 537 538 public void addSubElement(ElementExpr subElement) { 539 if (subElement instanceof Element) { 540 Element el = (Element) subElement; 541 if (el.isDefiningNewType()) { 543 if (debug) 544 System.out.println("SchemaNode new element type: name="+el.getElementName()); 545 definedTypes.put(el.getElementName(), el); 546 definedTypesFull.put(canonicalQName(el.getElementNamespace(), el.getElementName()), el); 547 } 548 } 549 if (subElement instanceof Attribute) { 550 Attribute attr = (Attribute) subElement; 551 if (attr.isDefiningNewType()) { 553 if (debug) 554 System.out.println("SchemaNode new element type: attr="+attr); 555 definedAttributes.put(attr.getAttributeName(), attr); 556 } 557 } 558 super.addSubElement(subElement); 559 } 560 561 public String getAttributeString() { 562 StringBuffer sb = new StringBuffer (" xmlns:"+getXSDNamespace()+"='"+getNamespaceURI(getXSDNamespace())+"'"); if (targetNamespace != null) 564 sb.append(" targetNamespace='"+getTargetNamespace()+"'"); if (documentNamespace != null) { 566 sb.append(" xmlns='"+documentNamespace+"'"); } 568 if (isElementFormQualified()) 569 sb.append(" elementFormDefault='qualified'"); 570 if (isAttributeFormQualified()) 571 sb.append(" attributeFormDefault='qualified'"); 572 return sb.toString(); 573 } 574 575 public void writeXMLSchema(XMLWriter out) throws IOException { 576 out.startTag(getXSDNamespace(), getName(), getAttributeString()); out.cr(); 578 Iterator it = requiredPredefinedTypes.keySet().iterator(); 579 while (it.hasNext()) { 580 String type = (String ) it.next(); 581 ElementExpr el = (ElementExpr) optionallyDefinedTypes.get(type); 583 el.writeXMLSchema(out); 584 } 585 586 super.writeXMLSchema(out, false); 587 out.endTag(); } 589 590 public boolean writeDTDName(StringBuffer out) { 591 return writeDTDSubElementNames(out, false); 592 } 593 594 public Map validSubElementTypeMap() { 595 return schemaValidSubElementTypeMap; 596 } 597 598 public boolean equals(Object o) { 599 if (!(o instanceof SchemaNode)) 600 return false; 601 SchemaNode el = (SchemaNode) o; 602 if (targetNamespace == null) { 603 if (el.targetNamespace != null) 604 return false; 605 } else { 606 if (!targetNamespace.equals(el.targetNamespace)) 607 return false; 608 } 609 if (elementFormQualifiedDefault != el.elementFormQualifiedDefault) 610 return false; 611 if (attributeFormQualifiedDefault != el.attributeFormQualifiedDefault) 612 return false; 613 614 return super.equals(el); 615 } 616 617 public int hashCode() { 618 int result = 17; 619 result = 37*result + ((targetNamespace == null) ? 0 : targetNamespace.hashCode()); 620 result = 37*result + (elementFormQualifiedDefault.booleanValue() ? 1 : 0); 621 result = 37*result + (attributeFormQualifiedDefault.booleanValue() ? 1 : 0); 622 result = 37*result + super.hashCode(); 623 return result; 624 } 625 } 626 static private Map schemaValidSubElementTypeMap = null; 627 static { 628 schemaValidSubElementTypeMap = new HashMap(); 629 schemaValidSubElementTypeMap.put(Annotation.class, null); 630 schemaValidSubElementTypeMap.put(SimpleType.class, null); 631 schemaValidSubElementTypeMap.put(ComplexType.class, null); 632 schemaValidSubElementTypeMap.put(Element.class, null); 633 schemaValidSubElementTypeMap.put(Attribute.class, null); 634 schemaValidSubElementTypeMap.put(AttributeGroup.class, null); 635 schemaValidSubElementTypeMap.put(Include.class, null); 636 schemaValidSubElementTypeMap.put(Import.class, null); 637 schemaValidSubElementTypeMap.put(Group.class, null); 638 } 639 640 public class ComplexType extends HasTypeName implements HasJavaTypeName { 643 private boolean mixed = false; 645 private boolean abstractType = false; 646 647 public ComplexType() { 648 } 649 650 public ComplexType(String typeName) { 651 if (typeName != null && !typeName.equals("")) { 652 typeName = setTypeName(typeName); 653 putSchemaTypeDef(typeName, this); 654 } 655 } 656 657 public String getName() { 658 return "complexType"; 659 } 660 661 public String getContentName() { 662 return getTypeName(); 663 } 664 665 public boolean isMixed() { 666 return mixed; 667 } 668 669 public boolean isAbstract() { 670 return abstractType; 671 } 672 673 public boolean writeDTDName(StringBuffer out) { 674 return writeDTDSubElementNames(out, false); 675 } 676 677 public String getAttributeString() { 678 String result = ""; 679 if (getTypeName() != null) 680 result += " name='"+getTypeName()+"'"; 681 if (mixed) 682 result += " mixed='true'"; 683 if (abstractType) 684 result += " abstract='true'"; 685 return result; 686 } 687 688 public Map validSubElementTypeMap() { 689 return complexTypeValidSubElementTypeMap; 690 } 691 692 public void readSchema(org.w3c.dom.Element node) { 693 String elementType = node.getAttribute("name"); String myMixed = node.getAttribute("mixed"); String myAbstract = node.getAttribute("abstract"); ComplexType el = new ComplexType(elementType); 697 if (myMixed != null && (myMixed.equals("true") || myMixed.equals("yes") || myMixed.equals("on"))) 698 el.mixed = true; 699 if (myAbstract != null && (myAbstract.equals("true") || myAbstract.equals("yes") || myAbstract.equals("on"))) 700 el.abstractType = true; 701 if (debug) 702 System.out.println("Created complexType "+elementType); 703 pushCurrent(el); 704 read(node); 705 popCurrent(); 706 } 707 708 public boolean equals(Object o) { 709 if (!(o instanceof ComplexType)) 710 return false; 711 ComplexType el = (ComplexType) o; 712 if ((getTypeName() == null) ? (el.getTypeName() == null) : getTypeName().equals(el.getTypeName())) 713 return false; 714 if (mixed != el.mixed) 715 return false; 716 if (abstractType != el.abstractType) 717 return false; 718 719 return super.equals(el); 720 } 721 722 public int hashCode() { 723 int result = 17; 724 result = 37*result + ((getTypeName() == null) ? 0 : getTypeName().hashCode()); 725 result = 37*result + (mixed ? 1 : 0); 726 result = 37*result + (abstractType ? 1 : 0); 727 result = 37*result + super.hashCode(); 728 return result; 729 } 730 } 731 static private Map complexTypeValidSubElementTypeMap = null; 732 static { 733 complexTypeValidSubElementTypeMap = new HashMap(); 734 complexTypeValidSubElementTypeMap.put(Annotation.class, null); 735 complexTypeValidSubElementTypeMap.put(Choice.class, null); 736 complexTypeValidSubElementTypeMap.put(Group.class, null); 737 complexTypeValidSubElementTypeMap.put(Sequence.class, null); 738 complexTypeValidSubElementTypeMap.put(All.class, null); 739 complexTypeValidSubElementTypeMap.put(Attribute.class, null); 740 complexTypeValidSubElementTypeMap.put(AttributeGroup.class, null); 741 complexTypeValidSubElementTypeMap.put(AnyAttribute.class, null); 742 complexTypeValidSubElementTypeMap.put(SimpleContent.class, null); 743 complexTypeValidSubElementTypeMap.put(ComplexContent.class, null); 744 } 745 746 public class SimpleContent extends ContainsSubElements implements HasJavaTypeName { 748 public SimpleContent() { 749 } 750 751 public String getName() { 752 return "simpleContent"; 753 } 754 755 public String getContentName() { 756 return null; 757 } 758 759 public void validate() { 760 } 761 762 public ElementExpr optimize() { 763 return this; 764 } 765 766 public void readSchema(org.w3c.dom.Element node) { 767 SimpleContent el = new SimpleContent(); 768 pushCurrent(el); 769 read(node); 770 popCurrent(); 771 } 772 773 public boolean writeDTDName(StringBuffer out) { 774 return writeDTDSubElementNames(out, false); 775 } 776 777 public Map validSubElementTypeMap() { 778 return simpleContentValidSubElementTypeMap; 779 } 780 } 781 static private Map simpleContentValidSubElementTypeMap = null; 782 static { 783 simpleContentValidSubElementTypeMap = new HashMap(); 784 simpleContentValidSubElementTypeMap.put(Annotation.class, null); 785 simpleContentValidSubElementTypeMap.put(Restriction.class, null); 786 simpleContentValidSubElementTypeMap.put(Extension.class, null); 787 } 788 789 790 public class ComplexContent extends ContainsSubElements implements HasJavaTypeName { 792 private boolean mixed; 793 794 public ComplexContent() { 795 } 796 797 public String getName() { 798 return "complexContent"; 799 } 800 801 public String getContentName() { 802 return null; 803 } 804 805 public void validate() { 806 } 807 808 public ElementExpr optimize() { 809 return this; 810 } 811 812 public boolean isMixed() { 813 return mixed; 814 } 815 816 public void readSchema(org.w3c.dom.Element node) { 817 ComplexContent el = new ComplexContent(); 818 String myMixed = node.getAttribute("mixed"); if (myMixed != null && (myMixed.equals("true") || myMixed.equals("yes") || myMixed.equals("on"))) 820 el.mixed = true; 821 pushCurrent(el); 822 read(node); 823 popCurrent(); 824 } 825 826 public boolean writeDTDName(StringBuffer out) { 827 return writeDTDSubElementNames(out, false); 828 } 829 830 public Map validSubElementTypeMap() { 831 return complexContentValidSubElementTypeMap; 832 } 833 834 public boolean equals(Object o) { 835 if (!(o instanceof ComplexContent)) 836 return false; 837 ComplexContent el = (ComplexContent) o; 838 if (mixed != el.mixed) 839 return false; 840 return super.equals(el); 841 } 842 843 public int hashCode() { 844 int result = 17; 845 result = 37*result + (mixed ? 0 : 1); 846 result = 37*result + super.hashCode(); 847 return result; 848 } 849 } 850 static private Map complexContentValidSubElementTypeMap = null; 851 static { 852 complexContentValidSubElementTypeMap = new HashMap(); 853 complexContentValidSubElementTypeMap.put(Annotation.class, null); 854 complexContentValidSubElementTypeMap.put(Restriction.class, null); 855 complexContentValidSubElementTypeMap.put(Extension.class, null); 856 } 857 858 public class SimpleType extends HasTypeName implements HasJavaTypeName { 861 private String javaTypeName = null; 863 864 public SimpleType(String tn) { 865 if (tn != null && !tn.equals("")) { 867 String typeName = normalizeTargetNamespace(tn).intern(); 868 ElementExpr previousDef = getSchemaTypeDef(typeName); 869 if (previousDef instanceof SimpleType && (((SimpleType)previousDef).javaTypeName != null || ((SimpleType)previousDef).subElements.size() > 0)) { 870 System.out.println("Keeping schemaTypeDefs1 for "+previousDef); 872 } else { 873 if (previousDef != null) 874 System.out.println("!!! Overwriting schemaTypeDefs1 ("+previousDef+") for "+this); 875 typeName = setTypeName(typeName); 876 putSchemaTypeDef(typeName, this); 877 } 878 } 879 } 880 881 886 public SimpleType(String tn, String javaTypeName) { 887 super(); 888 String typeName = setTypeName(tn); 890 this.javaTypeName = (javaTypeName == null) ? null : javaTypeName.intern(); 891 895 putSchemaTypeDef(typeName, this); 896 } 897 898 public SimpleType(String tn, Restriction restrict) { 899 super(); 900 String typeName = setTypeName(tn); 902 this.addSubElement(restrict); 903 907 putSchemaTypeDef(typeName, this); 908 } 909 910 public String getName() { 911 return "simpleType"; 912 } 913 914 public String getContentName() { 915 return getTypeName(); 916 } 917 918 public String getJavaTypeName() { 920 if (javaTypeName != null) 921 return javaTypeName; 922 return super.getJavaTypeName(); 923 } 924 925 public String getAttributeString() { 926 if (getTypeName() == null) 927 return null; 928 return (" name='"+getTypeName()+"'"); 929 } 930 931 public boolean compressWhiteSpaceInner() { 932 if (subElements.size() == 0) 933 return true; 934 ElementExpr subElement = (ElementExpr) subElementsIterator().next(); 935 if (subElement instanceof ContainsSubElements) 936 return ((ContainsSubElements)subElement).compressWhiteSpaceOuter(); 937 return true; 938 } 939 940 public boolean writeDTDName(StringBuffer out) { 941 return writeDTDSubElementNames(out, false); 942 } 943 944 public void readSchema(org.w3c.dom.Element node) { 945 String elementType = node.getAttribute("name"); SimpleType el = new SimpleType(elementType); 947 pushCurrent(el); 948 read(node); 949 popCurrent(); 950 } 951 952 public Map validSubElementTypeMap() { 953 return simpleTypeValidSubElementTypeMap; 954 } 955 956 public String toString() { 957 if (javaTypeName == null) { 958 if (getTypeName() == null) 959 return "simpleType (not named)"; else 961 return "simpleType "+getTypeName(); } else 963 return "simpleType "+getTypeName()+" "+javaTypeName; } 965 966 public boolean equals(Object o) { 967 if (!(o instanceof SimpleType)) 968 return false; 969 SimpleType el = (SimpleType) o; 970 if ((getTypeName() == null) ? (el.getTypeName() == null) : getTypeName().equals(el.getTypeName())) 971 return false; 972 return super.equals(el); 974 } 975 976 public int hashCode() { 977 int result = 17; 978 if (getTypeName() != null) 979 result = 37*result + getTypeName().hashCode(); 980 result = 37*result + super.hashCode(); 981 return result; 982 } 983 } 984 static private Map simpleTypeValidSubElementTypeMap = null; 985 static { 986 simpleTypeValidSubElementTypeMap = new HashMap(); 987 simpleTypeValidSubElementTypeMap.put(Annotation.class, null); 988 simpleTypeValidSubElementTypeMap.put(Restriction.class, null); 989 simpleTypeValidSubElementTypeMap.put(ListElement.class, null); 990 simpleTypeValidSubElementTypeMap.put(UnionType.class, null); 991 } 992 993 public class UnionType extends ContainsSubElements { 996 private String typeName = null; 997 private String memberTypes = null; 998 999 public UnionType(String tn, String memberTypes) { 1000 if (tn != null && !tn.equals("")) { 1002 typeName = normalizeTargetNamespace(tn).intern(); 1003 } 1004 this.memberTypes = memberTypes; 1005 } 1006 1007 public String getMemberTypes() { 1008 return memberTypes; 1009 } 1010 1011 public ElementExpr[] getMemberTypeElements() { 1012 if (memberTypes == null || memberTypes.trim().length() == 0) 1013 return null; 1014 ArrayList mlist = new ArrayList(); 1015 String [] members = memberTypes.trim().split(" "); 1016 for (int i=0; i < members.length; i++) { 1017 if (members[i].length() == 0) 1018 continue; 1019 SchemaRep.ElementExpr ee = getSchemaTypeDef(members[i]); 1020 if (ee != null) 1021 mlist.add(ee); 1022 } 1023 1024 ElementExpr[] memberList = new ElementExpr[mlist.size()]; 1025 memberList = (ElementExpr[]) mlist.toArray(memberList); 1026 return memberList; 1027 } 1028 1029 public String getName() { 1030 return "union"; 1031 } 1032 1033 public String getContentName() { 1034 return typeName; 1035 } 1036 1037 public String getTypeName() { 1038 return typeName; 1039 } 1040 1041 public String getAttributeString() { 1042 if (memberTypes == null) 1043 return null; 1044 return (" memberTypes='"+memberTypes+"'"); 1045 } 1046 1047 public boolean compressWhiteSpaceInner() { 1048 if (subElements.size() == 0) 1049 return true; 1050 ElementExpr subElement = (ElementExpr) subElementsIterator().next(); 1051 if (subElement instanceof ContainsSubElements) 1052 return ((ContainsSubElements)subElement).compressWhiteSpaceOuter(); 1053 return true; 1054 } 1055 1056 public boolean writeDTDName(StringBuffer out) { 1057 return writeDTDSubElementNames(out, false); 1058 } 1059 1060 public void readSchema(org.w3c.dom.Element node) { 1061 String memberTypes = node.getAttribute("memberTypes"); String typeName = "_union"; 1063 org.w3c.dom.Node parent = node.getParentNode(); 1064 if (parent instanceof org.w3c.dom.Element ) { 1065 do { 1066 org.w3c.dom.Element parentEl = (org.w3c.dom.Element ) parent; 1067 if (parent != null) { 1068 String name = parentEl.getAttribute("name"); 1069 if (name != null) { 1070 typeName = name + typeName; 1071 break; 1072 } else { 1073 typeName = "_simpleType" + typeName; 1075 } 1076 parent = parent.getParentNode(); 1077 } else 1078 break; 1079 } while (parent != null); 1080 } 1081 UnionType el = new UnionType(typeName, memberTypes); 1082 pushCurrent(el); 1083 read(node); 1084 popCurrent(); 1085 } 1086 1087 public Map validSubElementTypeMap() { 1088 return unionTypeValidSubElementTypeMap; 1089 } 1090 1091 public String toString() { 1092 if (typeName == null) 1093 return "unionType (not named)"; else 1095 return "unionType "+typeName; } 1097 1098 public boolean equals(Object o) { 1099 if (!(o instanceof UnionType)) 1100 return false; 1101 UnionType el = (UnionType) o; 1102 if (typeName != el.typeName) 1103 return false; 1104 if (memberTypes != el.memberTypes) 1105 return false; 1106 return super.equals(el); 1108 } 1109 1110 public int hashCode() { 1111 int result = 17; 1112 result = 37*result + memberTypes.hashCode(); 1113 result = 37*result + typeName.hashCode(); 1114 result = 37*result + super.hashCode(); 1115 return result; 1116 } 1117 } 1118 static private Map unionTypeValidSubElementTypeMap = null; 1119 static { 1120 unionTypeValidSubElementTypeMap = new HashMap(); 1121 unionTypeValidSubElementTypeMap.put(Annotation.class, null); 1122 unionTypeValidSubElementTypeMap.put(SimpleType.class, null); 1123 } 1124 1125 public interface EncodingStyle { 1126 } 1127 1128 public class HexBinary extends SimpleType implements EncodingStyle { 1129 public HexBinary() { 1130 super(getXSDNamespace()+":hexBinary", "byte[]"); 1131 } 1132 1133 public String toString() { 1134 return "hexBinary"; 1135 } 1136 } 1137 1138 public class Base64Binary extends SimpleType implements EncodingStyle { 1139 public Base64Binary() { 1140 super(getXSDNamespace()+":base64Binary", "byte[]"); 1141 } 1142 1143 public String toString() { 1144 return "base64Binary"; 1145 } 1146 } 1147 1148 public class Restriction extends ContainsSubElements implements HasJavaTypeName { 1151 protected String base; 1152 public Restriction() { 1153 } 1154 1155 public Restriction(String base) { 1156 setBase(base); 1157 } 1158 1159 public String getName() { 1160 return "restriction"; 1161 } 1162 1163 public String getContentName() { 1164 return null; 1165 } 1166 1167 public void setBase(String b) { 1168 if (b == null) { 1169 base = null; 1170 return; 1171 } 1172 base = normalizeDocumentNamespace(b).intern(); 1173 } 1174 1175 public String getBase() { 1176 return base; 1177 } 1178 1179 public String getJavaTypeName() { 1180 return schemaTypeToJavaType(base); 1181 } 1182 1183 public boolean compressWhiteSpaceOuter() { 1184 return subElements.size() == 0; 1185 } 1186 1187 public boolean compressWhiteSpaceInner() { 1188 return subElements.size() == 0; 1189 } 1190 1191 public boolean writeDTDName(StringBuffer out) { 1192 return writeDTDSubElementNames(out, false); 1193 } 1194 1195 public String getAttributeString() { 1196 if (base == null) 1197 return null; 1198 return (" base='"+getBase()+"'"); 1199 } 1200 1201 public void readSchema(org.w3c.dom.Element node) { 1202 String base = node.getAttribute("base"); Restriction el = new Restriction(base); 1204 pushCurrent(el); 1205 read(node); 1206 popCurrent(); 1207 } 1208 1209 public Map validSubElementTypeMap() { 1210 return restrictionValidSubElementTypeMap; 1211 } 1212 1213 public boolean equals(Object o) { 1214 if (!(o instanceof Restriction)) 1215 return false; 1216 Restriction el = (Restriction) o; 1217 if (base == null) { 1218 if (el.base != null) 1219 return false; 1220 } else { 1221 if (!base.equals(el.base)) 1222 return false; 1223 } 1224 1225 return super.equals(el); 1226 } 1227 1228 public String toString() { 1229 Iterator subElements = subElementsIterator(); 1230 StringBuffer sb = null; 1231 while (subElements.hasNext()) { 1232 ElementExpr ee = (ElementExpr) subElements.next(); 1233 if (ee instanceof RestrictionType) { 1234 if (sb == null) 1235 sb = new StringBuffer (); 1236 else 1237 sb.append(", "); 1238 RestrictionType restrictionType = (RestrictionType) ee; 1239 sb.append(restrictionType.toString()); 1240 } 1241 } 1242 1247 if (sb != null) 1248 return sb.toString(); 1249 else 1250 return ""; 1251 } 1252 1253 public int hashCode() { 1254 int result = 17; 1255 result = 37*result + ((base == null) ? 0: base.hashCode()); 1256 result = 37*result + super.hashCode(); 1257 return result; 1258 } 1259 } 1260 static private Map restrictionValidSubElementTypeMap = null; 1261 static { 1262 restrictionValidSubElementTypeMap = new HashMap(); 1263 restrictionValidSubElementTypeMap.put(Annotation.class, null); 1264 restrictionValidSubElementTypeMap.put(SimpleType.class, null); 1265 restrictionValidSubElementTypeMap.put(All.class, null); 1266 restrictionValidSubElementTypeMap.put(Choice.class, null); 1267 restrictionValidSubElementTypeMap.put(Sequence.class, null); 1268 restrictionValidSubElementTypeMap.put(Group.class, null); 1269 restrictionValidSubElementTypeMap.put(Attribute.class, null); 1270 restrictionValidSubElementTypeMap.put(AttributeGroup.class, null); 1271 restrictionValidSubElementTypeMap.put(AnyAttribute.class, null); 1272 restrictionValidSubElementTypeMap.put(MinExclusive.class, null); 1273 restrictionValidSubElementTypeMap.put(MaxExclusive.class, null); 1274 restrictionValidSubElementTypeMap.put(Enumeration.class, null); 1275 restrictionValidSubElementTypeMap.put(Pattern.class, null); 1276 restrictionValidSubElementTypeMap.put(MinLength.class, null); 1277 restrictionValidSubElementTypeMap.put(MaxLength.class, null); 1278 restrictionValidSubElementTypeMap.put(TotalDigits.class, null); 1279 restrictionValidSubElementTypeMap.put(MinInclusive.class, null); 1280 restrictionValidSubElementTypeMap.put(MaxInclusive.class, null); 1281 restrictionValidSubElementTypeMap.put(FractionDigits.class, null); 1282 restrictionValidSubElementTypeMap.put(Length.class, null); 1283 restrictionValidSubElementTypeMap.put(WhiteSpace.class, null); 1284 } 1285 1286 public class Extension extends ContainsSubElements implements HasJavaTypeName { 1288 protected String base; 1289 public Extension() { 1290 } 1291 1292 public Extension(String base) { 1293 setBase(base); 1294 } 1295 1296 public String getContentName() { 1297 return null; 1298 } 1299 1300 public String getName() { 1301 return "extension"; 1302 } 1303 1304 public void setBase(String b) { 1305 base = normalizeDocumentNamespace(b); 1306 } 1307 1308 public String getBase() { 1309 return base; 1310 } 1311 1312 public String getJavaTypeName() { 1313 return schemaTypeToJavaType(base); 1314 } 1315 1316 public boolean writeDTDName(StringBuffer out) { 1317 return writeDTDSubElementNames(out, false); 1318 } 1319 1320 public String getAttributeString() { 1321 if (base == null) 1322 return null; 1323 return (" base='"+getBase()+"'"); 1324 } 1325 1326 public void readSchema(org.w3c.dom.Element node) { 1327 String base = node.getAttribute("base"); Extension el = new Extension(base); 1329 pushCurrent(el); 1330 read(node); 1331 popCurrent(); 1332 } 1333 1334 public Map validSubElementTypeMap() { 1335 return extensionValidSubElementTypeMap; 1336 } 1337 1338 public boolean equals(Object o) { 1339 if (!(o instanceof Extension)) 1340 return false; 1341 Extension el = (Extension) o; 1342 if (base == null) { 1343 if (el.base != null) 1344 return false; 1345 } else { 1346 if (!base.equals(el.base)) 1347 return false; 1348 } 1349 1350 return super.equals(el); 1351 } 1352 1353 public int hashCode() { 1354 int result = 17; 1355 result = 37*result + ((base == null) ? 0: base.hashCode()); 1356 result = 37*result + super.hashCode(); 1357 return result; 1358 } 1359 } 1360 static private Map extensionValidSubElementTypeMap = null; 1361 static { 1362 extensionValidSubElementTypeMap = new HashMap(); 1363 extensionValidSubElementTypeMap.put(Annotation.class, null); 1364 extensionValidSubElementTypeMap.put(Attribute.class, null); 1365 extensionValidSubElementTypeMap.put(AttributeGroup.class, null); 1366 extensionValidSubElementTypeMap.put(AnyAttribute.class, null); 1367 extensionValidSubElementTypeMap.put(Choice.class, null); 1368 extensionValidSubElementTypeMap.put(Group.class, null); 1369 extensionValidSubElementTypeMap.put(Sequence.class, null); 1370 extensionValidSubElementTypeMap.put(All.class, null); 1371 } 1372 1373 public class ListElement extends ContainsSubElements { 1375 protected String itemType; 1376 public ListElement() { 1377 } 1378 1379 public ListElement(String itemType) { 1380 this.itemType = itemType; 1381 } 1382 1383 public String getContentName() { 1384 return null; 1385 } 1386 1387 public String getName() { 1388 return "list"; 1389 } 1390 1391 public void setItemType(String b) { 1392 itemType = b; 1393 } 1394 1395 public String getItemType() { 1396 return itemType; 1397 } 1398 1399 public String getJavaTypeName() { 1400 return schemaTypeToJavaType(itemType); 1401 } 1402 1403 public boolean writeDTDName(StringBuffer out) { 1404 return writeDTDSubElementNames(out, false); 1405 } 1406 1407 public String getAttributeString() { 1408 if (itemType == null) 1409 return null; 1410 return (" itemType='"+getItemType()+"'"); 1411 } 1412 1413 public void readSchema(org.w3c.dom.Element node) { 1414 String itemType = node.getAttribute("itemType"); ListElement el = new ListElement(itemType); 1416 pushCurrent(el); 1417 read(node); 1418 popCurrent(); 1419 } 1420 1421 public Map validSubElementTypeMap() { 1422 return listValidSubElementTypeMap; 1423 } 1424 1425 public boolean equals(Object o) { 1426 if (!(o instanceof ListElement)) 1427 return false; 1428 ListElement el = (ListElement) o; 1429 if (itemType == null) { 1430 if (el.itemType != null) 1431 return false; 1432 } else { 1433 if (!itemType.equals(el.itemType)) 1434 return false; 1435 } 1436 1437 return super.equals(el); 1438 } 1439 1440 public int hashCode() { 1441 int result = 17; 1442 result = 37*result + ((itemType == null) ? 0: itemType.hashCode()); 1443 result = 37*result + super.hashCode(); 1444 return result; 1445 } 1446 } 1447 static private Map listValidSubElementTypeMap = null; 1448 static { 1449 listValidSubElementTypeMap = new HashMap(); 1450 listValidSubElementTypeMap.put(Annotation.class, null); 1451 } 1452 1453 public abstract class RestrictionType extends ContainsSubElements { 1455 protected String value; 1456 1457 public RestrictionType(String value) { 1458 super(); 1459 this.value = (value == null) ? null : value.intern(); 1460 } 1461 1462 public String getContentName() { 1463 return null; 1464 } 1465 1466 public String getValue() { 1467 return value; 1468 } 1469 1470 public void writeDTD(StringBuffer out) { 1471 } 1473 public boolean writeDTDName(StringBuffer out) { 1474 return false; 1476 } 1477 1478 public void validate() { 1479 } 1480 1481 public ElementExpr optimize() { 1482 return this; 1483 } 1484 1485 public Map validSubElementTypeMap() { 1486 return restrictionTypeValidSubElementTypeMap; 1487 } 1488 1489 public void writeXMLSchema(XMLWriter out) throws IOException { 1490 out.startTag(getXSDNamespace(), getName(), false); 1491 out.write(" value='"); 1492 XMLUtil.printXML(out, value); 1493 out.write("'"); 1494 out.finishStartTag(false, true); 1495 } 1496 1497 public void readSchema(org.w3c.dom.Element node) { 1498 String myValue = node.getAttribute("value"); RestrictionType el = newInstance(myValue); 1500 pushCurrent(el); 1501 read(node); 1502 popCurrent(); 1503 } 1504 1505 protected abstract RestrictionType newInstance(String value); 1506 1507 1512 1513 public String toString() { 1514 return getName()+" ("+value+")"; 1515 } 1516 } 1517 static private Map restrictionTypeValidSubElementTypeMap = null; 1518 static { 1519 restrictionTypeValidSubElementTypeMap = new HashMap(); 1520 restrictionTypeValidSubElementTypeMap.put(Annotation.class, null); 1521 } 1522 1523 public class MaxExclusive extends RestrictionType implements DataTypeRestriction { 1525 public MaxExclusive(String value) { 1526 super(value); 1527 } 1528 1529 public String getName() { 1530 return "maxExclusive"; 1531 } 1532 1533 protected RestrictionType newInstance(String value) { 1534 return new MaxExclusive(value); 1535 } 1536 1537 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1538 if (!passCheck) { 1539 out.write("if ("+JavaUtil.compareToText(var, type, value)+" >= 0) {\n"); 1540 out.write(failVar+" = true;\n"); 1541 out.write("}\n"); 1542 } else { 1543 out.write("if ("+JavaUtil.compareToText(var, type, value)+" < 0) {\n"); 1544 out.write(failVar+" = true;\n"); 1545 out.write("}\n"); 1546 } 1547 } 1548 } 1549 1550 public class MinExclusive extends RestrictionType implements DataTypeRestriction { 1552 public MinExclusive(String value) { 1553 super(value); 1554 } 1555 1556 public String getName() { 1557 return "minExclusive"; 1558 } 1559 1560 protected RestrictionType newInstance(String value) { 1561 return new MinExclusive(value); 1562 } 1563 1564 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1565 if (!passCheck) { 1566 out.write("if ("+JavaUtil.compareToText(var, type, value)+" <= 0) {\n"); 1567 out.write(failVar+" = true;\n"); 1568 out.write("}\n"); 1569 } else { 1570 out.write("if ("+JavaUtil.compareToText(var, type, value)+" > 0) {\n"); 1571 out.write(failVar+" = true;\n"); 1572 out.write("}\n"); 1573 } 1574 } 1575 } 1576 1577 public class Enumeration extends RestrictionType implements DataEnumRestriction { 1579 public Enumeration(String value) { 1580 super(value); 1581 } 1582 1583 public String getName() { 1584 return "enumeration"; 1585 } 1586 1587 protected RestrictionType newInstance(String value) { 1588 return new Enumeration(value); 1589 } 1590 1591 public void genRestriction(Writer out, String type) throws IOException { 1592 out.write(JavaUtil.instanceFrom(type, value)); 1593 } 1594 } 1595 1596 public class Pattern extends RestrictionType implements DataTypeRestriction { 1598 public Pattern(String value) { 1599 super(value); 1600 } 1601 1602 public String getName() { 1603 return "pattern"; 1604 } 1605 1606 protected RestrictionType newInstance(String value) { 1607 return new Pattern(value); 1608 } 1609 1610 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1611 if (!passCheck) { 1612 out.write("if (!("+JavaUtil.typeToString(type, var)+").matches("+JavaUtil.instanceFrom("java.lang.String", value)+")) {\n"); 1613 out.write(failVar+" = true;\n"); 1614 out.write("}\n"); 1615 } else { 1616 out.write("if (("+JavaUtil.typeToString(type, var)+").matches("+JavaUtil.instanceFrom("java.lang.String", value)+")) {\n"); 1617 out.write(failVar+" = true;\n"); 1618 out.write("}\n"); 1619 } 1620 } 1621 } 1622 1623 public class MinLength extends RestrictionType implements DataTypeRestriction { 1625 public MinLength(String value) { 1626 super(value); 1627 } 1628 1629 public String getName() { 1630 return "minLength"; 1631 } 1632 1633 protected RestrictionType newInstance(String value) { 1634 return new MinLength(value); 1635 } 1636 1637 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1638 if (!passCheck) { 1639 out.write("if (("+JavaUtil.typeToString(type, var)+").length() < "+value+") {\n"); 1640 out.write(failVar+" = true;\n"); 1641 out.write("}\n"); 1642 } else { 1643 out.write("if (("+JavaUtil.typeToString(type, var)+").length() >= "+value+") {\n"); 1644 out.write(failVar+" = true;\n"); 1645 out.write("}\n"); 1646 } 1647 } 1648 } 1649 1650 public class MaxLength extends RestrictionType implements DataTypeRestriction { 1652 public MaxLength(String value) { 1653 super(value); 1654 } 1655 1656 public String getName() { 1657 return "maxLength"; 1658 } 1659 1660 protected RestrictionType newInstance(String value) { 1661 return new MaxLength(value); 1662 } 1663 1664 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1665 if (!passCheck) { 1666 out.write("if (("+JavaUtil.typeToString(type, var)+").length() > "+value+") {\n"); 1667 out.write(failVar+" = true;\n"); 1668 out.write("}\n"); 1669 } else { 1670 out.write("if (("+JavaUtil.typeToString(type, var)+").length() <= "+value+") {\n"); 1671 out.write(failVar+" = true;\n"); 1672 out.write("}\n"); 1673 } 1674 } 1675 } 1676 1677 public class TotalDigits extends RestrictionType implements DataTypeRestriction { 1679 public TotalDigits(String value) { 1680 super(value); 1681 } 1682 1683 public String getName() { 1684 return "totalDigits"; 1685 } 1686 1687 protected RestrictionType newInstance(String value) { 1688 return new TotalDigits(value); 1689 } 1690 1691 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1692 if (!passCheck) { 1693 out.write("{\n"); 1694 out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n"); 1695 out.write("int digitCount = 0;\n"); 1696 out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n"); 1697 out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n"); 1698 out.write("++digitCount;\n"); 1699 out.write("if (digitCount > "+value+") {\n"); 1700 out.write(failVar+" = true;\n"); 1701 out.write("break;\n"); 1702 out.write("}\n"); 1703 out.write("}\n"); 1704 out.write("}\n"); 1705 out.write("}\n"); 1706 } else { 1707 out.write("{\n"); 1708 out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n"); 1709 out.write("int digitCount = 0;\n"); 1710 out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n"); 1711 out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n"); 1712 out.write("++digitCount;\n"); 1713 out.write("}\n"); 1714 out.write("}\n"); 1715 out.write("if (digitCount <= "+value+") {\n"); 1716 out.write(failVar+" = true;\n"); 1717 out.write("}\n"); 1718 out.write("}\n"); 1719 } 1720 } 1721 } 1722 1723 public class MinInclusive extends RestrictionType implements DataTypeRestriction { 1725 public MinInclusive(String value) { 1726 super(value); 1727 } 1728 1729 public String getName() { 1730 return "minInclusive"; 1731 } 1732 1733 protected RestrictionType newInstance(String value) { 1734 return new MinInclusive(value); 1735 } 1736 1737 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1738 if (!passCheck) { 1739 out.write("if ("+JavaUtil.compareToText(var, type, value)+" < 0) {\n"); 1740 out.write(failVar+" = true;\n"); 1741 out.write("}\n"); 1742 } else { 1743 out.write("if ("+JavaUtil.compareToText(var, type, value)+" >= 0) {\n"); 1744 out.write(failVar+" = true;\n"); 1745 out.write("}\n"); 1746 } 1747 } 1748 } 1749 1750 public class MaxInclusive extends RestrictionType implements DataTypeRestriction { 1752 public MaxInclusive(String value) { 1753 super(value); 1754 } 1755 1756 public String getName() { 1757 return "maxInclusive"; 1758 } 1759 1760 protected RestrictionType newInstance(String value) { 1761 return new MaxInclusive(value); 1762 } 1763 1764 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1765 if (!passCheck) { 1766 out.write("if ("+JavaUtil.compareToText(var, type, value)+" > 0) {\n"); 1767 out.write(failVar+" = true;\n"); 1768 out.write("}\n"); 1769 } else { 1770 out.write("if ("+JavaUtil.compareToText(var, type, value)+" <= 0) {\n"); 1771 out.write(failVar+" = true;\n"); 1772 out.write("}\n"); 1773 } 1774 } 1775 } 1776 1777 public class FractionDigits extends RestrictionType implements DataTypeRestriction { 1779 public FractionDigits(String value) { 1780 super(value); 1781 } 1782 1783 public String getName() { 1784 return "fractionDigits"; 1785 } 1786 1787 protected RestrictionType newInstance(String value) { 1788 return new FractionDigits(value); 1789 } 1790 1791 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1792 if (!passCheck) { 1793 out.write("{\n"); 1794 out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n"); 1795 out.write("int dotPos = _tmp.indexOf('.');\n"); 1796 out.write("if (dotPos >= 0) {\n"); 1797 out.write("_tmp = _tmp.substring(dotPos+1, _tmp.length());\n"); 1798 out.write("int digitCount = 0;\n"); 1799 out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n"); 1800 out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n"); 1801 out.write("++digitCount;\n"); 1802 out.write("if (digitCount > "+value+") {\n"); 1803 out.write(failVar+" = true;\n"); 1804 out.write("break;\n"); 1805 out.write("}\n"); 1806 out.write("}\n"); 1807 out.write("}\n"); 1808 out.write("}\n"); 1809 out.write("}\n"); 1810 } else { 1811 out.write("{\n"); 1812 out.write("String _tmp = "+JavaUtil.typeToString(type, var)+";\n"); 1813 out.write("int dotPos = _tmp.indexOf('.');\n"); 1814 out.write("if (dotPos >= 0) {\n"); 1815 out.write("_tmp = _tmp.substring(dotPos+1, _tmp.length());\n"); 1816 out.write("int digitCount = 0;\n"); 1817 out.write("for (int _index1 = 0; _index1 < _tmp.length(); ++_index1) {\n"); 1818 out.write("if (Character.isDigit(_tmp.charAt(_index1))) {\n"); 1819 out.write("++digitCount;\n"); 1820 out.write("}\n"); 1821 out.write("}\n"); 1822 out.write("if (digitCount <= "+value+") {\n"); 1823 out.write(failVar+" = true;\n"); 1824 out.write("}\n"); 1825 out.write("}\n"); 1826 out.write("else\n"); 1827 out.write(failVar+" = true;\n"); 1828 out.write("}\n"); 1829 } 1830 } 1831 } 1832 1833 public class Length extends RestrictionType implements DataTypeRestriction { 1835 public Length(String value) { 1836 super(value); 1837 } 1838 1839 public String getName() { 1840 return "length"; 1841 } 1842 1843 protected RestrictionType newInstance(String value) { 1844 return new Length(value); 1845 } 1846 1847 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1848 if (!passCheck) { 1849 out.write("if ("+JavaUtil.typeToString(type, var)+".length() != "+value+") {\n"); 1850 out.write(failVar+" = true;\n"); 1851 out.write("}\n"); 1852 } else { 1853 out.write("if ("+JavaUtil.typeToString(type, var)+".length() == "+value+") {\n"); 1854 out.write(failVar+" = true;\n"); 1855 out.write("}\n"); 1856 } 1857 } 1858 } 1859 1860 public class WhiteSpace extends RestrictionType implements DataTypeRestriction { 1862 public WhiteSpace(String value) { 1863 super(value); 1864 } 1865 1866 public String getName() { 1867 return "whiteSpace"; 1868 } 1869 1870 public boolean isPreserve() { 1871 return "preserve" == value; 1872 } 1873 1874 public boolean isReplace() { 1875 return "replace" == value; 1876 } 1877 1878 public boolean isCollapse() { 1879 return "collapse" == value; 1880 } 1881 1882 public void validate() { 1883 super.validate(); 1884 if (value == null || value.equals("")) 1885 throw new IllegalStateException (Common.getMessage("MSG_InvalidWhiteSpaceValue", value)); 1886 if (!(value == "preserve" || value == "replace" || value == "collapse")) 1887 throw new IllegalStateException (Common.getMessage("MSG_InvalidWhiteSpaceValue", value)); 1888 } 1889 1890 protected RestrictionType newInstance(String value) { 1891 return new WhiteSpace(value); 1892 } 1893 1894 public void genRestriction(Writer out, String var, String type, String failVar, boolean passCheck) throws IOException { 1895 out.write("// has whitespace restriction\n"); 1897 } 1898 } 1899 1900 public abstract class ElementInformationItem extends ContainsSubElements { 1903 private String id; 1904 private String name; 1905 1906 public ElementInformationItem() { 1907 } 1908 1909 public ElementInformationItem(String id, String name) { 1910 this.id = id; 1911 this.name = name; 1912 } 1913 1914 public String getContentName() { 1915 return name; 1916 } 1917 1918 public String getId() { 1919 return id; 1920 } 1921 1922 public void setId(String id) { 1923 this.id = id; 1924 } 1925 1926 public String getElementName() { 1927 return name; 1928 } 1929 1930 public void setElementName(String name) { 1931 this.name = name; 1932 } 1933 1934 public boolean writeDTDName(StringBuffer out) { 1935 boolean hasNamedSubElements = writeDTDSubElementNames(out, true); 1936 return hasNamedSubElements; 1937 } 1938 1939 public String getAttributeString() { 1940 StringBuffer sb = new StringBuffer (); 1941 if (name != null) 1942 sb.append(" name='"+name+"'"); 1943 if (id != null) 1944 sb.append(" id='"+id+"'"); 1945 return sb.toString(); 1946 } 1947 1948 public void readSchema(org.w3c.dom.Element node) { 1949 String id = node.getAttribute("id"); String name = node.getAttribute("name"); ElementInformationItem el = newInstance(); 1952 pushCurrent(el); 1953 if (id != null && !id.equals("")) 1954 el.setId(id); 1955 if (name != null && !name.equals("")) 1956 el.setElementName(name); 1957 read(node); 1958 popCurrent(); 1959 } 1960 1961 protected abstract ElementInformationItem newInstance(); 1962 1963 public boolean equals(Object o) { 1964 if (!(o instanceof ElementInformationItem)) 1965 return false; 1966 ElementInformationItem el = (ElementInformationItem) o; 1967 if (id != el.id) 1968 return false; 1969 if (name != el.name) 1970 return false; 1971 1972 return super.equals(el); 1973 } 1974 1975 public int hashCode() { 1976 int result = 17; 1977 result = 37*result + ((id == null) ? 0 : id.hashCode()); 1978 result = 37*result + ((name == null) ? 0 : name.hashCode()); 1979 result = 37*result + super.hashCode(); 1980 return result; 1981 } 1982 } 1983 1984 public class Key extends ElementInformationItem { 1986 public Key() { 1987 } 1988 1989 public String getName() { 1990 return "key"; 1991 } 1992 1993 public Map validSubElementTypeMap() { 1994 return keyValidSubElementTypeMap; 1995 } 1996 1997 protected ElementInformationItem newInstance() { 1998 return new Key(); 1999 } 2000 } 2001 static private Map keyValidSubElementTypeMap = null; 2002 static { 2003 keyValidSubElementTypeMap = new HashMap(); 2004 keyValidSubElementTypeMap.put(Annotation.class, null); 2005 keyValidSubElementTypeMap.put(Selector.class, null); 2006 keyValidSubElementTypeMap.put(Field.class, null); 2007 } 2008 2009 public class Unique extends ElementInformationItem { 2011 public Unique() { 2012 } 2013 2014 public String getName() { 2015 return "unique"; 2016 } 2017 2018 public Map validSubElementTypeMap() { 2019 return uniqueValidSubElementTypeMap; 2020 } 2021 2022 protected ElementInformationItem newInstance() { 2023 return new Unique(); 2024 } 2025 } 2026 static private Map uniqueValidSubElementTypeMap = null; 2027 static { 2028 uniqueValidSubElementTypeMap = new HashMap(); 2029 uniqueValidSubElementTypeMap.put(Annotation.class, null); 2030 uniqueValidSubElementTypeMap.put(Selector.class, null); 2031 uniqueValidSubElementTypeMap.put(Field.class, null); 2032 } 2033 2034 public class KeyRef extends ElementInformationItem { 2036 private String refer; 2037 2038 public KeyRef() { 2039 } 2040 2041 public String getName() { 2042 return "keyref"; 2043 } 2044 2045 public void setRefer(String refer) { 2046 this.refer = refer; 2047 } 2048 2049 public String getRefer() { 2050 return refer; 2051 } 2052 2053 public String getAttributeString() { 2054 StringBuffer sb = new StringBuffer (super.getAttributeString()); 2055 if (refer != null) 2056 sb.append(" refer='"+refer+"'"); 2057 return sb.toString(); 2058 } 2059 2060 public void readSchema(org.w3c.dom.Element node) { 2061 String id = node.getAttribute("id"); String name = node.getAttribute("name"); String refer = node.getAttribute("refer"); KeyRef el = new KeyRef(); 2065 pushCurrent(el); 2066 if (id != null && !id.equals("")) 2067 el.setId(id); 2068 if (name != null && !name.equals("")) 2069 el.setElementName(name); 2070 if (refer != null && !refer.equals("")) 2071 el.setRefer(refer); 2072 read(node); 2073 popCurrent(); 2074 } 2075 2076 public Map validSubElementTypeMap() { 2077 return keyrefValidSubElementTypeMap; 2078 } 2079 2080 protected ElementInformationItem newInstance() { 2081 return new KeyRef(); 2082 } 2083 } 2084 static private Map keyrefValidSubElementTypeMap = null; 2085 static { 2086 keyrefValidSubElementTypeMap = new HashMap(); 2087 keyrefValidSubElementTypeMap.put(Annotation.class, null); 2088 keyrefValidSubElementTypeMap.put(Selector.class, null); 2089 keyrefValidSubElementTypeMap.put(Field.class, null); 2090 } 2091 2092 public abstract class SelectorOrField extends ContainsSubElements { 2094 private String id; 2095 private String xpath; 2096 2097 public SelectorOrField() { 2098 } 2099 2100 public String getContentName() { 2101 return null; 2102 } 2103 2104 public String getId() { 2105 return id; 2106 } 2107 2108 public void setId(String id) { 2109 this.id = id; 2110 } 2111 2112 public String getXPath() { 2113 return xpath; 2114 } 2115 2116 public void setXPath(String xpath) { 2117 this.xpath = xpath; 2118 } 2119 2120 public String getAttributeString() { 2121 StringBuffer sb = new StringBuffer (); 2122 if (xpath != null) 2123 sb.append(" xpath='"+xpath+"'"); 2124 if (id != null) 2125 sb.append(" id='"+id+"'"); 2126 return sb.toString(); 2127 } 2128 2129 public void readSchema(org.w3c.dom.Element node) { 2130 String id = node.getAttribute("id"); String xpath = node.getAttribute("xpath"); SelectorOrField el = newInstance(); 2133 pushCurrent(el); 2134 if (id != null && !id.equals("")) 2135 el.setId(id); 2136 if (xpath != null && !xpath.equals("")) 2137 el.setXPath(xpath); 2138 read(node); 2139 popCurrent(); 2140 } 2141 2142 protected abstract SelectorOrField newInstance(); 2143 2144 public void writeDTD(StringBuffer out) { 2145 } 2147 2148 public boolean writeDTDName(StringBuffer out) { 2149 return false; 2151 } 2152 2153 public boolean equals(Object o) { 2154 if (!(o instanceof SelectorOrField)) 2155 return false; 2156 SelectorOrField el = (SelectorOrField) o; 2157 if (id == null) { 2158 if (el.id != null) 2159 return false; 2160 } else if (id != el.id) 2161 return false; 2162 if (xpath == null) { 2163 if (el.xpath != null) 2164 return false; 2165 } else if (xpath != el.xpath) 2166 return false; 2167 2168 return super.equals(el); 2169 } 2170 2171 public int hashCode() { 2172 int result = 17; 2173 result = 37*result + ((id == null) ? 0 : id.hashCode()); 2174 result = 37*result + ((xpath == null) ? 0 : xpath.hashCode()); 2175 result = 37*result + super.hashCode(); 2176 return result; 2177 } 2178 } 2179 2180 public class Selector extends SelectorOrField { 2182 public Selector() { 2183 } 2184 2185 public String getName() { 2186 return "selector"; 2187 } 2188 2189 public Map validSubElementTypeMap() { 2190 return selectorValidSubElementTypeMap; 2191 } 2192 2193 protected SelectorOrField newInstance() { 2194 return new Selector(); 2195 } 2196 } 2197 static private Map selectorValidSubElementTypeMap = null; 2198 static { 2199 selectorValidSubElementTypeMap = new HashMap(); 2200 selectorValidSubElementTypeMap.put(Annotation.class, null); 2201 } 2202 2203 public class Field extends SelectorOrField { 2204 public Field() { 2205 } 2206 2207 public String getName() { 2208 return "field"; 2209 } 2210 2211 public Map validSubElementTypeMap() { 2212 return fieldValidSubElementTypeMap; 2213 } 2214 2215 protected SelectorOrField newInstance() { 2216 return new Field(); 2217 } 2218 } 2219 static private Map fieldValidSubElementTypeMap = null; 2220 static { 2221 fieldValidSubElementTypeMap = new HashMap(); 2222 fieldValidSubElementTypeMap.put(Annotation.class, null); 2223 } 2224 2225 public class Include extends ElementExpr { 2227 private String schemaLocation; 2228 2229 public Include(String schemaLocation) { 2230 this.schemaLocation = schemaLocation; 2231 } 2232 2233 public String getContentName() { 2234 return null; 2235 } 2236 2237 public void readSchema(org.w3c.dom.Element node) { 2238 String schemaLocation = node.getAttribute("schemaLocation"); if (includedAlready.containsKey(schemaLocation)) 2240 return; 2241 includedAlready.put(schemaLocation, null); 2242 2243 ParserSchemaState oldState = new ParserSchemaState(); 2245 try { 2246 readSchemaFromLocation(schemaLocation); 2247 } catch (org.xml.sax.SAXException e) { 2248 throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e); 2249 } catch (java.io.IOException e) { 2250 throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e); 2251 } finally { 2252 oldState.reload(); 2253 } 2254 2259 } 2260 2261 public ElementExpr optimize() { 2262 return this; 2263 } 2264 2265 public void validate() { 2266 } 2267 2268 public String getName() { 2269 return "include"; 2270 } 2271 2272 public void writeDTD(StringBuffer out) { 2273 } 2275 2276 public boolean writeDTDName(StringBuffer out) { 2277 return false; 2279 } 2280 2281 public void writeXMLSchema(XMLWriter out) throws IOException { 2282 out.startTag(getXSDNamespace(), getName(), false); 2283 if (schemaLocation != null) { 2284 out.write(" schemaLocation='"); 2285 out.write(schemaLocation); 2286 out.write("'"); 2287 } 2288 out.finishStartTag(false, true); 2289 } 2290 2291 public String toString() { 2292 if (schemaLocation == null) 2293 return getName(); 2294 else 2295 return getName()+" schemaLocation="+schemaLocation; 2296 } 2297 2298 public boolean equals(Object o) { 2299 if (!(o instanceof Include)) 2300 return false; 2301 Include el = (Include) o; 2302 if (schemaLocation == null) { 2303 if (el.schemaLocation != null) 2304 return false; 2305 } else if (!schemaLocation.equals(el.schemaLocation)) 2306 return false; 2307 2308 return super.equals(el); 2309 } 2310 2311 public int hashCode() { 2312 int result = 17; 2313 result = 37*result + ((schemaLocation == null) ? 0 : schemaLocation.hashCode()); 2314 result = 37*result + super.hashCode(); 2315 return result; 2316 } 2317 } 2318 2319 public class Import extends ElementExpr { 2321 private String theNamespace; 2322 private String schemaLocation; 2323 2324 public Import() { 2325 } 2326 2327 public String getContentName() { 2328 return null; 2329 } 2330 2331 public String getNamespace() { 2332 return theNamespace; 2333 } 2334 2335 public void setNamespace(String namespace) { 2336 theNamespace = namespace; 2337 } 2338 2339 public String getSchemaLocation() { 2340 return schemaLocation; 2341 } 2342 2343 public void setSchemaLocation(String schemaLocation) { 2344 this.schemaLocation = schemaLocation; 2345 } 2346 2347 public void readSchema(org.w3c.dom.Element node) { 2348 String namespace = node.getAttribute("namespace"); String schemaLocation = node.getAttribute("schemaLocation"); ParserSchemaState oldState = new ParserSchemaState(); 2352 if (namespace != null && !namespace.equals("")) 2353 targetNamespace = namespace; 2354 try { 2355 readSchemaFromLocation(schemaLocation); 2356 } catch (org.xml.sax.SAXException e) { 2357 throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e); 2358 } catch (java.io.IOException e) { 2359 throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e); 2360 } finally { 2361 oldState.reload(); 2362 } 2363 } 2365 2366 public ElementExpr optimize() { 2367 return this; 2368 } 2369 2370 public void validate() { 2371 } 2372 2373 public String getName() { 2374 return "import"; 2375 } 2376 2377 public void writeDTD(StringBuffer out) { 2378 } 2380 2381 public boolean writeDTDName(StringBuffer out) { 2382 return false; 2384 } 2385 2386 public void writeXMLSchema(XMLWriter out) throws IOException { 2387 out.startTag(getXSDNamespace(), getName(), false); 2388 if (theNamespace != null) { 2389 out.write(" theNamespace='"); 2390 out.write(theNamespace); 2391 out.write("'"); 2392 } 2393 if (schemaLocation != null) { 2394 out.write(" schemaLocation='"); 2395 out.write(schemaLocation); 2396 out.write("'"); 2397 } 2398 out.finishStartTag(false, true); 2399 } 2400 2401 public boolean equals(Object o) { 2402 if (!(o instanceof Import)) 2403 return false; 2404 Import el = (Import) o; 2405 if (theNamespace == null) { 2406 if (el.theNamespace != null) 2407 return false; 2408 } else if (!theNamespace.equals(el.theNamespace)) 2409 return false; 2410 if (schemaLocation == null) { 2411 if (el.schemaLocation != null) 2412 return false; 2413 } else if (!schemaLocation.equals(el.schemaLocation)) 2414 return false; 2415 2416 return super.equals(el); 2417 } 2418 2419 public int hashCode() { 2420 int result = 17; 2421 result = 37*result + ((theNamespace == null) ? 0 : theNamespace.hashCode()); 2422 result = 37*result + ((schemaLocation == null) ? 0 : schemaLocation.hashCode()); 2423 result = 37*result + super.hashCode(); 2424 return result; 2425 } 2426 } 2427 2428 public abstract class ModelGroup extends ContainsSubElements implements MinMaxOccurs { 2431 private String minOccurs; 2432 private String maxOccurs; 2433 2434 public ModelGroup() { 2435 super(); 2436 minOccurs = "1"; 2437 maxOccurs = "1"; 2438 } 2439 2440 public String getContentName() { 2441 return null; 2442 } 2443 2444 public void setMinOccurs(String mino) { 2445 if (mino == null) 2446 mino = "1"; 2447 minOccurs = mino.intern(); 2448 } 2449 2450 public void setMaxOccurs(String maxo) { 2451 if (maxo == null) 2452 maxo = "1"; 2453 maxOccurs = maxo.intern(); 2454 } 2455 2456 public String getMinOccurs() { 2457 return minOccurs; 2458 } 2459 2460 public String getMaxOccurs() { 2461 return maxOccurs; 2462 } 2463 2464 2469 public ElementExpr optimize() { 2470 super.optimize(); 2471 if (subElements.size() == 0) 2472 return null; 2473 2477 return this; 2478 } 2479 2480 public boolean writeDTDName(StringBuffer out) { 2481 boolean many = ("unbounded" == maxOccurs); 2482 boolean hasNamedSubElements = writeDTDSubElementNames(out, true); 2483 if (many) 2484 out.append("*"); 2485 return hasNamedSubElements; 2486 } 2487 2488 public String getAttributeString() { 2489 StringBuffer sb = new StringBuffer (); 2490 if (minOccurs != "1") 2491 sb.append(" minOccurs='"+minOccurs+"'"); 2492 if (maxOccurs != "1") 2493 sb.append(" maxOccurs='"+maxOccurs+"'"); 2494 return sb.toString(); 2495 } 2496 2497 public void readSchema(org.w3c.dom.Element node) { 2498 String minOccurs = node.getAttribute("minOccurs"); String maxOccurs = node.getAttribute("maxOccurs"); ModelGroup el = newInstance(); 2501 pushCurrent(el); 2502 if (minOccurs != null && !minOccurs.equals("")) 2503 el.setMinOccurs(minOccurs); 2504 if (maxOccurs != null && !maxOccurs.equals("")) 2505 el.setMaxOccurs(maxOccurs); 2506 read(node); 2507 popCurrent(); 2508 } 2509 2510 protected abstract ModelGroup newInstance(); 2511 2512 public boolean equals(Object o) { 2513 if (!(o instanceof ModelGroup)) 2514 return false; 2515 ModelGroup el = (ModelGroup) o; 2516 if (minOccurs != el.minOccurs) 2517 return false; 2518 if (maxOccurs != el.maxOccurs) 2519 return false; 2520 2521 return super.equals(el); 2522 } 2523 2524 public int hashCode() { 2525 int result = 17; 2526 result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode()); 2527 result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode()); 2528 result = 37*result + super.hashCode(); 2529 return result; 2530 } 2531 } 2532 2533 public class Sequence extends ModelGroup { 2536 public Sequence() { 2537 } 2538 2539 public String getName() { 2540 return "sequence"; 2541 } 2542 2543 public Map validSubElementTypeMap() { 2544 return sequenceValidSubElementTypeMap; 2545 } 2546 2547 protected ModelGroup newInstance() { 2548 return new Sequence(); 2549 } 2550 } 2551 static private Map sequenceValidSubElementTypeMap = null; 2552 static { 2553 sequenceValidSubElementTypeMap = new HashMap(); 2554 sequenceValidSubElementTypeMap.put(Annotation.class, null); 2555 sequenceValidSubElementTypeMap.put(Element.class, null); 2556 sequenceValidSubElementTypeMap.put(Any.class, null); 2557 sequenceValidSubElementTypeMap.put(Choice.class, null); 2558 sequenceValidSubElementTypeMap.put(Sequence.class, null); 2559 sequenceValidSubElementTypeMap.put(Group.class, null); 2560 } 2561 2562 public class Choice extends ModelGroup { 2564 public Choice() { 2565 } 2566 2567 public String getName() { 2568 return "choice"; 2569 } 2570 2571 public Map validSubElementTypeMap() { 2572 return choiceValidSubElementTypeMap; 2573 } 2574 2575 protected ModelGroup newInstance() { 2576 return new Choice(); 2577 } 2578 } 2579 static private Map choiceValidSubElementTypeMap = null; 2580 static { 2581 choiceValidSubElementTypeMap = new HashMap(); 2582 choiceValidSubElementTypeMap.put(Annotation.class, null); 2583 choiceValidSubElementTypeMap.put(Element.class, null); 2584 choiceValidSubElementTypeMap.put(Any.class, null); 2585 choiceValidSubElementTypeMap.put(Choice.class, null); 2586 choiceValidSubElementTypeMap.put(Sequence.class, null); 2587 choiceValidSubElementTypeMap.put(Group.class, null); 2588 } 2589 2590 public class All extends ModelGroup { 2592 public All() { 2593 } 2594 2595 public String getName() { 2596 return "all"; 2597 } 2598 2599 public Map validSubElementTypeMap() { 2600 return allValidSubElementTypeMap; 2601 } 2602 2603 protected ModelGroup newInstance() { 2604 return new All(); 2605 } 2606 } 2607 static private Map allValidSubElementTypeMap = null; 2608 static { 2609 allValidSubElementTypeMap = new HashMap(); 2610 allValidSubElementTypeMap.put(Annotation.class, null); 2611 allValidSubElementTypeMap.put(Element.class, null); 2612 allValidSubElementTypeMap.put(Any.class, null); 2613 } 2614 2615 public class Group extends ModelGroup implements CanRef { 2617 private String name; 2618 private String ref; 2619 2620 public Group() { 2621 } 2622 2623 public Group(String n) { 2624 if (n != null && !n.equals("")) { 2625 name = normalizeTargetNamespace(n).intern(); 2626 putSchemaTypeDef(name, this); 2627 } 2628 } 2629 2630 public ElementExpr optimize() { 2631 if (ref != null) 2632 return this; 2633 return super.optimize(); 2634 } 2635 2636 public String getName() { 2637 return "group"; 2638 } 2639 2640 public String getGroupName() { 2641 return name; 2642 } 2643 2644 public boolean hasRef() { 2645 return getRef() != null; 2646 } 2647 2648 2651 public String getRef() { 2652 return ref; 2653 } 2654 2655 public Group getRefGroup() { 2656 if (ref == null) 2657 return null; 2658 Object o = getSchemaTypeDef(ref); 2659 if (o instanceof Group) { 2660 Group referredGroup = (Group) o; 2661 return referredGroup; 2662 } else { 2663 throw new IllegalStateException (Common.getMessage("MSG_FailedToFindRef", 2664 ref, 2665 this.toString())); 2666 } 2667 } 2668 2669 public ElementExpr getRefElementExpr() { 2670 return getRefGroup(); 2671 } 2672 2673 public Map validSubElementTypeMap() { 2674 return groupValidSubElementTypeMap; 2675 } 2676 2677 public String getAttributeString() { 2678 StringBuffer sb = new StringBuffer (); 2679 if (name != null) 2680 sb.append(" name='"+name+"'"); 2681 if (ref != null) 2682 sb.append(" ref='"+ref+"'"); 2683 return sb.toString(); 2684 } 2685 2686 public void readSchema(org.w3c.dom.Element node) { 2687 String myName = node.getAttribute("name"); String myRef = node.getAttribute("ref"); Group g = new Group(myName); 2690 if (myRef != null && !myRef.equals("")) 2691 g.ref = myRef.intern(); 2692 pushCurrent(g); 2693 read(node); 2694 popCurrent(); 2695 } 2696 2697 protected ModelGroup newInstance() { 2698 return new Group(); 2699 } 2700 2701 public boolean equals(Object o) { 2702 if (!(o instanceof AttributeGroup)) 2703 return false; 2704 AttributeGroup el = (AttributeGroup) o; 2705 if (name != el.name) 2706 return false; 2707 if (ref != el.ref) 2708 return false; 2709 2710 return super.equals(el); 2711 } 2712 2713 public int hashCode() { 2714 int result = 17; 2715 result = 37*result + ((name == null) ? 0 : name.hashCode()); 2716 result = 37*result + ((ref == null) ? 0 : ref.hashCode()); 2717 result = 37*result + super.hashCode(); 2718 return result; 2719 } 2720 } 2721 static private Map groupValidSubElementTypeMap = null; 2722 static { 2723 groupValidSubElementTypeMap = new HashMap(); 2724 groupValidSubElementTypeMap.put(All.class, null); 2725 groupValidSubElementTypeMap.put(Choice.class, null); 2726 groupValidSubElementTypeMap.put(Sequence.class, null); 2727 groupValidSubElementTypeMap.put(Annotation.class, null); 2728 } 2729 2730 public class Annotation extends ContainsSubElements { 2734 public Annotation() { 2735 } 2736 2737 public String getName() { 2738 return "annotation"; 2739 } 2740 2741 public String getContentName() { 2742 return null; 2743 } 2744 2745 public ElementExpr optimize() { 2746 super.optimize(); 2747 if (subElements.size() == 0) 2748 return null; 2749 return this; 2750 } 2751 2752 public boolean compressWhiteSpaceInner() { 2753 return true; 2754 } 2755 2756 public boolean writeDTDName(StringBuffer out) { 2757 return false; 2758 } 2759 2760 public void readSchema(org.w3c.dom.Element node) { 2761 Annotation el = new Annotation(); 2762 pushCurrent(el); 2763 read(node); 2764 popCurrent(); 2765 } 2766 2767 public Map validSubElementTypeMap() { 2768 return annotationValidSubElementTypeMap; 2769 } 2770 } 2771 static private Map annotationValidSubElementTypeMap = null; 2772 static { 2773 annotationValidSubElementTypeMap = new HashMap(); 2774 annotationValidSubElementTypeMap.put(AppInfo.class, null); 2775 annotationValidSubElementTypeMap.put(Documentation.class, null); 2776 } 2777 2778 2779 public class AppInfo extends ContainsSubElements { 2782 public AppInfo() { 2783 } 2784 2785 public String getContentName() { 2786 return null; 2787 } 2788 2789 public boolean compressWhiteSpaceOuter() { 2790 return true; 2791 } 2792 2793 public boolean compressWhiteSpaceInner() { 2794 return true; 2795 } 2796 2797 public String getName() { 2798 return "appinfo"; 2799 } 2800 2801 public ElementExpr optimize() { 2802 super.optimize(); 2803 if (subElements.size() == 0) 2804 return null; 2805 return this; 2806 } 2807 2808 public boolean writeDTDName(StringBuffer out) { 2809 return false; 2810 } 2811 2812 public void readSchema(org.w3c.dom.Element node) { 2813 AppInfo el = new AppInfo(); 2814 pushCurrent(el); 2815 read(node); 2816 popCurrent(); 2817 } 2818 2819 public Map validSubElementTypeMap() { 2820 return appInfoValidSubElementTypeMap; 2821 } 2822 } 2823 static private Map appInfoValidSubElementTypeMap = null; 2824 static { 2825 appInfoValidSubElementTypeMap = new HashMap(); 2826 appInfoValidSubElementTypeMap.put(AnyNode.class, null); 2827 appInfoValidSubElementTypeMap.put(TextNode.class, null); 2828 } 2829 2830 2831 public class Documentation extends ContainsSubElements { 2834 public Documentation() { 2835 } 2836 2837 public String getContentName() { 2838 return null; 2839 } 2840 2841 public boolean compressWhiteSpaceOuter() { 2842 return true; 2843 } 2844 2845 public boolean compressWhiteSpaceInner() { 2846 return true; 2847 } 2848 2849 public String getName() { 2850 return "documentation"; 2851 } 2852 2853 public boolean writeDTDName(StringBuffer out) { 2854 return false; 2855 } 2856 2857 public void readSchema(org.w3c.dom.Element node) { 2858 Documentation el = new Documentation(); 2859 pushCurrent(el); 2860 read(node); 2861 popCurrent(); 2862 } 2863 2864 public Map validSubElementTypeMap() { 2865 return documentationValidSubElementTypeMap; 2866 } 2867 } 2868 static private Map documentationValidSubElementTypeMap = null; 2869 static { 2870 documentationValidSubElementTypeMap = new HashMap(); 2871 documentationValidSubElementTypeMap.put(AnyNode.class, null); 2872 documentationValidSubElementTypeMap.put(TextNode.class, null); 2873 } 2874 2875 2876 public class AnyNode extends ContainsSubElements { 2879 protected String name; 2880 protected String value; 2881 2882 public AnyNode(String n, String v) { 2883 name = n; 2884 value = v; 2885 } 2886 2887 public String getContentName() { 2888 return name; 2889 } 2890 2891 public String getName() { 2892 return name; 2893 } 2894 2895 public String getValue() { 2896 return value; 2897 } 2898 2899 public boolean compressWhiteSpaceInner() { 2900 return findSubElement("TextNode") != null; 2901 } 2902 2903 public ElementExpr optimize() { 2904 super.optimize(); 2905 if (name == null && value == null && subElements.size() == 0) 2906 return null; 2907 return this; 2908 } 2909 2910 public boolean writeDTDName(StringBuffer out) { 2911 return false; 2912 } 2913 2914 public void writeXMLSchema(XMLWriter out) throws IOException { 2915 if (value == null) 2916 this.writeXMLSchema(out, true); 2917 else { 2918 out.startTag(getName()); 2919 XMLUtil.printXML(out, value, false); 2920 this.writeXMLSchema(out, false); 2921 out.endTag(); 2922 } 2923 } 2924 2925 public void readSchema(org.w3c.dom.Element node) { 2926 String myName = node.getLocalName(); 2927 String myValue = node.getNodeValue(); 2928 AnyNode el = new AnyNode(myName, myValue); 2929 2930 pushCurrent(el); 2933 read(node); 2934 popCurrent(); 2935 } 2936 2937 public String toString() { 2938 return "AnyNode("+name+")"; 2939 } 2940 2941 public Map validSubElementTypeMap() { 2942 return anyNodeValidSubElementTypeMap; 2943 } 2944 2945 public boolean equals(Object o) { 2946 if (!(o instanceof AnyNode)) 2947 return false; 2948 AnyNode el = (AnyNode) o; 2949 if (value == null) { 2951 if (el.value != null) 2952 return false; 2953 } else if (!value.equals(el.value)) 2954 return false; 2955 if (name == null) { 2956 if (el.name != null) 2957 return false; 2958 } else if (!name.equals(el.name)) 2959 return false; 2960 return true; 2961 } 2962 2963 public int hashCode() { 2964 int result = 17; 2965 result = 37*result + ((value == null) ? 0 : value.hashCode()); 2966 result = 37*result + ((name == null) ? 0 : name.hashCode()); 2967 result = 37*result + super.hashCode(); 2968 return result; 2969 } 2970 } 2971 static private Map anyNodeValidSubElementTypeMap = null; 2972 static { 2973 anyNodeValidSubElementTypeMap = new HashMap(); 2974 anyNodeValidSubElementTypeMap.put(AnyNode.class, null); 2975 anyNodeValidSubElementTypeMap.put(TextNode.class, null); 2976 } 2977 2978 public class TextNode extends ContainsSubElements { 2981 protected String text; 2982 2983 public TextNode(String text) { 2984 this.text = text; 2985 } 2986 2987 public String getText() { 2988 return text; 2989 } 2990 2991 public String getName() { 2992 return "TextNode"; 2993 } 2994 2995 public String getContentName() { 2996 return null; 2997 } 2998 2999 public boolean compressWhiteSpaceInner() { 3000 return true; 3001 } 3002 3003 public ElementExpr optimize() { 3004 super.optimize(); 3005 if (text == null || text.equals("")) 3006 return null; 3007 return this; 3008 } 3009 3010 public boolean writeDTDName(StringBuffer out) { 3011 return false; 3012 } 3013 3014 public void writeXMLSchema(XMLWriter out) throws IOException { 3015 XMLUtil.printXML(out, text, false); 3016 this.writeXMLSchema(out, false); 3017 } 3018 3019 public void readSchema(org.w3c.dom.Element node) { 3020 readSchema((Text) node); 3021 } 3022 3023 public void readSchema(Text node) { 3024 TextNode el = new TextNode(node.getData()); 3025 pushCurrent(el); 3027 read(node); 3028 popCurrent(); 3029 } 3030 3031 public String toString() { 3032 return "TextNode("+text+")"; 3033 } 3034 3035 public Map validSubElementTypeMap() { 3036 return textNodeValidSubElementTypeMap; 3037 } 3038 3039 public boolean equals(Object o) { 3040 if (!(o instanceof TextNode)) 3041 return false; 3042 TextNode el = (TextNode) o; 3043 if (text == null) { 3044 if (el.text != null) 3045 return false; 3046 } else if (!text.equals(el.text)) 3047 return false; 3048 return true; 3049 } 3050 3051 public int hashCode() { 3052 int result = 17; 3053 result = 37*result + ((text == null) ? 0 : text.hashCode()); 3054 result = 37*result + super.hashCode(); 3055 return result; 3056 } 3057 } 3058 static private Map textNodeValidSubElementTypeMap = null; 3059 static { 3060 textNodeValidSubElementTypeMap = new HashMap(); 3061 textNodeValidSubElementTypeMap.put(AnyNode.class, null); 3062 textNodeValidSubElementTypeMap.put(TextNode.class, null); 3063 } 3064 3065 public class Element extends ContainsSubElements implements MinMaxOccurs, CanRef { 3069 private String elementName; 3070 private String elementNamespace; private String defaultTargetNamespace; 3072 private boolean defaultFormQualified; 3073 private Boolean formQualified; 3074 private String type; 3077 private String xmlSchemaType; 3078 private String ref; 3079 private String refWithNamespace; 3080 private String minOccurs = "1"; 3081 private String maxOccurs = "1"; 3082 private boolean nillable = false; 3083 private String defaultValue; 3084 private ElementExpr parentElementExpr; 3085 3086 protected Element(String n, String t) { 3087 this(n); 3088 setType(t); 3089 putSchemaTypeDef(type, this); 3090 } 3091 3092 protected Element(String n) { 3093 setElementName(n); 3094 type = null; 3095 } 3096 3097 private Element() { 3098 } 3099 3100 public String getName() { 3101 return "element"; 3102 } 3103 3104 public String getContentName() { 3105 return elementName; 3106 } 3107 3108 3112 private void setElementName(String n) { 3113 if (n != null && !n.equals("")) { 3114 String prefix = prefixOf(n); 3115 if (prefix == null) { 3116 if (parentElementExpr instanceof SchemaNode) { 3122 elementNamespace = defaultTargetNamespace; 3123 } else { 3124 if (isFormQualified()) 3125 elementNamespace = defaultTargetNamespace; 3126 else { 3127 elementNamespace = null; 3129 } 3130 } 3131 3139 elementName = n; 3140 } else { 3141 elementNamespace = getNamespaceURI(prefix); 3142 elementName = removePrefix(n); 3143 } 3144 } else { 3145 elementName = null; 3146 elementNamespace = null; 3147 } 3148 } 3149 3150 public String getElementName() { 3151 return elementName; 3152 } 3153 3154 3157 public String getElementNamespace() { 3158 return elementNamespace; 3159 } 3160 3161 public String getPrefix() { 3162 return getNamespace(elementNamespace); 3163 } 3164 3165 public boolean isFormQualified() { 3166 if (formQualified != null) 3167 return formQualified.booleanValue(); 3168 return defaultFormQualified; 3169 } 3170 3171 public String getType() { 3172 return type; 3173 } 3174 3175 public boolean hasRef() { 3176 return getRef() != null; 3177 } 3178 3179 3182 public String getRef() { 3183 return ref; 3184 } 3185 3186 public Element getRefElement() { 3187 if (ref == null) 3188 return null; 3189 Element referredElement = getDefinedElementResolvedNamespace(refWithNamespace); 3191 return referredElement; 3192 } 3193 3194 public ElementExpr getRefElementExpr() { 3195 return getRefElement(); 3196 } 3197 3198 3205 public void setType(String t) { 3206 if (t != null) 3207 t = t.intern(); 3208 if ("void" == t) 3209 t = null; 3210 String oldType = type; 3211 type = t; 3212 if (schemaTypeDefs.containsKey(oldType)) schemaTypeDefs.remove(oldType); 3214 if (t == null) 3215 return; 3216 putSchemaTypeDef(type, this); 3217 ref = null; 3218 if (debug) 3219 System.out.println("setType("+t+")"); 3220 if (optionallyDefinedTypes.containsKey(t)) { 3221 requiredPredefinedTypes.put(t, "keep"); } 3224 } 3225 3226 3233 public void setXMLSchemaType(String t) { 3234 if (t == null) { 3235 xmlSchemaType = null; 3236 } else { 3237 xmlSchemaType = normalizeDocumentNamespace(t).intern(); 3238 ref = null; 3239 } 3240 } 3241 3242 public String getJavaType() { 3243 if (type != null) 3245 return type; 3246 if (ref != null) { 3247 Element referredElement = getRefElement(); 3248 return referredElement.getJavaType(); 3250 } 3251 if (xmlSchemaType == null) 3252 return null; 3253 String javaType = schemaTypeToJavaType(xmlSchemaType); 3254 if (nillable) { 3255 javaType = JavaUtil.toObjectType(javaType); 3256 } 3257 return javaType; 3258 } 3259 3260 public String getXMLSchemaType() { 3261 return xmlSchemaType; 3262 } 3263 3264 public void setMinOccurs(String mino) { 3265 if (mino == null) 3266 mino = "1"; 3267 minOccurs = mino.intern(); 3268 } 3269 3270 public void setMaxOccurs(String maxo) { 3271 if (maxo == null) 3272 maxo = "1"; 3273 maxOccurs = maxo.intern(); 3274 } 3275 3276 public String getMinOccurs() { 3277 return minOccurs; 3278 } 3279 3280 public String getMaxOccurs() { 3281 return maxOccurs; 3282 } 3283 3284 public boolean isNillable() { 3285 return nillable; 3286 } 3287 3288 public String getDefault() { 3289 return defaultValue; 3290 } 3291 3292 public void setDefault(String d) { 3293 defaultValue = d; 3294 } 3295 3296 public void writeDTD(StringBuffer out) { 3297 Element firstElement = (Element) elementTable.get(getElementName()); 3298 if (firstElement == null) 3299 elementTable.put(getElementName(), this); 3300 else { 3301 if (debug) 3303 System.out.println("Found another element named "+getElementName()); 3304 if (!equals(firstElement)) { 3305 throw new RuntimeException (Common.getMessage("MSG_SameNameDifferentContents", getElementName())); 3306 } 3307 return; 3308 } 3309 3310 out.append("<!ELEMENT "+getElementName()+" "); 3311 if (subElements.size() == 0) { 3313 if (type == null || type == "void") 3314 out.append("()"); 3315 else 3316 out.append("#PCDATA"); 3317 } else { 3318 if (!writeDTDSubElementNames(out)) 3319 out.append("()"); 3320 } 3321 out.append(">\n"); 3322 3323 super.writeDTD(out); 3325 } 3326 3327 public boolean hasNamedSubElements() { 3328 if (subElements.size() == 0) { 3329 return false; 3330 } else { 3331 if (!writeDTDSubElementNames(new StringBuffer ())) 3332 return false; 3333 else 3334 return true; 3335 } 3336 } 3337 3338 public String getAttributeString() { 3339 StringBuffer sb = new StringBuffer (); 3340 if (elementName != null) { 3341 sb.append(" name='"); 3342 if (elementNamespace != null && !elementNamespace.equals(targetNamespace)) { 3343 sb.append(getNamespace(elementNamespace)); 3344 sb.append(':'); 3345 } 3346 sb.append(elementName); 3347 sb.append("'"); 3348 } 3349 if (ref != null) { 3350 sb.append(" ref='"); 3351 sb.append(ref); 3352 sb.append("'"); 3353 } 3354 if (xmlSchemaType != null) { 3355 sb.append(" type='"); 3356 sb.append(xmlSchemaType); 3357 sb.append("'"); 3358 } else if (type != null) { 3359 String theXmlSchemaType = javaType2XMLSchemaTypeComplex(getType()); 3360 if (theXmlSchemaType != null) { 3361 sb.append(" type='"); 3362 sb.append(theXmlSchemaType); 3363 sb.append("'"); 3364 } else { 3365 } 3367 } 3368 if (minOccurs != "1") 3369 sb.append(" minOccurs='"+minOccurs+"'"); 3370 if (maxOccurs != "1") 3371 sb.append(" maxOccurs='"+maxOccurs+"'"); 3372 if (nillable) 3373 sb.append(" nillable='true'"); 3374 if (defaultValue != null) 3375 sb.append(" default='"+defaultValue+"'"); 3376 if (formQualified != null) 3377 sb.append(" form='"+(formQualified.booleanValue() ? "qualified" : "unqualified")+"'"); 3378 return sb.toString(); 3379 } 3380 3381 public boolean isDefiningNewType() { 3382 if (ref == null) 3383 return true; 3384 return (subElements.size() >= 1 && type != null); 3385 } 3386 3387 public boolean writeDTDName(StringBuffer out) { 3388 out.append(getElementName()); 3389 if ("unbounded" == maxOccurs) 3390 out.append("*"); 3391 return true; 3392 } 3393 3394 public Map validSubElementTypeMap() { 3395 return elementValidSubElementTypeMap; 3396 } 3397 3398 public void readSchema(org.w3c.dom.Element node) { 3399 String elementName = node.getAttribute("name"); String elementType = node.getAttribute("type"); String elementRef = node.getAttribute("ref"); String minOccurs = node.getAttribute("minOccurs"); String maxOccurs = node.getAttribute("maxOccurs"); String myNillable = node.getAttribute("nillable"); String myDefault = node.getAttribute("default"); String myForm = node.getAttribute("form"); String defaultTargetNamespace; 3408 boolean defaultFormQualified; 3409 ElementExpr parent = peekCurrent(); 3410 SchemaNode parentSchema; 3412 if (parent instanceof SchemaNode) { 3413 parentSchema = (SchemaNode) parent; 3414 } else { 3415 parentSchema = (SchemaNode) findAncestor(SchemaNode.class); 3416 } 3417 if (parentSchema != null) { 3418 defaultTargetNamespace = targetNamespace; 3425 defaultFormQualified = elementFormQualifiedDefault; 3427 } else { 3428 defaultTargetNamespace = targetNamespace; 3429 defaultFormQualified = false; 3430 } 3431 Element el = new Element(); 3432 el.parentElementExpr = parent; 3433 el.defaultTargetNamespace = defaultTargetNamespace; 3434 el.defaultFormQualified = defaultFormQualified; 3435 el.setElementName(elementName); 3436 if (myForm != null && !"".equals(myForm)) 3437 el.formQualified = Boolean.valueOf("qualified".equals(myForm)); 3438 if (elementRef != null && !elementRef.equals("")) { 3439 el.ref = elementRef.intern(); 3440 el.refWithNamespace = resolveNamespace(el.ref); 3441 } 3442 if (elementType != null && !elementType.equals("")) 3443 el.setXMLSchemaType(elementType); 3444 if (minOccurs != null && !minOccurs.equals("")) 3445 el.setMinOccurs(minOccurs); 3446 if (maxOccurs != null && !maxOccurs.equals("")) 3447 el.setMaxOccurs(maxOccurs); 3448 if (myNillable != null && (myNillable.equals("true") || myNillable.equals("yes") || myNillable.equals("on"))) 3449 el.nillable = true; 3450 if (myDefault != null && !"".equals(myDefault)) 3451 el.setDefault(myDefault); 3452 pushCurrent(el); 3453 read(node); 3454 popCurrent(); 3455 } 3456 3457 public boolean equals(Object o) { 3458 if (!(o instanceof Element)) 3459 return false; 3460 Element el = (Element) o; 3461 if (type != el.type) 3463 return false; 3464 if (ref != el.ref) 3465 return false; 3466 if (xmlSchemaType != el.xmlSchemaType) 3467 return false; 3468 if (minOccurs != el.minOccurs) 3469 return false; 3470 if (maxOccurs != el.maxOccurs) 3471 return false; 3472 if (nillable != el.nillable) 3473 return false; 3474 if (formQualified == null) { 3475 if (el.formQualified != null) 3476 return false; 3477 } else { 3478 if (el.formQualified == null 3479 || formQualified.booleanValue() != el.formQualified.booleanValue()) 3480 return false; 3481 } 3482 3483 if (!elementName.equals(el.elementName)) 3484 return false; 3485 3486 return super.equals(el); 3487 } 3488 3489 public int hashCode() { 3490 int result = 17; 3491 result = 37*result + ((type == null) ? 0 : type.hashCode()); 3492 result = 37*result + ((ref == null) ? 0 : ref.hashCode()); 3493 result = 37*result + ((xmlSchemaType == null) ? 0 : xmlSchemaType.hashCode()); 3494 result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode()); 3495 result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode()); 3496 result = 37*result + (nillable ? 1 : 0); 3497 result = 37*result + ((elementName == null) ? 0 : elementName.hashCode()); 3498 result = 37*result + super.hashCode(); 3499 result = 37*result + ((formQualified == null) ? 0 : formQualified.hashCode()); 3500 return result; 3501 } 3502 } 3503 static private Map elementValidSubElementTypeMap = null; 3504 static { 3505 elementValidSubElementTypeMap = new HashMap(); 3506 elementValidSubElementTypeMap.put(Annotation.class, null); 3507 elementValidSubElementTypeMap.put(SimpleType.class, null); 3508 elementValidSubElementTypeMap.put(ComplexType.class, null); 3509 elementValidSubElementTypeMap.put(Unique.class, null); 3510 elementValidSubElementTypeMap.put(Key.class, null); 3511 elementValidSubElementTypeMap.put(KeyRef.class, null); 3512 } 3513 3514 public class Any extends ContainsSubElements implements MinMaxOccurs { 3515 private String minOccurs; 3516 private String maxOccurs; 3517 private String namespace; 3518 private String processContents; 3519 3520 public Any() { 3521 init(); 3522 } 3523 3524 private void init() { 3525 minOccurs = "1"; 3526 maxOccurs = "1"; 3527 } 3528 3529 public String getName() { 3530 return "any"; 3531 } 3532 3533 public String getContentName() { 3534 return null; 3535 } 3536 3537 public String getNamespace() { 3538 return namespace; 3539 } 3540 3541 public void setNamespace(String n) { 3542 namespace = (n == null) ? null : n.intern(); 3543 } 3544 3545 public String getProcessContents() { 3546 return processContents; 3547 } 3548 3549 public void setProcessContents(String pc) { 3550 processContents = (pc == null) ? null : pc.intern(); 3551 } 3552 3553 public void setMinOccurs(String mino) { 3554 if (mino == null) 3555 mino = "1"; 3556 minOccurs = mino.intern(); 3557 } 3558 3559 public void setMaxOccurs(String maxo) { 3560 if (maxo == null) 3561 maxo = "1"; 3562 maxOccurs = maxo.intern(); 3563 } 3564 3565 public String getMinOccurs() { 3566 return minOccurs; 3567 } 3568 3569 public String getMaxOccurs() { 3570 return maxOccurs; 3571 } 3572 3573 public String getAttributeString() { 3574 StringBuffer sb = new StringBuffer (); 3575 if (minOccurs != "1") 3576 sb.append(" minOccurs='"+minOccurs+"'"); 3577 if (maxOccurs != "1") 3578 sb.append(" maxOccurs='"+maxOccurs+"'"); 3579 if (namespace != null) 3580 sb.append(" namespace='"+namespace+"'"); 3581 if (processContents != null) 3582 sb.append(" processContents='"+processContents+"'"); 3583 return sb.toString(); 3584 } 3585 3586 public Map validSubElementTypeMap() { 3587 return anyValidSubElementTypeMap; 3588 } 3589 3590 public boolean writeDTDName(StringBuffer out) { 3591 return false; 3592 } 3593 3594 public void readSchema(org.w3c.dom.Element node) { 3595 String namespace = node.getAttribute("namespace"); String processContents = node.getAttribute("processContents"); String minOccurs = node.getAttribute("minOccurs"); String maxOccurs = node.getAttribute("maxOccurs"); Any el = new Any(); 3600 if (namespace != null && !namespace.equals("")) 3601 el.setNamespace(namespace); 3602 if (processContents != null && !processContents.equals("")) 3603 el.setProcessContents(processContents); 3604 if (minOccurs != null && !minOccurs.equals("")) 3605 el.setMinOccurs(minOccurs); 3606 if (maxOccurs != null && !maxOccurs.equals("")) 3607 el.setMaxOccurs(maxOccurs); 3608 pushCurrent(el); 3609 read(node); 3610 popCurrent(); 3611 } 3612 3613 public boolean equals(Object o) { 3614 if (!(o instanceof Any)) 3615 return false; 3616 Any el = (Any) o; 3617 if (minOccurs != el.minOccurs) 3618 return false; 3619 if (maxOccurs != el.maxOccurs) 3620 return false; 3621 if (namespace != el.namespace) 3622 return false; 3623 if (processContents != el.processContents) 3624 return false; 3625 return super.equals(el); 3626 } 3627 3628 public int hashCode() { 3629 int result = 17; 3630 result = 37*result + ((namespace == null) ? 0 : namespace.hashCode()); 3631 result = 37*result + ((processContents == null) ? 0 : processContents.hashCode()); 3632 result = 37*result + ((minOccurs == null) ? 0 : minOccurs.hashCode()); 3633 result = 37*result + ((maxOccurs == null) ? 0 : maxOccurs.hashCode()); 3634 return result; 3635 } 3636 } 3637 static private Map anyValidSubElementTypeMap = null; 3638 static { 3639 anyValidSubElementTypeMap = new HashMap(); 3640 anyValidSubElementTypeMap.put(Annotation.class, null); 3641 } 3642 3643 public class AnyAttribute extends ContainsSubElements { 3644 private String namespace; 3645 private String processContents; 3646 3647 public AnyAttribute() { 3648 } 3649 3650 public String getName() { 3651 return "anyAttribute"; 3652 } 3653 3654 public String getContentName() { 3655 return null; 3656 } 3657 3658 public String getNamespace() { 3659 return namespace; 3660 } 3661 3662 public void setNamespace(String n) { 3663 namespace = (n == null) ? null : n.intern(); 3664 } 3665 3666 public String getProcessContents() { 3667 return processContents; 3668 } 3669 3670 public void setProcessContents(String pc) { 3671 processContents = (pc == null) ? null : pc.intern(); 3672 } 3673 3674 public String getAttributeString() { 3675 StringBuffer sb = new StringBuffer (); 3676 if (namespace != null) 3677 sb.append(" namespace='"+namespace+"'"); 3678 if (processContents != null) 3679 sb.append(" processContents='"+processContents+"'"); 3680 return sb.toString(); 3681 } 3682 3683 public Map validSubElementTypeMap() { 3684 return anyAttributeValidSubElementTypeMap; 3685 } 3686 3687 public boolean writeDTDName(StringBuffer out) { 3688 return false; 3689 } 3690 3691 public void readSchema(org.w3c.dom.Element node) { 3692 String namespace = node.getAttribute("namespace"); String processContents = node.getAttribute("processContents"); AnyAttribute el = new AnyAttribute(); 3695 if (namespace != null && !namespace.equals("")) 3696 el.setNamespace(namespace); 3697 if (processContents != null && !processContents.equals("")) 3698 el.setProcessContents(processContents); 3699 pushCurrent(el); 3700 read(node); 3701 popCurrent(); 3702 } 3703 3704 public boolean equals(Object o) { 3705 if (!(o instanceof AnyAttribute)) 3706 return false; 3707 AnyAttribute el = (AnyAttribute) o; 3708 if (namespace != el.namespace) 3709 return false; 3710 if (processContents != el.processContents) 3711 return false; 3712 return super.equals(el); 3713 } 3714 3715 public int hashCode() { 3716 int result = 17; 3717 result = 37*result + ((namespace == null) ? 0 : namespace.hashCode()); 3718 result = 37*result + ((processContents == null) ? 0 : processContents.hashCode()); 3719 return result; 3720 } 3721 } 3722 static private Map anyAttributeValidSubElementTypeMap = null; 3723 static { 3724 anyAttributeValidSubElementTypeMap = new HashMap(); 3725 anyAttributeValidSubElementTypeMap.put(Annotation.class, null); 3726 } 3727 3728 public class Attribute extends ContainsSubElements implements CanRef { 3733 private String namespace; 3734 private String name; 3735 private String type; 3736 private String defaultValue; 3737 private String id; 3738 private String ref; 3739 private String fixed; 3740 private String use; 3741 private String arrayType; 3743 public Attribute(String n) { 3744 this(n, targetNamespace); 3745 } 3746 3747 public Attribute(String n, String ns) { 3748 this(n, ns, null); 3749 } 3750 3751 public Attribute(String n, String ns, String type) { 3752 if (n == null || n.equals("")) 3753 name = null; 3754 else 3755 name = n.intern(); 3756 namespace = ns; 3757 setType(type); 3758 } 3759 3760 public String getName() { 3761 return "attribute"; 3762 } 3763 3764 public String getContentName() { 3765 return "@"+name; 3766 } 3767 3768 public String getAttributeName() { 3769 if (namespace == null || namespace.equals(targetNamespace)) { 3770 return name; 3771 } 3772 return getNamespace(namespace)+":"+name; 3773 } 3774 3775 public String getAttributeNamespace() { 3776 return namespace; 3777 } 3778 3779 public String getAttributeNameNoNS() { 3780 return name; 3781 } 3782 3783 private void setType(String ty) { 3784 if (ty == null) { 3785 type = null; 3786 return; 3787 } 3788 type = normalizeDocumentNamespace(ty).intern(); 3789 } 3790 3791 public String getType() { 3792 return type; 3793 } 3794 3795 public String getJavaType() { 3796 if (type == null) 3797 return null; 3798 String javaType = schemaTypeToJavaType(type); 3799 if (false) { 3800 javaType = JavaUtil.toObjectType(javaType); 3801 } 3802 return javaType; 3803 } 3804 3805 public String getFixed() { 3806 return fixed; 3807 } 3808 3809 public boolean isRequired() { 3810 return use == "required"; 3811 } 3812 3813 public boolean isOptional() { 3814 return use == "optional"; 3815 } 3816 3817 public boolean isProhibited() { 3818 return use == "prohibited"; 3819 } 3820 3821 public String getUse() { 3822 return use; 3823 } 3824 3825 public boolean isAttributeNamed(String n) { 3826 n = resolveNamespace(n); 3827 String fullName = resolveNamespace(namespace, name); 3828 return n.equals(fullName); 3829 } 3830 3831 public boolean hasRef() { 3832 return getRef() != null; 3833 } 3834 3835 3838 public String getRef() { 3839 return ref; 3840 } 3841 3842 public Attribute getRefAttribute() { 3843 if (ref == null) 3844 return null; 3845 Object o = definedAttributes.get(ref); 3846 if (o instanceof Attribute) { 3847 Attribute referredAttribute = (Attribute) o; 3848 return referredAttribute; 3849 } else { 3850 throw new IllegalStateException (Common.getMessage("MSG_FailedToFindRef", 3851 ref, 3852 this.toString())); 3853 } 3854 } 3855 3856 public ElementExpr getRefElementExpr() { 3857 return getRefAttribute(); 3858 } 3859 3860 public boolean isDefiningNewType() { 3861 if (ref == null) 3862 return true; 3863 return (subElements.size() >= 1 && type != null); 3864 } 3865 3866 public String getArrayType() { 3867 return arrayType; 3868 } 3869 3870 public String getDefaultValue() { 3871 return defaultValue; 3872 } 3873 3874 public void writeDTD(StringBuffer out) { 3875 writeDTD(out, "UNKNOWN"); 3876 } 3877 3878 public void writeDTD(StringBuffer out, String elementName) { 3879 out.append("<!ATTLIST "+elementName+" "+name+" "); 3880 out.append(">\n"); 3881 } 3882 3883 public boolean writeDTDName(StringBuffer out) { 3884 out.append(name); 3885 return false; 3886 } 3887 3888 public void validate() { 3889 3890 super.validate(); 3891 } 3892 3893 public Map validSubElementTypeMap() { 3894 return attributeValidSubElementTypeMap; 3895 } 3896 3897 public String getAttributeString() { 3898 StringBuffer sb = new StringBuffer (); 3899 if (name != null) 3900 sb.append(" name='"+getAttributeName()+"'"); 3901 if (type != null) 3902 sb.append(" type='"+type+"'"); 3903 if (fixed != null) { 3904 sb.append(" fixed='"); 3905 XMLUtil.printXML(sb, fixed, true); 3906 sb.append("'"); 3907 } 3908 if (ref != null) 3909 sb.append(" ref='"+ref+"'"); 3910 if (id != null) 3911 sb.append(" id='"+id+"'"); 3912 if (defaultValue != null) { 3913 sb.append(" default='"); 3914 XMLUtil.printXML(sb, defaultValue, true); 3915 sb.append("'"); 3916 } 3917 if (use != null) 3918 sb.append(" use='"+use+"'"); 3919 if (arrayType != null) 3920 sb.append(" wsdl:arrayType='"+arrayType+"'"); 3921 return sb.toString(); 3922 } 3923 3924 public void readSchema(org.w3c.dom.Element node) { 3925 String myName = node.getAttribute("name"); String myType = node.getAttribute("type"); String myFixed = node.getAttribute("fixed"); String myRef = node.getAttribute("ref"); String myId = node.getAttribute("id"); String myDefault = node.getAttribute("default"); String myUse = node.getAttribute("use"); String myArrayType = node.getAttributeNS("http://schemas.xmlsoap.org/wsdl/", "arrayType"); 3933 3934 Attribute attr = new Attribute(myName); 3935 if (myType != null && !myType.equals("")) 3936 attr.setType(myType.intern()); 3937 if (myFixed != null && !myFixed.equals("")) 3938 attr.fixed = myFixed.intern(); 3939 if (myRef != null && !myRef.equals("")) 3940 attr.ref = myRef.intern(); 3941 if (myId != null && !myId.equals("")) 3942 attr.id = myId.intern(); 3943 if (myDefault != null && !myDefault.equals("")) 3944 attr.defaultValue = myDefault.intern(); 3945 if (myUse != null && !myUse.equals("")) 3946 attr.use = myUse.intern(); 3947 if (myArrayType != null && !myArrayType.equals("")) 3948 attr.arrayType = myArrayType.intern(); 3949 pushCurrent(attr); 3950 read(node); 3951 popCurrent(); 3952 } 3953 3954 public boolean equals(Object o) { 3955 if (!(o instanceof Attribute)) 3956 return false; 3957 Attribute el = (Attribute) o; 3958 if (name != el.name) 3959 return false; 3960 if (type != el.type) 3961 return false; 3962 if (fixed != el.fixed) 3963 return false; 3964 if (ref != el.ref) 3965 return false; 3966 if (id != el.id) 3967 return false; 3968 if (use != el.use) 3969 return false; 3970 if (arrayType != el.arrayType) 3971 return false; 3972 3973 return super.equals(el); 3974 } 3975 3976 public int hashCode() { 3977 int result = 17; 3978 result = 37*result + ((name == null) ? 0 : name.hashCode()); 3979 result = 37*result + ((type == null) ? 0 : type.hashCode()); 3980 result = 37*result + ((fixed == null) ? 0 : fixed.hashCode()); 3981 result = 37*result + ((ref == null) ? 0 : ref.hashCode()); 3982 result = 37*result + ((id == null) ? 0 : id.hashCode()); 3983 result = 37*result + ((use == null) ? 0 : use.hashCode()); 3984 result = 37*result + ((arrayType == null) ? 0 : arrayType.hashCode()); 3985 result = 37*result + super.hashCode(); 3986 return result; 3987 } 3988 } 3989 static private Map attributeValidSubElementTypeMap = null; 3990 static { 3991 attributeValidSubElementTypeMap = new HashMap(); 3992 attributeValidSubElementTypeMap.put(Annotation.class, null); 3993 attributeValidSubElementTypeMap.put(SimpleType.class, null); 3994 } 3995 3996 3997 public class AttributeGroup extends ContainsSubElements implements CanRef { 4000 private String name; 4001 private String ref; 4002 4003 public AttributeGroup() { 4004 } 4005 4006 4009 public AttributeGroup(String n) { 4010 if (n != null && !n.equals("")) { 4011 name = normalizeTargetNamespace(n).intern(); 4012 putSchemaTypeDef(name, this); 4013 } 4014 } 4015 4016 public Map validSubElementTypeMap() { 4017 return attributeGroupValidSubElementTypeMap; 4018 } 4019 4020 public String getName() { 4021 return "attributeGroup"; 4022 } 4023 4024 public String getContentName() { 4025 return name; 4026 } 4027 4028 public String getGroupName() { 4029 return name; 4030 } 4031 4032 public boolean hasRef() { 4033 return getRef() != null; 4034 } 4035 4036 public String getRef() { 4037 return ref; 4038 } 4039 4040 public AttributeGroup getRefAttributeGroup() { 4041 if (ref == null) 4042 return null; 4043 Object o = getSchemaTypeDef(ref); 4044 if (o instanceof AttributeGroup) { 4045 AttributeGroup referredGroup = (AttributeGroup) o; 4046 return referredGroup; 4047 } else { 4048 throw new IllegalStateException (Common.getMessage("MSG_FailedToFindRef", 4049 ref, 4050 this.toString())); 4051 } 4052 } 4053 4054 public ElementExpr getRefElementExpr() { 4055 return getRefAttributeGroup(); 4056 } 4057 4058 public boolean writeDTDName(StringBuffer out) { 4059 return writeDTDSubElementNames(out, false); 4060 } 4061 4062 public String getAttributeString() { 4063 StringBuffer sb = new StringBuffer (); 4064 if (name != null) 4065 sb.append(" name='"+name+"'"); 4066 if (ref != null) 4067 sb.append(" ref='"+ref+"'"); 4068 return sb.toString(); 4069 } 4070 4071 public void readSchema(org.w3c.dom.Element node) { 4072 String myName = node.getAttribute("name"); String myRef = node.getAttribute("ref"); AttributeGroup ag = new AttributeGroup(myName); 4075 if (myRef != null && !myRef.equals("")) 4076 ag.ref = myRef.intern(); 4077 pushCurrent(ag); 4078 read(node); 4079 popCurrent(); 4080 } 4081 4082 public boolean equals(Object o) { 4083 if (!(o instanceof AttributeGroup)) 4084 return false; 4085 AttributeGroup el = (AttributeGroup) o; 4086 if (name != el.name) 4087 return false; 4088 if (ref != el.ref) 4089 return false; 4090 4091 return super.equals(el); 4092 } 4093 4094 public int hashCode() { 4095 int result = 17; 4096 result = 37*result + ((name == null) ? 0 : name.hashCode()); 4097 result = 37*result + ((ref == null) ? 0 : ref.hashCode()); 4098 result = 37*result + super.hashCode(); 4099 return result; 4100 } 4101 } 4102 static private Map attributeGroupValidSubElementTypeMap = null; 4103 static { 4104 attributeGroupValidSubElementTypeMap = new HashMap(); 4105 attributeGroupValidSubElementTypeMap.put(Annotation.class, null); 4106 attributeGroupValidSubElementTypeMap.put(Attribute.class, null); 4107 attributeGroupValidSubElementTypeMap.put(AttributeGroup.class, null); 4108 attributeGroupValidSubElementTypeMap.put(AnyAttribute.class, null); 4109 } 4110 4111 4113 private class ParserSchemaState { 4114 private String targetNamespace; 4115 private String documentNamespace; 4116 private boolean elementFormQualifiedDefault; 4117 private boolean attributeFormQualifiedDefault; 4118 4119 public ParserSchemaState() { 4120 this.targetNamespace = SchemaRep.this.targetNamespace; 4121 this.documentNamespace = SchemaRep.this.documentNamespace; 4122 this.elementFormQualifiedDefault = SchemaRep.this.elementFormQualifiedDefault; 4123 this.attributeFormQualifiedDefault = SchemaRep.this.attributeFormQualifiedDefault; 4124 } 4125 4126 public void reload() { 4127 SchemaRep.this.targetNamespace = this.targetNamespace; 4128 SchemaRep.this.documentNamespace = this.documentNamespace; 4129 SchemaRep.this.elementFormQualifiedDefault = this.elementFormQualifiedDefault; 4130 SchemaRep.this.attributeFormQualifiedDefault = this.attributeFormQualifiedDefault; 4131 } 4132 } 4133 4134 4136 protected Map namespaceTable; protected String targetNamespace; 4139 protected String documentNamespace; 4140 private boolean elementFormQualifiedDefault; 4141 private boolean attributeFormQualifiedDefault; 4142 protected ContainsSubElements rootElement; 4143 4144 protected Map elementTable; 4148 protected Map optionallyDefinedTypes = null; 4153 protected Map definedTypes; protected Map definedTypesFull; 4158 protected Map definedAttributes; 4160 protected Map predefinedSchemaTypes; 4163 protected Map requiredPredefinedTypes; 4167 protected Stack currentElementStack; 4170 protected Annotation topAnnotation; 4173 4174 protected ElementExpr lastPopped; 4176 4177 protected Map sampleNodes; 4179 4180 private Map schemaTypeDefs; 4183 protected Map includedAlready = new HashMap(); 4186 private String currentParsedURI; 4187 4188 private boolean useBigDataTypes = true; 4189 4190 public SchemaRep() { 4191 init(); 4192 } 4193 4194 public SchemaRep(Document schemaDoc, String uri) { 4195 init(); 4196 currentParsedURI = uri; 4197 readDocument(schemaDoc); 4198 } 4199 4200 public SchemaRep(Document schemaDoc, String uri, boolean useBigDataTypes) { 4201 this.useBigDataTypes = useBigDataTypes; 4202 init(); 4203 currentParsedURI = uri; 4204 readDocument(schemaDoc); 4205 } 4206 4207 private void init() { 4208 targetNamespace = null; 4209 documentNamespace = null; 4210 namespaceTable = new HashMap(); 4211 addToNamespace("xml", "http://www.w3.org/XML/1998/namespace"); 4212 addToNamespace("xsd", XSD_NS); 4213 4214 schemaTypeDefs = new HashMap(); 4215 predefinedSchemaTypes = new HashMap(); 4216 insertPredefinedSchemaTypes(predefinedSchemaTypes); 4217 4218 currentElementStack = new Stack(); 4219 topAnnotation = null; 4220 definedTypes = new HashMap(); 4221 definedTypesFull = new HashMap(); 4222 definedAttributes = new HashMap(); 4223 mapSimpleAttributes(definedAttributes); 4225 requiredPredefinedTypes = new HashMap(); 4226 if (optionallyDefinedTypes == null) { 4227 optionallyDefinedTypes = new HashMap(); 4228 mapSimpleJavaTypesOptional(optionallyDefinedTypes); 4229 } 4230 4231 sampleNodes = new HashMap(); 4233 putSampleNode(new Element("dummy")); putSampleNode(new ComplexType()); 4235 putSampleNode(new SimpleType(null)); putSampleNode(new UnionType(null, null)); putSampleNode(new Restriction()); 4238 putSampleNode(new Sequence()); 4239 putSampleNode(new Choice()); 4240 putSampleNode(new All()); 4241 putSampleNode(new Group()); 4242 putSampleNode(new Annotation()); 4243 putSampleNode(new AppInfo()); 4244 putSampleNode(new Documentation()); 4245 putSampleNode(new Attribute("dummy")); putSampleNode(new AttributeGroup()); 4247 putSampleNode(new MaxExclusive("dummy")); putSampleNode(new MinExclusive("dummy")); putSampleNode(new Enumeration("dummy")); putSampleNode(new Pattern("dummy")); putSampleNode(new MinLength("0")); putSampleNode(new MaxLength("0")); putSampleNode(new TotalDigits("dummy")); putSampleNode(new MinInclusive("dummy")); putSampleNode(new MaxInclusive("dummy")); putSampleNode(new FractionDigits("dummy")); putSampleNode(new Length("dummy")); putSampleNode(new WhiteSpace("dummy")); putSampleNode(new Key()); 4260 putSampleNode(new Unique()); 4261 putSampleNode(new KeyRef()); 4262 putSampleNode(new Selector()); 4263 putSampleNode(new Field()); 4264 putSampleNode(new Include("dummy")); putSampleNode(new Import()); 4266 putSampleNode(new SimpleContent()); 4267 putSampleNode(new ComplexContent()); 4268 putSampleNode(new Extension()); 4269 putSampleNode(new ListElement()); 4270 putSampleNode(new Any()); 4271 putSampleNode(new AnyAttribute()); 4272 } 4273 4274 4275 4278 public void addToNamespace(String ns, String URI) { 4279 namespaceTable.put(ns, URI); 4281 } 4282 4283 4288 public String getNamespaceURI(String ns) { 4289 return (String ) namespaceTable.get(ns); 4290 } 4291 4292 4297 public String getNamespace(String URI) { 4298 String ns; 4300 Iterator it = namespaceTable.keySet().iterator(); 4301 while (it.hasNext()) { 4302 ns = (String ) it.next(); 4303 if (URI.equals(namespaceTable.get(ns))) 4304 return ns; 4305 } 4306 ns = guessPrefix(URI); 4307 String baseNs = ns; 4309 for (int count = 2; namespaceTable.containsKey(ns); ++count) 4310 ns = baseNs+count; 4311 namespaceTable.put(ns, URI); 4312 return ns; 4313 } 4314 4315 4318 public Set getAllNamespaces() { 4319 return namespaceTable.keySet(); 4320 } 4321 4322 public String getXSDNamespace() { 4323 String ns = getNamespace(XSD_NS); 4324 if (ns == null) 4325 return "xsd"; 4326 return ns; 4327 } 4328 4329 public String getXMLNamespace() { 4330 String ns = getNamespace("http://www.w3.org/XML/1998/namespace"); 4331 if (ns == null) 4332 return "xml"; 4333 return ns; 4334 } 4335 4336 public Attribute getAttribute(String name) { 4337 if (name == null) 4338 return null; 4339 String ns = prefixOf(name); 4340 if (ns == null) 4341 name = resolveNamespace(documentNamespace, removePrefix(name)); 4342 else 4343 name = resolveNamespace(ns, removePrefix(name)); 4344 Attribute result = (Attribute) definedAttributes.get(name); 4346 if (result == null && ns == null) { 4347 name = resolveNamespace(targetNamespace, name); 4349 result = (Attribute) definedAttributes.get(name); 4351 } 4352 return result; 4353 } 4354 4355 protected void putSampleNode(ElementExpr ee) { 4356 sampleNodes.put(ee.getName(), ee); 4357 } 4358 4359 protected ElementExpr getSampleNode(String name) { 4360 return (ElementExpr) sampleNodes.get(name); 4361 } 4362 4363 public void setTargetNamespace(String ns) { 4364 targetNamespace = ns; 4365 } 4366 4367 public String getTargetNamespace() { 4368 return targetNamespace; 4369 } 4370 4371 public void setCurrentParsedURI(String uri) { 4372 currentParsedURI = uri; 4373 } 4374 4375 protected String getCurrentParsedURI() { 4376 return currentParsedURI; 4377 } 4378 4379 public void setRootElement(ContainsSubElements el) { 4380 if (debug) 4381 System.out.println("Changing rootElement of "+this+" to "+el); 4382 rootElement = el; 4383 } 4384 4385 public ContainsSubElements getRootElement() { 4386 return rootElement; 4387 } 4388 4389 public void addToTopAnnotation(ElementExpr subElement) { 4390 if (topAnnotation == null) 4391 topAnnotation = new Annotation(); 4392 topAnnotation.addSubElement(subElement); 4393 } 4394 4395 public void addAppInfoToTopAnnotation(String name, String value) { 4396 AppInfo ai = new AppInfo(); 4397 AnyNode ue = new AnyNode(name, value); 4398 ai.addSubElement(ue); 4399 addToTopAnnotation(ai); 4400 } 4401 4402 public void pushCurrent(ElementExpr el) { 4403 if (currentElementStack.empty()) { 4404 setRootElement((ContainsSubElements)el); 4406 } else { 4407 peekCurrentNeedSub().addSubElement(el); 4409 } 4410 currentElementStack.push(el); 4411 } 4412 4413 4417 public void pushElement(String elementName, String elementType) { 4418 pushCurrent(createElement(elementName, elementType)); 4419 } 4420 4421 public void pushSchemaNode() { 4422 pushCurrent(new SchemaNode()); 4423 } 4424 4425 public void pushComplexType() { 4426 pushCurrent(new ComplexType()); 4427 } 4428 4429 public void pushSequence() { 4430 pushCurrent(new Sequence()); 4431 } 4432 4433 public ElementExpr popCurrent() { 4434 lastPopped = (ElementExpr) currentElementStack.pop(); 4435 return lastPopped; 4436 } 4437 4438 public ElementExpr peekCurrent() { 4439 return (ElementExpr) currentElementStack.peek(); 4440 } 4441 4442 4446 private ElementExpr findAncestor(Class type) { 4447 for (int i = currentElementStack.size() - 1; i >= 0; --i) { 4448 ElementExpr ee = (ElementExpr) currentElementStack.get(i); 4449 if (type.isAssignableFrom(ee.getClass())) 4450 return ee; 4451 } 4452 return null; 4453 } 4454 4455 4459 public ContainsSubElements peekCurrentNeedSub() { 4460 if (!(currentElementStack.peek() instanceof ContainsSubElements)) 4461 throw new ClassCastException ("Expected ContainsSubElements, but got "+currentElementStack.peek().getClass()+" instead on object "+currentElementStack.peek()); 4462 return (ContainsSubElements) currentElementStack.peek(); 4463 } 4464 4465 public ElementExpr getLastPopped() { 4466 return lastPopped; 4467 } 4468 4469 4472 public void addElement(String name, String type) { 4473 Element el = createElement(name); 4474 el.setType(type); 4475 if (currentElementStack.empty()) 4476 setRootElement(el); 4477 else 4478 peekCurrentNeedSub().addSubElement(el); 4479 } 4480 4481 4484 public void addAppInfo(String name, String value) { 4485 ElementExpr e = peekCurrent(); 4486 Annotation ann; 4487 if (e instanceof Annotation) 4488 ann = (Annotation) e; 4489 else 4490 ann = new Annotation(); 4491 AppInfo ai = new AppInfo(); 4492 AnyNode ue = new AnyNode(name, value); 4493 ai.addSubElement(ue); 4494 ann.addSubElement(ai); 4495 peekCurrentNeedSub().addSubElement(ann); 4496 } 4497 4498 4502 public void setType(String t) { 4503 ElementExpr e = peekCurrent(); 4504 if (e instanceof Element) 4505 ((Element)e).setType(t); 4506 else 4507 throw new IllegalStateException (Common.getMessage("MSG_TryingToCallOnWrongClass", "setType", e.getClass())); 4508 } 4509 4510 public List findAllSubElements(String name) { 4511 List lst = new LinkedList(); 4512 rootElement.findAllSubElements(name, lst); 4513 return lst; 4514 } 4515 4516 4520 4530 4531 4535 public void setXMLSchemaType(String t) { 4536 ElementExpr e = peekCurrent(); 4537 if (e instanceof Element) 4538 ((Element)e).setXMLSchemaType(t); 4539 else 4540 throw new IllegalStateException (Common.getMessage("MSG_TryingToCallOnWrongClass", "setXMLSchemaType", e.getClass())); 4541 } 4542 4543 4546 public void setMinOccurs(String t) { 4547 ElementExpr e = peekCurrent(); 4548 if (e instanceof Element) 4549 ((Element)e).setMinOccurs(t); 4550 else if (e instanceof ModelGroup) 4551 ((ModelGroup)e).setMinOccurs(t); 4552 else 4553 throw new IllegalStateException (Common.getMessage("MSG_TryingToCallOnWrongClass", "setMinOccurs", e.getClass())); 4554 } 4555 4556 4559 public void setMaxOccurs(String t) { 4560 ElementExpr e = peekCurrent(); 4561 if (e instanceof Element) 4562 ((Element)e).setMaxOccurs(t); 4563 else if (e instanceof ModelGroup) 4564 ((ModelGroup)e).setMaxOccurs(t); 4565 else 4566 throw new IllegalStateException (Common.getMessage("MSG_TryingToCallOnWrongClass", "setMaxOccurs", e.getClass())); 4567 } 4568 4569 4572 public Element createElement(String name) { 4573 Element el = new Element(name); 4574 return el; 4575 } 4576 4577 4580 public Element createElement(String name, String type) { 4581 Element el = new Element(name); 4582 el.setType(type); 4583 return el; 4584 } 4585 4586 4596 4597 public void addSubElement(String elementName, ElementExpr subElement) { 4598 Element el = createElement(elementName); 4599 el.addSubElement(subElement); 4600 } 4601 4602 public void addSubElement(String elementName, List subElements) { 4603 Element el = createElement(elementName); 4604 el.addSubElement(subElements); 4605 } 4606 4607 public void addSubElement(String elementName, String subElementName) { 4608 Element subElement = createElement(subElementName); 4609 addSubElement(elementName, subElement); 4610 } 4611 4612 4616 public void addSubElementCurrent(String subElementName) { 4617 Element subElement = createElement(subElementName); 4618 peekCurrentNeedSub().addSubElement(subElement); 4619 } 4620 4621 4624 public ElementExpr optimize() { 4625 if (rootElement == null) 4626 return null; 4627 return rootElement.optimize(); 4628 } 4629 4630 4633 4645 4646 4650 public void writeDTD(Writer out) throws java.io.IOException { 4651 if (rootElement == null) 4652 return; 4653 elementTable = new HashMap(); 4654 optimize(); 4655 rootElement.validate(); 4656 StringBuffer outBuffer = new StringBuffer (); 4657 rootElement.writeDTD(outBuffer); 4658 out.write(outBuffer.toString()); 4659 elementTable = null; 4660 } 4661 4662 4665 public InputStream dtdInputStream() throws java.io.IOException { 4666 StringWriter strWriter = new StringWriter(); 4667 PrintWriter pw = new PrintWriter(strWriter); 4668 writeDTD(pw); 4669 pw.flush(); 4670 ByteArrayInputStream bais = new ByteArrayInputStream(strWriter.toString().getBytes()); 4671 return bais; 4672 } 4673 4674 4678 public void writeXMLSchemaStandalone(Writer out) throws java.io.IOException { 4679 if (rootElement == null) 4680 return; 4681 XMLWriter xw = new XMLWriter(); 4682 ContainsSubElements realRootElement = rootElement; 4683 if (!(realRootElement instanceof SchemaNode)) { 4684 SchemaNode sn = new SchemaNode(); 4685 sn.addSubElement(realRootElement); 4686 realRootElement = sn; 4687 if (topAnnotation != null) { 4688 sn.addSubElement(topAnnotation); 4689 } 4690 } 4691 4692 if (true || optimize() != null) { 4693 realRootElement.validate(); 4694 realRootElement.writeXMLSchema(xw); 4695 } 4696 xw.writeTo(out); 4697 } 4698 4699 4703 public InputStream xmlSchemaInputStream() throws java.io.IOException { 4704 StringWriter strWriter = new StringWriter(); 4705 PrintWriter pw = new PrintWriter(strWriter); 4706 writeXMLSchemaStandalone(pw); 4707 pw.flush(); 4708 ByteArrayInputStream bais = new ByteArrayInputStream(strWriter.toString().getBytes()); 4709 return bais; 4710 } 4711 4712 public void writeXMLSchema(XMLWriter out) throws IOException { 4713 if (rootElement == null) 4714 return; 4715 rootElement.writeXMLSchema(out); 4716 } 4717 4718 public void readSchemaFromLocation(String schemaLocation) throws IOException, SAXException { 4719 if (debug) 4720 System.out.println("Reading schema from "+schemaLocation); 4721 if (schemaLocation == null || "".equals(schemaLocation)) 4722 return; 4723 4724 String oldParsedURI = currentParsedURI; 4725 try { 4726 javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); 4727 dbf.setNamespaceAware(true); 4728 dbf.setIgnoringComments(true); 4729 dbf.setIgnoringElementContentWhitespace(true); 4730 javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); 4731 Document doc = null; 4732 IOException ioException = null; 4733 SAXException saxException = null; 4734 try { 4735 doc = db.parse(schemaLocation); 4736 currentParsedURI = schemaLocation; 4737 } catch (java.io.FileNotFoundException e) { 4738 ioException = e; 4739 } catch (org.xml.sax.SAXParseException e) { 4740 saxException = e; 4743 } 4744 if (ioException != null || saxException != null) { 4745 if (currentParsedURI != null) { 4747 java.net.URI uri; 4749 try { 4750 uri = new java.net.URI (currentParsedURI); 4751 } catch (java.net.URISyntaxException e) { 4752 uri = new File(currentParsedURI).toURI(); 4753 } 4754 if (debug) 4755 System.out.println("uri="+uri); 4756 java.net.URI schemaLocationURI = uri.resolve(schemaLocation); 4757 currentParsedURI = schemaLocationURI.toString(); 4758 if (debug) 4759 System.out.println("Since the first try failed, now trying to read currentParsedURI:"+ currentParsedURI); 4760 doc = db.parse(currentParsedURI); 4761 ioException = null; 4762 saxException = null; 4763 } 4764 } 4765 if (ioException != null) 4766 throw ioException; 4767 if (saxException != null) 4768 throw saxException; 4769 org.w3c.dom.Element childNode = doc.getDocumentElement(); 4770 if (childNode != null) { 4771 if (!childNode.getLocalName().equals("schema")) throw new IllegalStateException (Common.getMessage("MSG_ExpectedNode", "schema", childNode.getNodeName())); 4773 readSchemaElement(childNode); 4775 } 4776 } catch (javax.xml.parsers.ParserConfigurationException e) { 4777 throw new Schema2BeansRuntimeException(Common.getMessage("MSG_FailedToParse", schemaLocation), e); 4778 } finally { 4779 currentParsedURI = oldParsedURI; 4780 } 4781 } 4782 4783 public void readDocument(Document doc) { 4784 includedAlready.clear(); 4785 org.w3c.dom.Element childNode = doc.getDocumentElement(); 4786 if (childNode != null) { 4787 if (!childNode.getLocalName().equals("schema")) throw new IllegalStateException (Common.getMessage("MSG_ExpectedNode", "schema", childNode.getNodeName())); 4789 pushSchemaNode(); 4790 peekCurrentNeedSub().readSchema(childNode); 4791 readSchemaElement(childNode); 4792 popCurrent(); 4793 } 4794 } 4795 4796 public void readSchemaElement(org.w3c.dom.Element el) { 4797 NamedNodeMap attrs = el.getAttributes(); 4798 String theTargetNamespace = null; 4799 String theDocumentNamespaceURI = null; 4800 for (int i = 0, size = attrs.getLength(); i < size; ++i) { 4801 Node attr = attrs.item(i); 4802 if ("xmlns".equals(attr.getPrefix())) { 4804 addToNamespace(attr.getLocalName(), attr.getNodeValue()); 4805 } else if ("targetNamespace".equals(attr.getNodeName())) { 4806 theTargetNamespace = attr.getNodeValue(); 4807 } else if ("xmlns".equals(attr.getNodeName())) { 4808 theDocumentNamespaceURI = attr.getNodeValue(); 4809 } 4810 } 4811 if (theDocumentNamespaceURI != null) { 4812 documentNamespace = theDocumentNamespaceURI; 4813 } 4815 if (theTargetNamespace != null) { 4816 targetNamespace = theTargetNamespace; 4818 } 4820 ContainsSubElements re = getRootElement(); 4821 if (re instanceof SchemaNode) { 4822 SchemaNode schemaNode = (SchemaNode) re; 4823 schemaNode.merge(el); 4824 } 4825 read(el); 4826 } 4827 4828 protected void read(Node node) { 4829 String nodeName; 4830 boolean ignoreUnknown = false; 4831 boolean keepText = false; 4832 if (peekCurrent() instanceof AnyNode || peekCurrent() instanceof Documentation || peekCurrent() instanceof AppInfo || peekCurrent() instanceof TextNode) { 4833 keepText = true; 4834 ignoreUnknown = true; 4835 } 4836 AnyNode anyNode = new AnyNode("dummy1", "dummy2"); TextNode textNode = new TextNode("dummy"); NodeList children = node.getChildNodes(); 4839 for (int i = 0; i < children.getLength(); ++i) { 4840 Node childNode = children.item(i); 4841 if (childNode instanceof org.w3c.dom.Element ) { 4842 org.w3c.dom.Element childElement = (org.w3c.dom.Element ) childNode; 4843 nodeName = childElement.getLocalName().intern(); 4845 ElementExpr ee = getSampleNode(nodeName); 4847 if (ee == null) { 4848 if (!ignoreUnknown) 4849 System.out.println("SchemaRep Warning: unknown node at "+getXPathExpr(childElement)); 4850 anyNode.readSchema(childElement); 4851 } else { 4852 ee.readSchema(childElement); 4853 } 4854 } else if (keepText && childNode instanceof Text) { 4855 textNode.readSchema((Text)childNode); 4856 } 4857 } 4858 } 4859 4860 static public String getXPathExpr(Node node) { 4861 return getXPathExpr(node, true); 4862 } 4863 4864 static public String getXPathExpr(Node node, boolean addDescription) { 4865 if (node instanceof Document) 4866 return "/"; 4867 Node parentNode = node.getParentNode(); 4868 if (parentNode instanceof Document) 4869 return "/" + node.getNodeName(); 4870 String curNodeName = node.getNodeName(); 4871 if (addDescription && node instanceof org.w3c.dom.Element ) { 4872 String nameAttr = ((org.w3c.dom.Element )node).getAttribute("name"); 4873 if (nameAttr != null && !"".equals(nameAttr)) 4874 curNodeName += " name='"+nameAttr+"'"; 4875 } 4876 return getXPathExpr(node.getParentNode(), addDescription) + "/" + curNodeName; 4877 } 4878 4879 public String schemaTypeToJavaType(String xmlSchemaType) { 4880 ElementExpr schemaTypeDef = getSchemaTypeDef(xmlSchemaType); 4881 if (schemaTypeDef == null) { 4882 System.out.println(Common.getMessage("MSG_UnableToFindTypeDef", xmlSchemaType)); 4883 return null; 4884 } 4885 4886 String javaType = null; 4887 if (schemaTypeDef instanceof HasJavaTypeName) { 4889 javaType = ((SchemaRep.HasJavaTypeName)schemaTypeDef).getJavaTypeName(); 4890 } else { 4891 System.out.println("!!! What's the java type of "+schemaTypeDef+" for "+xmlSchemaType); 4892 } 4893 return javaType; 4895 } 4896 4897 4904 public ElementExpr getSchemaTypeDef(String origTypeName) { 4905 String typeName = origTypeName; 4906 if (typeName == null) 4908 return null; 4909 typeName = resolveNamespace(typeName); 4910 ElementExpr result = (ElementExpr) schemaTypeDefs.get(typeName); 4911 if (result != null) 4912 return result; 4913 String ns = prefixOf(origTypeName); 4914 if (ns == null) 4915 typeName = canonicalQName(documentNamespace, 4916 removePrefix(origTypeName)); 4917 result = (ElementExpr) schemaTypeDefs.get(typeName); 4918 if (result != null) 4919 return result; 4920 4928 return (ElementExpr) definedTypes.get(origTypeName); 4929 } 4930 4931 4934 public ElementExpr getSchemaTypeDefResolvedNamespace(String typeName) { 4935 ElementExpr result = (ElementExpr) schemaTypeDefs.get(typeName); 4936 if (result == null) { 4937 result = (ElementExpr) definedTypes.get(typeName); 4938 } 4939 return result; 4940 } 4941 4942 4946 public Set getSchemaTypeNames() { 4947 return schemaTypeDefs.keySet(); 4948 } 4949 4950 4951 public Element getDefinedElement(String typeName) { 4952 String t = resolveNamespace(typeName); 4953 return getDefinedElementResolvedNamespace(t); 4954 } 4955 4956 4960 public Element getDefinedElementResolvedNamespace(String typeName) { 4961 return (Element) definedTypesFull.get(typeName); 4962 } 4963 4964 4968 public Set getDefinedElementNames() { 4969 return definedTypesFull.keySet(); 4970 } 4971 4972 public void putSchemaTypeDef(String typeName, ElementExpr ee) { 4973 if (typeName == null) 4974 return; 4975 String ns = prefixOf(typeName); 4976 if (ns == null) 4977 typeName = resolveNamespace(targetNamespace, removePrefix(typeName)); 4978 else 4979 typeName = resolveNamespace(ns, removePrefix(typeName)); 4980 schemaTypeDefs.put(typeName, ee); 4982 } 4983 4984 4989 public String resolveNamespace(String typeName) { 4990 return resolveNamespaceDefault(typeName, targetNamespace); 4991 } 4992 4993 protected String resolveNamespaceDefault(String typeName, String defaultNS) { 4994 if (typeName == null) 4995 return null; 4996 String prefix = prefixOf(typeName); 4997 typeName = resolveNamespaceDefault(prefix, defaultNS, removePrefix(typeName)); 4999 return typeName; 5001 } 5002 5003 public String resolveNamespace(String ns, String type) { 5004 return resolveNamespaceDefault(ns, targetNamespace, type); 5005 } 5006 5007 protected String resolveNamespaceDefault(String prefix, String defaultNS, String type) { 5008 String uri; 5009 if (prefix == null) { 5010 if (defaultNS == null) 5011 return type; 5012 uri = defaultNS; 5013 } else { 5014 uri = getNamespaceURI(prefix); 5015 if (uri == null) { 5016 System.out.println("resolveNamespace: Namespace prefix '"+prefix+"' was not found (type="+type+")."); 5017 new Exception ().printStackTrace(); 5018 } 5019 } 5020 return canonicalQName(uri, type); 5021 } 5022 5023 public static String canonicalQName(String namespaceURI, String localPart) { 5024 if (namespaceURI == null || namespaceURI.equals("")) 5025 return localPart; 5026 return "{" + namespaceURI + "}" + localPart; 5027 } 5028 5029 5035 protected String normalizeTargetNamespace(String type) { 5036 if (type == null) 5037 return null; 5038 if (targetNamespace == null) 5039 return type; 5040 if (type.indexOf(':') >= 0) 5041 return type; 5042 return normalizeNamespace(targetNamespace, type); 5043 } 5044 5045 5051 protected String normalizeNamespace(String namespaceURI, String localPart) { 5052 if (localPart == null) 5053 return null; 5054 if (namespaceURI == null || "".equals(namespaceURI)) 5055 return localPart; 5056 String prefix = getNamespace(namespaceURI); 5057 return prefix + ":" + localPart; 5058 } 5059 5060 protected String normalizeDocumentNamespace(String type) { 5061 if (type == null) 5062 return null; 5063 if (documentNamespace == null) 5064 return type; 5065 if (type.indexOf(':') >= 0) 5066 return type; 5067 return normalizeNamespace(documentNamespace, type); 5068 } 5069 5070 5074 public void setSchemaTypeMapping(String schemaTypeNamespace, 5075 String schemaTypeName, String javaType) { 5076 String ns; 5078 SimpleType st; 5079 if (schemaTypeNamespace == null) { 5080 st = new SimpleType(schemaTypeName, javaType); 5081 } else { 5082 ns = getNamespace(schemaTypeNamespace); 5083 st = new SimpleType(ns+":"+schemaTypeName, javaType); 5084 } 5085 } 5086 5087 5090 public void setSchemaTypesForME(boolean isFloatingPoint) { 5091 setSchemaTypeMapping(XSD_NS, "anyURI", "java.lang.String"); 5092 setSchemaTypeMapping(XSD_NS, "decimal", isFloatingPoint ? "double" : "java.lang.String"); 5093 setSchemaTypeMapping(XSD_NS, "integer", "long"); 5094 if (!isFloatingPoint) { 5095 setSchemaTypeMapping(XSD_NS, "double", "java.lang.String"); 5096 setSchemaTypeMapping(XSD_NS, "float", "java.lang.String"); 5097 } 5098 } 5099 5100 private void insertPredefinedSchemaTypes(Map st) { 5101 String namespace = getXSDNamespace()+":"; 5103 String [] types = new String [] {"java.lang.String", "java.util.Calendar", "java.util.Calendar", 5104 "long", "int", "char", "short", "double", "float", "byte", "boolean", 5105 "java.lang.String", "long", "long", "long", "long", "long", "int", "short", 5106 "byte", "java.net.URI", "javax.xml.namespace.QName", "java.lang.String", "java.lang.String", 5107 "java.lang.String", "java.lang.String", "java.lang.String", 5108 "java.lang.String", "java.lang.String", "int", "java.lang.String", "java.lang.String", 5109 "java.lang.String", "java.lang.String"}; 5110 String [] schemaType = new String [] {"string", "dateTime", "date", 5111 "long", "int", "char", "short", "double", "float", "byte", "boolean", 5112 "NMTOKEN", "positiveInteger", "nonNegativeInteger", "nonPositiveInteger", "negativeInteger", "unsignedLong", "unsignedInt", "unsignedShort", 5113 "unsignedByte", "anyURI", "QName", "NCName", "Name", 5114 "duration", "time", "ID", 5115 "token", "normalizedString", "gYear", "gYearMonth", "gMonthDay", 5116 "gDay", "gMonth"}; 5117 for (int i = 0; i < schemaType.length; ++i) 5118 st.put(schemaType[i], new SimpleType(namespace+schemaType[i], types[i])); 5119 if (useBigDataTypes) { 5120 st.put("decimal", new SimpleType(namespace+"decimal", "java.math.BigDecimal")); 5121 st.put("integer", new SimpleType(namespace+"integer", "java.math.BigInteger")); 5122 } else { 5123 st.put("decimal", new SimpleType(namespace+"decimal", "double")); 5124 st.put("integer", new SimpleType(namespace+"integer", "long")); 5125 } 5126 String [] whiteSpaceReplace = 5127 new String [] {"normalizedString"}; 5128 for (int i = 0; i < whiteSpaceReplace.length; ++i) { 5129 SimpleType t = (SimpleType) st.get(whiteSpaceReplace[i]); 5130 Restriction restrict = new Restriction(namespace+"string"); 5131 t.addSubElement(restrict); 5132 WhiteSpace ws = new WhiteSpace("replace"); 5133 restrict.addSubElement(ws); 5134 } 5135 String [] whiteSpaceCollapse = 5136 new String [] {"token", "NMTOKEN", "Name", "NCName", "ID"}; 5137 for (int i = 0; i < whiteSpaceCollapse.length; ++i) { 5138 SimpleType t = (SimpleType) st.get(whiteSpaceCollapse[i]); 5139 Restriction restrict = new Restriction(namespace+"normalizedString"); 5140 t.addSubElement(restrict); 5141 WhiteSpace ws = new WhiteSpace("collapse"); 5142 restrict.addSubElement(ws); 5143 } 5144 addRestriction((SimpleType) st.get("nonPositiveInteger"), namespace+"integer", new MaxInclusive("0")); 5145 addRestriction((SimpleType) st.get("negativeInteger"), namespace+"nonPositiveInteger", new MaxExclusive("0")); 5146 addRestriction((SimpleType) st.get("nonNegativeInteger"), namespace+"integer", new MinInclusive("0")); 5147 addRestriction((SimpleType) st.get("unsignedLong"), namespace+"nonNegativeInteger", new MinInclusive("0")); 5148 addRestriction((SimpleType) st.get("unsignedInt"), namespace+"unsignedLong", new MinInclusive("0")); 5149 addRestriction((SimpleType) st.get("positiveInteger"), namespace+"nonNegativeInteger", new MinExclusive("0")); 5150 5151 st.put("hexBinary", new HexBinary()); 5152 st.put("base64Binary", new Base64Binary()); 5153 } 5154 5155 protected void addRestriction(ContainsSubElements cse, 5156 String restrictionBase, RestrictionType rt) { 5157 Restriction restrict = new Restriction(restrictionBase); 5158 cse.addSubElement(restrict); 5159 restrict.addSubElement(rt); 5160 } 5161 5162 5165 public void mapSimpleJavaTypesOptional(Map optionalElementMap) { 5166 String namespace = getXSDNamespace()+":"; 5168 String [] types = new String [] {"java.lang.Integer", "java.lang.Short", "java.lang.Long", "java.lang.Double", "java.lang.Float", "java.lang.Boolean", "java.lang.Character", "java.lang.StringBuffer", "java.lang.Byte", "java.math.BigInteger", "char[]", "char"}; 5171 String [] restrictions = new String [] {"int", "short", "long", "double", "float", "boolean", "string", "string", "byte", "integer", "string", "string"}; 5172 for (int i = 0; i < types.length; ++i) 5173 optionalElementMap.put(types[i], new SimpleType(javaType2XMLSchemaType(types[i]), new Restriction(namespace+restrictions[i]))); 5174 } 5175 5176 private void mapSimpleAttributes(Map a) { 5177 a.put("xml:lang", new Attribute("lang", getNamespaceURI("xml"), 5178 "xsd:string")); 5179 } 5180 5181 5184 public void mapSimpleJavaTypesPredefined(Map definedElementMap) { 5185 String namespace = getXSDNamespace()+":"; 5187 String [] types = new String [] {"java.lang.String", "String", "java.math.BigDecimal", "java.util.Calendar", "long", "int", "char", "short", "double", "float", "byte", "boolean"}; 5190 String [] schemaType = new String [] {"string", "string", "decimal", "dateTime", "long", "int", "char", "short", "double", "float", "byte", "boolean"}; 5191 for (int i = 0; i < types.length; ++i) { 5192 ElementExpr def = getSchemaTypeDef(schemaType[i]); 5193 if (def == null) 5194 def = new SimpleType(namespace+schemaType[i], types[i]); 5195 definedElementMap.put(types[i], def); 5196 } 5197 } 5198 5199 5204 public static void printExtraJavaTypes(Writer out, int level) throws java.io.IOException { 5205 SchemaRep schema = new SchemaRep(); 5209 Map elementMap = new HashMap(); 5210 schema.mapSimpleJavaTypesOptional(elementMap); 5211 Iterator it = elementMap.keySet().iterator(); 5212 XMLWriter xw = new XMLWriter(false); 5213 while (it.hasNext()) { 5214 Object key = it.next(); 5215 ElementExpr el = (ElementExpr) elementMap.get(key); 5216 el.writeXMLSchema(xw); 5217 } 5218 xw.writeTo(out); 5219 out.write("\n"); } 5221 5222 private static String javaType2XMLSchemaType(String typeName) { 5223 return javaType2XMLSchemaType(typeName, false); 5224 } 5225 5229 private static String javaType2XMLSchemaType(String typeName, 5230 boolean unknownOkay) { 5231 if ("java.lang.String".equals(typeName) || "String".equals(typeName)) 5233 return "xsd:string"; 5234 if ("java.lang.Integer".equals(typeName) || 5235 "java.lang.Short".equals(typeName) || 5236 "java.lang.Long".equals(typeName) || 5237 "java.lang.Double".equals(typeName) || 5238 "java.lang.Boolean".equals(typeName) || 5239 "java.lang.Character".equals(typeName) || 5240 "java.lang.Float".equals(typeName) || 5241 "java.lang.StringBuffer".equals(typeName) || 5242 "java.lang.Byte".equals(typeName) || 5243 "java.math.BigInteger".equals(typeName) || 5244 "char".equals(typeName)) 5245 return typeName; 5246 if ("char[]".equals(typeName)) 5247 return "char_lb_rb"; 5248 if (JavaUtil.isPrimitiveType(typeName)) 5249 return "xsd:"+typeName; 5250 if ("java.math.BigDecimal".equals(typeName)) 5251 return "xsd:decimal"; 5252 if ("java.util.Calendar".equals(typeName)) 5253 return "xsd:dateTime"; 5254 if ("java.net.URI".equals(typeName)) 5255 return "xsd:anyURI"; 5256 if ("javax.xml.namespace.QName".equals(typeName)) 5257 return "xsd:QName"; 5258 if (!unknownOkay) { 5259 String err = "javaType2XMLSchemaType: No known XML Schema type for '"+typeName+"'."; 5260 System.out.println(err); 5261 return typeName; 5262 } 5263 return null; 5264 } 5266 5267 5272 private static boolean isXMLSchemaDefinedType(String typeName) { 5273 if (javaType2XMLSchemaType(typeName, true) == null) 5274 return false; 5275 return true; 5276 } 5277 5278 public String javaType2XMLSchemaTypeComplex(String type) { 5279 Object el; 5281 el = definedTypes.get(type); 5282 if (el == null) 5283 el = optionallyDefinedTypes.get(type); 5284 if (el == null) 5285 el = predefinedSchemaTypes.get(type); 5286 if (el == null) { 5287 if (debug) 5288 System.out.println("No type found for "+type); 5289 return javaType2XMLSchemaType(type); 5290 } 5291 if (el instanceof SimpleType) 5292 return ((SimpleType)el).getTypeName(); 5293 if (el instanceof String ) 5294 return (String ) el; 5295 if (el instanceof Element) 5296 return ((Element)el).getElementName(); 5297 return el.getClass().toString(); 5298 } 5299 5300 public boolean isDefinedType(String type) { 5301 if (optionallyDefinedTypes.containsKey(type)) 5302 return true; 5303 if (definedTypes.containsKey(type)) 5304 return true; 5305 return false; 5306 } 5307 5308 5312 public static String removePrefix(String typeName) { 5313 int pos = typeName.indexOf(':'); 5314 if (pos < 0) 5315 return typeName; 5316 return typeName.substring(pos+1); 5317 } 5318 5319 5322 public static String prefixOf(String typeName) { 5323 int pos = typeName.indexOf(':'); 5324 if (pos < 0) 5325 return null; 5326 return typeName.substring(0, pos); 5327 } 5328 5329 5332 public static String guessPrefix(String namespaceURI) { 5333 if ("http://www.w3.org/XML/1998/namespace".equals(namespaceURI)) 5334 return "xml"; 5335 String prefix; 5336 int pos = namespaceURI.lastIndexOf('/'); 5337 if (pos >= 0) 5338 prefix = namespaceURI.substring(pos+1, namespaceURI.length()); 5339 else 5340 prefix = namespaceURI; 5341 prefix = prefix.replace('#', '_').replace('\'', '_').replace('"', '_'). 5342 replace(':', '_'); 5343 if (prefix.startsWith("xml")) 5345 prefix = "x"+prefix; 5346 return prefix; 5347 } 5348 5349 5354 public String guessPrefixFromURI(String uri) { 5355 String prefix = getNamespace(uri); 5356 if (prefix != null) 5357 return prefix; 5358 return guessPrefix(uri); 5359 } 5360} 5361 | Popular Tags |