View Javadoc

1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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       * Extension registry for the serializer
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       * Document[]  serializeSchema(XmlSchema schemaObj,
63       * boolean serializeIncluded)
64       * <p/>
65       * Serialize XmlSchema object pass back the document containing a schema
66       * element as its root.
67       * <p/>
68       * Parameter:
69       * schemaObj - Schema object to serialize.
70       * serialzeIncluded - whether to serialize the included(imported)
71       * schema or not. pass true for serialize all included
72       * schema.
73       * <p/>
74       * Return:
75       * Array of Documents that include/imported.
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             //if the namespace is not entered then add 
109             //the targetNamespace as its
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         //todo: implement xml:lang, 
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         //add the extra namespace decalarations if any are available
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         //after serialize the schema add into documentation
187         //and add to document collection array  which at the end 
188         //returned
189         serializeSchemaChild(items, serializedSchema, serializedSchemaDocs,
190                 schemaObj, serializeIncluded);
191 
192         //process extension elements/attributes
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          * For each of the items that belong to this schema, 
214          * serialize each member found.  
215          * Permittable member is: element, simpleType, complexType,
216          * group, attrributeGroup, Attribute, include, import and redefine.
217          * if any of the member found then serialize the component.
218          */
219 
220         // Since imports and includes need to be the first items of the
221         // serialized schema. So this loop does the serialization of the
222         // imports and includes
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         // reloop to serialize the others
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      * Set up <schema> namespaces appropriately and append that attr
276      * into specified element
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         //for schema that not set the xmlns attrib member
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             //let it crash for null pointer because then either the schema
303             //is wrong(namespace not set properly or bug in setting ns)
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      * Element serializeInclude(Document doc, XmlSchemaInclude includeObj,
317      * XmlSchema schema)throws XmlSchemaSerializerException
318      * <p/>
319      * set appropriate attribute as per this object attribute availability.
320      * Call included schema to append to this schema document collection.
321      * Then add the document created into document pool.
322      * <p/>
323      * Parameter:
324      * doc          - Document the parent use.
325      * includeObj   - XmlSchemaInclude that will be serialized.
326      * schema       - Schema Document object of the parent.
327      * <p/>
328      * Return:
329      * Element object representation of XmlSchemaInclude
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         //Get the XmlSchema obj and append that to the content
354         XmlSchema includedSchemaObj = includeObj.getSchema();
355         if (includedSchemaObj != null && serializeIncluded) {
356             XmlSchemaSerializer includeSeri = new XmlSchemaSerializer();
357             includeSeri.serializeSchemaElement(includedSchemaObj, true);
358 //            XmlSchemaObjectCollection ii = includedSchemaObj.getItems();
359             docs.addAll(includeSeri.docs);
360         }
361 
362         //process includes
363         processExtensibilityComponents(includeObj,includeEl);
364 
365         return includeEl;
366     }
367 
368     /**
369      * *********************************************************************
370      * Element serializeImport(Document doc, XmlSchemaImport importObj,
371      * XmlSchema schema)throws XmlSchemaSerializerException
372      * <p/>
373      * Add each of the attribute of XmlSchemaImport obj into import Element
374      * Then serialize schema that is included by this import.  Include the
375      * serialized schema into document pool.
376      * <p/>
377      * Parameter:
378      * doc          - Document the parent use.
379      * includeObj   - XmlSchemaInclude that will be serialized.
380      * schema       - Schema Document object of the parent.
381      * <p/>
382      * Return:
383      * Element object representation of XmlSchemaImport
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          //process extension
420         processExtensibilityComponents(importObj,importEl);
421 
422         return importEl;
423     }
424 
425     /**
426      * *********************************************************************
427      * Element serializeRedefine(Document doc, XmlSchemaRedefine redefineObj,
428      * XmlSchema schema)throws XmlSchemaSerializerException
429      * <p/>
430      * Add each of the attribute of XmlSchemaImport obj into import Element
431      * Then serialize schema that is included by this import.  Include the
432      * serialized schema into document pool.
433      * <p/>
434      * Parameter:
435      * doc           - Document the parent use.
436      * redefineObj   - XmlSchemaInclude that will be serialized.
437      * schema        - Schema Document object of the parent.
438      * <p/>
439      * Return:
440      * Element object representation of XmlSchemaRedefine
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             //process extension
495         processExtensibilityComponents(redefineObj,redefine);
496 
497         return redefine;
498     }
499 
500     /**
501      * *********************************************************************
502      * Element serializeElement(Document doc, XmlSchemaElement elementObj,
503      * XmlSchema schema) throws XmlSchemaSerializerException
504      * <p/>
505      * Each member of Element will be appended and pass the element
506      * created.  Element processed according to w3c Recommendation
507      * May 2 2001.
508      * <p/>
509      * Parameter:
510      * doc           - Document the parent use.
511      * elementObj   - XmlSchemaInclude that will be serialized.
512      * schema        - Schema Document object of the parent.
513      * <p/>
514      * Return:
515      * Element object of element.
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         //else not serialized
570 
571         /*if(elementObj.minOccurs >1)
572             serializedEl.setAttribute("minOccurs",
573             elementObj.minOccurs + "");*/
574 
575         //Change - SK and Ragu cos it wasnt picking up
576         // minOccurs = 0
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             if(elementObj.maxOccursString != null)
586             serializedEl.setAttribute("maxOccurs",
587             elementObj.maxOccursString);
588             else if(elementObj.maxOccurs > 1)
589             serializedEl.setAttribute("maxOccurs",
590             elementObj.maxOccurs + "");
591 
592             if(elementObj.minOccurs > 1)
593             serializedEl.setAttribute("minOccurs",
594             elementObj.minOccurs + "");
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             //process extension
635         processExtensibilityComponents(elementObj,serializedEl);
636 
637         return serializedEl;
638     }
639 
640     /**
641      * *********************************************************************
642      * Element serializeSimpleType(Document doc,
643      * XmlSchemaSimpleType simpleTypeObj, XmlSchema schema)
644      * throws XmlSchemaSerializerException{
645      * <p/>
646      * Each member of simple type will be appended and pass the element
647      * created.  Simple type processed according to w3c Recommendation
648      * May 2 2001.
649      * <p/>
650      * Parameter:
651      * doc               - Document the parent use.
652      * simpleTypeObj     - XmlSchemaSimpleType that will be serialized.
653      * schema            - Schema Document object of the parent.
654      * <p/>
655      * Return:
656      * Element object of SimpleType
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             }/*else 
699 			   throw new XmlSchemaSerializerException("Invalid type inserted "
700 			   + "in simpleType content, the content is: " 
701 			   + simpleTypeObj.content.getClass().getName()
702 			   + " valid content should be XmlSchemaSimpleTypeunion, "
703 			   + "XmlSchemaSimpleTyperestriction or list");*/
704         }/*else
705 		   throw new XmlSchemaSerializerException("simple type must be set "
706 		   + "with content, either union, restriction or list");*/
707 
708             //process extension
709         processExtensibilityComponents(simpleTypeObj,serializedSimpleType);
710 
711         return serializedSimpleType;
712     }
713 
714     /**
715      * *********************************************************************
716      * Element serializeSimpleTypeRestriction(Document doc,
717      * XmlSchemaSimpleTypeRestriction restrictionObj, XmlSchema schema)
718      * throws XmlSchemaSerializerException{
719      * <p/>
720      * Each member of simple type will be appended and pass the element
721      * created.  Simple type's <restriction> processed according to w3c
722      * Recommendation May 2 2001.
723      * <p/>
724      * Parameter:
725      * doc               - Document the parent use.
726      * restrictionObj    - XmlSchemaRestriction that will be serialized.
727      * schema            - Schema Document object of the parent.
728      * <p/>
729      * Return:
730      * Element of simple type restriction and its child.
731      * **********************************************************************
732      */
733     Element serializeSimpleTypeRestriction(Document doc,
734                                            XmlSchemaSimpleTypeRestriction restrictionObj, XmlSchema schema)
735             throws XmlSchemaSerializerException {
736         //todo: need to implement any attribute that related to non schema namespace
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             //process extension
773         processExtensibilityComponents(restrictionObj,serializedRestriction);
774 
775         return serializedRestriction;
776     }
777 
778     /**
779      * *********************************************************************
780      * Element serializeFacet(Document doc, XmlSchemaFacet facetObj,
781      * XmlSchema schema) throws XmlSchemaSerializerException{
782      * <p/>
783      * detect what type of facet and cass appropriatelly,
784      * construct the element and pass it.
785      * <p/>
786      * Parameter:
787      * doc       - Document the parent use.
788      * facetObj  - XmlSchemaFacet that will be serialized.
789      * schema    - Schema Document object of the parent.
790      * <p/>
791      * Return:
792      * Element of simple type with facet.
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 //        if (facetObj.annotation != null) {
843 //            Element annotation = serializeAnnotation(doc, facetObj.annotation,
844 //                                                     schema);
845 //            serializedFacet.appendChild(annotation);
846 //        }
847 
848             //process extension
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      * Element serializeComplexType(Document doc,
876      * XmlSchemaComplexType complexTypeObj, XmlSchema schema)
877      * throws XmlSchemaSerializerException{
878      * <p/>
879      * Each member of complex type will be appended and pass the element
880      * created.  Complex type processed according to w3c Recommendation
881      * May 2 2001.
882      * <p/>
883      * Parameter:
884      * doc             - Document the parent use.
885      * complexTypeObj  - XmlSchemaFacet that will be serialized.
886      * schema          - Schema Document object of the parent.
887      * <p/>
888      * Return:
889      * Element of complexType.
890      * **********************************************************************
891      */
892     Element serializeComplexType(Document doc,
893                                  XmlSchemaComplexType complexTypeObj, XmlSchema schema)
894             throws XmlSchemaSerializerException {
895 
896         //todo: need to implement abstract, id, mixed
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         /*if(complexTypeObj.annotation != null){
904 		  Element annotationEl = serializeAnnotation(doc, 
905 		  complexTypeObj.annotation, schema);
906 		  serializedComplexType.appendChild(annotationEl);
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             //process extension
966         processExtensibilityComponents(complexTypeObj,serializedComplexType);
967 
968         return serializedComplexType;
969     }
970 
971     /**
972      * *********************************************************************
973      * Element serializeSequence(Document doc, XmlSchemaSequence sequenceObj,
974      * XmlSchema schema)throws XmlSchemaSerializerException{
975      * <p/>
976      * Each member of complex type will be appended and pass the element
977      * created.  `Complex type processed according to w3c Recommendation
978      * May 2 2001.
979      * <p/>
980      * Parameter:
981      * doc             - Document the parent use.
982      * sequenceObj  - XmlSchemaFacet that will be serialized.
983      * schema          - Schema Document object of the parent.
984      * <p/>
985      * Return:
986      * Element of sequence particle.
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         //else not serialized
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             //process extension
1040         processExtensibilityComponents(sequenceObj,sequence);
1041 
1042         return sequence;
1043     }
1044 
1045     /**
1046      * *********************************************************************
1047      * Element serializeAttribute(Document doc, XmlSchemaAttribute attributeObj,
1048      * XmlSchema schema) throws XmlSchemaSerializerException{
1049      * <p/>
1050      * Each member of complex type will be appended and pass the element
1051      * created.  `Complex type processed according to w3c Recommendation
1052      * May 2 2001.
1053      * <p/>
1054      * Parameter:
1055      * doc             - Document the parent use.
1056      * attributeObj    - XmlSchemaAttribute that will be serialized.
1057      * schema          - Schema Document object of the parent.
1058      * <p/>
1059      * Return:
1060      * Element of attribute.
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             // this is to make the wsdl:arrayType work 
1128             // since unhandles attributes are not handled this is a special case
1129             // but the basic idea is to see if there is any attibute whose value has ":"
1130             // if it is present then it is likely that it is a namespace prefix
1131             // do what is neccesary to get the real namespace for it and make 
1132             // required changes to the prefix 
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             //process extension
1172         processExtensibilityComponents(attributeObj,attribute);
1173 
1174         return attribute;
1175     }
1176 
1177     /**
1178      * *********************************************************************
1179      * Element serializeChoice(Document doc, XmlSchemaChoice choiceObj,
1180      * XmlSchema schema) throws XmlSchemaSerializerException{
1181      * <p/>
1182      * Each member of complex type will be appended and pass the element
1183      * created.  Complex type processed according to w3c Recommendation
1184      * May 2 2001.
1185      * <p/>
1186      * Parameter:
1187      * doc             - Document the parent use.
1188      * choiceObj       - XmlSchemaChoice that will be serialized.
1189      * schema          - Schema Document object of the parent.
1190      * <p/>
1191      * Return:
1192      * Element of choice schema object.
1193      * **********************************************************************
1194      */
1195     Element serializeChoice(Document doc, XmlSchemaChoice choiceObj,
1196                             XmlSchema schema) throws XmlSchemaSerializerException {
1197         //todo: handle any non schema attri ?
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         //else not serialized
1213 
1214         if (choiceObj.minOccurs != 1)
1215             choice.setAttribute("minOccurs",
1216                     choiceObj.minOccurs + "");
1217 
1218 
1219         /*
1220             if(choiceObj.maxOccursString != null)
1221             choice.setAttribute("maxOccurs",
1222             choiceObj.maxOccursString);
1223             else if(choiceObj.maxOccurs > 1)
1224             choice.setAttribute("maxOccurs",
1225             choiceObj.maxOccurs +"");
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             //process extension
1268         processExtensibilityComponents(choiceObj,choice);
1269 
1270         return choice;
1271     }
1272 
1273     /**
1274      * *********************************************************************
1275      * Element serializeAll(Document doc, XmlSchemaAll allObj, XmlSchema schema)
1276      * throws XmlSchemaSerializerException{
1277      * <p/>
1278      * Each member of complex type will be appended and pass the element
1279      * created.  Complex type processed according to w3c Recommendation
1280      * May 2 2001.
1281      * <p/>
1282      * Parameter:
1283      * doc             - Document the parent use.
1284      * allObj          - XmlSchemaAll that will be serialized.
1285      * schema          - Schema Document object of the parent.
1286      * <p/>
1287      * Return:
1288      * Element of particle all.
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             //process extension
1324         processExtensibilityComponents(allObj,allEl);
1325 
1326         return allEl;
1327     }
1328 
1329     /**
1330      * *********************************************************************
1331      * Element serializeSimpleTypeList(Document doc,
1332      * XmlSchemaSimpleTypeList listObj, XmlSchema schema)
1333      * throws XmlSchemaSerializerException{
1334      * <p/>
1335      * Each member of complex type will be appended and pass the element
1336      * created.  Complex type processed according to w3c Recommendation
1337      * May 2 2001.
1338      * <p/>
1339      * Parameter:
1340      * doc             - Document the parent use.
1341      * listObj         - XmlSchemaSimpleTypeList that will be serialized.
1342      * schema          - Schema Document object of the parent.
1343      * <p/>
1344      * Return:
1345      * Element of simple type with list method.
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             //process extension
1374         processExtensibilityComponents(listObj,list);
1375 
1376         return list;
1377     }
1378 
1379     /**
1380      * *********************************************************************
1381      * Element serializeSimpleTypeUnion(Document doc,
1382      * XmlSchemaSimpleTypeUnion unionObj, XmlSchema schema)
1383      * throws XmlSchemaSerializerException{
1384      * <p/>
1385      * Each member of complex type will be appended and pass the element
1386      * created.  Complex type processed according to w3c Recommendation
1387      * May 2 2001.
1388      * <p/>
1389      * Parameter:
1390      * doc              - Document the parent use.
1391      * unionObj         - XmlSchemaSimpleTypeUnion that will be serialized.
1392      * schema           - Schema Document object of the parent.
1393      * <p/>
1394      * Return:
1395      * Element of simple type with union method.
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             //process extension
1433         processExtensibilityComponents(unionObj,union);
1434 
1435         return union;
1436     }
1437 
1438     /**
1439      * *********************************************************************
1440      * Element serializeAny(Document doc, XmlSchemaAny anyObj, XmlSchema schema)
1441      * <p/>
1442      * Each member of complex type will be appended and pass the element
1443      * created.  Complex type processed according to w3c Recommendation
1444      * May 2 2001.
1445      * <p/>
1446      * Parameter:
1447      * doc              - Document the parent use.
1448      * anyObj           - XmlSchemaAny that will be serialized.
1449      * schema           - Schema Document object of the parent.
1450      * <p/>
1451      * Return:
1452      * Element of any that is part of its parent.
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         //else not serialized
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             //process extension
1494         processExtensibilityComponents(anyObj,anyEl);
1495 
1496         return anyEl;
1497     }
1498 
1499     /**
1500      * *********************************************************************
1501      * Element serializeGroup(Document doc, XmlSchemaGroup groupObj,
1502      * XmlSchema schema) throws XmlSchemaSerializerException{
1503      * <p/>
1504      * Each member of complex type will be appended and pass the element
1505      * created.  Complex type processed according to w3c Recommendation
1506      * May 2 2001.
1507      * <p/>
1508      * Parameter:
1509      * doc                - Document the parent use.
1510      * groupObj           - XmlSchemaGroup that will be serialized.
1511      * schema             - Schema Document object of the parent.
1512      * <p/>
1513      * Return:
1514      * Element of group elements.
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         /* annotations are supposed to be written first!!!!! */
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             //process extension
1553         processExtensibilityComponents(groupObj,group);
1554 
1555         return group;
1556     }
1557 
1558     /**
1559      * *********************************************************************
1560      * Element serializeGroupRef(Document doc, XmlSchemaGroupRef groupRefObj,
1561      * XmlSchema schema) throws XmlSchemaSerializerException{
1562      * <p/>
1563      * Each member of complex type will be appended and pass the element
1564      * created.  Complex type processed according to w3c Recommendation
1565      * May 2 2001.
1566      * <p/>
1567      * Parameter:
1568      * doc                   - Document the parent use.
1569      * groupRefObj           - XmlSchemaGroupRef that will be serialized.
1570      * schema                - Schema Document object of the parent.
1571      * <p/>
1572      * Return:
1573      * Element of group elements ref inside its parent.
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         //else not serialized
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             //process extension
1623         processExtensibilityComponents(groupRefObj,groupRef);
1624 
1625         return groupRef;
1626     }
1627 
1628     /**
1629      * *********************************************************************
1630      * Element serializeSimpleContent(Document doc,
1631      * XmlSchemaSimpleContent simpleContentObj, XmlSchema schema)
1632      * throws XmlSchemaSerializerException{
1633      * <p/>
1634      * Each member of complex type will be appended and pass the element
1635      * created.  Complex type processed according to w3c Recommendation
1636      * May 2 2001.
1637      * <p/>
1638      * Parameter:
1639      * doc               - Document the parent use.
1640      * simpleContentObj  - XmlSchemaSimpleContent that will be serialized.
1641      * schema            - Schema Document object of the parent.
1642      * <p/>
1643      * Return:
1644      * Element of complex type simple content.
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             //process extension
1676         processExtensibilityComponents(simpleContentObj,simpleContent);
1677 
1678         return simpleContent;
1679     }
1680 
1681     /**
1682      * *********************************************************************
1683      * Element serializeComplexContent(Document doc,
1684      * XmlSchemaComplexContent complexContentObj, XmlSchema schema)
1685      * throws XmlSchemaSerializerException{
1686      * <p/>
1687      * Each member of complex type will be appended and pass the element
1688      * created.  Complex type processed according to w3c Recommendation
1689      * May 2 2001.
1690      * <p/>
1691      * Parameter:
1692      * doc                - Document the parent use.
1693      * complexContentObj  - XmlSchemaComplexContent that will be serialized.
1694      * schema             - Schema Document object of the parent.
1695      * <p/>
1696      * Return:
1697      * Element of complex type complex content.
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             //process extension
1739         processExtensibilityComponents(complexContentObj,complexContent);
1740 
1741         return complexContent;
1742     }
1743 
1744     /**
1745      * *********************************************************************
1746      * Element serializeIdentityConstraint(Document doc,
1747      * XmlSchemaIdentityConstraint constraintObj, XmlSchema schema)
1748      * throws XmlSchemaSerializerException{
1749      * <p/>
1750      * Each member of complex type will be appended and pass the element
1751      * created.  Complex type processed according to w3c Recommendation
1752      * May 2 2001.
1753      * <p/>
1754      * Parameter:
1755      * doc               - Document the parent use.
1756      * constraintObj     - XmlSchemaIdentityConstraint that will be serialized.
1757      * schema            - Schema Document object of the parent.
1758      * <p/>
1759      * Return:
1760      * Element of key, keyref or unique that part of its parent.
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             //process extension
1813         processExtensibilityComponents(constraintObj,constraint);
1814 
1815         return constraint;
1816     }
1817 
1818     /**
1819      * *********************************************************************
1820      * Element serializeSelector(Document doc, XmlSchemaXPath selectorObj,
1821      * XmlSchema schema) throws XmlSchemaSerializerException{
1822      * <p/>
1823      * Each member of complex type will be appended and pass the element
1824      * created.  Complex type processed according to w3c Recommendation
1825      * May 2 2001.
1826      * <p/>
1827      * Parameter:
1828      * doc               - Document the parent use.
1829      * selectorObj      - XmlSchemaXPath that will be serialized.
1830      * schema            - Schema Document object of the parent.
1831      * <p/>
1832      * Return:
1833      * Element of selector with collection of xpath as its attrib.  The selector
1834      * itself is the part of identity type.  eg <key><selector xpath="..."
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             //process extension
1855         processExtensibilityComponents(selectorObj,selector);
1856         return selector;
1857     }
1858 
1859     /**
1860      * *********************************************************************
1861      * Element serializeField(Document doc, XmlSchemaXPath fieldObj,
1862      * XmlSchema schema) throws XmlSchemaSerializerException
1863      * <p/>
1864      * Each member of complex type will be appended and pass the element
1865      * created.  Complex type processed according to w3c Recommendation
1866      * May 2 2001.
1867      * <p/>
1868      * Parameter:
1869      * doc           - Document the parent use.
1870      * fieldObj      - XmlSchemaXPath that will be serialized.
1871      * schema        - Schema Document object of the parent.
1872      * <p/>
1873      * Return:
1874      * field element that part of constraint.
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             //process extension
1895         processExtensibilityComponents(fieldObj,field);
1896 
1897         return field;
1898     }
1899 
1900     /**
1901      * *********************************************************************
1902      * Element serializeAnnotation(Document doc, XmlSchemaAnnotation
1903      * annotationObj, XmlSchema schema)
1904      * <p/>
1905      * <p/>
1906      * Each member of complex type will be appended and pass the element
1907      * created.  Complex type processed according to w3c Recommendation
1908      * May 2 2001.
1909      * <p/>
1910      * Parameter:
1911      * doc               - Document the parent use.
1912      * annotationObj      - XmlSchemaAnnotation that will be serialized.
1913      * schema            - Schema Document object of the parent.
1914      * <p/>
1915      * Return:
1916      * annotation element that part of any type. will contain document and
1917      * appinfo for child.
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             //process extension
1949         processExtensibilityComponents(annotationObj,annotation);
1950 
1951         return annotation;
1952     }
1953 
1954     /**
1955      * *********************************************************************
1956      * Element serializeAppInfo(Document doc,XmlSchemaAppInfo appInfoObj,
1957      * XmlSchema schema)
1958      * <p/>
1959      * <p/>
1960      * Each member of complex type will be appended and pass the element
1961      * created.  Complex type processed according to w3c Recommendation
1962      * May 2 2001.
1963      * <p/>
1964      * Parameter:
1965      * doc               - Document the parent use.
1966      * appInfoObj        - XmlSchemaAppInfo that will be serialized.
1967      * schema            - Schema Document object of the parent.
1968      * <p/>
1969      * Return:
1970      * App info element that is part of the annotation.
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             //process extension
2001         processExtensibilityComponents(appInfoObj,appInfoEl);
2002 
2003         return appInfoEl;
2004     }
2005 
2006     /**
2007      * *********************************************************************
2008      * Element serializeDocumentation(Document doc,XmlSchemaDocumentation
2009      * documentationObj, XmlSchema schema){
2010      * <p/>
2011      * <p/>
2012      * Each member of complex type will be appended and pass the element
2013      * created.  Complex type processed according to w3c Recommendation
2014      * May 2 2001.
2015      * <p/>
2016      * Parameter:
2017      * doc               - Document the parent use.
2018      * documentationObj        - XmlSchemaAppInfo that will be serialized.
2019      * schema            - Schema Document object of the parent.
2020      * <p/>
2021      * Return:
2022      * Element representation of documentation that is part of annotation.
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             //process extension
2057         processExtensibilityComponents(documentationObj,documentationEl);
2058 
2059         return documentationEl;
2060     }
2061 
2062     /**
2063      * *********************************************************************
2064      * Element serializeSimpleContentRestriction(Document doc,
2065      * XmlSchemaSimpleContentRestriction restrictionObj, XmlSchema schema)
2066      * throws XmlSchemaSerializerException{
2067      * <p/>
2068      * <p/>
2069      * Each member of complex type will be appended and pass the element
2070      * created.  Complex type processed according to w3c Recommendation
2071      * May 2 2001.
2072      * <p/>
2073      * Parameter:
2074      * doc               - Document the parent use.
2075      * restrictionObj    - XmlSchemaSimpleContentRestriction that will be
2076      * serialized.
2077      * schema            - Schema Document object of the parent.
2078      * <p/>
2079      * Return:
2080      * Element of simple content restriction.
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             //process extension
2138         processExtensibilityComponents(restrictionObj,restriction);
2139 
2140         return restriction;
2141     }
2142 
2143     /**
2144      * *********************************************************************
2145      * Element serializeSimpleContentExtension(Document doc,
2146      * XmlSchemaSimpleContentExtension extensionObj, XmlSchema schema)
2147      * throws XmlSchemaSerializerException{
2148      * <p/>
2149      * <p/>
2150      * Each member of complex type will be appended and pass the element
2151      * created.  Complex type processed according to w3c Recommendation
2152      * May 2 2001.
2153      * <p/>
2154      * Parameter:
2155      * doc                 - Document the parent use.
2156      * extensionObj        - XmlSchemaSimpleContentExtension
2157      * that will be serialized.
2158      * schema              - Schema Document object of the parent.
2159      * <p/>
2160      * Return:
2161      * Element of simple content extension.
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          * anyAttribute must come *after* any other attributes
2205          */
2206         if (extensionObj.anyAttribute != null) {
2207             Element anyAttribute = serializeAnyAttribute(doc,
2208                     extensionObj.anyAttribute, schema);
2209             extension.appendChild(anyAttribute);
2210         }
2211 
2212             //process extension
2213         processExtensibilityComponents(extensionObj,extension);
2214 
2215         return extension;
2216     }
2217 
2218     /**
2219      * *********************************************************************
2220      * Element serializeComplexContentRestriction(Document doc,
2221      * XmlSchemaComplexContentRestriction restrictionObj, XmlSchema schema)
2222      * throws XmlSchemaSerializerException{
2223      * <p/>
2224      * Each member of complex type will be appended and pass the element
2225      * created.  Complex type processed according to w3c Recommendation
2226      * May 2 2001.
2227      * <p/>
2228      * Parameter:
2229      * doc                 - Document the parent use.
2230      * restrictionObj        - XmlSchemaSimpleContentRestriction
2231      * that will be serialized.
2232      * schema              - Schema Document object of the parent.
2233      * <p/>
2234      * Return:
2235      * Element of simple content restriction.
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             //process extension
2301         processExtensibilityComponents(restrictionObj,restriction);
2302 
2303         return restriction;
2304     }
2305 
2306     /**
2307      * *********************************************************************
2308      * Element serializeComplexContentExtension(Document doc,
2309      * XmlSchemaComplexContentExtension extensionObj, XmlSchema schema)
2310      * throws XmlSchemaSerializerException{
2311      * <p/>
2312      * Each member of complex type will be appended and pass the element
2313      * created.  Complex type processed according to w3c Recommendation
2314      * May 2 2001.
2315      * <p/>
2316      * Parameter:
2317      * doc                 - Document the parent use.
2318      * extensionObj        - XmlSchemaComplexContentRestriction
2319      * that will be serialized.
2320      * schema              - Schema Document object of the parent.
2321      * <p/>
2322      * Return:
2323      * Element of complex content extension.
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             //process extension
2384         processExtensibilityComponents(extensionObj,extension);
2385 
2386         return extension;
2387     }
2388 
2389     /**
2390      * *********************************************************************
2391      * Element serializeAnyAttribute(Document doc,
2392      * XmlSchemaAnyAttribute anyAttributeObj, XmlSchema schema)
2393      * <p/>
2394      * Each member of complex type will be appended and pass the element
2395      * created.  Complex type processed according to w3c Recommendation
2396      * May 2 2001.
2397      * <p/>
2398      * Parameter:
2399      * doc                 - Document the parent use.
2400      * anyAttributeObj     - XmlSchemaAnyAttribute
2401      * that will be serialized.
2402      * schema              - Schema Document object of the parent.
2403      * <p/>
2404      * Return:
2405      * Element of any attribute element.
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             //process extension
2436         processExtensibilityComponents(anyAttributeObj,anyAttribute);
2437 
2438         return anyAttribute;
2439     }
2440 
2441     /**
2442      * *********************************************************************
2443      * Element serializeAttributeGroupRef(Document doc,
2444      * XmlSchemaAttributeGroupRef attributeGroupObj, XmlSchema schema)
2445      * throws XmlSchemaSerializerException
2446      * <p/>
2447      * Each member of complex type will be appended and pass the element
2448      * created.  Complex type processed according to w3c Recommendation
2449      * May 2 2001.
2450      * <p/>
2451      * Parameter:
2452      * doc                 - Document the parent use.
2453      * attributeGroupObj     - XmlSchemaAttributeGroupRef
2454      * that will be serialized.
2455      * schema              - Schema Document object of the parent.
2456      * <p/>
2457      * Return:
2458      * Element of attribute group ref that part of type.
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             //process extension
2487         processExtensibilityComponents(attributeGroupObj,attributeGroupRef);
2488 
2489         return attributeGroupRef;
2490     }
2491 
2492     /**
2493      * *********************************************************************
2494      * Element serializeAttributeGroup(Document doc,
2495      * XmlSchemaAttributeGroup attributeGroupObj, XmlSchema schema)
2496      * throws XmlSchemaSerializerException{
2497      * <p/>
2498      * Each member of complex type will be appended and pass the element
2499      * created.  Complex type processed according to w3c Recommendation
2500      * May 2 2001.
2501      * <p/>
2502      * Parameter:
2503      * doc                 - Document the parent use.
2504      * attributeGroupObj     - XmlSchemaAttributeGroup
2505      * that will be serialized.
2506      * schema              - Schema Document object of the parent.
2507      * <p/>
2508      * Return:
2509      * Element of attribute group.
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             //process extension
2556         processExtensibilityComponents(attributeGroupObj,attributeGroup);
2557 
2558         return attributeGroup;
2559     }
2560 
2561     //recursively add any attribute, text and children append all 
2562     //found children base on parent as its root.
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         //check if child node has attribute  
2570         //create new element and append it if found
2571         int attributeLength = attributes.getLength();
2572         for (int i = 0; i < attributeLength; i++) {
2573             Node n = attributes.item(i);
2574             //assuming attributes got to throw exception if not later
2575             el.setAttribute(n.getNodeName(),
2576                     n.getNodeValue());
2577         }
2578 
2579         //check any descendant of this node
2580         //if there then append its child
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     //break string with prefix into two parts part[0]:prefix , part[1]:namespace
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     //Convert given string to lower case or w3c standard
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     //Create new element with given local name and namespaces check whether
2621     //the prefix is there or not.
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      * will search whether the prefix is available in global hash table, if it
2631      * is there than append the prefix to the element name.  If not then it will
2632      * create new prefix corresponding to that namespace and store that in
2633      * hash table.  Finally add the new prefix and namespace to <schema>
2634      * element
2635      * @param names
2636      * @param schemaObj
2637      * @return resolved QName of the string
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         // If the namespace is "" then the prefix is also ""
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                 //setting xmlns in schema
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     //for each collection if it is an attribute serialize attribute and 
2676     //append that child to container element.
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      * A generic method to process the extra attributes and the the extra
2704      * elements present within the schema.
2705      * What are considered extensions are  child elements with non schema namespace
2706      * and child attributes with any namespace
2707      * @param schemaObject
2708      * @param parentElement
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                 //get the extra objects and call the respective deserializers
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 }