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