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