1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.ws.commons.schema;
19
20 import org.apache.ws.commons.schema.XmlSchemaCollection.SchemaKey;
21 import org.apache.ws.commons.schema.constants.Constants;
22 import org.apache.ws.commons.schema.extensions.ExtensionRegistry;
23 import org.apache.ws.commons.schema.utils.NodeNamespaceContext;
24 import org.apache.ws.commons.schema.utils.TargetNamespaceValidator;
25 import org.apache.ws.commons.schema.utils.XDOMUtil;
26 import org.w3c.dom.*;
27 import org.xml.sax.InputSource;
28
29 import javax.xml.namespace.NamespaceContext;
30 import javax.xml.namespace.QName;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import java.util.StringTokenizer;
33 import java.util.Vector;
34
35 public class SchemaBuilder {
36 Document doc;
37 XmlSchema schema;
38 XmlSchemaCollection collection;
39 private final TargetNamespaceValidator validator;
40 DocumentBuilderFactory docFac;
41
42
43
44
45
46 private ExtensionRegistry extReg = null;
47
48 public ExtensionRegistry getExtReg() {
49 return extReg;
50 }
51
52 public void setExtReg(ExtensionRegistry extReg) {
53 this.extReg = extReg;
54 }
55
56
57
58
59
60 SchemaBuilder(XmlSchemaCollection collection, TargetNamespaceValidator validator) {
61 this.collection = collection;
62 this.validator = validator;
63
64 if (collection.getExtReg()!=null){
65 this.extReg = collection.getExtReg();
66 }
67
68 schema = new XmlSchema(collection);
69 }
70
71
72
73
74
75
76
77 XmlSchema build(Document doc, String uri, ValidationEventHandler veh) {
78 Element schemaEl = doc.getDocumentElement();
79 return handleXmlSchemaElement(schemaEl, uri);
80 }
81
82
83
84
85
86
87 XmlSchema handleXmlSchemaElement(Element schemaEl, String uri) {
88
89 schema.setNamespaceContext(new NodeNamespaceContext(schemaEl));
90 setNamespaceAttributes(schema, schemaEl);
91
92 XmlSchemaCollection.SchemaKey schemaKey = new XmlSchemaCollection.SchemaKey(schema.logicalTargetNamespace, uri);
93 if (!collection.containsSchema(schemaKey)) {
94 collection.addSchema(schemaKey, schema);
95 }
96
97 schema.setElementFormDefault(this.getFormDefault(schemaEl,
98 "elementFormDefault"));
99 schema.setAttributeFormDefault(this.getFormDefault(schemaEl,
100 "attributeFormDefault"));
101 schema.setBlockDefault(this.getDerivation(schemaEl, "blockDefault"));
102 schema.setFinalDefault(this.getDerivation(schemaEl, "finalDefault"));
103
104 if (schemaEl.hasAttribute("id")) {
105 schema.id = schemaEl.getAttribute("id");
106 }
107
108 schema.setSourceURI(uri);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 Element el = XDOMUtil.getFirstChildElementNS(schemaEl, XmlSchema.SCHEMA_NS);
137 if (el == null && XDOMUtil.getFirstChildElementNS(schemaEl, "http://www.w3.org/1999/XMLSchema") != null) {
138 throw new XmlSchemaException("Schema defined using \"http://www.w3.org/1999/XMLSchema\" is not supported. " +
139 "Please update the schema to the \"" + XmlSchema.SCHEMA_NS + "\" namespace");
140 }
141 for (; el != null;
142 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
143
144
145
146 if (el.getLocalName().equals("simpleType")) {
147 XmlSchemaType type = handleSimpleType(schema, el, schemaEl);
148 schema.addType(type);
149 schema.items.add(type);
150 collection.resolveType(type.getQName(), type);
151 } else if (el.getLocalName().equals("complexType")) {
152 XmlSchemaType type = handleComplexType(schema, el, schemaEl);
153 schema.addType(type);
154 schema.items.add(type);
155 collection.resolveType(type.getQName(), type);
156 } else if (el.getLocalName().equals("element")) {
157 XmlSchemaElement element = handleElement(schema, el, schemaEl, true);
158 if (element.qualifiedName != null)
159 schema.elements.collection.put(element.qualifiedName, element);
160 else if (element.refName != null)
161 schema.elements.collection.put(element.refName, element);
162 schema.items.add(element);
163 } else if (el.getLocalName().equals("include")) {
164 XmlSchemaInclude include = handleInclude(schema,
165 el, schemaEl);
166 schema.includes.add(include);
167 schema.items.add(include);
168
169 } else if (el.getLocalName().equals("import")) {
170 XmlSchemaImport schemaImport = handleImport(schema,
171 el, schemaEl);
172 schema.includes.add(schemaImport);
173 schema.items.add(schemaImport);
174
175 } else if (el.getLocalName().equals("group")) {
176 XmlSchemaGroup group = handleGroup(schema, el, schemaEl);
177 schema.groups.collection.put(
178 new QName(schema.getTargetNamespace(), group.name), group);
179 schema.items.add(group);
180 } else if (el.getLocalName().equals("attributeGroup")) {
181 XmlSchemaAttributeGroup group = handleAttributeGroup(schema,
182 el, schemaEl);
183 schema.attributeGroups.collection.put(
184 new QName(schema.getTargetNamespace(), group.name), group);
185 schema.items.add(group);
186 } else if (el.getLocalName().equals("attribute")) {
187 XmlSchemaAttribute attr = handleAttribute(schema,
188 el, schemaEl);
189 schema.attributes.collection.put(attr.qualifiedName, attr);
190 schema.items.add(attr);
191 } else if (el.getLocalName().equals("redefine")) {
192 XmlSchemaRedefine redefine = handleRedefine(schema,
193 el, schemaEl);
194 schema.includes.add(redefine);
195 } else if (el.getLocalName().equals("notation")) {
196 XmlSchemaNotation notation = handleNotation(el);
197 schema.notations.collection.put(
198 new QName(schema.getTargetNamespace(), notation.name), notation);
199 schema.items.add(notation);
200 } else if (el.getLocalName().equals("annotation")) {
201 XmlSchemaAnnotation annotation = handleAnnotation(el);
202 schema.setAnnotation(annotation);
203 }
204 }
205
206
207 processExtensibilityComponents(schema,schemaEl);
208
209 return schema;
210 }
211
212 private XmlSchemaNotation handleNotation(
213 Element notationEl
214 ) {
215
216 XmlSchemaNotation notation = new XmlSchemaNotation();
217
218 if (notationEl.hasAttribute("id")) {
219 notation.id = notationEl.getAttribute("id");
220 }
221
222 if (notationEl.hasAttribute("name")) {
223 notation.name = notationEl.getAttribute("name");
224 }
225
226 if (notationEl.hasAttribute("public")) {
227 notation.publicNotation = notationEl.getAttribute("public");
228 }
229
230 if (notationEl.hasAttribute("system")) {
231 notation.system = notationEl.getAttribute("system");
232 }
233
234 Element annotationEl =
235 XDOMUtil.getFirstChildElementNS(notationEl,
236 XmlSchema.SCHEMA_NS, "annotation");
237
238 if (annotationEl != null) {
239 XmlSchemaAnnotation annotation = handleAnnotation(annotationEl);
240 notation.setAnnotation(annotation);
241 }
242
243 return notation;
244 }
245
246 private XmlSchemaRedefine handleRedefine(XmlSchema schema,
247 Element redefineEl, Element schemaEl) {
248
249 XmlSchemaRedefine redefine = new XmlSchemaRedefine();
250 redefine.schemaLocation =
251 redefineEl.getAttribute("schemaLocation");
252 final TargetNamespaceValidator validator = newIncludeValidator(schema);
253 redefine.schema =
254 resolveXmlSchema(schema.logicalTargetNamespace, redefine.schemaLocation, validator);
255
256 for (Element el = XDOMUtil.getFirstChildElementNS(redefineEl,
257 XmlSchema.SCHEMA_NS)
258 ; el != null;
259 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
260
261 if (el.getLocalName().equals("simpleType")) {
262 XmlSchemaType type =
263 handleSimpleType(schema, el, schemaEl);
264
265 redefine.schemaTypes.collection.put(type.getQName(),
266 type);
267 redefine.items.add(type);
268 } else if (el.getLocalName().equals("complexType")) {
269
270 XmlSchemaType type = handleComplexType(schema, el,
271 schemaEl);
272
273 redefine.schemaTypes.collection.put(type.getQName(),
274 type);
275 redefine.items.add(type);
276 } else if (el.getLocalName().equals("group")) {
277 XmlSchemaGroup group = handleGroup(schema, el,
278 schemaEl);
279 redefine.groups.collection.put(group.name, group);
280 redefine.items.add(group);
281 } else if (el.getLocalName().equals("attributeGroup")) {
282 XmlSchemaAttributeGroup group =
283 handleAttributeGroup(schema, el, schemaEl);
284
285 redefine.attributeGroups.collection.put(group.name, group);
286 redefine.items.add(group);
287 } else if (el.getLocalName().equals("annotation")) {
288 XmlSchemaAnnotation annotation = handleAnnotation(el);
289 redefine.setAnnotation(annotation);
290 }
291
292 }
293 return redefine;
294 }
295
296 void setNamespaceAttributes(XmlSchema schema, Element schemaEl) {
297
298 if (schemaEl.getAttributeNode("targetNamespace") != null) {
299 String contain = schemaEl.getAttribute("targetNamespace");
300 schema.setTargetNamespace(contain);
301 } else {
302
303 }
304 if (validator != null) {
305 validator.validate(schema);
306 }
307 }
308
309
310
311
312
313
314
315 XmlSchemaSimpleType handleSimpleType(XmlSchema schema,
316 Element simpleEl, Element schemaEl) {
317 XmlSchemaSimpleType simpleType = new XmlSchemaSimpleType(schema);
318 if (simpleEl.hasAttribute("name")) {
319 simpleType.name = simpleEl.getAttribute("name");
320 }
321
322 if (simpleEl.hasAttribute("final")) {
323 String finalstr = simpleEl.getAttribute("final");
324
325 if (finalstr.equalsIgnoreCase("all") |
326 finalstr.equalsIgnoreCase("#all"))
327
328 simpleType.setFinal(new XmlSchemaDerivationMethod(Constants.BlockConstants.ALL));
329 else
330 simpleType.setFinal(new XmlSchemaDerivationMethod(finalstr));
331 }
332
333 Element simpleTypeAnnotationEl =
334 XDOMUtil.getFirstChildElementNS(simpleEl,
335 XmlSchema.SCHEMA_NS, "annotation");
336
337 if (simpleTypeAnnotationEl != null) {
338 XmlSchemaAnnotation simpleTypeAnnotation =
339 handleAnnotation(simpleTypeAnnotationEl);
340
341 simpleType.setAnnotation(simpleTypeAnnotation);
342 }
343
344 Element unionEl, listEl, restrictionEl;
345
346 if ((restrictionEl =
347 XDOMUtil.getFirstChildElementNS(simpleEl, XmlSchema.SCHEMA_NS, "restriction")) != null) {
348
349 XmlSchemaSimpleTypeRestriction restriction =
350 new XmlSchemaSimpleTypeRestriction();
351
352 Element restAnnotationEl =
353 XDOMUtil.getFirstChildElementNS(restrictionEl,
354 XmlSchema.SCHEMA_NS, "annotation");
355
356 if (restAnnotationEl != null) {
357 XmlSchemaAnnotation restAnnotation =
358 handleAnnotation(restAnnotationEl);
359 restriction.setAnnotation(restAnnotation);
360 }
361
362
363
364
365
366
367
368
369
370
371
372 Element inlineSimpleType =
373 XDOMUtil.getFirstChildElementNS(restrictionEl,
374 XmlSchema.SCHEMA_NS, "simpleType");
375
376 if (restrictionEl.hasAttribute("base")) {
377 NamespaceContext ctx = new NodeNamespaceContext(restrictionEl);
378 restriction.baseTypeName = getRefQName(restrictionEl.getAttribute("base"), ctx);
379 } else if (inlineSimpleType != null) {
380
381 restriction.baseType = handleSimpleType(schema, inlineSimpleType, schemaEl);
382 }
383 for (Element el = XDOMUtil.getFirstChildElementNS(restrictionEl, XmlSchema.SCHEMA_NS)
384 ; el != null;
385 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
386
387 if (!el.getLocalName().equals("annotation")
388 &&
389 !el.getLocalName().equals("simpleType")) {
390
391 XmlSchemaFacet facet = XmlSchemaFacet.construct(el);
392 Element annotation =
393 XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS,
394 "annotation");
395
396 if (annotation != null) {
397 XmlSchemaAnnotation facetAnnotation =
398 handleAnnotation(annotation);
399 facet.setAnnotation(facetAnnotation);
400 }
401 restriction.facets.add(facet);
402 }
403
404 }
405 simpleType.content = restriction;
406
407 } else if ((listEl = XDOMUtil.getFirstChildElementNS(simpleEl,
408 XmlSchema.SCHEMA_NS, "list")) != null) {
409
410 XmlSchemaSimpleTypeList list = new XmlSchemaSimpleTypeList();
411
412
413
414
415
416
417
418
419
420 Element inlineListType, listAnnotationEl;
421 if (listEl.hasAttribute("itemType")) {
422 String name = listEl.getAttribute("itemType");
423 list.itemTypeName = getRefQName(name, listEl);
424 } else if ((inlineListType =
425 XDOMUtil.getFirstChildElementNS(listEl, XmlSchema.SCHEMA_NS,
426 "simpleType")) != null) {
427
428 list.itemType = handleSimpleType(schema, inlineListType, schemaEl);
429 }
430
431 if ((listAnnotationEl =
432 XDOMUtil.getFirstChildElementNS(listEl, XmlSchema.SCHEMA_NS,
433 "annotation")) != null) {
434
435 XmlSchemaAnnotation listAnnotation =
436 handleAnnotation(listAnnotationEl);
437
438 list.setAnnotation(listAnnotation);
439 }
440 simpleType.content = list;
441
442 } else if ((unionEl =
443 XDOMUtil.getFirstChildElementNS(simpleEl, XmlSchema.SCHEMA_NS,
444 "union")) != null) {
445
446 XmlSchemaSimpleTypeUnion union =
447 new XmlSchemaSimpleTypeUnion();
448
449
450
451
452
453
454
455
456
457
458 if (unionEl.hasAttribute("memberTypes")) {
459 String memberTypes = unionEl.getAttribute("memberTypes");
460 union.memberTypesSource = memberTypes;
461 Vector v = new Vector();
462 StringTokenizer tokenizer = new StringTokenizer(memberTypes, " ");
463 while (tokenizer.hasMoreTokens()) {
464 String member = tokenizer.nextToken();
465 v.add(getRefQName(member, unionEl));
466 }
467 union.memberTypesQNames = new QName[v.size()];
468 v.copyInto(union.memberTypesQNames);
469 }
470
471 Element inlineUnionType =
472 XDOMUtil.getFirstChildElementNS(unionEl, XmlSchema.SCHEMA_NS,
473 "simpleType");
474 while (inlineUnionType != null) {
475
476 XmlSchemaSimpleType unionSimpleType =
477 handleSimpleType(schema, inlineUnionType,
478 schemaEl);
479
480 union.baseTypes.add(unionSimpleType);
481
482 if(unionSimpleType.name != null) {
483 union.memberTypesSource += " " + unionSimpleType.name;
484 }
485
486 inlineUnionType =
487 XDOMUtil.getNextSiblingElementNS(inlineUnionType,
488 XmlSchema.SCHEMA_NS,
489 "simpleType");
490 }
491
492
493
494 Element unionAnnotationEl =
495 XDOMUtil.getFirstChildElementNS(unionEl, XmlSchema.SCHEMA_NS,
496 "annotation");
497
498 if (unionAnnotationEl != null) {
499 XmlSchemaAnnotation unionAnnotation =
500 handleAnnotation(unionAnnotationEl);
501
502 union.setAnnotation(unionAnnotation);
503 }
504 simpleType.content = union;
505 }
506
507
508 processExtensibilityComponents(simpleType,simpleEl);
509
510 return simpleType;
511 }
512
513 private QName getRefQName(String pName, Node pNode) {
514 return getRefQName(pName, new NodeNamespaceContext(pNode));
515 }
516
517 private QName getRefQName(String pName, NamespaceContext pContext) {
518 final int offset = pName.indexOf(':');
519 String uri;
520 final String localName;
521 final String prefix;
522 if (offset == -1) {
523 uri = pContext.getNamespaceURI(Constants.DEFAULT_NS_PREFIX);
524 if (Constants.NULL_NS_URI.equals(uri)) {
525 return new QName(schema.logicalTargetNamespace, pName);
526 }
527 localName = pName;
528 prefix = Constants.DEFAULT_NS_PREFIX;
529 } else {
530 prefix = pName.substring(0, offset);
531 uri = pContext.getNamespaceURI(prefix);
532 if (uri == null || Constants.NULL_NS_URI.equals(uri)) {
533 if(schema.parent != null && schema.parent.getNamespaceContext() != null) {
534 uri = schema.parent.getNamespaceContext().getNamespaceURI(prefix);
535 }
536 }
537
538 if (uri == null || Constants.NULL_NS_URI.equals(uri)) {
539 throw new IllegalStateException("The prefix " + prefix + " is not bound.");
540 }
541 localName = pName.substring(offset+1);
542 }
543 return new QName(uri, localName, prefix);
544 }
545
546
547
548
549
550
551
552 XmlSchemaComplexType handleComplexType(XmlSchema schema,
553 Element complexEl, Element schemaEl) {
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589 XmlSchemaComplexType ct = new XmlSchemaComplexType(schema);
590
591 if (complexEl.hasAttribute("name")) {
592
593
594
595
596 ct.name = complexEl.getAttribute("name");
597 }
598 for (Element el = XDOMUtil.getFirstChildElementNS(complexEl,
599 XmlSchema.SCHEMA_NS)
600 ; el != null;
601 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
602
603
604
605
606 if (el.getLocalName().equals("sequence")) {
607 ct.particle = handleSequence(schema,
608 el, schemaEl);
609 } else if (el.getLocalName().equals("choice")) {
610 ct.particle = handleChoice(schema,
611 el, schemaEl);
612 } else if (el.getLocalName().equals("all")) {
613 ct.particle = handleAll(schema, el, schemaEl);
614 } else if (el.getLocalName().equals("attribute")) {
615 ct.attributes.add(handleAttribute(schema,
616 el, schemaEl));
617 } else if (el.getLocalName().equals("attributeGroup")) {
618 ct.attributes.add(handleAttributeGroupRef(el));
619 } else if (el.getLocalName().equals("group")) {
620 XmlSchemaGroupRef group =
621 handleGroupRef(schema, el, schemaEl);
622 ct.particle = (group.particle == null) ?
623 (XmlSchemaParticle) group : group.particle;
624 } else if (el.getLocalName().equals("simpleContent")) {
625 ct.contentModel = handleSimpleContent(schema, el, schemaEl);
626 } else if (el.getLocalName().equals("complexContent")) {
627 ct.contentModel = handleComplexContent(schema, el, schemaEl);
628 } else if (el.getLocalName().equals("annotation")) {
629 ct.setAnnotation(handleAnnotation(el));
630 } else if (el.getLocalName().equals("anyAttribute")) {
631 ct.setAnyAttribute(handleAnyAttribute(schema,el,schemaEl));
632 }
633
634 }
635 if (complexEl.hasAttribute("block")) {
636 String blockStr = complexEl.getAttribute("block");
637 if (blockStr.equalsIgnoreCase("all") |
638 blockStr.equalsIgnoreCase("#all")) {
639
640 ct.setBlock(new XmlSchemaDerivationMethod(Constants.BlockConstants.ALL));
641 } else
642 ct.setBlock(new XmlSchemaDerivationMethod(blockStr));
643
644 }
645 if (complexEl.hasAttribute("final")) {
646 String finalstr = complexEl.getAttribute("final");
647 if (finalstr.equalsIgnoreCase("all") |
648 finalstr.equalsIgnoreCase("#all")) {
649
650 ct.setFinal(new XmlSchemaDerivationMethod(Constants.BlockConstants.ALL));
651 } else
652 ct.setFinal(new XmlSchemaDerivationMethod(finalstr));
653 }
654 if (complexEl.hasAttribute("abstract")) {
655 String abs = complexEl.getAttribute("abstract");
656 if (abs.equalsIgnoreCase("true"))
657 ct.setAbstract(true);
658 else
659 ct.setAbstract(false);
660 }
661 if (complexEl.hasAttribute("mixed")) {
662 String mixed = complexEl.getAttribute("mixed");
663 if (mixed.equalsIgnoreCase("true"))
664 ct.setMixed(true);
665 else
666 ct.setMixed(false);
667 }
668
669
670 processExtensibilityComponents(ct,complexEl);
671
672 return ct;
673 }
674
675 private XmlSchemaSimpleContent
676 handleSimpleContent(XmlSchema schema, Element simpleEl,
677 Element schemaEl) {
678
679 XmlSchemaSimpleContent simpleContent =
680 new XmlSchemaSimpleContent();
681
682 for (Element el = XDOMUtil.getFirstChildElementNS(simpleEl,
683 XmlSchema.SCHEMA_NS)
684 ; el != null;
685 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
686
687 if (el.getLocalName().equals("restriction")) {
688 simpleContent.content = handleSimpleContentRestriction(schema, el, schemaEl);
689 } else if (el.getLocalName().equals("extension")) {
690 simpleContent.content = handleSimpleContentExtension(schema, el, schemaEl);
691 } else if (el.getLocalName().equals("annotation")) {
692 simpleContent.setAnnotation(handleAnnotation(el));
693 }
694 }
695 return simpleContent;
696 }
697
698 private XmlSchemaComplexContent handleComplexContent(XmlSchema schema,
699 Element complexEl,
700 Element schemaEl) {
701
702 XmlSchemaComplexContent complexContent =
703 new XmlSchemaComplexContent();
704
705 for (Element el =
706 XDOMUtil.getFirstChildElementNS(complexEl, XmlSchema.SCHEMA_NS);
707 el != null;
708 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
709
710 if (el.getLocalName().equals("restriction")) {
711 complexContent.content = handleComplexContentRestriction(schema, el,
712 schemaEl);
713 } else if (el.getLocalName().equals("extension")) {
714 complexContent.content = handleComplexContentExtension(schema, el,
715 schemaEl);
716 } else if (el.getLocalName().equals("annotation")) {
717 complexContent.setAnnotation(handleAnnotation(el));
718 }
719 }
720 return complexContent;
721 }
722
723 private XmlSchemaSimpleContentRestriction
724 handleSimpleContentRestriction(XmlSchema schema,
725 Element restrictionEl, Element schemaEl) {
726
727 XmlSchemaSimpleContentRestriction restriction =
728 new XmlSchemaSimpleContentRestriction();
729
730 if (restrictionEl.hasAttribute("base")) {
731 String name = restrictionEl.getAttribute("base");
732 restriction.baseTypeName = getRefQName(name, restrictionEl);
733 }
734
735 if (restrictionEl.hasAttribute("id"))
736 restriction.id = restrictionEl.getAttribute("id");
737
738
739 for (
740 Element el = XDOMUtil.getFirstChildElementNS(restrictionEl,
741 XmlSchema.SCHEMA_NS)
742 ; el != null;
743 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
744
745 if (el.getLocalName().equals("attribute")) {
746 XmlSchemaAttribute attr =
747 handleAttribute(schema, el, schemaEl);
748 restriction.attributes.add(attr);
749 } else if (el.getLocalName().equals("attributeGroup")) {
750 XmlSchemaAttributeGroupRef attrGroup =
751 handleAttributeGroupRef(el);
752 restriction.attributes.add(attrGroup);
753 } else if (el.getLocalName().equals("simpleType")) {
754 restriction.baseType = handleSimpleType(schema, el, schemaEl);
755 } else if (el.getLocalName().equals("anyAttribute")) {
756 restriction.anyAttribute =
757 handleAnyAttribute(schema, el, schemaEl);
758 } else if (el.getLocalName().equals("annotation")) {
759 restriction.setAnnotation(handleAnnotation(el));
760 } else {
761 XmlSchemaFacet facet = XmlSchemaFacet.construct(el);
762 NodeList annotations =
763 el.getElementsByTagNameNS(XmlSchema.SCHEMA_NS, "annotation");
764
765 if (annotations.getLength() > 0) {
766 XmlSchemaAnnotation facetAnnotation =
767 handleAnnotation(el);
768 facet.setAnnotation(facetAnnotation);
769 }
770 restriction.facets.add(facet);
771 }
772 }
773 return restriction;
774 }
775
776 private XmlSchemaSimpleContentExtension
777 handleSimpleContentExtension(XmlSchema schema, Element extEl,
778 Element schemaEl) {
779
780 XmlSchemaSimpleContentExtension ext =
781 new XmlSchemaSimpleContentExtension();
782
783 if (extEl.hasAttribute("base")) {
784 String name = extEl.getAttribute("base");
785 ext.baseTypeName = getRefQName(name, extEl);
786 }
787
788 for (
789 Element el = XDOMUtil.getFirstChildElementNS(extEl, XmlSchema.SCHEMA_NS)
790 ; el != null;
791 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
792
793 if (el.getLocalName().equals("attribute")) {
794 XmlSchemaAttribute attr =
795 handleAttribute(schema, el, schemaEl);
796 ext.attributes.add(attr);
797 } else if (el.getLocalName().equals("attributeGroup")) {
798 XmlSchemaAttributeGroupRef attrGroup =
799 handleAttributeGroupRef(el);
800 ext.attributes.add(attrGroup);
801 } else if (el.getLocalName().equals("anyAttribute")) {
802 ext.anyAttribute =
803 handleAnyAttribute(schema, el, schemaEl);
804 } else if (el.getLocalName().equals("annotation")) {
805 XmlSchemaAnnotation ann = handleAnnotation(el);
806 ext.setAnnotation(ann);
807 }
808 }
809 return ext;
810 }
811
812 private XmlSchemaComplexContentRestriction
813 handleComplexContentRestriction(XmlSchema schema,
814 Element restrictionEl, Element schemaEl) {
815
816 XmlSchemaComplexContentRestriction restriction =
817 new XmlSchemaComplexContentRestriction();
818
819 if (restrictionEl.hasAttribute("base")) {
820 String name = restrictionEl.getAttribute("base");
821 restriction.baseTypeName = getRefQName(name, restrictionEl);
822 }
823 for (Element el = XDOMUtil.getFirstChildElementNS(restrictionEl,
824 XmlSchema.SCHEMA_NS)
825 ; el != null;
826 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
827
828 if (el.getLocalName().equals("sequence")) {
829 restriction.particle = handleSequence(schema, el, schemaEl);
830 } else if (el.getLocalName().equals("choice")) {
831 restriction.particle = handleChoice(schema, el, schemaEl);
832 } else if (el.getLocalName().equals("all")) {
833 restriction.particle = handleAll(schema, el, schemaEl);
834 } else if (el.getLocalName().equals("attribute")) {
835 restriction.attributes.add(handleAttribute(schema, el, schemaEl));
836 } else if (el.getLocalName().equals("attributeGroup")) {
837 restriction.attributes.add(handleAttributeGroupRef(el));
838 } else if (el.getLocalName().equals("group")) {
839 restriction.particle = handleGroupRef(schema, el, schemaEl);
840 } else if (el.getLocalName().equals("anyAttribute")) {
841 restriction.anyAttribute =
842 handleAnyAttribute(schema, el, schemaEl);
843 } else if (el.getLocalName().equals("annotation")) {
844 restriction.setAnnotation(handleAnnotation(el));
845 }
846 }
847 return restriction;
848 }
849
850 private XmlSchemaComplexContentExtension
851 handleComplexContentExtension(XmlSchema schema,
852 Element extEl, Element schemaEl) {
853
854 XmlSchemaComplexContentExtension ext =
855 new XmlSchemaComplexContentExtension();
856
857 if (extEl.hasAttribute("base")) {
858 String name = extEl.getAttribute("base");
859 ext.baseTypeName = getRefQName(name, extEl);
860 }
861
862 for (Element el = XDOMUtil.getFirstChildElementNS(extEl, XmlSchema.SCHEMA_NS)
863 ; el != null;
864 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
865
866 if (el.getLocalName().equals("sequence")) {
867 ext.particle = handleSequence(schema, el, schemaEl);
868 } else if (el.getLocalName().equals("choice")) {
869 ext.particle = handleChoice(schema, el, schemaEl);
870 } else if (el.getLocalName().equals("all")) {
871 ext.particle = handleAll(schema, el, schemaEl);
872 } else if (el.getLocalName().equals("attribute")) {
873 ext.attributes.add(handleAttribute(schema, el, schemaEl));
874 } else if (el.getLocalName().equals("attributeGroup")) {
875 ext.attributes.add(handleAttributeGroupRef(el));
876 } else if (el.getLocalName().equals("group")) {
877 ext.particle = handleGroupRef(schema, el, schemaEl);
878 } else if (el.getLocalName().equals("anyAttribute")) {
879 ext.anyAttribute =
880 handleAnyAttribute(schema, el, schemaEl);
881 } else if (el.getLocalName().equals("annotation")) {
882 ext.setAnnotation(handleAnnotation(el));
883 }
884 }
885 return ext;
886 }
887
888 private XmlSchemaAttributeGroupRef
889 handleAttributeGroupRef(Element attrGroupEl
890 ) {
891
892 XmlSchemaAttributeGroupRef attrGroup =
893 new XmlSchemaAttributeGroupRef();
894
895 if (attrGroupEl.hasAttribute("ref")) {
896 String ref = attrGroupEl.getAttribute("ref");
897 attrGroup.refName = getRefQName(ref, attrGroupEl);
898 }
899
900 if (attrGroupEl.hasAttribute("id"))
901 attrGroup.id = attrGroupEl.getAttribute("id");
902
903 Element annotationEl =
904 XDOMUtil.getFirstChildElementNS(attrGroupEl,
905 XmlSchema.SCHEMA_NS, "annotation");
906
907 if (annotationEl != null) {
908 XmlSchemaAnnotation annotation = handleAnnotation(annotationEl);
909 attrGroup.setAnnotation(annotation);
910 }
911 return attrGroup;
912 }
913
914 private XmlSchemaSequence handleSequence(XmlSchema schema,
915 Element sequenceEl,
916 Element schemaEl) {
917
918 XmlSchemaSequence sequence = new XmlSchemaSequence();
919 for (Element el = XDOMUtil.getFirstChildElementNS(sequenceEl, XmlSchema.SCHEMA_NS)
920 ; el != null;
921 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
922
923 if (el.getLocalName().equals("sequence")) {
924 XmlSchemaSequence seq = handleSequence(schema, el,
925 schemaEl);
926 sequence.items.add(seq);
927 } else if (el.getLocalName().equals("element")) {
928 XmlSchemaElement element = handleElement(schema, el,
929 schemaEl, false);
930 sequence.items.add(element);
931 } else if (el.getLocalName().equals("group")) {
932 XmlSchemaGroupRef group = handleGroupRef(schema, el,
933 schemaEl);
934 sequence.items.add(group);
935 } else if (el.getLocalName().equals("choice")) {
936 XmlSchemaChoice choice = handleChoice(schema, el,
937 schemaEl);
938 sequence.items.add(choice);
939 } else if (el.getLocalName().equals("any")) {
940 XmlSchemaAny any = handleAny(schema, el, schemaEl);
941 sequence.items.add(any);
942 } else if (el.getLocalName().equals("annotation")) {
943 XmlSchemaAnnotation annotation = handleAnnotation(el);
944 sequence.setAnnotation(annotation);
945 }
946 }
947 return sequence;
948 }
949
950
951 private XmlSchemaAny handleAny(XmlSchema schema,
952 Element anyEl,
953 Element schemaEl) {
954
955 XmlSchemaAny any = new XmlSchemaAny();
956
957 if (anyEl.hasAttribute("namespace"))
958 any.namespace = anyEl.getAttribute("namespace");
959
960 if (anyEl.hasAttribute("processContents")) {
961 String processContent = getEnumString(anyEl,
962 "processContents");
963
964 any.processContent =
965 new XmlSchemaContentProcessing(processContent);
966 }
967
968 Element annotationEl = XDOMUtil.getFirstChildElementNS(anyEl,
969 XmlSchema.SCHEMA_NS, "annotation");
970
971 if (annotationEl != null) {
972 XmlSchemaAnnotation annotation =
973 handleAnnotation(annotationEl);
974 any.setAnnotation(annotation);
975 }
976 any.minOccurs = getMinOccurs(anyEl);
977 any.maxOccurs = getMaxOccurs(anyEl);
978
979 return any;
980 }
981
982 private XmlSchemaChoice handleChoice(XmlSchema schema, Element choiceEl,
983 Element schemaEl) {
984 XmlSchemaChoice choice = new XmlSchemaChoice();
985
986 if (choiceEl.hasAttribute("id"))
987 choice.id = choiceEl.getAttribute("id");
988
989 choice.minOccurs = getMinOccurs(choiceEl);
990 choice.maxOccurs = getMaxOccurs(choiceEl);
991
992 for (Element el = XDOMUtil.getFirstChildElementNS(choiceEl,
993 XmlSchema.SCHEMA_NS)
994 ; el != null;
995 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
996
997 if (el.getLocalName().equals("sequence")) {
998 XmlSchemaSequence seq =
999 handleSequence(schema, el, schemaEl);
1000 choice.items.add(seq);
1001 } else if (el.getLocalName().equals("element")) {
1002 XmlSchemaElement element =
1003 handleElement(schema, el, schemaEl, false);
1004 choice.items.add(element);
1005 } else if (el.getLocalName().equals("group")) {
1006 XmlSchemaGroupRef group =
1007 handleGroupRef(schema, el, schemaEl);
1008 choice.items.add(group);
1009 } else if (el.getLocalName().equals("choice")) {
1010 XmlSchemaChoice choiceItem =
1011 handleChoice(schema, el, schemaEl);
1012 choice.items.add(choiceItem);
1013 } else if (el.getLocalName().equals("any")) {
1014 XmlSchemaAny any = handleAny(schema, el, schemaEl);
1015 choice.items.add(any);
1016 } else if (el.getLocalName().equals("annotation")) {
1017 XmlSchemaAnnotation annotation = handleAnnotation(el);
1018 choice.setAnnotation(annotation);
1019 }
1020 }
1021 return choice;
1022 }
1023
1024 private XmlSchemaAll handleAll(XmlSchema schema, Element allEl,
1025 Element schemaEl) {
1026
1027 XmlSchemaAll all = new XmlSchemaAll();
1028
1029 for (Element el = XDOMUtil.getFirstChildElementNS(allEl, XmlSchema.SCHEMA_NS);
1030 el != null; el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
1031
1032 if (el.getLocalName().equals("element")) {
1033 XmlSchemaElement element = handleElement(schema, el, schemaEl, false);
1034 all.items.add(element);
1035 } else if (el.getLocalName().equals("annotation")) {
1036 XmlSchemaAnnotation annotation = handleAnnotation(el);
1037 all.setAnnotation(annotation);
1038 }
1039 }
1040 return all;
1041 }
1042
1043 private XmlSchemaGroup handleGroup(XmlSchema schema, Element groupEl,
1044 Element schemaEl) {
1045
1046 XmlSchemaGroup group = new XmlSchemaGroup();
1047 group.name = groupEl.getAttribute("name");
1048
1049 for (Element el = XDOMUtil.getFirstChildElementNS(groupEl,
1050 XmlSchema.SCHEMA_NS);
1051 el != null;
1052 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
1053
1054 if (el.getLocalName().equals("all")) {
1055 group.particle = handleAll(schema, el, schemaEl);
1056 } else if (el.getLocalName().equals("sequence")) {
1057 group.particle = handleSequence(schema, el, schemaEl);
1058 } else if (el.getLocalName().equals("choice")) {
1059 group.particle = handleChoice(schema, el, schemaEl);
1060 } else if (el.getLocalName().equals("annotation")) {
1061 XmlSchemaAnnotation groupAnnotation =
1062 handleAnnotation(el);
1063 group.setAnnotation(groupAnnotation);
1064 }
1065 }
1066 return group;
1067 }
1068
1069 private XmlSchemaAttributeGroup handleAttributeGroup(XmlSchema schema,
1070 Element groupEl,
1071 Element schemaEl) {
1072 XmlSchemaAttributeGroup attrGroup =
1073 new XmlSchemaAttributeGroup();
1074
1075 if (groupEl.hasAttribute("name"))
1076 attrGroup.name = groupEl.getAttribute("name");
1077 if (groupEl.hasAttribute("id"))
1078 attrGroup.id = groupEl.getAttribute("id");
1079
1080 for (Element el = XDOMUtil.getFirstChildElementNS(groupEl,
1081 XmlSchema.SCHEMA_NS);
1082 el != null;
1083 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
1084
1085 if (el.getLocalName().equals("attribute")) {
1086 XmlSchemaAttribute attr =
1087 handleAttribute(schema, el, schemaEl);
1088 attrGroup.attributes.add(attr);
1089 } else if (el.getLocalName().equals("attributeGroup")) {
1090 XmlSchemaAttributeGroupRef attrGroupRef =
1091 handleAttributeGroupRef(el);
1092 attrGroup.attributes.add(attrGroupRef);
1093 } else if (el.getLocalName().equals("anyAttribute")) {
1094 attrGroup.anyAttribute = handleAnyAttribute(schema,
1095 el, schemaEl);
1096 } else if (el.getLocalName().equals("annotation")) {
1097 XmlSchemaAnnotation ann = handleAnnotation(el);
1098 attrGroup.setAnnotation(ann);
1099 }
1100 }
1101 return attrGroup;
1102 }
1103
1104
1105 private XmlSchemaAnyAttribute handleAnyAttribute(XmlSchema schema,
1106 Element anyAttrEl,
1107 Element schemaEl) {
1108
1109 XmlSchemaAnyAttribute anyAttr = new XmlSchemaAnyAttribute();
1110
1111 if (anyAttrEl.hasAttribute("namespace"))
1112 anyAttr.namespace = anyAttrEl.getAttribute("namespace");
1113
1114 if (anyAttrEl.hasAttribute("processContents")) {
1115
1116 String contentProcessing = getEnumString(anyAttrEl,
1117 "processContents");
1118
1119 anyAttr.processContent = new XmlSchemaContentProcessing(contentProcessing);
1120 }
1121 if (anyAttrEl.hasAttribute("id"))
1122 anyAttr.id = anyAttrEl.getAttribute("id");
1123
1124 Element annotationEl =
1125 XDOMUtil.getFirstChildElementNS(anyAttrEl,
1126 XmlSchema.SCHEMA_NS, "annotation");
1127
1128 if (annotationEl != null) {
1129 XmlSchemaAnnotation annotation =
1130 handleAnnotation(annotationEl);
1131
1132 anyAttr.setAnnotation(annotation);
1133 }
1134 return anyAttr;
1135 }
1136
1137 private XmlSchemaGroupRef handleGroupRef(XmlSchema schema,
1138 Element groupEl, Element schemaEl) {
1139
1140 XmlSchemaGroupRef group = new XmlSchemaGroupRef();
1141
1142 Element annotationEl = XDOMUtil.getFirstChildElementNS(groupEl,
1143 XmlSchema.SCHEMA_NS,
1144 "annotation");
1145
1146 if (annotationEl != null) {
1147 XmlSchemaAnnotation annotation =
1148 handleAnnotation(annotationEl);
1149
1150 group.setAnnotation(annotation);
1151 }
1152
1153 if (groupEl.hasAttribute("ref")) {
1154 String ref = groupEl.getAttribute("ref");
1155 group.refName = getRefQName(ref, groupEl);
1156 return group;
1157 }
1158 for (Element el = XDOMUtil.getFirstChildElementNS(groupEl,
1159 XmlSchema.SCHEMA_NS)
1160 ; el != null;
1161 el = XDOMUtil.getNextSiblingElement(el)) {
1162
1163 if (el.getLocalName().equals("sequence")) {
1164 group.particle = handleSequence(schema, el, schemaEl);
1165 } else if (el.getLocalName().equals("all")) {
1166 group.particle = handleAll(schema, el, schemaEl);
1167 } else if (el.getLocalName().equals("choice")) {
1168 group.particle = handleChoice(schema, el,
1169 schemaEl);
1170 }
1171 }
1172 return group;
1173 }
1174
1175 private QName newLocalQName(String pLocalName) {
1176 String uri = schema.logicalTargetNamespace;
1177 if (uri == null) {
1178 uri = Constants.NULL_NS_URI;
1179 }
1180 return new QName(uri, pLocalName);
1181 }
1182
1183
1184 private XmlSchemaAttribute handleAttribute(XmlSchema schema,
1185 Element attrEl, Element schemaEl) {
1186
1187
1188
1189 XmlSchemaAttribute attr = new XmlSchemaAttribute();
1190
1191 if (attrEl.hasAttribute("name")) {
1192 String name = attrEl.getAttribute("name");
1193
1194
1195
1196 attr.name = name;
1197 }
1198
1199 boolean isQualified = schema.getAttributeFormDefault().getValue().equals(XmlSchemaForm.QUALIFIED);
1200 if (attr.name != null) {
1201 final String name = attr.name;
1202 attr.qualifiedName = (isQualified) ? newLocalQName(name) : new QName(name);
1203 }
1204
1205 if (attrEl.hasAttribute("type")) {
1206 String name = attrEl.getAttribute("type");
1207 attr.schemaTypeName = getRefQName(name, attrEl);
1208 }
1209
1210 if (attrEl.hasAttribute("default"))
1211 attr.defaultValue = attrEl.getAttribute("default");
1212
1213 if (attrEl.hasAttribute("fixed"))
1214 attr.fixedValue = attrEl.getAttribute("fixed");
1215
1216 if (attrEl.hasAttribute("form")) {
1217 String formValue = getEnumString(attrEl, "form");
1218 attr.form = new XmlSchemaForm(formValue);
1219 }
1220 if (attrEl.hasAttribute("id"))
1221 attr.id = attrEl.getAttribute("id");
1222
1223
1224 if (attrEl.hasAttribute("use")) {
1225 String useType = getEnumString(attrEl, "use");
1226 attr.use = new XmlSchemaUse(useType);
1227 }
1228 if (attrEl.hasAttribute("ref")) {
1229 String name = attrEl.getAttribute("ref");
1230 attr.refName = getRefQName(name, attrEl);
1231 attr.name = name;
1232 }
1233
1234 Element simpleTypeEl =
1235 XDOMUtil.getFirstChildElementNS(attrEl,
1236 XmlSchema.SCHEMA_NS, "simpleType");
1237
1238 if (simpleTypeEl != null) {
1239 attr.schemaType = handleSimpleType(schema, simpleTypeEl,
1240 schemaEl);
1241 }
1242
1243 Element annotationEl =
1244 XDOMUtil.getFirstChildElementNS(attrEl,
1245 XmlSchema.SCHEMA_NS, "annotation");
1246
1247 if (annotationEl != null) {
1248 XmlSchemaAnnotation annotation =
1249 handleAnnotation(annotationEl);
1250
1251 attr.setAnnotation(annotation);
1252 }
1253
1254 NamedNodeMap attrNodes = attrEl.getAttributes();
1255 Vector attrs = new Vector();
1256 NodeNamespaceContext ctx = null;
1257 for (int i = 0; i < attrNodes.getLength(); i++) {
1258 Attr att = (Attr) attrNodes.item(i);
1259 String attName = att.getName();
1260 if (!attName.equals("name") &&
1261 !attName.equals("type") &&
1262 !attName.equals("default") &&
1263 !attName.equals("fixed") &&
1264 !attName.equals("form") &&
1265 !attName.equals("id") &&
1266 !attName.equals("use") &&
1267 !attName.equals("ref")) {
1268
1269
1270 attrs.add(att);
1271 String value = att.getValue();
1272
1273 if (value.indexOf(":") > -1) {
1274
1275 String prefix = value.substring(0, value.indexOf(":"));
1276 if (ctx == null) {
1277 ctx = new NodeNamespaceContext(attrEl);
1278 }
1279 String namespace = ctx.getNamespaceURI(prefix);
1280 if (!Constants.NULL_NS_URI.equals(namespace)) {
1281 Attr nsAttr = attrEl.getOwnerDocument().createAttribute("xmlns:" + prefix);
1282 nsAttr.setValue(namespace);
1283 attrs.add(nsAttr);
1284 }
1285 }
1286 }
1287 }
1288
1289 if (attrs.size() > 0)
1290 attr.setUnhandledAttributes((Attr[]) attrs.toArray(new Attr[0]));
1291 return attr;
1292 }
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 XmlSchemaElement handleElement(XmlSchema schema,
1329 Element el,
1330 Element schemaEl,
1331 boolean isGlobal) {
1332
1333 XmlSchemaElement element = new XmlSchemaElement();
1334
1335 if (el.getAttributeNode("name") != null)
1336 element.name = el.getAttribute("name");
1337
1338
1339
1340
1341 boolean isQualified = schema.getElementFormDefault().getValue().equals(XmlSchemaForm.QUALIFIED);
1342 if (el.hasAttribute("form")) {
1343 String formDef = el.getAttribute("form");
1344 element.form = new XmlSchemaForm(formDef);
1345 isQualified = formDef.equals(XmlSchemaForm.QUALIFIED);
1346 }
1347
1348 if (element.name != null) {
1349 final String name = element.name;
1350 element.qualifiedName = (isQualified || isGlobal) ? newLocalQName(name) : new QName(Constants.NULL_NS_URI, name);
1351 }
1352
1353 Element annotationEl =
1354 XDOMUtil.getFirstChildElementNS(el,
1355 XmlSchema.SCHEMA_NS,
1356 "annotation");
1357
1358 if (annotationEl != null) {
1359 XmlSchemaAnnotation annotation =
1360 handleAnnotation(annotationEl);
1361
1362 element.setAnnotation(annotation);
1363 }
1364 if (el.getAttributeNode("type") != null) {
1365 String typeName = el.getAttribute("type");
1366 QName typeQName = element.schemaTypeName = getRefQName(typeName, el);
1367
1368 XmlSchemaType type = collection.getTypeByQName(typeQName);
1369 if (type == null) {
1370
1371 collection.addUnresolvedType(typeQName, element);
1372 }
1373 element.schemaType = type;
1374 } else if (el.getAttributeNode("ref") != null) {
1375 String refName = el.getAttribute("ref");
1376 QName refQName = getRefQName(refName, el);
1377 element.setRefName(refQName);
1378 element.name = refQName.getLocalPart();
1379 }
1380
1381 Element simpleTypeEl, complexTypeEl, keyEl, keyrefEl, uniqueEl;
1382
1383 if ((simpleTypeEl = XDOMUtil.getFirstChildElementNS(el,
1384 XmlSchema.SCHEMA_NS, "simpleType")) != null) {
1385
1386 XmlSchemaSimpleType simpleType =
1387 handleSimpleType(schema, simpleTypeEl, schemaEl);
1388 element.schemaType = simpleType;
1389 element.schemaTypeName = simpleType.getQName();
1390 } else if ((complexTypeEl =
1391 XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS,
1392 "complexType")) != null) {
1393
1394 element.schemaType = handleComplexType(schema, complexTypeEl, schemaEl);
1395 }
1396
1397 if ((keyEl = XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS, "key")) != null) {
1398 while(keyEl != null) {
1399 element.constraints.add(handleConstraint(keyEl, "Key"));
1400 keyEl = XDOMUtil.getNextSiblingElement(keyEl, "key");
1401 }
1402 }
1403
1404 if ((keyrefEl = XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS, "keyref")) != null) {
1405 while(keyrefEl != null) {
1406 XmlSchemaKeyref keyRef = (XmlSchemaKeyref) handleConstraint(keyrefEl, "Keyref");
1407 if(keyrefEl.hasAttribute("refer")) {
1408 String name = keyrefEl.getAttribute("refer");
1409 keyRef.refer = getRefQName(name, el);
1410 }
1411 element.constraints.add(keyRef);
1412 keyrefEl = XDOMUtil.getNextSiblingElement(keyrefEl, "keyref");
1413 }
1414 }
1415
1416 if ((uniqueEl = XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS, "unique")) != null) {
1417 while(uniqueEl != null) {
1418 element.constraints.add(handleConstraint(uniqueEl, "Unique"));
1419 uniqueEl = XDOMUtil.getNextSiblingElement(uniqueEl, "unique");
1420 }
1421 }
1422
1423 if (el.hasAttribute("abstract")) {
1424 element.isAbstract =
1425 Boolean.valueOf(el.getAttribute("abstract")).booleanValue();
1426 }
1427
1428 if (el.hasAttribute("block"))
1429 element.block = getDerivation(el, "block");
1430
1431 if (el.hasAttribute("default"))
1432 element.defaultValue = el.getAttribute("default");
1433
1434 if (el.hasAttribute("final"))
1435 element.finalDerivation = getDerivation(el, "final");
1436
1437 if (el.hasAttribute("fixed"))
1438 element.fixedValue = el.getAttribute("fixed");
1439
1440 if (el.hasAttribute("id"))
1441 element.id = el.getAttribute("id");
1442
1443 if (el.hasAttribute("nillable"))
1444 element.isNillable =
1445 Boolean.valueOf(el.getAttribute("nillable")).booleanValue();
1446
1447 if (el.hasAttribute("substitutionGroup")) {
1448 String substitutionGroup = el.getAttribute("substitutionGroup");
1449 element.setSubstitutionGroup(getRefQName(substitutionGroup, el));
1450 }
1451
1452 element.minOccurs = getMinOccurs(el);
1453 element.maxOccurs = getMaxOccurs(el);
1454
1455
1456 processExtensibilityComponents(element,el);
1457
1458 return element;
1459 }
1460
1461 private XmlSchemaIdentityConstraint handleConstraint(
1462 Element constraintEl, String type) {
1463
1464 try {
1465 XmlSchemaIdentityConstraint constraint =
1466 (XmlSchemaIdentityConstraint) Class.forName("org.apache.ws.commons.schema.XmlSchema" + type).newInstance();
1467
1468 if (constraintEl.hasAttribute("name"))
1469 constraint.name = constraintEl.getAttribute("name");
1470
1471 if (constraintEl.hasAttribute("refer")) {
1472 String name = constraintEl.getAttribute("refer");
1473 ((XmlSchemaKeyref) constraint).refer = getRefQName(name, constraintEl);
1474 }
1475 for (Element el = XDOMUtil.getFirstChildElementNS(constraintEl,
1476 XmlSchema.SCHEMA_NS);
1477 el != null;
1478 el = XDOMUtil.getNextSiblingElementNS(el, XmlSchema.SCHEMA_NS)) {
1479
1480
1481
1482
1483 if (el.getLocalName().equals("selector")) {
1484 XmlSchemaXPath selectorXPath =
1485 new XmlSchemaXPath();
1486 selectorXPath.xpath = el.getAttribute("xpath");
1487
1488 Element annotationEl =
1489 XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS,
1490 "annotation");
1491 if (annotationEl != null) {
1492 XmlSchemaAnnotation annotation =
1493 handleAnnotation(annotationEl);
1494
1495 selectorXPath.setAnnotation(annotation);
1496 }
1497 constraint.selector = selectorXPath;
1498 } else if (el.getLocalName().equals("field")) {
1499 XmlSchemaXPath fieldXPath = new XmlSchemaXPath();
1500 fieldXPath.xpath = el.getAttribute("xpath");
1501 constraint.fields.add(fieldXPath);
1502
1503 Element annotationEl =
1504 XDOMUtil.getFirstChildElementNS(el, XmlSchema.SCHEMA_NS,
1505 "annotation");
1506
1507 if (annotationEl != null) {
1508 XmlSchemaAnnotation annotation =
1509 handleAnnotation(annotationEl);
1510
1511 fieldXPath.setAnnotation(annotation);
1512 }
1513 } else if (el.getLocalName().equals("annotation")) {
1514 XmlSchemaAnnotation constraintAnnotation =
1515 handleAnnotation(el);
1516 constraint.setAnnotation(constraintAnnotation);
1517 }
1518 }
1519 return constraint;
1520 } catch (ClassNotFoundException e) {
1521 throw new XmlSchemaException(e.getMessage());
1522 } catch (InstantiationException e) {
1523 throw new XmlSchemaException(e.getMessage());
1524 } catch (IllegalAccessException e) {
1525 throw new XmlSchemaException(e.getMessage());
1526 }
1527 }
1528
1529
1530
1531
1532
1533
1534
1535
1536 XmlSchemaImport handleImport(XmlSchema schema, Element importEl,
1537 Element schemaEl) {
1538
1539 XmlSchemaImport schemaImport = new XmlSchemaImport();
1540
1541 Element annotationEl =
1542 XDOMUtil.getFirstChildElementNS(importEl, XmlSchema.SCHEMA_NS, "annotation");
1543
1544 if (annotationEl != null) {
1545 XmlSchemaAnnotation importAnnotation =
1546 handleAnnotation(annotationEl);
1547 schemaImport.setAnnotation(importAnnotation);
1548 }
1549
1550 final String uri = schemaImport.namespace = importEl.getAttribute("namespace");
1551 schemaImport.schemaLocation =
1552 importEl.getAttribute("schemaLocation");
1553
1554 TargetNamespaceValidator validator = new TargetNamespaceValidator(){
1555 private boolean isEmpty(String pValue) {
1556 return pValue == null || Constants.NULL_NS_URI.equals(pValue);
1557 }
1558 public void validate(XmlSchema pSchema) {
1559 final boolean valid;
1560 if (isEmpty(uri)) {
1561 valid = isEmpty(pSchema.syntacticalTargetNamespace);
1562 } else {
1563 valid = pSchema.syntacticalTargetNamespace.equals(uri);
1564 }
1565 if (!valid) {
1566 throw new XmlSchemaException("An imported schema was announced to have the namespace "
1567 + uri + ", but has the namespace " +
1568 pSchema.syntacticalTargetNamespace);
1569 }
1570 }
1571 };
1572 if ((schemaImport.schemaLocation != null) && (!schemaImport.schemaLocation.equals(""))) {
1573 if(schema.getSourceURI()!=null) {
1574 schemaImport.schema =
1575 resolveXmlSchema(
1576 uri,
1577 schemaImport.schemaLocation,
1578 schema.getSourceURI(),
1579 validator);
1580 } else {
1581 schemaImport.schema =
1582 resolveXmlSchema(
1583 schemaImport.namespace,
1584 schemaImport.schemaLocation,
1585 validator);
1586 }
1587 }
1588 return schemaImport;
1589 }
1590
1591
1592
1593
1594
1595
1596
1597 XmlSchemaInclude handleInclude(final XmlSchema schema,
1598 Element includeEl, Element schemaEl) {
1599
1600 XmlSchemaInclude include = new XmlSchemaInclude();
1601
1602 Element annotationEl =
1603 XDOMUtil.getFirstChildElementNS(includeEl,
1604 XmlSchema.SCHEMA_NS, "annotation");
1605
1606 if (annotationEl != null) {
1607 XmlSchemaAnnotation includeAnnotation =
1608 handleAnnotation(annotationEl);
1609 include.setAnnotation(includeAnnotation);
1610 }
1611
1612 include.schemaLocation =
1613 includeEl.getAttribute("schemaLocation");
1614
1615
1616
1617
1618
1619 final TargetNamespaceValidator validator = newIncludeValidator(schema);
1620 if(schema.getSourceURI()!=null) {
1621 include.schema =
1622 resolveXmlSchema(
1623 schema.logicalTargetNamespace,
1624 include.schemaLocation,
1625 schema.getSourceURI(),
1626 validator);
1627 } else {
1628 include.schema =
1629 resolveXmlSchema(
1630 schema.logicalTargetNamespace,
1631 include.schemaLocation,
1632 validator);
1633 }
1634
1635
1636 processExtensibilityComponents(include,includeEl);
1637 return include;
1638 }
1639
1640 private TargetNamespaceValidator newIncludeValidator(final XmlSchema schema) {
1641 return new TargetNamespaceValidator(){
1642 private boolean isEmpty(String pValue) {
1643 return pValue == null || Constants.NULL_NS_URI.equals(pValue);
1644 }
1645 public void validate(XmlSchema pSchema) {
1646 if (isEmpty(pSchema.syntacticalTargetNamespace)) {
1647 pSchema.logicalTargetNamespace = schema.logicalTargetNamespace;
1648 } else {
1649 if (!pSchema.syntacticalTargetNamespace.equals(schema.logicalTargetNamespace)) {
1650 String msg = "An included schema was announced to have the default target namespace";
1651 if (!isEmpty(schema.logicalTargetNamespace)) {
1652 msg += " or the target namespace " + schema.logicalTargetNamespace;
1653 }
1654 throw new XmlSchemaException(msg + ", but has the target namespace "
1655 + pSchema.logicalTargetNamespace);
1656 }
1657 }
1658 }
1659 };
1660 }
1661
1662
1663
1664
1665
1666
1667
1668 XmlSchemaAnnotation handleAnnotation(Element annotEl) {
1669 XmlSchemaObjectCollection content = new XmlSchemaObjectCollection();
1670 XmlSchemaAppInfo appInfoObj;
1671 XmlSchemaDocumentation docsObj;
1672
1673 for (Element appinfo = XDOMUtil.getFirstChildElementNS(annotEl,
1674 XmlSchema.SCHEMA_NS, "appinfo");
1675 appinfo != null;
1676 appinfo = XDOMUtil.getNextSiblingElementNS(appinfo, XmlSchema.SCHEMA_NS, "appinfo")) {
1677
1678 appInfoObj = handleAppInfo(appinfo);
1679 if (appInfoObj != null) {
1680 content.add(appInfoObj);
1681 }
1682 }
1683 for (Element documentation = XDOMUtil.getFirstChildElementNS(annotEl,
1684 XmlSchema.SCHEMA_NS, "documentation");
1685 documentation != null;
1686 documentation = XDOMUtil.getNextSiblingElementNS(documentation,
1687
1688
1689 XmlSchema.SCHEMA_NS, "documentation")) {
1690
1691 docsObj = handleDocumentation(documentation);
1692 if (docsObj != null) {
1693 content.add(docsObj);
1694 }
1695 }
1696
1697 XmlSchemaAnnotation annotation = new XmlSchemaAnnotation();
1698 annotation.items = content;
1699
1700
1701 processExtensibilityComponents(annotation,annotEl);
1702 return annotation;
1703 }
1704
1705
1706
1707
1708
1709
1710
1711
1712 XmlSchemaAppInfo handleAppInfo(Element content) {
1713 XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo();
1714 NodeList markup = getChild(content);
1715
1716 if (!content.hasAttribute("source") &&
1717 (markup == null || markup.getLength() <= 0)) {
1718 return null;
1719 }
1720 appInfo.setSource(getAttribute(content, "source"));
1721 appInfo.setMarkup(markup);
1722 return appInfo;
1723 }
1724
1725
1726 XmlSchemaDocumentation handleDocumentation(Element content) {
1727 XmlSchemaDocumentation documentation = new XmlSchemaDocumentation();
1728 NodeList markup = getChild(content);
1729
1730 if (!content.hasAttribute("source") &&
1731 !content.hasAttribute("xml:lang") &&
1732 (markup == null || markup.getLength() <= 0))
1733 return null;
1734
1735 documentation.setSource(getAttribute(content, "source"));
1736 documentation.setLanguage(getAttribute(content, "xml:lang"));
1737 documentation.setMarkup(getChild(content));
1738
1739 return documentation;
1740 }
1741
1742 private String getAttribute(Element content, String attrName) {
1743 if (content.hasAttribute(attrName))
1744 return content.getAttribute(attrName);
1745 return null;
1746 }
1747
1748 private NodeList getChild(Element content) {
1749 NodeList childs = content.getChildNodes();
1750 if (childs.getLength() > 0)
1751 return childs;
1752 return null;
1753 }
1754
1755 long getMinOccurs(Element el) {
1756 try {
1757 if (el.getAttributeNode("minOccurs") != null) {
1758 String value = el.getAttribute("minOccurs");
1759 if (value.equals("unbounded"))
1760 return Long.MAX_VALUE;
1761 else
1762 return Long.parseLong(value);
1763 }
1764 return 1;
1765 } catch (java.lang.NumberFormatException e) {
1766 return 1;
1767 }
1768 }
1769
1770 long getMaxOccurs(Element el) {
1771 try {
1772 if (el.getAttributeNode("maxOccurs") != null) {
1773 String value = el.getAttribute("maxOccurs");
1774 if (value.equals("unbounded"))
1775 return Long.MAX_VALUE;
1776 else
1777 return Long.parseLong(value);
1778 }
1779 return 1;
1780 } catch (java.lang.NumberFormatException e) {
1781 return 1;
1782 }
1783 }
1784
1785 XmlSchemaForm getFormDefault(Element el, String attrName) {
1786 if (el.getAttributeNode(attrName) != null) {
1787 String value = el.getAttribute(attrName);
1788 return new XmlSchemaForm(value);
1789 } else
1790 return new XmlSchemaForm("unqualified");
1791 }
1792
1793
1794
1795
1796 XmlSchemaDerivationMethod getDerivation(Element el, String attrName) {
1797 if (el.hasAttribute(attrName) && !el.getAttribute(attrName).equals("")) {
1798
1799 String derivationMethod = el.getAttribute(attrName).trim();
1800 if (derivationMethod.equals("#all"))
1801 return new XmlSchemaDerivationMethod(Constants.BlockConstants.ALL);
1802 else
1803 return new XmlSchemaDerivationMethod(derivationMethod);
1804 }
1805 return new XmlSchemaDerivationMethod(Constants.BlockConstants.NONE);
1806 }
1807
1808
1809 String getEnumString(Element el, String attrName) {
1810 if (el.hasAttribute(attrName)) {
1811 return el.getAttribute(attrName).trim();
1812 }
1813 return Constants.BlockConstants.NONE;
1814 }
1815
1816
1817
1818
1819
1820
1821 XmlSchema resolveXmlSchema(String targetNamespace,
1822 String schemaLocation,
1823 String baseUri,
1824 TargetNamespaceValidator validator) {
1825
1826 InputSource source = collection.schemaResolver.
1827 resolveEntity(targetNamespace,schemaLocation,baseUri);
1828
1829 final String systemId = source.getSystemId() == null ? schemaLocation : source.getSystemId();
1830 final SchemaKey key = new XmlSchemaCollection.SchemaKey(targetNamespace, systemId);
1831 XmlSchema schema = collection.getSchema(key);
1832 if (schema != null) {
1833 return schema;
1834 }
1835 if (collection.check(key)) {
1836 collection.push(key);
1837 try {
1838 return collection.read(source, null, validator);
1839 } catch (Exception e) {
1840 throw new RuntimeException(e);
1841 } finally {
1842 collection.pop();
1843 }
1844 }
1845 return null;
1846 }
1847
1848
1849
1850
1851
1852
1853 XmlSchema resolveXmlSchema(String targetNamespace,
1854 String schemaLocation,
1855 TargetNamespaceValidator validator) {
1856 return resolveXmlSchema(targetNamespace, schemaLocation,
1857 collection.baseUri, validator);
1858
1859 }
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869 private void processExtensibilityComponents(XmlSchemaObject schemaObject,Element parentElement){
1870
1871 if (extReg!=null){
1872
1873 NamedNodeMap attributes = parentElement.getAttributes();
1874 for (int i=0 ;i < attributes.getLength();i++){
1875 Attr attribute = (Attr)attributes.item(i);
1876
1877 String namespaceURI = attribute.getNamespaceURI();
1878 String name = attribute.getLocalName();
1879
1880 if (namespaceURI!= null &&
1881 !"".equals(namespaceURI) &&
1882 !name.startsWith(Constants.XMLNS_ATTRIBUTE) &&
1883 !Constants.URI_2001_SCHEMA_XSD.equals(namespaceURI))
1884
1885 {
1886 QName qName = new QName(namespaceURI,name);
1887 extReg.deserializeExtension(schemaObject,qName,attribute);
1888
1889
1890 }
1891 }
1892
1893
1894 NodeList allChildren = parentElement.getChildNodes();
1895 for (int i=0 ;i < allChildren.getLength();i++){
1896 if (allChildren.item(i).getNodeType()==Node.ELEMENT_NODE){
1897
1898 Element extElement = (Element)allChildren.item(i);
1899
1900 String namespaceURI = extElement.getNamespaceURI();
1901 String name = extElement.getLocalName();
1902
1903 if (namespaceURI!= null &&
1904 !Constants.URI_2001_SCHEMA_XSD.equals(namespaceURI))
1905
1906 {
1907 QName qName = new QName(namespaceURI,name);
1908 extReg.deserializeExtension(schemaObject,qName,extElement);
1909
1910 }
1911 }
1912 }
1913 }
1914
1915 }
1916
1917 }