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