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