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