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