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