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 java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Hashtable;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import javax.xml.XMLConstants;
29 import javax.xml.namespace.QName;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
33
34 import org.apache.ws.commons.schema.constants.Constants;
35 import org.apache.ws.commons.schema.extensions.ExtensionRegistry;
36 import org.apache.ws.commons.schema.utils.NamespacePrefixList;
37 import org.w3c.dom.Attr;
38 import org.w3c.dom.CDATASection;
39 import org.w3c.dom.Comment;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NamedNodeMap;
43 import org.w3c.dom.Node;
44 import org.w3c.dom.NodeList;
45 import org.w3c.dom.Text;
46
47
48
49
50
51 public class XmlSchemaSerializer {
52
53
54
55
56
57 private ExtensionRegistry extReg;
58
59
60
61
62
63 public ExtensionRegistry getExtReg() {
64 return extReg;
65 }
66
67
68
69
70
71 public void setExtReg(ExtensionRegistry extReg) {
72 this.extReg = extReg;
73 }
74
75
76 private Hashtable schema_ns;
77
78 String xsdPrefix = "xs";
79 String xsdNamespace = "http://www.w3.org/2001/XMLSchema";
80 ArrayList docs;
81 Element schemaElement;
82
83 private static final String XMLNS_NAMESPACE_URI = "http://www.w3.org/2000/xmlns/";
84
85
86
87
88 public XmlSchemaSerializer() {
89 docs = new ArrayList();
90 schema_ns = new Hashtable();
91 }
92
93
94
95
96
97
98
99
100
101
102 public Document[] serializeSchema(XmlSchema schemaObj,
103 boolean serializeIncluded) throws XmlSchemaSerializerException {
104 return serializeSchemaElement(schemaObj, serializeIncluded);
105 }
106
107 Document[] serializeSchemaElement(XmlSchema schemaObj,
108 boolean serializeIncluded) throws XmlSchemaSerializerException {
109
110 XmlSchemaObjectCollection items = schemaObj.getItems();
111 Document serializedSchemaDocs;
112 try {
113 DocumentBuilderFactory docFac = DocumentBuilderFactory.newInstance();
114 docFac.setNamespaceAware(true);
115 DocumentBuilder builder = docFac.newDocumentBuilder();
116 serializedSchemaDocs = builder.newDocument();
117 } catch (ParserConfigurationException e) {
118 throw new XmlSchemaException(e.getMessage());
119 }
120
121 Element serializedSchema;
122
123 serializedSchema = setupNamespaces(serializedSchemaDocs, schemaObj);
124 schemaElement = serializedSchema;
125
126 if (schemaObj.syntacticalTargetNamespace != null) {
127 serializedSchema.setAttribute("targetNamespace", schemaObj.syntacticalTargetNamespace);
128
129 String targetNS =
130 (String)schema_ns.get(schemaObj.syntacticalTargetNamespace);
131
132
133
134 if (targetNS == null) {
135 String prefix = null;
136 if(schemaObj.getNamespaceContext() != null) {
137 prefix = schemaObj.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
138 }
139 if(prefix == null && schemaObj.parent != null && schemaObj.parent.getNamespaceContext() != null) {
140 prefix = schemaObj.parent.getNamespaceContext().getPrefix(schemaObj.syntacticalTargetNamespace);
141 }
142
143 if(prefix == null) {
144 if (serializedSchema.getAttributeNode("xmlns") == null) {
145 prefix = "";
146 }
147 } else {
148 String ns = serializedSchema.getAttribute("xmlns:" + prefix);
149 if (ns != null && !"".equals(ns)) {
150 prefix = null;
151 }
152 }
153 if (prefix == null) {
154
155 int count = 0;
156 prefix = "tns";
157 String ns = serializedSchema.getAttribute("xmlns:" + prefix);
158 while (ns != null && !"".equals(ns)) {
159 ++count;
160 prefix = "tns" + count;
161 ns = serializedSchema.getAttribute("xmlns:" + prefix);
162 }
163 }
164 if ("".equals(prefix)) {
165 serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
166 "xmlns", schemaObj.syntacticalTargetNamespace);
167 } else {
168 serializedSchema.setAttributeNS(XMLNS_NAMESPACE_URI,
169 "xmlns:" + prefix, schemaObj.syntacticalTargetNamespace);
170 }
171 schema_ns.put(schemaObj.syntacticalTargetNamespace, prefix);
172 }
173 }
174
175
176
177 if (schemaObj.attributeFormDefault != null) {
178 String formQualified = schemaObj.attributeFormDefault.getValue();
179
180 if (!formQualified.equals(XmlSchemaForm.NONE))
181 serializedSchema.setAttribute("attributeFormDefault", convertString(formQualified));
182 }
183
184 if (schemaObj.elementFormDefault != null) {
185 String formQualified = schemaObj.elementFormDefault.getValue();
186
187 if (!formQualified.equals(XmlSchemaForm.NONE))
188 serializedSchema.setAttribute("elementFormDefault", convertString(formQualified));
189 }
190
191
192 if (schemaObj.annotation != null) {
193 Element annotation = serializeAnnotation(serializedSchemaDocs,
194 schemaObj.annotation, schemaObj);
195 serializedSchema.appendChild(annotation);
196 }
197 if (schemaObj.id != null) {
198 serializedSchema.setAttribute("id",
199 schemaObj.id);
200 }
201 if (schemaObj.blockDefault != null) {
202 String blockDefault = schemaObj.blockDefault.getValue();
203 if (!blockDefault.equals(Constants.BlockConstants.NONE)) {
204 blockDefault = convertString(blockDefault);
205 serializedSchema.setAttribute("blockDefault", blockDefault);
206 }
207 }
208 if (schemaObj.finalDefault != null) {
209 String finalDefault = schemaObj.finalDefault.getValue();
210 if (!finalDefault.equals(Constants.BlockConstants.NONE)) {
211 finalDefault = convertString(finalDefault);
212 serializedSchema.setAttribute("finalDefault", finalDefault);
213 }
214 }
215
216 if (schemaObj.version != null) {
217 serializedSchema.setAttribute("version", schemaObj.version);
218 }
219
220
221
222
223 serializeSchemaChild(items, serializedSchema, serializedSchemaDocs,
224 schemaObj, serializeIncluded);
225
226
227 processExtensibilityComponents(schemaObj,serializedSchema);
228
229
230 serializedSchemaDocs.appendChild(serializedSchema);
231 docs.add(serializedSchemaDocs);
232
233
234 Document[] serializedDocs = new Document[docs.size()];
235 docs.toArray(serializedDocs);
236
237 return serializedDocs;
238 }
239
240 private void serializeSchemaChild(XmlSchemaObjectCollection items,
241 Element serializedSchema, Document serializedSchemaDocs,
242 XmlSchema schemaObj, boolean serializeIncluded)
243 throws XmlSchemaSerializerException {
244
245 int itemsLength = items.getCount();
246
247
248
249
250
251
252
253
254
255
256
257
258 for (int i = 0; i < itemsLength; i++) {
259 XmlSchemaObject obj = items.getItem(i);
260 if (obj instanceof XmlSchemaInclude) {
261 Element e = serializeInclude(serializedSchemaDocs,
262 (XmlSchemaInclude) obj, schemaObj, serializeIncluded);
263 serializedSchema.appendChild(e);
264 } else if (obj instanceof XmlSchemaImport) {
265 Element e = serializeImport(serializedSchemaDocs,
266 (XmlSchemaImport) obj, schemaObj, serializeIncluded);
267 serializedSchema.appendChild(e);
268 }
269 }
270
271
272 for (int i = 0; i < itemsLength; i++) {
273 XmlSchemaObject obj = items.getItem(i);
274
275 if (obj instanceof XmlSchemaElement) {
276 Element e = serializeElement(serializedSchemaDocs,
277 (XmlSchemaElement) obj, schemaObj);
278 serializedSchema.appendChild(e);
279
280 } else if (obj instanceof XmlSchemaSimpleType) {
281 Element e = serializeSimpleType(serializedSchemaDocs,
282 (XmlSchemaSimpleType) obj, schemaObj);
283 serializedSchema.appendChild(e);
284 } else if (obj instanceof XmlSchemaComplexType) {
285 Element e = serializeComplexType(serializedSchemaDocs,
286 (XmlSchemaComplexType) obj, schemaObj);
287 serializedSchema.appendChild(e);
288 } else if (obj instanceof XmlSchemaGroup) {
289 Element e = serializeGroup(serializedSchemaDocs,
290 (XmlSchemaGroup) obj, schemaObj);
291 serializedSchema.appendChild(e);
292 } else if (obj instanceof XmlSchemaAttributeGroup) {
293 Element e = serializeAttributeGroup(serializedSchemaDocs,
294 (XmlSchemaAttributeGroup) obj, schemaObj);
295 serializedSchema.appendChild(e);
296 } else if (obj instanceof XmlSchemaAttribute) {
297 Element e = serializeAttribute(serializedSchemaDocs,
298 (XmlSchemaAttribute) obj, schemaObj);
299 serializedSchema.appendChild(e);
300 } else if (obj instanceof XmlSchemaRedefine) {
301 Element e = serializeRedefine(serializedSchemaDocs,
302 (XmlSchemaRedefine) obj, schemaObj);
303 serializedSchema.appendChild(e);
304 }
305 }
306 }
307
308
309
310
311
312 private Element setupNamespaces(Document schemaDocs, XmlSchema schemaObj) {
313 NamespacePrefixList ctx = schemaObj.getNamespaceContext();
314 if (ctx == null) {
315 schemaObj.schema_ns_prefix = null;
316 xsdPrefix = null;
317 } else {
318 schemaObj.schema_ns_prefix = ctx.getPrefix(xsdNamespace);
319 xsdPrefix = schemaObj.schema_ns_prefix;
320 }
321
322 if(xsdPrefix == null) {
323
324 xsdPrefix = XMLConstants.DEFAULT_NS_PREFIX;
325
326
327 if (ctx != null && !"".equals(ctx.getNamespaceURI(xsdPrefix))) {
328 xsdPrefix = "xsd";
329 }
330 int count = 0;
331 while (ctx != null && !"".equals(ctx.getNamespaceURI(xsdPrefix))) {
332 xsdPrefix = "xsd" + ++count;
333 }
334 schemaObj.schema_ns_prefix = xsdPrefix;
335 }
336
337 Element schemaEl = createNewElement(schemaDocs, "schema",
338 schemaObj.schema_ns_prefix, XmlSchema.SCHEMA_NS);
339
340 if (ctx != null) {
341 String[] prefixes = ctx.getDeclaredPrefixes();
342 for (int i = 0; i < prefixes.length; i++) {
343 String prefix = prefixes[i];
344 String uri = ctx.getNamespaceURI(prefix);
345 if (uri != null && prefix != null) {
346 if ("".equals(prefix) || !schema_ns.containsKey(uri)) {
347 schema_ns.put(uri, prefix);
348 }
349 prefix = (prefix.length() > 0) ? "xmlns:" + prefix : "xmlns";
350 schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
351 prefix, uri);
352 }
353 }
354 }
355
356 if (schema_ns.get(xsdNamespace) == null) {
357 schema_ns.put(xsdNamespace, xsdPrefix);
358 if ("".equals(xsdPrefix)) {
359 schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
360 "xmlns", xsdNamespace);
361 } else {
362 schemaEl.setAttributeNS(XMLNS_NAMESPACE_URI,
363 "xmlns:" + xsdPrefix, xsdNamespace);
364 }
365 schemaObj.schema_ns_prefix = xsdPrefix;
366 }
367 return schemaEl;
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388 Element serializeInclude(Document doc, XmlSchemaInclude includeObj,
389 XmlSchema schema, boolean serializeIncluded)
390 throws XmlSchemaSerializerException {
391
392 Element includeEl = createNewElement(doc, "include",
393 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
394
395 if (includeObj.schemaLocation != null) {
396 includeEl.setAttribute("schemaLocation",
397 includeObj.schemaLocation);
398 }
399
400 if (includeObj.id != null)
401 includeEl.setAttribute("id", includeObj.id);
402
403 if (includeObj.annotation != null) {
404 Element annotation = serializeAnnotation(doc,
405 includeObj.annotation, schema);
406 includeEl.appendChild(annotation);
407 }
408
409
410 XmlSchema includedSchemaObj = includeObj.getSchema();
411 if (includedSchemaObj != null && serializeIncluded) {
412 XmlSchemaSerializer includeSeri = new XmlSchemaSerializer();
413 includeSeri.serializeSchemaElement(includedSchemaObj, true);
414
415 docs.addAll(includeSeri.docs);
416 }
417
418
419 processExtensibilityComponents(includeObj,includeEl);
420
421 return includeEl;
422 }
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442 Element serializeImport(Document doc, XmlSchemaImport importObj,
443 XmlSchema schema, boolean serializeIncluded)
444 throws XmlSchemaSerializerException {
445
446 Element importEl = createNewElement(doc, "import",
447 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
448
449 if (importObj.namespace != null)
450 importEl.setAttribute("namespace",
451 importObj.namespace);
452
453 if (importObj.schemaLocation != null && !importObj.schemaLocation.trim().equals(""))
454 importEl.setAttribute("schemaLocation",
455 importObj.schemaLocation);
456
457 if (importObj.id != null)
458 importEl.setAttribute("id", importObj.id);
459
460 if (importObj.annotation != null) {
461 Element annotation = serializeAnnotation(doc,
462 importObj.annotation, schema);
463
464 importEl.appendChild(annotation);
465 }
466
467 if (importObj.schema != null && serializeIncluded) {
468
469
470 XmlSchemaSerializer importSeri = new XmlSchemaSerializer();
471 importSeri.serializeSchemaElement(importObj.schema, serializeIncluded);
472 docs.addAll(importSeri.docs);
473 }
474
475
476 processExtensibilityComponents(importObj,importEl);
477
478 return importEl;
479 }
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499 Element serializeRedefine(Document doc, XmlSchemaRedefine redefineObj,
500 XmlSchema schema) throws XmlSchemaSerializerException {
501
502 Element redefine = createNewElement(doc, "redefine",
503 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
504
505 if (redefineObj.schemaLocation != null)
506 redefine.setAttribute("schemaLocation",
507 redefineObj.schemaLocation);
508 else
509 throw new XmlSchemaSerializerException("redefine must have "
510 + "schemaLocation fields fill");
511
512 if (redefineObj.id != null)
513 redefine.setAttribute("id", redefineObj.id);
514
515 if (redefineObj.annotation != null) {
516 Element annotation = serializeAnnotation(doc,
517 redefineObj.annotation, schema);
518 redefine.appendChild(annotation);
519 }
520 int itemsLength = redefineObj.items.getCount();
521 for (int i = 0; i < itemsLength; i++) {
522 XmlSchemaObject obj = redefineObj.items.getItem(i);
523 if (obj instanceof XmlSchemaSimpleType) {
524 Element simpleType = serializeSimpleType(doc,
525 (XmlSchemaSimpleType) obj, schema);
526 redefine.appendChild(simpleType);
527 } else if (obj instanceof XmlSchemaComplexType) {
528 Element complexType = serializeComplexType(doc,
529 (XmlSchemaComplexType) obj, schema);
530 redefine.appendChild(complexType);
531 } else if (obj instanceof XmlSchemaGroupRef) {
532 Element groupRef = serializeGroupRef(doc,
533 (XmlSchemaGroupRef) obj, schema);
534 redefine.appendChild(groupRef);
535 } else if (obj instanceof XmlSchemaGroup) {
536 Element group = serializeGroup(doc,
537 (XmlSchemaGroup) obj, schema);
538 redefine.appendChild(group);
539 } else if (obj instanceof XmlSchemaAttributeGroup) {
540 Element attributeGroup = serializeAttributeGroup(doc,
541 (XmlSchemaAttributeGroup) obj, schema);
542 redefine.appendChild(attributeGroup);
543 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
544 Element attributeGroupRef = serializeAttributeGroupRef(doc,
545 (XmlSchemaAttributeGroupRef) obj, schema);
546 redefine.appendChild(attributeGroupRef);
547 }
548 }
549
550
551 processExtensibilityComponents(redefineObj,redefine);
552
553 return redefine;
554 }
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574 Element serializeElement(Document doc, XmlSchemaElement elementObj,
575 XmlSchema schema) throws XmlSchemaSerializerException {
576 Element serializedEl = createNewElement(doc, "element",
577 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
578
579
580 if (elementObj.refName != null) {
581
582 String resolvedName = resolveQName(elementObj.refName, schema);
583 serializedEl.setAttribute("ref", resolvedName);
584 } else if (elementObj.name != null && elementObj.name.length() > 0) {
585 serializedEl.setAttribute("name",
586 elementObj.name);
587 }
588
589 if (elementObj.isAbstract)
590 serializedEl.setAttribute("abstract", "true");
591
592 String block = elementObj.block.getValue();
593 if (!block.equals(Constants.BlockConstants.NONE)) {
594 block = convertString(block);
595 serializedEl.setAttribute("block", block);
596 }
597 if (elementObj.defaultValue != null)
598 serializedEl.setAttribute("default",
599 elementObj.defaultValue);
600
601 String finalDerivation = elementObj.finalDerivation.getValue();
602 if (!finalDerivation.equals(Constants.BlockConstants.NONE)) {
603 finalDerivation = convertString(finalDerivation);
604 serializedEl.setAttribute("final",
605 finalDerivation);
606 }
607 if (elementObj.fixedValue != null)
608 serializedEl.setAttribute("fixed",
609 elementObj.fixedValue);
610
611 String formDef = elementObj.form.getValue();
612 if (!formDef.equals(XmlSchemaForm.NONE)) {
613 formDef = convertString(formDef);
614 serializedEl.setAttribute("form", formDef);
615 }
616 if (elementObj.id != null)
617 serializedEl.setAttribute("id", elementObj.id);
618
619
620 serializeMaxMinOccurs(elementObj, serializedEl);
621
622
623 if (elementObj.substitutionGroup != null) {
624 String resolvedQName = resolveQName(elementObj.substitutionGroup, schema);
625 serializedEl.setAttribute("substitutionGroup",
626 resolvedQName);
627 }
628 if (elementObj.schemaTypeName != null) {
629 String resolvedName = resolveQName(elementObj.schemaTypeName, schema);
630 serializedEl.setAttribute("type", resolvedName);
631 }
632 if (elementObj.annotation != null) {
633 Element annotationEl = serializeAnnotation(doc,
634 elementObj.annotation, schema);
635 serializedEl.appendChild(annotationEl);
636 }
637 if (elementObj.schemaType != null && elementObj.schemaTypeName == null) {
638 if (elementObj.schemaType instanceof XmlSchemaComplexType) {
639
640 Element complexType = serializeComplexType(doc,
641 (XmlSchemaComplexType) elementObj.schemaType, schema);
642 serializedEl.appendChild(complexType);
643 } else if (elementObj.schemaType instanceof XmlSchemaSimpleType) {
644 Element simpleType = serializeSimpleType(doc,
645 (XmlSchemaSimpleType) elementObj.schemaType, schema);
646 serializedEl.appendChild(simpleType);
647 }
648 }
649 if (elementObj.constraints.getCount() > 0) {
650 for (int i = 0; i < elementObj.constraints.getCount(); i++) {
651 Element constraint = serializeIdentityConstraint(doc,
652 (XmlSchemaIdentityConstraint) elementObj.constraints.getItem(i),
653 schema);
654 serializedEl.appendChild(constraint);
655 }
656 }
657 if (elementObj.isNillable) {
658 serializedEl.setAttribute("nillable", "true");
659 }
660
661
662 processExtensibilityComponents(elementObj,serializedEl);
663
664 return serializedEl;
665 }
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686 Element serializeSimpleType(Document doc, XmlSchemaSimpleType simpleTypeObj,
687 XmlSchema schema) throws XmlSchemaSerializerException {
688
689 Element serializedSimpleType = createNewElement(doc, "simpleType",
690 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
691
692
693 String tmp;
694 tmp = simpleTypeObj.finalDerivation.getValue();
695 if (!tmp.equals(Constants.BlockConstants.NONE)) {
696
697 tmp = convertString(tmp);
698 serializedSimpleType.setAttribute("final", tmp);
699 }
700 if (simpleTypeObj.id != null)
701 serializedSimpleType.setAttribute("id",
702 simpleTypeObj.id);
703 if ((simpleTypeObj.name != null) && (!simpleTypeObj.name.equals("")))
704 serializedSimpleType.setAttribute("name",
705 simpleTypeObj.name);
706 if (simpleTypeObj.annotation != null) {
707 Element annotationEl = serializeAnnotation(doc,
708 simpleTypeObj.annotation, schema);
709 serializedSimpleType.appendChild(annotationEl);
710 }
711 if (simpleTypeObj.content != null) {
712 if (simpleTypeObj.content instanceof XmlSchemaSimpleTypeRestriction) {
713 Element restEl = serializeSimpleTypeRestriction(doc,
714 (XmlSchemaSimpleTypeRestriction) simpleTypeObj.content,
715 schema);
716 serializedSimpleType.appendChild(restEl);
717 } else if (simpleTypeObj.content instanceof XmlSchemaSimpleTypeList) {
718 Element listEl = serializeSimpleTypeList(doc,
719 (XmlSchemaSimpleTypeList) simpleTypeObj.content, schema);
720 serializedSimpleType.appendChild(listEl);
721 } else if (simpleTypeObj.content instanceof XmlSchemaSimpleTypeUnion) {
722 Element unionEl = serializeSimpleTypeUnion(doc,
723 (XmlSchemaSimpleTypeUnion) simpleTypeObj.content, schema);
724 serializedSimpleType.appendChild(unionEl);
725 }
726
727
728
729
730
731 }
732
733
734
735
736 processExtensibilityComponents(simpleTypeObj,serializedSimpleType);
737
738 return serializedSimpleType;
739 }
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760 Element serializeSimpleTypeRestriction(Document doc,
761 XmlSchemaSimpleTypeRestriction restrictionObj, XmlSchema schema)
762 throws XmlSchemaSerializerException {
763
764 Element serializedRestriction = createNewElement(doc, "restriction",
765 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
766
767 if (schema.schema_ns_prefix.length() > 0)
768 serializedRestriction.setPrefix(schema.schema_ns_prefix);
769 if (restrictionObj.baseTypeName != null) {
770 String baseType = resolveQName(restrictionObj.baseTypeName, schema);
771 serializedRestriction.setAttribute("base", baseType);
772 } else if (restrictionObj.baseType != null && restrictionObj.baseType
773 instanceof XmlSchemaSimpleType) {
774 Element inlineSimpleType = serializeSimpleType(doc,
775 restrictionObj.baseType, schema);
776 serializedRestriction.appendChild(inlineSimpleType);
777 } else
778 throw new XmlSchemaSerializerException("restriction must be define "
779 + "with specifying base or inline simpleType");
780
781 if (restrictionObj.id != null)
782 serializedRestriction.setAttribute("id",
783 restrictionObj.id);
784
785 if (restrictionObj.annotation != null) {
786 Element annotation = serializeAnnotation(doc,
787 restrictionObj.annotation, schema);
788 serializedRestriction.appendChild(annotation);
789 }
790 if (restrictionObj.facets.getCount() > 0) {
791 int facetsNum = restrictionObj.facets.getCount();
792 for (int i = 0; i < facetsNum; i++) {
793 Element facetEl = serializeFacet(doc,
794 (XmlSchemaFacet) restrictionObj.facets.getItem(i), schema);
795 serializedRestriction.appendChild(facetEl);
796 }
797 }
798
799
800 processExtensibilityComponents(restrictionObj,serializedRestriction);
801
802 return serializedRestriction;
803 }
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822 Element serializeFacet(Document doc, XmlSchemaFacet facetObj,
823 XmlSchema schema) throws XmlSchemaSerializerException {
824
825 Element serializedFacet;
826
827 if (facetObj instanceof XmlSchemaMinExclusiveFacet)
828 serializedFacet = constructFacet(facetObj, doc, schema,
829 "minExclusive");
830 else if (facetObj instanceof XmlSchemaMinInclusiveFacet)
831 serializedFacet = constructFacet(facetObj, doc, schema,
832 "minInclusive");
833 else if (facetObj instanceof XmlSchemaMaxExclusiveFacet)
834 serializedFacet = constructFacet(facetObj, doc, schema,
835 "maxExclusive");
836 else if (facetObj instanceof XmlSchemaMaxInclusiveFacet)
837 serializedFacet = constructFacet(facetObj, doc, schema,
838 "maxInclusive");
839 else if (facetObj instanceof XmlSchemaTotalDigitsFacet)
840 serializedFacet = constructFacet(facetObj, doc, schema,
841 "totalDigits");
842 else if (facetObj instanceof XmlSchemaFractionDigitsFacet)
843 serializedFacet = constructFacet(facetObj, doc, schema,
844 "fractionDigits");
845 else if (facetObj instanceof XmlSchemaLengthFacet)
846 serializedFacet = constructFacet(facetObj, doc, schema,
847 "length");
848 else if (facetObj instanceof XmlSchemaMinLengthFacet)
849 serializedFacet = constructFacet(facetObj, doc, schema,
850 "minLength");
851 else if (facetObj instanceof XmlSchemaMaxLengthFacet)
852 serializedFacet = constructFacet(facetObj, doc, schema,
853 "maxLength");
854 else if (facetObj instanceof XmlSchemaEnumerationFacet)
855 serializedFacet = constructFacet(facetObj, doc, schema,
856 "enumeration");
857 else if (facetObj instanceof XmlSchemaWhiteSpaceFacet)
858 serializedFacet = constructFacet(facetObj, doc, schema,
859 "whiteSpace");
860 else if (facetObj instanceof XmlSchemaPatternFacet)
861 serializedFacet = constructFacet(facetObj, doc, schema,
862 "pattern");
863 else
864 throw new XmlSchemaSerializerException("facet not exist "
865 + facetObj.getClass().getName());
866
867 if (facetObj.id != null)
868 serializedFacet.setAttribute("id", facetObj.id);
869
870
871
872
873
874
875
876 processExtensibilityComponents(facetObj,serializedFacet);
877
878 return serializedFacet;
879 }
880
881 private Element constructFacet(XmlSchemaFacet facetObj, Document doc,
882 XmlSchema schema, String tagName) {
883
884 Element facetEl = createNewElement(doc, tagName,
885 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
886
887 facetEl.setAttribute("value",
888 facetObj.value.toString());
889 if (facetObj.fixed)
890 facetEl.setAttribute("fixed", "true");
891
892 if (facetObj.annotation != null) {
893 Element annotation = serializeAnnotation(doc,
894 facetObj.annotation, schema);
895 facetEl.appendChild(annotation);
896 }
897 return facetEl;
898 }
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919 Element serializeComplexType(Document doc,
920 XmlSchemaComplexType complexTypeObj, XmlSchema schema)
921 throws XmlSchemaSerializerException {
922
923
924 Element serializedComplexType = createNewElement(doc,
925 "complexType", schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
926
927 if ((complexTypeObj.name != null) && (!complexTypeObj.name.equals("")))
928 serializedComplexType.setAttribute("name",
929 complexTypeObj.name);
930
931
932
933
934
935
936 if (complexTypeObj.isMixed)
937 serializedComplexType.setAttribute("mixed", "true");
938 if (complexTypeObj.isAbstract)
939 serializedComplexType.setAttribute(
940 "abstract", "true");
941 if (complexTypeObj.id != null)
942 serializedComplexType.setAttribute("id",
943 complexTypeObj.id);
944
945 if (complexTypeObj.annotation != null) {
946 Element annotationEl = serializeAnnotation(doc,
947 complexTypeObj.annotation, schema);
948 serializedComplexType.appendChild(annotationEl);
949 }
950
951 if (complexTypeObj.contentModel instanceof XmlSchemaSimpleContent) {
952 Element simpleContent = serializeSimpleContent(doc,
953 (XmlSchemaSimpleContent) complexTypeObj.contentModel, schema);
954 serializedComplexType.appendChild(simpleContent);
955 } else if (complexTypeObj.contentModel instanceof
956 XmlSchemaComplexContent) {
957
958 Element complexContent = serializeComplexContent(doc,
959 (XmlSchemaComplexContent) complexTypeObj.contentModel, schema);
960 serializedComplexType.appendChild(complexContent);
961 }
962
963 if (complexTypeObj.particle instanceof XmlSchemaSequence) {
964 Element sequence = serializeSequence(doc,
965 (XmlSchemaSequence) complexTypeObj.particle, schema);
966 serializedComplexType.appendChild(sequence);
967 } else if (complexTypeObj.particle instanceof XmlSchemaChoice) {
968 Element choice = serializeChoice(doc,
969 (XmlSchemaChoice) complexTypeObj.particle, schema);
970 serializedComplexType.appendChild(choice);
971 } else if (complexTypeObj.particle instanceof XmlSchemaAll) {
972 Element all = serializeAll(doc,
973 (XmlSchemaAll) complexTypeObj.particle, schema);
974 serializedComplexType.appendChild(all);
975 } else if (complexTypeObj.particle instanceof XmlSchemaGroupRef) {
976 Element group = serializeGroupRef(doc,
977 (XmlSchemaGroupRef) complexTypeObj.particle, schema);
978 serializedComplexType.appendChild(group);
979 }
980
981 String block = complexTypeObj.block.getValue();
982 if (!block.equals(Constants.BlockConstants.NONE)) {
983 block = convertString(block);
984 serializedComplexType.setAttribute(
985 "block", block);
986 }
987 String finalDerivation = complexTypeObj.finalDerivation.getValue();
988 if (!finalDerivation.equals(Constants.BlockConstants.NONE)) {
989 finalDerivation = convertString(finalDerivation);
990 serializedComplexType.setAttribute("final",
991 finalDerivation);
992 }
993
994 XmlSchemaObjectCollection attrColl = complexTypeObj.attributes;
995 if (attrColl.getCount() > 0)
996 setupAttr(doc, attrColl, schema, serializedComplexType);
997
998 XmlSchemaAnyAttribute anyAttribute = complexTypeObj.getAnyAttribute();
999 if(anyAttribute != null) {
1000 serializedComplexType.appendChild(serializeAnyAttribute(doc, anyAttribute, schema));
1001 }
1002
1003
1004
1005 processExtensibilityComponents(complexTypeObj,serializedComplexType);
1006
1007 return serializedComplexType;
1008 }
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 Element serializeSequence(Document doc, XmlSchemaSequence sequenceObj,
1029 XmlSchema schema) throws XmlSchemaSerializerException {
1030
1031 Element sequence = createNewElement(doc, "sequence",
1032 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1033
1034
1035 if (sequenceObj.id != null)
1036 sequence.setAttribute("id", sequenceObj.id);
1037
1038
1039 serializeMaxMinOccurs(sequenceObj, sequence);
1040
1041 XmlSchemaObjectCollection seqColl = sequenceObj.items;
1042 int containLength = seqColl.getCount();
1043 for (int i = 0; i < containLength; i++) {
1044 XmlSchemaObject obj = seqColl.getItem(i);
1045 if (obj instanceof XmlSchemaElement) {
1046 Element el = serializeElement(doc,
1047 (XmlSchemaElement) obj, schema);
1048 sequence.appendChild(el);
1049 } else if (obj instanceof XmlSchemaGroupRef) {
1050 Element group = serializeGroupRef(doc,
1051 (XmlSchemaGroupRef) obj, schema);
1052 sequence.appendChild(group);
1053 } else if (obj instanceof XmlSchemaChoice) {
1054 Element choice = serializeChoice(doc,
1055 (XmlSchemaChoice) obj, schema);
1056 sequence.appendChild(choice);
1057 } else if (obj instanceof XmlSchemaSequence) {
1058 Element sequenceChild = serializeSequence(doc,
1059 (XmlSchemaSequence) obj, schema);
1060 sequence.appendChild(sequenceChild);
1061 } else if (obj instanceof XmlSchemaAny) {
1062 Element any = serializeAny(doc, (XmlSchemaAny) obj, schema);
1063 sequence.appendChild(any);
1064 }
1065 }
1066
1067
1068 processExtensibilityComponents(sequenceObj,sequence);
1069
1070 return sequence;
1071 }
1072
1073
1074
1075
1076
1077
1078 private void serializeMaxMinOccurs(XmlSchemaParticle particle,
1079 Element element) {
1080 if (particle.maxOccurs < Long.MAX_VALUE &&
1081 (particle.maxOccurs > 1 || particle.maxOccurs == 0))
1082 element.setAttribute("maxOccurs",
1083 particle.maxOccurs + "");
1084 else if (particle.maxOccurs == Long.MAX_VALUE)
1085 element.setAttribute("maxOccurs",
1086 "unbounded");
1087
1088
1089
1090
1091 if (particle.minOccurs > 1 || particle.minOccurs == 0)
1092 element.setAttribute("minOccurs",
1093 particle.minOccurs + "");
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114 Element serializeAttribute(Document doc, XmlSchemaAttribute attributeObj,
1115 XmlSchema schema) throws XmlSchemaSerializerException {
1116
1117 Element attribute = createNewElement(doc, "attribute",
1118 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1119 if (attributeObj.refName != null) {
1120 String refName =
1121 resolveQName(attributeObj.refName, schema);
1122 attribute.setAttribute("ref", refName);
1123 } else if (attributeObj.name != null)
1124 attribute.setAttribute("name",
1125 attributeObj.name);
1126
1127 if (attributeObj.schemaTypeName != null) {
1128 String typeName =
1129 resolveQName(attributeObj.schemaTypeName, schema);
1130 attribute.setAttribute("type", typeName);
1131 }
1132
1133 if (attributeObj.defaultValue != null)
1134 attribute.setAttribute("default",
1135 attributeObj.defaultValue);
1136 if (attributeObj.fixedValue != null)
1137 attribute.setAttribute("fixed",
1138 attributeObj.fixedValue);
1139
1140 String formType = attributeObj.form.getValue();
1141 if (!formType.equals(XmlSchemaForm.NONE)) {
1142 formType = convertString(formType);
1143 attribute.setAttribute("form", formType);
1144 }
1145 if (attributeObj.id != null)
1146 attribute.setAttribute("id", attributeObj.id);
1147
1148 String useType = attributeObj.use.getValue();
1149 if (!useType.equals(Constants.BlockConstants.NONE)) {
1150 useType = convertString(useType);
1151 attribute.setAttribute("use", useType);
1152 }
1153 if (attributeObj.annotation != null) {
1154 Element annotation = serializeAnnotation(doc,
1155 attributeObj.annotation, schema);
1156 attribute.appendChild(annotation);
1157 }
1158
1159
1160 if (attributeObj.schemaType != null) {
1161 try {
1162 XmlSchemaSimpleType simpleType =
1163 attributeObj.schemaType;
1164 Element simpleTypeEl = serializeSimpleType(doc,
1165 simpleType, schema);
1166 attribute.appendChild(simpleTypeEl);
1167 } catch (ClassCastException e) {
1168 throw new XmlSchemaSerializerException("only inline simple type allow as attribute's inline type");
1169 }
1170 }
1171
1172 Attr[] unhandled = attributeObj.getUnhandledAttributes();
1173
1174 Hashtable namespaces = new Hashtable();
1175
1176 if (unhandled != null) {
1177
1178
1179
1180
1181
1182
1183
1184
1185 for (int i = 0; i < unhandled.length; i++) {
1186 String name = unhandled[i].getNodeName();
1187 String value = unhandled[i].getNodeValue();
1188 if (name.equals("xmlns")) {
1189 namespaces.put("", value);
1190 } else if (name.startsWith("xmlns")) {
1191 namespaces.put(name.substring(name.indexOf(":") + 1), value);
1192 }
1193 }
1194
1195 for (int i = 0; i < unhandled.length; i++) {
1196 String value = unhandled[i].getNodeValue();
1197 String nodeName = unhandled[i].getNodeName();
1198 if (value.indexOf(":") > -1 && !nodeName.startsWith("xmlns")) {
1199 String prefix = value.substring(0, value.indexOf(":"));
1200 String oldNamespace;
1201 if ((oldNamespace = (String) namespaces.get(prefix)) != null) {
1202 value = value.substring(value.indexOf(":") + 1);
1203 NamespacePrefixList ctx = schema.getNamespaceContext();
1204 String[] prefixes = ctx.getDeclaredPrefixes();
1205 for (int j = 0; j < prefixes.length; j++) {
1206 String pref = prefixes[j];
1207 String uri = ctx.getNamespaceURI(pref);
1208 if (uri.equals(oldNamespace)) {
1209 value = prefix + ":" + value;
1210 }
1211 }
1212 }
1213
1214 }
1215 if (unhandled[i].getNamespaceURI() != null)
1216 attribute.setAttributeNS(unhandled[i].getNamespaceURI(), nodeName, value);
1217 else
1218 attribute.setAttribute(nodeName, value);
1219 }
1220 }
1221
1222
1223 processExtensibilityComponents(attributeObj,attribute);
1224
1225 return attribute;
1226 }
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246 Element serializeChoice(Document doc, XmlSchemaChoice choiceObj,
1247 XmlSchema schema) throws XmlSchemaSerializerException {
1248
1249
1250 Element choice = createNewElement(doc, "choice",
1251 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1252 if (choiceObj.id != null)
1253 if (choiceObj.id.length() > 0)
1254 choice.setAttribute("id", choiceObj.id);
1255
1256
1257 serializeMaxMinOccurs(choiceObj, choice);
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 if (choiceObj.annotation != null) {
1271 Element annotation = serializeAnnotation(doc,
1272 choiceObj.annotation, schema);
1273 choice.appendChild(annotation);
1274 }
1275
1276
1277 XmlSchemaObjectCollection itemColl = choiceObj.items;
1278
1279 if (itemColl != null) {
1280 int itemLength = itemColl.getCount();
1281
1282 for (int i = 0; i < itemLength; i++) {
1283 XmlSchemaObject obj = itemColl.getItem(i);
1284
1285 if (obj instanceof XmlSchemaElement) {
1286 Element el = serializeElement(doc,
1287 (XmlSchemaElement) obj, schema);
1288 choice.appendChild(el);
1289 } else if (obj instanceof XmlSchemaGroupRef) {
1290 Element group = serializeGroupRef(doc,
1291 (XmlSchemaGroupRef) obj, schema);
1292 choice.appendChild(group);
1293 } else if (obj instanceof XmlSchemaChoice) {
1294 Element inlineChoice = serializeChoice(doc,
1295 (XmlSchemaChoice) obj, schema);
1296 choice.appendChild(inlineChoice);
1297 } else if (obj instanceof XmlSchemaSequence) {
1298 Element inlineSequence = serializeSequence(doc,
1299 (XmlSchemaSequence) obj, schema);
1300 choice.appendChild(inlineSequence);
1301 } else if (obj instanceof XmlSchemaAny) {
1302 Element any = serializeAny(doc, (XmlSchemaAny) obj, schema);
1303 choice.appendChild(any);
1304 }
1305 }
1306 }
1307
1308
1309 processExtensibilityComponents(choiceObj,choice);
1310
1311 return choice;
1312 }
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332 Element serializeAll(Document doc, XmlSchemaAll allObj, XmlSchema schema)
1333 throws XmlSchemaSerializerException {
1334 Element allEl = createNewElement(doc, "all", schema.schema_ns_prefix,
1335 XmlSchema.SCHEMA_NS);
1336
1337 serializeMaxMinOccurs(allObj, allEl);
1338
1339 if (allObj.annotation != null) {
1340 Element annotation = serializeAnnotation(doc, allObj.annotation,
1341 schema);
1342 allEl.appendChild(annotation);
1343 }
1344
1345 XmlSchemaObjectCollection itemColl = allObj.items;
1346
1347 if (itemColl != null) {
1348 int itemLength = itemColl.getCount();
1349
1350 for (int i = 0; i < itemLength; i++) {
1351 XmlSchemaObject obj = itemColl.getItem(i);
1352 if (obj instanceof XmlSchemaElement) {
1353 Element el = serializeElement(doc, (XmlSchemaElement) obj,
1354 schema);
1355 allEl.appendChild(el);
1356 } else
1357 throw new XmlSchemaSerializerException("Only element "
1358 + "allowed as child of all model type");
1359 }
1360 }
1361
1362
1363 processExtensibilityComponents(allObj,allEl);
1364
1365 return allEl;
1366 }
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387 Element serializeSimpleTypeList(Document doc,
1388 XmlSchemaSimpleTypeList listObj, XmlSchema schema)
1389 throws XmlSchemaSerializerException {
1390
1391 Element list = createNewElement(doc, "list",
1392 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1393
1394 if (listObj.itemTypeName != null) {
1395 String listItemType = resolveQName(listObj.itemTypeName,
1396 schema);
1397 list.setAttribute("itemType", listItemType);
1398 }
1399 if (listObj.id != null)
1400 list.setAttribute("id", listObj.id);
1401
1402 else if (listObj.itemType != null) {
1403 Element inlineSimpleEl = serializeSimpleType(doc, listObj.itemType,
1404 schema);
1405 list.appendChild(inlineSimpleEl);
1406 }
1407 if (listObj.annotation != null) {
1408 Element annotation = serializeAnnotation(doc, listObj.annotation, schema);
1409 list.appendChild(annotation);
1410 }
1411
1412
1413 processExtensibilityComponents(listObj,list);
1414
1415 return list;
1416 }
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 Element serializeSimpleTypeUnion(Document doc,
1438 XmlSchemaSimpleTypeUnion unionObj, XmlSchema schema)
1439 throws XmlSchemaSerializerException {
1440
1441
1442 Element union = createNewElement(doc, "union",
1443 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1444 if (unionObj.id != null)
1445 union.setAttribute("id", unionObj.id);
1446
1447 if (unionObj.memberTypesSource != null)
1448 union.setAttribute("memberTypes",
1449 unionObj.memberTypesSource);
1450 if (unionObj.baseTypes.getCount() > 0) {
1451 int baseTypesLength = unionObj.baseTypes.getCount();
1452 Element baseType;
1453 for (int i = 0; i < baseTypesLength; i++) {
1454 try {
1455 baseType = serializeSimpleType(doc,
1456 (XmlSchemaSimpleType) unionObj.baseTypes.getItem(i),
1457 schema);
1458 union.appendChild(baseType);
1459 } catch (ClassCastException e) {
1460 throw new XmlSchemaSerializerException("only inline simple type allow as attribute's "
1461 + "inline type");
1462 }
1463 }
1464 }
1465 if (unionObj.annotation != null) {
1466 Element annotation = serializeAnnotation(doc, unionObj.annotation,
1467 schema);
1468 union.appendChild(annotation);
1469 }
1470
1471
1472 processExtensibilityComponents(unionObj,union);
1473
1474 return union;
1475 }
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494 Element serializeAny(Document doc, XmlSchemaAny anyObj, XmlSchema schema) {
1495 Element anyEl = createNewElement(doc, "any", schema.schema_ns_prefix,
1496 XmlSchema.SCHEMA_NS);
1497 if (anyObj.id != null)
1498 if (anyObj.id.length() > 0)
1499 anyEl.setAttribute("id", anyObj.id);
1500
1501 serializeMaxMinOccurs(anyObj, anyEl);
1502
1503
1504 if (anyObj.namespace != null)
1505 anyEl.setAttribute("namespace",
1506 anyObj.namespace);
1507
1508 if (anyObj.processContent != null) {
1509 String value = anyObj.processContent.getValue();
1510 if (!value.equals(Constants.BlockConstants.NONE)) {
1511 String processContent = convertString(value);
1512 anyEl.setAttribute("processContents",
1513 processContent);
1514 }
1515 }
1516 if (anyObj.annotation != null) {
1517 Element annotation = serializeAnnotation(doc,
1518 anyObj.annotation, schema);
1519 anyEl.appendChild(annotation);
1520 }
1521
1522
1523 processExtensibilityComponents(anyObj,anyEl);
1524
1525 return anyEl;
1526 }
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546 Element serializeGroup(Document doc, XmlSchemaGroup groupObj,
1547 XmlSchema schema) throws XmlSchemaSerializerException {
1548
1549 Element group = createNewElement(doc, "group",
1550 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1551
1552 if (groupObj.name != null) {
1553 String grpName = groupObj.name.getLocalPart();
1554 if (grpName.length() > 0) {
1555 group.setAttribute("name", grpName);
1556 }
1557 } else
1558 throw new XmlSchemaSerializerException("Group must have " +
1559 "name or ref");
1560
1561
1562 if (groupObj.annotation != null) {
1563 Element annotation = serializeAnnotation(doc,
1564 groupObj.annotation, schema);
1565 group.appendChild(annotation);
1566 }
1567
1568 if (groupObj.particle instanceof XmlSchemaSequence) {
1569 Element sequence = serializeSequence(doc,
1570 (XmlSchemaSequence) groupObj.particle, schema);
1571 group.appendChild(sequence);
1572 } else if (groupObj.particle instanceof XmlSchemaChoice) {
1573 Element choice = serializeChoice(doc,
1574 (XmlSchemaChoice) groupObj.particle, schema);
1575 group.appendChild(choice);
1576 } else if (groupObj.particle instanceof XmlSchemaAll) {
1577 Element all = serializeAll(doc,
1578 (XmlSchemaAll) groupObj.particle, schema);
1579 group.appendChild(all);
1580 }
1581
1582
1583 processExtensibilityComponents(groupObj,group);
1584
1585 return group;
1586 }
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606 Element serializeGroupRef(Document doc, XmlSchemaGroupRef groupRefObj,
1607 XmlSchema schema) throws XmlSchemaSerializerException {
1608
1609 Element groupRef = createNewElement(doc, "group",
1610 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1611
1612 if (groupRefObj.refName != null) {
1613 String groupRefName = resolveQName(groupRefObj.refName,
1614 schema);
1615 groupRef.setAttribute("ref", groupRefName);
1616 } else
1617 throw new XmlSchemaSerializerException("Group must have name or ref");
1618
1619
1620 serializeMaxMinOccurs(groupRefObj, groupRef);
1621
1622
1623
1624 if (groupRefObj.particle != null) {
1625 if (groupRefObj.particle instanceof XmlSchemaChoice)
1626 serializeChoice(doc, (XmlSchemaChoice) groupRefObj.particle, schema);
1627 else if (groupRefObj.particle instanceof XmlSchemaSequence)
1628 serializeSequence(doc,(XmlSchemaSequence) groupRefObj.particle, schema);
1629 else if (groupRefObj.particle instanceof XmlSchemaAll)
1630 serializeAll(doc,(XmlSchemaAll) groupRefObj.particle, schema);
1631 else
1632 throw new XmlSchemaSerializerException("The content of group "
1633 + "ref particle should be"
1634 + " sequence, choice or all reference: "
1635 + "www.w3.org/TR/xmlschema-1#element-group-3.7.2");
1636 }
1637 if (groupRefObj.annotation != null) {
1638 Element annotation = serializeAnnotation(doc,
1639 groupRefObj.annotation, schema);
1640 groupRef.appendChild(annotation);
1641 }
1642
1643
1644 processExtensibilityComponents(groupRefObj,groupRef);
1645
1646 return groupRef;
1647 }
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668 Element serializeSimpleContent(Document doc,
1669 XmlSchemaSimpleContent simpleContentObj, XmlSchema schema)
1670 throws XmlSchemaSerializerException {
1671 Element simpleContent = createNewElement(doc, "simpleContent",
1672 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1673
1674 Element content;
1675 if (simpleContentObj.annotation != null) {
1676 Element annotation = serializeAnnotation(doc,
1677 simpleContentObj.annotation, schema);
1678 simpleContent.appendChild(annotation);
1679 }
1680 if (simpleContentObj.content instanceof
1681 XmlSchemaSimpleContentRestriction)
1682 content = serializeSimpleContentRestriction(doc,
1683 (XmlSchemaSimpleContentRestriction) simpleContentObj.content,
1684 schema);
1685 else if (simpleContentObj.content instanceof
1686 XmlSchemaSimpleContentExtension)
1687 content = serializeSimpleContentExtension(doc,
1688 (XmlSchemaSimpleContentExtension) simpleContentObj.content,
1689 schema);
1690 else
1691 throw new XmlSchemaSerializerException("content of simple content "
1692 + "must be restriction or extension");
1693
1694 simpleContent.appendChild(content);
1695
1696
1697 processExtensibilityComponents(simpleContentObj,simpleContent);
1698
1699 return simpleContent;
1700 }
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721 Element serializeComplexContent(Document doc,
1722 XmlSchemaComplexContent complexContentObj, XmlSchema schema)
1723 throws XmlSchemaSerializerException {
1724
1725 Element complexContent = createNewElement(doc, "complexContent",
1726 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1727
1728
1729 if (complexContentObj.annotation != null) {
1730 Element annotation = serializeAnnotation(doc,
1731 complexContentObj.annotation, schema);
1732 complexContent.appendChild(annotation);
1733 }
1734
1735 if (complexContentObj.mixed)
1736 complexContent.setAttribute("mixed", "true");
1737 if (complexContentObj.id != null)
1738 complexContent.setAttribute("id",
1739 complexContentObj.id);
1740
1741 Element content;
1742 if (complexContentObj.content instanceof
1743 XmlSchemaComplexContentRestriction)
1744
1745 content = serializeComplexContentRestriction(doc,
1746 (XmlSchemaComplexContentRestriction) complexContentObj.content,
1747 schema);
1748 else if (complexContentObj.content instanceof
1749 XmlSchemaComplexContentExtension)
1750 content = serializeComplexContentExtension(doc,
1751 (XmlSchemaComplexContentExtension) complexContentObj.content,
1752 schema);
1753 else
1754 throw new XmlSchemaSerializerException("content of complexContent "
1755 + "must be restriction or extension");
1756
1757 complexContent.appendChild(content);
1758
1759
1760 processExtensibilityComponents(complexContentObj,complexContent);
1761
1762 return complexContent;
1763 }
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784 Element serializeIdentityConstraint(Document doc,
1785 XmlSchemaIdentityConstraint constraintObj, XmlSchema schema)
1786 throws XmlSchemaSerializerException {
1787
1788 Element constraint;
1789
1790 if (constraintObj instanceof XmlSchemaUnique)
1791 constraint = createNewElement(doc, "unique",
1792 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1793 else if (constraintObj instanceof XmlSchemaKey)
1794 constraint = createNewElement(doc, "key",
1795 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1796 else if (constraintObj instanceof XmlSchemaKeyref) {
1797 constraint = createNewElement(doc, "keyref",
1798 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1799 XmlSchemaKeyref keyref = (XmlSchemaKeyref) constraintObj;
1800 if (keyref.refer != null) {
1801 String keyrefStr = resolveQName(keyref.refer, schema);
1802 constraint.setAttribute(
1803 "refer", keyrefStr);
1804 }
1805 } else
1806 throw new XmlSchemaSerializerException("not valid identity "
1807 + "constraint");
1808
1809 if (constraintObj.name != null)
1810 constraint.setAttribute("name",
1811 constraintObj.name);
1812 if (constraintObj.annotation != null) {
1813 Element annotation = serializeAnnotation(doc,
1814 constraintObj.annotation, schema);
1815 constraint.appendChild(annotation);
1816 }
1817
1818 if (constraintObj.selector != null) {
1819 Element selector = serializeSelector(doc,
1820 constraintObj.selector, schema);
1821 constraint.appendChild(selector);
1822 }
1823 XmlSchemaObjectCollection fieldColl = constraintObj.fields;
1824 if (fieldColl != null) {
1825 int fieldLength = fieldColl.getCount();
1826 for (int i = 0; i < fieldLength; i++) {
1827 Element field = serializeField(doc,
1828 (XmlSchemaXPath) fieldColl.getItem(i), schema);
1829 constraint.appendChild(field);
1830 }
1831 }
1832
1833
1834 processExtensibilityComponents(constraintObj,constraint);
1835
1836 return constraint;
1837 }
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858 Element serializeSelector(Document doc, XmlSchemaXPath selectorObj,
1859 XmlSchema schema) throws XmlSchemaSerializerException {
1860
1861 Element selector = createNewElement(doc, "selector",
1862 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1863
1864 if (selectorObj.xpath != null)
1865 selector.setAttribute("xpath",
1866 selectorObj.xpath);
1867 else
1868 throw new XmlSchemaSerializerException("xpath can't be null");
1869
1870 if (selectorObj.annotation != null) {
1871 Element annotation = serializeAnnotation(doc,
1872 selectorObj.annotation, schema);
1873 selector.appendChild(annotation);
1874 }
1875
1876 processExtensibilityComponents(selectorObj,selector);
1877 return selector;
1878 }
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898 Element serializeField(Document doc, XmlSchemaXPath fieldObj,
1899 XmlSchema schema) throws XmlSchemaSerializerException {
1900
1901 Element field = createNewElement(doc, "field", schema.schema_ns_prefix,
1902 XmlSchema.SCHEMA_NS);
1903
1904 if (fieldObj.xpath != null)
1905 field.setAttribute("xpath", fieldObj.xpath);
1906 else
1907 throw new XmlSchemaSerializerException("xpath can't be null");
1908
1909 if (fieldObj.annotation != null) {
1910 Element annotation = serializeAnnotation(doc,
1911 fieldObj.annotation, schema);
1912 field.appendChild(annotation);
1913 }
1914
1915
1916 processExtensibilityComponents(fieldObj,field);
1917
1918 return field;
1919 }
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 Element serializeAnnotation(Document doc, XmlSchemaAnnotation annotationObj,
1942 XmlSchema schema) {
1943
1944 Element annotation = createNewElement(doc, "annotation",
1945 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1946
1947 XmlSchemaObjectCollection contents = annotationObj.items;
1948 int contentLength = contents.getCount();
1949
1950 for (int i = 0; i < contentLength; i++) {
1951 XmlSchemaObject obj = contents.getItem(i);
1952
1953 if (obj instanceof XmlSchemaAppInfo) {
1954 XmlSchemaAppInfo appinfo = (XmlSchemaAppInfo) obj;
1955 Element appInfoEl = serializeAppInfo(doc, appinfo, schema);
1956 annotation.appendChild(appInfoEl);
1957 } else if (obj instanceof XmlSchemaDocumentation) {
1958 XmlSchemaDocumentation documentation =
1959 (XmlSchemaDocumentation) obj;
1960
1961 Element documentationEl = serializeDocumentation(doc,
1962 documentation, schema);
1963
1964
1965 annotation.appendChild(documentationEl);
1966 }
1967 }
1968
1969
1970 processExtensibilityComponents(annotationObj,annotation);
1971
1972 return annotation;
1973 }
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994 Element serializeAppInfo(Document doc, XmlSchemaAppInfo appInfoObj,
1995 XmlSchema schema) {
1996
1997 Element appInfoEl = createNewElement(doc, "appinfo",
1998 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
1999 if (appInfoObj.source != null)
2000 appInfoEl.setAttribute("source",
2001 appInfoObj.source);
2002
2003 if (appInfoObj.markup != null) {
2004 int markupLength = appInfoObj.markup.getLength();
2005 for (int j = 0; j < markupLength; j++) {
2006 Node n = (Node) appInfoObj.markup.item(j);
2007 appInfoEl.appendChild(doc.importNode(n,true));
2008 }
2009 }
2010
2011
2012 processExtensibilityComponents(appInfoObj,appInfoEl);
2013
2014 return appInfoEl;
2015 }
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036 Element serializeDocumentation(Document doc, XmlSchemaDocumentation
2037 documentationObj, XmlSchema schema) {
2038
2039
2040 Element documentationEl = createNewElement(doc, "documentation",
2041 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2042 if (documentationObj.source != null)
2043 documentationEl.setAttribute("source",
2044 documentationObj.source);
2045 if (documentationObj.language != null)
2046 documentationEl.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:lang",
2047 documentationObj.language);
2048
2049 if (documentationObj.markup != null) {
2050 int markupLength = documentationObj.markup.getLength();
2051 for (int j = 0; j < markupLength; j++) {
2052 Node n = (Node) documentationObj.markup.item(j);
2053
2054 switch (n.getNodeType()) {
2055 case Node.ELEMENT_NODE:
2056 appendElement(doc, documentationEl, n, schema);
2057 break;
2058 case Node.TEXT_NODE:
2059 Text t = doc.createTextNode(n.getNodeValue());
2060 documentationEl.appendChild(t);
2061 break;
2062 case Node.CDATA_SECTION_NODE:
2063 CDATASection s = doc.createCDATASection(n.getNodeValue());
2064 documentationEl.appendChild(s);
2065 break;
2066 case Node.COMMENT_NODE:
2067 Comment c = doc.createComment(n.getNodeValue());
2068 documentationEl.appendChild(c);
2069 break;
2070 default:
2071 break;
2072 }
2073 }
2074 }
2075
2076 processExtensibilityComponents(documentationObj,documentationEl);
2077
2078 return documentationEl;
2079 }
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102 Element serializeSimpleContentRestriction(Document doc,
2103 XmlSchemaSimpleContentRestriction restrictionObj, XmlSchema schema)
2104 throws XmlSchemaSerializerException {
2105
2106 Element restriction = createNewElement(doc, "restriction",
2107 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2108
2109 if (restrictionObj.baseTypeName != null) {
2110 String baseTypeName =
2111 resolveQName(restrictionObj.baseTypeName, schema);
2112
2113 restriction.setAttribute("base", baseTypeName);
2114
2115 }
2116 if (restrictionObj.id != null)
2117 restriction.setAttribute("id", restrictionObj.id);
2118
2119 if (restrictionObj.annotation != null) {
2120 Element annotation = serializeAnnotation(doc,
2121 restrictionObj.annotation, schema);
2122 restriction.appendChild(annotation);
2123 }
2124 int attrCollLength = restrictionObj.attributes.getCount();
2125 for (int i = 0; i < attrCollLength; i++) {
2126 XmlSchemaObject obj = restrictionObj.attributes.getItem(i);
2127
2128 if (obj instanceof XmlSchemaAttribute) {
2129 Element attribute = serializeAttribute(doc,
2130 (XmlSchemaAttribute) obj, schema);
2131 restriction.appendChild(attribute);
2132 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2133 Element attributeGroup = serializeAttributeGroupRef(doc,
2134 (XmlSchemaAttributeGroupRef) obj, schema);
2135 restriction.appendChild(attributeGroup);
2136 }
2137 }
2138 if (restrictionObj.baseType != null) {
2139 Element inlineSimpleType = serializeSimpleType(doc,
2140 restrictionObj.baseType, schema);
2141 restriction.appendChild(inlineSimpleType);
2142 }
2143 if (restrictionObj.anyAttribute != null) {
2144 Element anyAttribute = serializeAnyAttribute(doc,
2145 restrictionObj.anyAttribute, schema);
2146 restriction.appendChild(anyAttribute);
2147 }
2148 XmlSchemaObjectCollection facets = restrictionObj.facets;
2149 int facetLength = facets.getCount();
2150 for (int i = 0; i < facetLength; i++) {
2151 Element facet = serializeFacet(doc,
2152 (XmlSchemaFacet) facets.getItem(i), schema);
2153 restriction.appendChild(facet);
2154 }
2155
2156
2157 processExtensibilityComponents(restrictionObj,restriction);
2158
2159 return restriction;
2160 }
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183 Element serializeSimpleContentExtension(Document doc,
2184 XmlSchemaSimpleContentExtension extensionObj, XmlSchema schema)
2185 throws XmlSchemaSerializerException {
2186
2187 Element extension = createNewElement(doc, "extension",
2188 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2189
2190 if (extensionObj.baseTypeName != null) {
2191 String baseTypeName =
2192 resolveQName(extensionObj.baseTypeName, schema);
2193
2194 extension.setAttribute("base", baseTypeName);
2195 }
2196
2197 if (extensionObj.id != null)
2198 extension.setAttribute("id", extensionObj.id);
2199
2200 if (extensionObj.annotation != null) {
2201 Element annotation = serializeAnnotation(doc,
2202 extensionObj.annotation, schema);
2203 extension.appendChild(annotation);
2204 }
2205
2206 XmlSchemaObjectCollection attributes = extensionObj.attributes;
2207 int attributeLength = attributes.getCount();
2208 for (int i = 0; i < attributeLength; i++) {
2209 XmlSchemaObject obj = attributes.getItem(i);
2210
2211 if (obj instanceof XmlSchemaAttribute) {
2212 Element attribute = serializeAttribute(doc,
2213 (XmlSchemaAttribute) obj, schema);
2214 extension.appendChild(attribute);
2215 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2216 Element attributeGroupRef = serializeAttributeGroupRef(doc,
2217 (XmlSchemaAttributeGroupRef) obj, schema);
2218 extension.appendChild(attributeGroupRef);
2219 }
2220 }
2221
2222
2223
2224
2225 if (extensionObj.anyAttribute != null) {
2226 Element anyAttribute = serializeAnyAttribute(doc,
2227 extensionObj.anyAttribute, schema);
2228 extension.appendChild(anyAttribute);
2229 }
2230
2231
2232 processExtensibilityComponents(extensionObj,extension);
2233
2234 return extension;
2235 }
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257 Element serializeComplexContentRestriction(Document doc,
2258 XmlSchemaComplexContentRestriction restrictionObj, XmlSchema schema)
2259 throws XmlSchemaSerializerException {
2260
2261 Element restriction = createNewElement(doc, "restriction",
2262 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2263
2264 if (restrictionObj.baseTypeName != null) {
2265 String baseTypeName = resolveQName(restrictionObj.baseTypeName, schema);
2266 restriction.setAttribute(
2267 "base", baseTypeName);
2268 }
2269
2270 if (restrictionObj.id != null)
2271 restriction.setAttribute("id",
2272 restrictionObj.id);
2273
2274 if (restrictionObj.annotation != null) {
2275 Element annotation = serializeAnnotation(doc,
2276 restrictionObj.annotation, schema);
2277 restriction.appendChild(annotation);
2278 }
2279
2280 if (restrictionObj.particle instanceof XmlSchemaSequence) {
2281 Element sequenceParticle = serializeSequence(doc,
2282 (XmlSchemaSequence) restrictionObj.particle, schema);
2283 restriction.appendChild(sequenceParticle);
2284 } else if (restrictionObj.particle instanceof XmlSchemaChoice) {
2285 Element choiceParticle = serializeChoice(doc,
2286 (XmlSchemaChoice) restrictionObj.particle, schema);
2287 restriction.appendChild(choiceParticle);
2288 } else if (restrictionObj.particle instanceof XmlSchemaAll) {
2289 Element allParticle = serializeAll(doc,
2290 (XmlSchemaAll) restrictionObj.particle, schema);
2291 restriction.appendChild(allParticle);
2292 } else if (restrictionObj.particle instanceof XmlSchemaGroupRef) {
2293 Element groupRefParticle = serializeGroupRef(doc,
2294 (XmlSchemaGroupRef) restrictionObj.particle, schema);
2295 restriction.appendChild(groupRefParticle);
2296 }
2297
2298 int attributesLength = restrictionObj.attributes.getCount();
2299 for (int i = 0; i < attributesLength; i++) {
2300 XmlSchemaObject obj = restrictionObj.attributes.getItem(i);
2301
2302 if (obj instanceof XmlSchemaAttribute) {
2303 Element attr = serializeAttribute(doc,
2304 (XmlSchemaAttribute) obj, schema);
2305 restriction.appendChild(attr);
2306 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2307 Element attrGroup = serializeAttributeGroupRef(doc,
2308 (XmlSchemaAttributeGroupRef) obj, schema);
2309 restriction.appendChild(attrGroup);
2310 }
2311 }
2312
2313 if (restrictionObj.anyAttribute != null) {
2314 Element anyAttribute = serializeAnyAttribute(doc,
2315 restrictionObj.anyAttribute, schema);
2316 restriction.appendChild(anyAttribute);
2317 }
2318
2319
2320 processExtensibilityComponents(restrictionObj,restriction);
2321
2322 return restriction;
2323 }
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345 Element serializeComplexContentExtension(Document doc,
2346 XmlSchemaComplexContentExtension extensionObj, XmlSchema schema)
2347 throws XmlSchemaSerializerException {
2348
2349 Element extension = createNewElement(doc, "extension",
2350 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2351 if (extensionObj.baseTypeName != null) {
2352 String baseType = resolveQName(extensionObj.baseTypeName,
2353 schema);
2354 extension.setAttribute("base", baseType);
2355 }
2356 if (extensionObj.annotation != null) {
2357 Element annotation = serializeAnnotation(doc,
2358 extensionObj.annotation, schema);
2359 extension.appendChild(annotation);
2360 }
2361
2362
2363 if (extensionObj.particle instanceof XmlSchemaSequence) {
2364 Element sequenceParticle = serializeSequence(doc,
2365 (XmlSchemaSequence) extensionObj.particle, schema);
2366 extension.appendChild(sequenceParticle);
2367 } else if (extensionObj.particle instanceof XmlSchemaChoice) {
2368 Element choiceParticle = serializeChoice(doc,
2369 (XmlSchemaChoice) extensionObj.particle, schema);
2370 extension.appendChild(choiceParticle);
2371 } else if (extensionObj.particle instanceof XmlSchemaAll) {
2372 Element allParticle = serializeAll(doc,
2373 (XmlSchemaAll) extensionObj.particle, schema);
2374 extension.appendChild(allParticle);
2375 } else if (extensionObj.particle instanceof XmlSchemaGroupRef) {
2376 Element groupRefParticle = serializeGroupRef(doc,
2377 (XmlSchemaGroupRef) extensionObj.particle, schema);
2378 extension.appendChild(groupRefParticle);
2379 }
2380
2381 int attributesLength = extensionObj.attributes.getCount();
2382 for (int i = 0; i < attributesLength; i++) {
2383 XmlSchemaObject obj = extensionObj.attributes.getItem(i);
2384
2385 if (obj instanceof XmlSchemaAttribute) {
2386 Element attr = serializeAttribute(doc,
2387 (XmlSchemaAttribute) obj, schema);
2388 extension.appendChild(attr);
2389 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2390 Element attrGroup = serializeAttributeGroupRef(doc,
2391 (XmlSchemaAttributeGroupRef) obj, schema);
2392 extension.appendChild(attrGroup);
2393 }
2394 }
2395
2396 if (extensionObj.anyAttribute != null) {
2397 Element anyAttribute = serializeAnyAttribute(doc,
2398 extensionObj.anyAttribute, schema);
2399 extension.appendChild(anyAttribute);
2400 }
2401
2402
2403 processExtensibilityComponents(extensionObj,extension);
2404
2405 return extension;
2406 }
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427 Element serializeAnyAttribute(Document doc,
2428 XmlSchemaAnyAttribute anyAttributeObj, XmlSchema schema) {
2429
2430 Element anyAttribute = createNewElement(doc, "anyAttribute",
2431 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2432
2433
2434 if (anyAttributeObj.namespace != null)
2435 anyAttribute.setAttribute("namespace",
2436 anyAttributeObj.namespace);
2437
2438 if (anyAttributeObj.id != null)
2439 anyAttribute.setAttribute("id",
2440 anyAttributeObj.id);
2441
2442 if (anyAttributeObj.processContent != null) {
2443 String processContent = anyAttributeObj.processContent.getValue();
2444 if(!Constants.BlockConstants.NONE.equals(processContent)){
2445 processContent = convertString(processContent);
2446 anyAttribute.setAttribute("processContents",
2447 processContent);
2448 }
2449 }
2450 if (anyAttributeObj.annotation != null) {
2451 Element annotation = serializeAnnotation(doc,
2452 anyAttributeObj.annotation, schema);
2453 anyAttribute.appendChild(annotation);
2454 }
2455
2456
2457 processExtensibilityComponents(anyAttributeObj,anyAttribute);
2458
2459 return anyAttribute;
2460 }
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482 Element serializeAttributeGroupRef(Document doc,
2483 XmlSchemaAttributeGroupRef attributeGroupObj, XmlSchema schema)
2484 throws XmlSchemaSerializerException {
2485
2486 Element attributeGroupRef = createNewElement(doc, "attributeGroup",
2487 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2488
2489 if (attributeGroupObj.refName != null) {
2490 String refName = resolveQName(attributeGroupObj.refName,
2491 schema);
2492 attributeGroupRef.setAttribute("ref", refName);
2493 } else
2494 throw new XmlSchemaSerializerException("Attribute group must have "
2495 + "ref name set");
2496
2497 if (attributeGroupObj.id != null)
2498 attributeGroupRef.setAttribute("id",
2499 attributeGroupObj.id);
2500
2501 if (attributeGroupObj.annotation != null) {
2502 Element annotation = serializeAnnotation(doc,
2503 attributeGroupObj.annotation, schema);
2504 attributeGroupRef.appendChild(annotation);
2505 }
2506
2507
2508 processExtensibilityComponents(attributeGroupObj,attributeGroupRef);
2509
2510 return attributeGroupRef;
2511 }
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533 Element serializeAttributeGroup(Document doc,
2534 XmlSchemaAttributeGroup attributeGroupObj, XmlSchema schema)
2535 throws XmlSchemaSerializerException {
2536
2537 Element attributeGroup = createNewElement(doc, "attributeGroup",
2538 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2539
2540 if (attributeGroupObj.name != null) {
2541 String attGroupName = attributeGroupObj.name.getLocalPart();
2542 attributeGroup.setAttribute("name",
2543 attGroupName);
2544 }else
2545 throw new XmlSchemaSerializerException("Attribute group must"
2546 + "have name");
2547 if (attributeGroupObj.id != null)
2548 attributeGroup.setAttribute("id",
2549 attributeGroupObj.id);
2550
2551 if (attributeGroupObj.annotation != null) {
2552 Element annotation = serializeAnnotation(doc,
2553 attributeGroupObj.annotation, schema);
2554 attributeGroup.appendChild(annotation);
2555 }
2556 int attributesLength = attributeGroupObj.attributes.getCount();
2557 for (int i = 0; i < attributesLength; i++) {
2558 XmlSchemaObject obj = attributeGroupObj.attributes.getItem(i);
2559
2560 if (obj instanceof XmlSchemaAttribute) {
2561 Element attr = serializeAttribute(doc, (XmlSchemaAttribute) obj,
2562 schema);
2563 attributeGroup.appendChild(attr);
2564 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2565 Element attrGroup = serializeAttributeGroupRef(doc,
2566 (XmlSchemaAttributeGroupRef) obj, schema);
2567 attributeGroup.appendChild(attrGroup);
2568 }
2569 }
2570
2571 if (attributeGroupObj.anyAttribute != null) {
2572 Element anyAttribute = serializeAnyAttribute(doc,
2573 attributeGroupObj.anyAttribute, schema);
2574 attributeGroup.appendChild(anyAttribute);
2575 }
2576
2577
2578 processExtensibilityComponents(attributeGroupObj,attributeGroup);
2579
2580 return attributeGroup;
2581 }
2582
2583
2584
2585 private void appendElement(Document doc, Element parent,
2586 Node children, XmlSchema schema) {
2587 Element elTmp = (Element) children;
2588 Element el = createNewElement(doc, elTmp.getLocalName(),
2589 schema.schema_ns_prefix, XmlSchema.SCHEMA_NS);
2590 NamedNodeMap attributes = el.getAttributes();
2591
2592
2593 int attributeLength = attributes.getLength();
2594 for (int i = 0; i < attributeLength; i++) {
2595 Node n = attributes.item(i);
2596
2597 el.setAttribute(n.getNodeName(),
2598 n.getNodeValue());
2599 }
2600
2601
2602
2603 NodeList decendants = el.getChildNodes();
2604 int decendantLength = decendants.getLength();
2605 for (int i = 0; i < decendantLength; i++) {
2606 Node n = decendants.item(i);
2607 short nodeType = n.getNodeType();
2608 if (nodeType == Node.TEXT_NODE) {
2609 String nValue = n.getNodeValue();
2610 Text t = doc.createTextNode(nValue);
2611 el.appendChild(t);
2612 } else if (nodeType == Node.CDATA_SECTION_NODE) {
2613 String nValue = n.getNodeValue();
2614 CDATASection s = doc.createCDATASection(nValue);
2615 el.appendChild(s);
2616 } else if (nodeType == Node.ELEMENT_NODE) {
2617 appendElement(doc, el, n, schema);
2618 }
2619 }
2620 }
2621
2622
2623 private static String[] getParts(String name) {
2624 String[] parts = new String[2];
2625
2626 int index = name.indexOf(":");
2627 if (index > -1) {
2628 parts[0] = name.substring(0, index);
2629 parts[1] = name.substring(index + 1);
2630 } else {
2631 parts[0] = "";
2632 parts[1] = name;
2633 }
2634 return parts;
2635 }
2636
2637
2638 private String convertString(String convert) {
2639 String input = convert.trim();
2640 if (input.equals(Constants.BlockConstants.ALL)) {
2641 return "#all";
2642 } else
2643 return input.toLowerCase();
2644 }
2645
2646
2647
2648 private Element createNewElement(Document docs, String localName,
2649 String prefix, String namespace) {
2650 String elementName = ((prefix.length() > 0) ? prefix += ":" : "")
2651 + localName;
2652 return docs.createElementNS(namespace, elementName);
2653 }
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666 private String resolveQName(QName names,
2667 XmlSchema schemaObj) {
2668
2669 String namespace = names.getNamespaceURI();
2670 String type[] = getParts(names.getLocalPart());
2671 String typeName = (type.length > 1) ? type[1] : type[0];
2672 String prefixStr;
2673
2674
2675 Object prefix = ("".equals(namespace)) ? "" : schema_ns.get(namespace);
2676
2677 if (prefix == null) {
2678 if (Constants.XMLNS_URI.equals(namespace)) {
2679 prefix = Constants.XMLNS_PREFIX;
2680 } else {
2681 int magicNumber = 0;
2682 Collection prefixes = schema_ns.values();
2683 while(prefixes.contains("ns" + magicNumber)){
2684 magicNumber++;
2685 }
2686 prefix = "ns" + magicNumber;
2687 schema_ns.put(namespace, prefix);
2688
2689
2690 schemaElement.setAttributeNS(XMLNS_NAMESPACE_URI,
2691 "xmlns:" + prefix.toString(), namespace);
2692 }
2693 }
2694
2695 prefixStr = prefix.toString();
2696 prefixStr = (prefixStr.trim().length() > 0) ? prefixStr + ":" : "";
2697
2698 return prefixStr + typeName;
2699 }
2700
2701
2702
2703 void setupAttr(Document doc, XmlSchemaObjectCollection collectionObj,
2704 XmlSchema schema, Element container) throws XmlSchemaSerializerException {
2705 int collectionLength = collectionObj.getCount();
2706 for (int i = 0; i < collectionLength; i++) {
2707 XmlSchemaObject obj = collectionObj.getItem(i);
2708 if (obj instanceof XmlSchemaAttribute) {
2709 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
2710 Element attrEl = serializeAttribute(doc, attr, schema);
2711 container.appendChild(attrEl);
2712 } else if (obj instanceof XmlSchemaAttributeGroupRef) {
2713 XmlSchemaAttributeGroupRef attr = (XmlSchemaAttributeGroupRef) obj;
2714 Element attrEl = serializeAttributeGroupRef(doc, attr, schema);
2715 container.appendChild(attrEl);
2716 }
2717 }
2718 }
2719
2720
2721
2722
2723 public static class XmlSchemaSerializerException extends Exception {
2724
2725
2726
2727
2728 private static final long serialVersionUID = 1L;
2729
2730
2731
2732
2733
2734 public XmlSchemaSerializerException(String msg) {
2735 super(msg);
2736 }
2737 }
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748 private void processExtensibilityComponents(XmlSchemaObject schemaObject, Element parentElement) {
2749
2750 if (extReg!=null){
2751 Map metaInfoMap = schemaObject.getMetaInfoMap();
2752 if (metaInfoMap!=null && !metaInfoMap.isEmpty()) {
2753
2754 Iterator keysIt = metaInfoMap.keySet().iterator();
2755 while (keysIt.hasNext()) {
2756 Object key = keysIt.next();
2757 extReg.serializeExtension(schemaObject,metaInfoMap.get(key).getClass(),parentElement);
2758
2759 }
2760
2761 }
2762
2763 }
2764
2765 }
2766
2767 }