View Javadoc

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