1 22 23 package org.xquark.schema.loader; 24 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.util.HashMap ; 28 import java.util.StringTokenizer ; 29 30 import org.xml.sax.*; 31 import org.xquark.schema.*; 32 33 public class Loader extends org.xquark.util.SAXLoader 34 implements SchemaConstants, ErrorHandler { 35 private static final String RCSRevision = "$Revision: 1.1 $"; 36 private static final String RCSName = "$Name: $"; 37 38 private Schema schema = null; 39 private boolean topLevel = false; 40 private SchemaManager manager; 41 private SchemaLocator locator; 42 private boolean noIncludeTargetNamespace = false; 43 44 private HashMap componentContextLocators = new HashMap (); 45 46 public Loader(SchemaManager manager) { 47 this(manager, manager, null); 48 } 49 50 public Loader(SchemaManager manager, SchemaLocator locator) { 51 this(manager, locator, null); 52 } 53 54 public Loader(SchemaManager manager, SchemaLocator locator, Schema schema) { 55 super(null); 56 this.manager = manager; 57 this.locator = locator; 58 this.schema = schema; 59 if (schema == null) topLevel = true; 60 setElementHandler(new SchemaHandler()); 61 } 62 63 public Schema getSchema() throws SAXException { 64 return schema; 65 } 66 67 public String getDocumentBase() { 68 if (getDocumentLocator() != null) 69 return getDocumentLocator().getSystemId(); 70 return null; 71 } 72 73 public Schema load(URL url) throws java.io.IOException , SAXException { 74 org.xml.sax.InputSource source = new org.xml.sax.InputSource (url.openStream()); 75 source.setSystemId(url.toString()); 76 return load(source); 77 } 78 79 public Schema load(org.xml.sax.InputSource source) throws SAXException, java.io.IOException { 80 org.xml.sax.XMLReader reader = locator.getSchemaReader(); 81 reader.setContentHandler(this); 82 reader.setErrorHandler(this); 83 reader.parse(source); 84 return schema; 85 } 86 87 private void setComponentContextLocators(HashMap map) { 88 componentContextLocators = map; 89 } 90 91 private Schema resolvePrefix(String qname) 92 throws SchemaException { 93 String prefix = ""; 94 int index = qname.indexOf(':'); 95 if (index > 0) prefix = qname.substring(0, index); 96 String ns = getPrefixMapping(prefix); 97 if (ns == null && !prefix.equals("")) { 98 throw new SchemaException("src-qname.1.1"); 100 } 101 102 if ((ns == null && schema.getNamespace() == null) 103 || (ns != null && ns.equals(schema.getNamespace()))) 104 return schema; 105 if ( ns == null && noIncludeTargetNamespace ) return schema; 107 if (schema.isImported(ns)) 108 return manager.getSchema(ns); 109 110 throw new SchemaException("src-resolve.4"); 112 } 113 114 private void initialize() throws SAXException { 116 try { 117 InitializationVisitor initializer = new InitializationVisitor(schema, componentContextLocators, this); 118 119 java.util.Iterator it = schema.getTypes().iterator(); 120 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 121 122 it = schema.getElementDeclarations().iterator(); 123 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 124 125 it = schema.getAttributeDeclarations().iterator(); 126 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 127 128 it = schema.getAttributeGroupDefinitions().iterator(); 129 while (it.hasNext()) ((SchemaVisitable)it.next()).accept(initializer); 130 } 131 catch ( SchemaException se ) { 132 Object invalidObject = se.getInvalidObject(); 133 Locator componentLocator = (Locator)componentContextLocators.get(invalidObject); 134 throw new SAXParseException(se.getMessage(), componentLocator, se); 135 } 136 } 137 138 public void addComp2ComponentContextLocators(Object newComp, Object oldComp) { 139 Object componentLocator = componentContextLocators.get(oldComp); 140 if ( componentLocator != null ) 141 componentContextLocators.put(newComp, componentLocator); 142 } 143 144 public org.xml.sax.Locator getComponentLocaltor(Object obj) { 145 return (org.xml.sax.Locator )componentContextLocators.get(obj); 146 } 147 148 public boolean isSchemaNamespace(String uri) { 149 return uri.equals(XMLSCHEMA_URI); 150 } 151 152 private String getLocalName(String qname) { 153 int index = qname.indexOf(':'); 154 if (index > 0) return qname.substring(index+1); 155 else return qname; 156 } 157 158 private Loader getLoader() { 159 return this; 160 } 161 162 public Type getType(String ref) throws SchemaException { 163 if (ref == null) return null; 164 165 TypeRef typeRef = new TypeRef(resolvePrefix(ref), getLocalName(ref)); 166 167 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 169 componentContextLocators.put(typeRef, contextLoc); 170 171 return typeRef; 172 } 173 174 public SchemaComponentRef getRef(String ref, int type) throws SchemaException { 175 if (ref == null) return null; 176 177 SchemaComponentRef compRef = new SchemaComponentRef(resolvePrefix(ref), getLocalName(ref), type); 178 179 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 181 componentContextLocators.put(compRef, contextLoc); 182 183 return compRef; 184 } 185 186 public Facet buildFacet(String localName, Attributes atts) { 187 Facet result = null; 188 for (int i = 0; i < FACETS.length; i++) { 189 if (FACETS[i].equals(localName)) { 190 result = new Facet(localName, atts.getValue("", VALUE_ATTR), atts.getValue("", FIXED_ATTR)); 191 break; 192 } 193 } 194 195 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 197 componentContextLocators.put(result, contextLoc); 198 199 return result; 200 } 201 202 public NotationDeclaration buildNotation(Attributes atts) { 203 String name = atts.getValue("", NAME_ATTR); 204 String publicId = atts.getValue("", PUBLIC_ATTR); 205 String systemId = atts.getValue("", SYSTEM_ATTR); 206 NotationDeclaration result = new NotationDeclaration(schema, name, publicId, systemId); 207 208 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 210 componentContextLocators.put(result, contextLoc); 211 212 return result; 213 } 214 215 private int getMethod(String method, int mask) { 216 StringTokenizer st = new StringTokenizer (method); 217 int result = 0; 218 while (st.hasMoreTokens()) { 219 String tok = st.nextToken(); 220 if ((LIST & mask) != 0 && LIST_VALUE.equals(tok)) 221 result |= LIST; 222 else if ((UNION & mask) != 0 && UNION_VALUE.equals(tok)) 223 result |= UNION; 224 else if ((RESTRICTION & mask) != 0 && RESTRICTION_VALUE.equals(tok)) 225 result |= RESTRICTION; 226 else if ((EXTENSION & mask) != 0 && EXTENSION_VALUE.equals(tok)) 227 result |= EXTENSION; 228 else if ((SUBSTITUTION & mask) != 0 && SUBSTITUTION_VALUE.equals(tok)) 229 result |= SUBSTITUTION; 230 else if (ALL_VALUE.equals(tok)) 231 return mask; 232 } 233 return result; 234 } 235 236 private void setOccurrence(Particle particle, Attributes atts) 237 throws SchemaException { 238 String minOccurs = atts.getValue("", MIN_OCCURS_ATTR); 239 if (minOccurs != null) { 240 try { 241 particle.setMinOccurs(Integer.parseInt(minOccurs)); 242 } 243 catch (NumberFormatException ex) { 244 } 246 } 247 248 String maxOccurs = atts.getValue("", MAX_OCCURS_ATTR); 249 if (maxOccurs != null) { 250 if (maxOccurs.equals(UNBOUNDED_VALUE)) { 251 particle.setMaxOccursUnbounded(); 252 } 253 else { 254 try { 255 particle.setMaxOccurs(Integer.parseInt(maxOccurs)); 256 } 257 catch (NumberFormatException ex) { 258 } 260 } 261 } 262 263 int nMin = particle.getMinOccurs(); 265 int nMax = particle.getMaxOccurs(); 266 267 if ( nMax < nMin ) { 268 throw new SchemaException("p-props-correct.2.1"); 270 } 271 } 272 273 public Particle buildParticle(String localName, SchemaScope scope, Attributes atts) 274 throws SchemaException { 275 Particle result = null; 276 if (localName.equals(ELEMENT_TAG)) { 277 result = new Particle(buildElementDeclaration(atts, scope)); 278 } 279 else if (localName.equals(GROUP_TAG)) { 280 String refName = atts.getValue("", REF_ATTR); 281 result = new Particle(getRef(refName, Schema.MODEL_GROUP_DEFINITION)); 282 } 283 else if (localName.equals(SEQUENCE_TAG)) { 284 result = new Particle(new SequenceModelGroup()); 285 } 286 else if (localName.equals(CHOICE_TAG)) { 287 result = new Particle(new ChoiceModelGroup()); 288 } 289 else if (localName.equals(ALL_TAG)) { 290 result = new Particle(new AllModelGroup()); 291 } 292 else if (localName.equals(ANY_TAG)) { 293 result = new Particle(buildWildcard(atts)); 294 } 295 296 if (result != null) { 297 setOccurrence(result, atts); 298 } 299 300 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 302 componentContextLocators.put(result, contextLoc); 303 304 return result; 305 } 306 307 public Wildcard buildWildcard(Attributes atts) 308 throws SchemaException { 309 Wildcard result = new Wildcard(schema, true); 310 String namespaces = atts.getValue("", NAMESPACE_ATTR); 311 if (namespaces != null) { 312 if (OTHER_VALUE.equals(namespaces)) { 313 result.exclude(schema.getNamespace()); 314 } else if (!ANY_VALUE.equals(namespaces)) { 315 StringTokenizer st = new StringTokenizer (namespaces); 316 while (st.hasMoreTokens()) { 317 String tok = st.nextToken(); 318 if (TARGET_NAMESPACE_VALUE.equals(tok)) { 319 result.add(schema.getNamespace()); 320 } else if (LOCAL_VALUE.equals(tok)) { 321 result.add(null); 322 } else { 323 result.add(tok); 324 } 325 } 326 } 327 } 328 String process = atts.getValue("", PROCESS_CONTENTS_ATTR); 329 if (process != null) { 330 if (LAX_VALUE.equals(process)) result.setProcessContents(LAX); 331 else if (SKIP_VALUE.equals(process)) result.setProcessContents(SKIP); 332 else if (STRICT_VALUE.equals(process)) result.setProcessContents(STRICT); 333 } 334 335 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 337 componentContextLocators.put(result, contextLoc); 338 339 return result; 340 } 341 342 private void initSchema(Attributes atts) 344 throws SchemaException { 345 String targetNamespace = atts.getValue("", TARGET_NAMESPACE_ATTR); 346 if ( XSI_URI.equals(targetNamespace) && 347 getDocumentBase() != null && 348 !getDocumentBase().endsWith("XMLSchema-instance.xsd")) { 349 throw new SchemaException("no-xsi"); 350 } 351 if (schema == null) { 352 String schemaLocation = null; 353 if (getDocumentLocator() != null) 354 schemaLocation = getDocumentLocator().getSystemId(); 355 schema = new Schema(targetNamespace, schemaLocation, manager); 356 } else if (targetNamespace != null && !targetNamespace.equals(schema.getNamespace())) { 357 throw new SchemaException("src-include.2.1"); 359 } else if ( targetNamespace == null ) { 360 noIncludeTargetNamespace = true; 361 } 362 363 schema.setDefault(ATTRIBUTE_FORM_DEFAULT_ATTR, 364 atts.getValue("", ATTRIBUTE_FORM_DEFAULT_ATTR)); 365 schema.setDefault(BLOCK_DEFAULT_ATTR, 366 atts.getValue("", BLOCK_DEFAULT_ATTR)); 367 schema.setDefault(ELEMENT_FORM_DEFAULT_ATTR, 368 atts.getValue("", ELEMENT_FORM_DEFAULT_ATTR)); 369 schema.setDefault(FINAL_DEFAULT_ATTR, 370 atts.getValue("", FINAL_DEFAULT_ATTR)); 371 372 schema.setPrefixMap(getElementPrefixMap()); 373 } 374 375 private URL resolveLocation(String location, String base) throws SAXException { 376 URL context = null; 377 if (base != null) { 378 try { 379 context = new URL (base); 380 } 381 catch (MalformedURLException ex0) { 382 } 385 } 386 387 try { 388 return new URL (context, location); 389 } 390 catch (MalformedURLException ex1) { 391 try { 392 java.io.File file = new java.io.File (location); 393 return file.toURL(); 394 } 395 catch (MalformedURLException ex2) { 396 } 399 } 400 401 return null; 402 } 403 404 private void includeSchema(Attributes atts) 405 throws SAXException { 406 String location = atts.getValue("", SCHEMA_LOCATION_ATTR); 407 location = locator.resolveInclude(location); 408 String schemaLocation = null; 409 if (getDocumentLocator() != null) 410 schemaLocation = getDocumentLocator().getSystemId(); 411 412 URL url = null; 413 url = resolveLocation(location, schemaLocation); 414 if (url == null) 415 reportLoadingError("Could not resolve location "+location, new SchemaException("schema_reference")); 416 location = url.toString(); 417 if (!schema.isIncluded(location)) { 418 schema.addIncludedLocation(location); 419 schema.pushDefaults(); 420 Loader loader = new Loader(manager, locator, schema); 421 loader.setComponentContextLocators(componentContextLocators); 422 try { 423 loader.load(url); 424 } catch ( java.io.IOException e) { 425 reportLoadingError(new SchemaException("src-include.1")); 428 } 429 schema.popDefaults(); 430 } 431 } 432 433 private void importSchema(Attributes atts) throws SAXException { 434 String location = atts.getValue("", SCHEMA_LOCATION_ATTR); 435 String schemaLocation = null; 436 if (getDocumentLocator() != null) 437 schemaLocation = getDocumentLocator().getSystemId(); 438 String ns = atts.getValue("", NAMESPACE_ATTR); 439 if (!schema.isImported(ns)) { 440 Schema imported = null; 441 imported = manager.getSchema(ns); 442 if (imported == null) { 443 if (location != null) { 445 URL url = null; 446 url = resolveLocation(location, schemaLocation); 447 if (url != null) { 448 imported = manager.loadSchema(locator, ns, url); 449 } 450 if (imported == null) imported = manager.loadSchema(locator, ns); 451 } 452 else { 453 URL url = null; 454 url = resolveLocation(ns, schemaLocation); 455 if (url != null) { 456 imported = manager.loadSchema(locator, ns, url); 457 } 458 if (imported == null) imported = manager.loadSchema(locator, ns); 459 } 460 } 461 if (imported == null) { 462 reportLoadingError(new SchemaException("src-import")); 464 } 465 schema.addImportedNamespace(ns); 466 } 467 } 468 469 public SimpleType buildSimpleType(Attributes atts) 475 throws SchemaException { 476 String strName = atts.getValue("", NAME_ATTR); 477 String strId = atts.getValue("", ID_ATTR); 478 479 SimpleType aType = new SimpleType(schema, strName, null); 480 String fin = atts.getValue("", FINAL_ATTR); 481 if ( fin == null ) fin = schema.getDefault(FINAL_DEFAULT_ATTR); 482 if ( fin != null ) 483 aType.setFinal(getMethod(fin, RESTRICTION | EXTENSION | LIST | UNION)); 484 485 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 487 componentContextLocators.put(aType, contextLoc); 488 489 return aType; 490 } 491 492 public ComplexType buildComplexType(Attributes atts) 500 throws SchemaException { 501 String name = atts.getValue("", NAME_ATTR); 502 ComplexType type = new ComplexType(schema, name, null); 503 504 String booleanValue = atts.getValue("", ABSTRACT_ATTR); 505 if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) ) 506 type.setAbstract(true); 507 508 booleanValue = atts.getValue("", MIXED_VALUE); 513 if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) ) 514 type.setContentModel(new ContentModel(MIXED)); 515 516 String block = atts.getValue("", BLOCK_ATTR); 517 if ( block == null ) block = schema.getDefault(BLOCK_DEFAULT_ATTR); 518 if (block != null ) type.setBlock(getMethod(block, RESTRICTION | EXTENSION)); 519 520 String fin = atts.getValue("", FINAL_ATTR); 521 if ( fin == null ) fin = schema.getDefault(FINAL_DEFAULT_ATTR); 522 if ( fin != null ) 523 type.setFinal(getMethod(fin, RESTRICTION | EXTENSION)); 524 525 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 527 componentContextLocators.put(type, contextLoc); 528 529 return type; 530 } 531 532 public ElementDeclaration buildElementDeclaration(Attributes atts, SchemaScope scope) 536 throws SchemaException { 537 538 boolean global = (scope != null && scope.isGlobalScope()); 539 String name = atts.getValue("", NAME_ATTR); 540 String ref = atts.getValue("", REF_ATTR); 541 ElementDeclaration decl = null; 542 if (name != null) { 543 if ( ref != null ) { 544 throw new SchemaException("src-element.2.1"); 545 } 546 decl = new ElementDeclaration(schema, name, scope); 547 decl.setType(getType(atts.getValue("", TYPE_ATTR))); 548 if (global) { 549 decl.setQualified(true); 550 decl.setSubstitutionGroup(getRef(atts.getValue("", SUBSTITUTION_GROUP_ATTR), Schema.ELEMENT_DECLARATION)); 551 String fin = atts.getValue("", FINAL_ATTR); 552 if (fin == null) fin = schema.getDefault(FINAL_DEFAULT_ATTR); 553 if (fin != null) decl.setFinal(getMethod(fin, RESTRICTION | EXTENSION)); 554 } 555 else { 556 String form = atts.getValue("", FORM_ATTR); 557 if (form == null) form = schema.getDefault(ELEMENT_FORM_DEFAULT_ATTR); 558 if (QUALIFIED_VALUE.equals(form)) decl.setQualified(true); 559 } 560 561 String booleanValue = atts.getValue("", ABSTRACT_ATTR); 562 if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) ) 563 decl.setAbstract(true); 564 565 String block = atts.getValue("", BLOCK_ATTR); 566 if (block == null) block = schema.getDefault(BLOCK_DEFAULT_ATTR); 567 if (block != null) decl.setBlock(getMethod(block, RESTRICTION | EXTENSION | SUBSTITUTION)); 568 569 } else { 570 if ( global ) { 571 } 573 else if ( ref == null ) { 574 throw new SchemaException("src-element.2.1"); 576 } 577 else { 578 if ( atts.getValue("", NILLABLE_ATTR) != null || 579 atts.getValue("", DEFAULT_ATTR) != null || 580 atts.getValue("", FIXED_ATTR) != null || 581 atts.getValue("", FORM_ATTR) != null || 582 atts.getValue("", BLOCK_ATTR) != null || 583 atts.getValue("", TYPE_ATTR) != null ) { 584 throw new SchemaException("src-element.2.2"); 586 } 587 decl = new ElementDeclarationRef(resolvePrefix(ref), getLocalName(ref), scope); 588 decl.setQualified(true); 589 } 590 } 591 592 String booleanValue = atts.getValue("", NILLABLE_ATTR); 593 if ( TRUE_VALUE.equals(booleanValue) || ONE_VALUE.equals(booleanValue) ) 594 decl.setNillable(true); 595 596 String defaultValue = atts.getValue("", DEFAULT_ATTR); 598 String fixedValue = atts.getValue("", FIXED_ATTR); 599 600 if ( defaultValue != null ) { 601 decl.setDefaultValue(defaultValue); 602 if ( fixedValue != null ) { 603 throw new SchemaException("src-element.1"); 605 } 606 } 607 else if ( fixedValue != null ) { 608 decl.setFixedValue(fixedValue); 609 } 610 611 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 613 componentContextLocators.put(decl, contextLoc); 614 615 return decl; 616 } 617 618 public AttributeDeclaration buildAttributeDeclaration(Attributes atts, SchemaScope scope) 621 throws SchemaException { 622 623 boolean global = (scope != null && scope.isGlobalScope()); 624 String name = atts.getValue("", NAME_ATTR); 625 String ref = atts.getValue("", REF_ATTR); 626 AttributeDeclaration decl = null; 627 628 if ( name != null ) { 630 if ( XMLNS_PREFIX.equals(name) ) 631 throw new SchemaException("no-xmlns"); 632 if ( ref != null ) { 633 throw new SchemaException("src-attribute.3.1"); 635 } 636 decl = new AttributeDeclaration(schema, name, scope); 637 decl.setType(getType(atts.getValue("", TYPE_ATTR))); 638 if ( global ) { 639 decl.setQualified(true); 641 } else { 642 String form = atts.getValue("", FORM_ATTR); 644 if ( form == null ) 645 form = schema.getDefault(ATTRIBUTE_FORM_DEFAULT_ATTR); 646 if ( QUALIFIED_VALUE.equals(form) ) decl.setQualified(true); 647 } 648 } else { 649 if (ref == null) { 651 throw new SchemaException("src-attribute.3.1"); 653 } 654 else { 655 if (atts.getValue("", FORM_ATTR) != null || 656 atts.getValue("", TYPE_ATTR) != null ) { 657 throw new SchemaException("src-attribute.3.2"); 659 } 660 decl = new AttributeDeclarationRef(resolvePrefix(ref), getLocalName(ref), scope); 661 decl.setQualified(true); 662 } 663 } 664 665 String use = atts.getValue("", USE_ATTR); 667 if ( use != null ) { 668 if ( OPTIONAL_VALUE.equals(use) ) 669 decl.setUse(OPTIONAL); 670 else if ( PROHIBITED_VALUE.equals(use) ) 671 decl.setUse(PROHIBITED); 672 else if ( REQUIRED_VALUE.equals(use) ) 673 decl.setUse(REQUIRED); 674 } 675 676 String defaultValue = atts.getValue("", DEFAULT_ATTR); 678 String fixedValue = atts.getValue("", FIXED_ATTR); 679 680 if ( defaultValue != null ) { 681 if ( fixedValue != null ) { 682 throw new SchemaException("src-attribute.1"); 684 } 685 if (use != null && !OPTIONAL_VALUE.equals(use)) { 686 throw new SchemaException("src-attribute.2"); 688 } 689 decl.setDefaultValue(defaultValue); 690 } 691 else if ( fixedValue != null ) { 692 decl.setFixedValue(fixedValue); 693 } 694 695 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 697 componentContextLocators.put(decl, contextLoc); 698 699 return decl; 700 } 701 702 public ModelGroupDefinition buildModelGroupDefinition(Attributes atts, Schema schema) { 703 String name = atts.getValue("", NAME_ATTR); 704 ModelGroupDefinition def = new ModelGroupDefinition(schema, name); 705 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 706 componentContextLocators.put(def, contextLoc); 707 return def; 708 } 709 710 public AttributeGroupDefinition buildAttributeGroupDefinition(Attributes atts, Schema schema) { 711 String name = atts.getValue("", NAME_ATTR); 712 AttributeGroupDefinition def = new AttributeGroupDefinition(schema, name); 713 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 714 componentContextLocators.put(def, contextLoc); 715 return def; 716 } 717 718 public IdentityConstraint buildIdentityConstraint(Attributes atts, 721 String localName, ElementDeclaration context) 722 throws SchemaException { 723 724 String name = atts.getValue("", NAME_ATTR); 725 String refer = atts.getValue("", REFER_ATTR); 726 727 IdentityConstraint identityConstraint = new IdentityConstraint(schema, name, context); 728 if (localName.equals(KEY_TAG)) 729 identityConstraint.setCategory(IdentityConstraint.CATEGORY_KEY); 730 else if (localName.equals(KEYREF_TAG)) 731 identityConstraint.setCategory(IdentityConstraint.CATEGORY_KEYREF); 732 else if (localName.equals(UNIQUE_TAG)) 733 identityConstraint.setCategory(IdentityConstraint.CATEGORY_UNIQUE); 734 735 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 737 componentContextLocators.put(identityConstraint, contextLoc); 738 739 if (identityConstraint.getCategory() == IdentityConstraint.CATEGORY_KEYREF) { 740 IdentityConstraintRef icRef = new IdentityConstraintRef(resolvePrefix(refer), getLocalName(refer)); 741 componentContextLocators.put(icRef, contextLoc); 742 identityConstraint.setReferencedKey(icRef); 743 } 744 745 746 return identityConstraint; 747 } 748 749 public java.util.List buildXPaths(String xpath) throws SchemaException { 750 java.util.ArrayList result = new java.util.ArrayList (4); 751 while (xpath != null) { 752 int i = xpath.indexOf('|'); 753 String current = null; 754 if (i == -1) { 755 current = xpath; 756 xpath = null; 757 } else { 758 current = xpath.substring(0, i); 759 xpath = xpath.substring(i+1); 760 } 761 result.add(buildXPath(current)); 762 } 763 return result; 764 } 765 766 public XPathExpr buildXPath(String xpath) throws SchemaException { 767 boolean isAnyLevel = false; 768 boolean isAttribute = false; 769 if (xpath.startsWith(".//")) { 770 isAnyLevel = true; 771 xpath = xpath.substring(3); 772 } 773 XPathExpr result = new XPathExpr(isAnyLevel); 774 while (xpath != null) { 775 int i = xpath.indexOf('/'); 776 String qName = null; 777 if (i == -1) { 778 qName = xpath; 779 xpath = null; 780 } else { 781 qName = xpath.substring(0, i); 782 xpath = xpath.substring(i+1); 783 } 784 if (qName.startsWith("child::")) { 785 qName = qName.substring(7); 786 } else if (qName.startsWith("@")) { 787 isAttribute = true; 788 qName = qName.substring(1); 789 } else if (qName.startsWith("attribute::")) { 790 isAttribute = true; 791 qName = qName.substring(11); 792 } 793 result.setAttribute(isAttribute); 794 if (!qName.equals(".")) { 795 String ns = null; 796 String localName = null; 797 int j = qName.indexOf(':'); 798 if (j == -1) { 799 localName = qName; 800 if (!isAttribute && !"*".equals(qName)) 802 ns = getPrefixMapping(""); 803 } else { 804 ns = getPrefixMapping(qName.substring(0, j)); 805 if ( ns == null ) 806 throw new SchemaException("src-identity-constraint"); 807 localName = qName.substring(j+1); 808 } 809 result.addStep(ns, localName); 810 } 811 } 812 813 ContextLocatorImpl contextLoc = new ContextLocatorImpl(super.getNamespaceContext(), getDocumentBase(), super.getDocumentLocator()); 815 componentContextLocators.put(result, contextLoc); 816 817 return result; 818 } 819 820 public void warning(SAXParseException ex) throws SAXException { 821 throw ex; 822 } 823 824 public void error(SAXParseException ex) throws SAXException { 825 throw ex; 826 } 827 828 public void fatalError(SAXParseException ex) throws SAXException { 829 throw ex; 830 } 831 832 public void notifyUnknownElement(String uriNamespace, String localName ) 833 throws SAXException { 834 String errMsg = "Unknown element : {" + uriNamespace + "}" + localName; 837 reportLoadingError(errMsg, null); 838 } 839 840 public void reportLoadingError(SchemaException se) throws SAXException { 841 reportLoadingError(null, se); 842 } 843 844 public void reportLoadingError(String message, SchemaException se) throws SAXException { 845 if ( message != null && se != null ) message += "\n"+se.getMessage(); 846 throw new SAXParseException(message, super.getDocumentLocator(), se); 847 } 848 849 class SchemaHandler extends org.xquark.util.DefaultElementHandler { 850 private static final String RCSRevision = "$Revision: 1.1 $"; 851 private static final String RCSName = "$Name: $"; 852 public org.xquark.util.ElementHandler startElement(String strNamespaceURI, String localName, Attributes atts) 853 throws SAXException { 854 855 if (isSchemaNamespace(strNamespaceURI)) { 856 857 if (localName.equals(SCHEMA_TAG)) { 858 try { 859 initSchema(atts); 860 } 861 catch ( SchemaException se ) { 862 String errMsg = "Error while processing localName -> " + localName; 863 reportLoadingError(errMsg, se); 864 } 865 return this; 866 } 867 868 else if (localName.equals(INCLUDE_TAG)) { 869 includeSchema(atts); 870 return this; 871 } 872 873 else if (localName.equals(IMPORT_TAG)) { 874 importSchema(atts); 875 return this; 876 } 877 878 else if (localName.equals(REDEFINE_TAG)) { 879 includeSchema(atts); 880 return new RedefineHandler(getLoader()); 881 } 882 883 else if ( localName.equals(SIMPLE_TYPE_TAG) ) { 884 SimpleType type = null; 885 try { 886 type = buildSimpleType(atts); 887 schema.register(type); 888 } 889 catch ( SchemaException se ) { 890 String errMsg = "Error while processing localName -> " + localName; 891 reportLoadingError(errMsg, se); 892 } 893 return new SimpleTypeHandler(getLoader(), type, false, false); 894 } 895 896 else if (localName.equals(COMPLEX_TYPE_TAG)) { 897 ComplexType type = null; 898 try { 899 type = buildComplexType(atts); 900 schema.register(type); 901 } 902 catch ( SchemaException se ) { 903 String errMsg = "Error while processing localName -> " + localName; 904 reportLoadingError(errMsg, se); 905 } 906 return new ComplexTypeHandler(getLoader(), type, false, false); 907 } 908 909 else if (localName.equals(ELEMENT_TAG)) { 910 ElementDeclaration decl = null; 911 try { 912 decl = buildElementDeclaration(atts, schema); 913 schema.register(decl); 914 } 915 catch ( SchemaException se ) { 916 String errMsg = "Error while processing localName -> " + localName; 917 reportLoadingError(errMsg, se); 918 } 919 return new ElementTypeHandler(getLoader(), decl); 920 } 921 922 else if (localName.equals(ATTRIBUTE_TAG)) { 923 AttributeDeclaration decl = null; 924 try { 925 decl = buildAttributeDeclaration(atts, schema); 926 schema.register(decl); 927 } 928 catch ( SchemaException se ) { 929 String errMsg = "Error while processing localName -> " + localName; 930 reportLoadingError(errMsg, se); 931 } 932 return new AttributeTypeHandler(getLoader(), decl); 933 } 934 935 else if (localName.equals(GROUP_TAG)) { 936 ModelGroupDefinition def = buildModelGroupDefinition(atts, schema); 937 try { 938 schema.register(def); 939 } 940 catch ( SchemaException se ) { 941 String errMsg = "Error while processing localName -> " + localName; 942 reportLoadingError(errMsg, se); 943 } 944 return new ContentModelHandler(getLoader(), def, false); 945 } 946 947 else if (localName.equals(ATTRIBUTE_GROUP_TAG)) { 948 AttributeGroupDefinition def = buildAttributeGroupDefinition(atts, schema); 949 try { 950 schema.register(def); 951 } 952 catch ( SchemaException se ) { 953 String errMsg = "Error while processing localName -> " + localName; 954 reportLoadingError(errMsg, se); 955 } 956 return new AttributeGroupHandler(getLoader(), def); 957 } 958 959 else if (localName.equals(NOTATION_TAG)) { 960 NotationDeclaration decl = buildNotation(atts); 961 try { 962 schema.register(decl); 963 } 964 catch ( SchemaException se ) { 965 String errMsg = "Error while processing localName -> " + localName; 966 reportLoadingError(errMsg, se); 967 } 968 return this; 969 } 970 971 else if (localName.equals(ANNOTATION_TAG)) { 972 return new AnnotationHandler(); 973 } 974 } 975 976 notifyUnknownElement(strNamespaceURI, localName); 977 return this; 978 } 979 980 public void endElement(String namespace, String localName) 981 throws SAXException { 982 super.endElement(namespace, localName); 983 if ( isSchemaNamespace(namespace) && localName.equals(SCHEMA_TAG) && topLevel ) { 984 initialize(); 985 } 986 } 987 } 988 989 } 990 | Popular Tags |