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