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