private void writeSerializeGlobalMethod()

in src/wsdl/org/apache/axis/wsdl/wsdl2ws/cpp/BeanParamWriter.java [311:615]


    private void writeSerializeGlobalMethod() throws IOException, WrapperFault 
    {
        /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
        /* NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE   */
        /* ----------------------------------------------------------------   */
        /* CHANGES TO FILE MAY NEED TO BE PROPAGATED TO THE                   */
        /* C-EQUIVALENT FILE IN SUPPORT OF THE C-BINDING INTERFACES.          */
        /* ----------------------------------------------------------------   */
        /* NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE   */
        /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

        CUtils.printMethodComment(c_writer, "Function to serialize an object of type "  
                + c_classname + ".");        
        
        c_writer.write( "int Axis_Serialize_" + c_classname 
                     + "( " + c_classname + "* param, IWrapperSoapSerializer* pSZ, bool bArray)\n");
        c_writer.write( "{\n");
       
        //=============================================================================
        // No attributes or elements to serialize? Then serialize extension and return.
        //=============================================================================        

        if (attribs.length == 0) 
        {
            CUtils.printBlockComment(c_writer, "No attributes or elements to serialize.");
            
            c_writer.write("\tpSZ->serialize(\">\", NULL);\n");

            writeSerializeExtensionCode();
            
            c_writer.write("\treturn AXIS_SUCCESS;\n");
            c_writer.write("}\n");
            return;
        }
        
        //=============================================================================
        // NULL param passed in? Assume nillable although type does not know whether
        // it is used as a nillable parameter so this may not be the appropriate place
        // to put this, or we need to find a way to determine if nillable.
        //=============================================================================        

        CUtils.printBlockComment(c_writer, "If null input, serialize as nil element.");
        
        c_writer.write("\tif ( param == NULL )\n\t{\n");
        c_writer.write("\t\tpSZ->serializeAsAttribute( \"xsi:nil\", 0, (void*)&(xsd_boolean_true), XSD_BOOLEAN);\n");
        
        c_writer.write("\t\tpSZ->serialize( \">\", NULL);\n");
        c_writer.write("\t\treturn AXIS_SUCCESS;\n");
        c_writer.write("\t}\n");
        
        //=============================================================================
        // This is the only real difference for the serializer between rpc/encoded and 
        // doc/literal objects
        //=============================================================================        
        
        if (wscontext.getWrapperInfo().getBindingStyle().equals("rpc"))
            writeRPCArrayPortionOfSerializeGlobalMethod();
        else
            writeDOCArrayPortionOfSerializeGlobalMethod();
        
        //=============================================================================
        // Serialize attributes, if any
        //=============================================================================        

        if (attributeParamCount > 0)
            CUtils.printBlockComment(c_writer, "Serialize attributes.");
        
        for (int i = 0; i < attributeParamCount; i++)
        {
            if (attribs[i].isArray() || !(attribs[i].isSimpleType() || attribs[i].getType().isSimpleType()))
                throw new WrapperFault("Error : an attribute is not basic type");
            
            String soapTagName = attribs[i].getParamNameAsSOAPString();

            Type attrType = attribs[i].getType();
            String basicType = null;
            
            if (!attribs[i].isSimpleType() && attrType.isSimpleType())
                basicType = CUtils.getSimpleType(attrType.getBaseType());
            else
                basicType = attribs[i].getTypeName();

            if (CUtils.isPointerType(basicType) || attribs[i].isOptional())
            {
                // TODO: Add check if ptr type and not optional and is null, throw exception.
                c_writer.write("\tif (0 != param->" + attribs[i].getParamNameAsMember() + ")\n\t");                
                c_writer.write("\tpSZ->serializeAsAttribute(\""
                        + soapTagName + "\", 0, (void*)(param->"
                        + attribs[i].getParamNameAsMember() + "), "
                        + CUtils.getXSDEnumeratorForType(basicType) + ");\n");
            }
            else
            {
                c_writer.write("\tpSZ->serializeAsAttribute(\""
                        + soapTagName + "\", 0, (void*)&(param->"
                        + attribs[i].getParamNameAsMember() + "), "
                        + CUtils.getXSDEnumeratorForType(attribs[i].getTypeName()) + ");\n");
            }
        }

        //=============================================================================
        // Serialization relating to faults
        //=============================================================================                           
                
        if (type.isFault())
        {
            c_writer.write("\tif(Axis_URI_" + c_classname + ")\n\t{\n");
            c_writer.write("\t\tbool blnIsNewPrefix = false;\n");
            c_writer.write("\t\tconst AxisChar* sPrefix = pSZ->getNamespacePrefix(Axis_URI_"
                        + c_classname + ", blnIsNewPrefix);\n");
            c_writer.write("\t\tpSZ->serialize(\" xmlns:\", sPrefix, \"=\\\"\",");
            c_writer.write("Axis_URI_" + c_classname + ", \" " + " \\\"\"");
            c_writer.write(", NULL);\n\t}\n");
        }               
        
        if (wscontext.getWrapperInfo().getBindingStyle().equals("document"))
            c_writer.write("\tpSZ->serialize( \">\", 0);\n");
        
        //=============================================================================
        // Serialize extension, if any
        //=============================================================================                           
        
        writeSerializeExtensionCode();

        //=============================================================================
        // Serialize elements, if any
        //=============================================================================                
        
        if (attributeParamCount < attribs.length)
            CUtils.printBlockComment(c_writer, "Serialize sub-elements.");

        boolean firstIfWritten = false;
        int anyCounter = 0; //counter for any types.
        String arrayType;
        
        for (int i = attributeParamCount; i < attribs.length; i++)
        {
            String namespace = "NULL";
            if (attribs[i].getNsQualified())
            {
                namespace = "Axis_URI_" + c_classname;
                
                // Elements can reference other elements in different schema...need to handle.
                QName elementName = attribs[i].getElementName();
                if (elementName != null)
                   namespace = "\"" + elementName.getNamespaceURI() + "\"";
            }
            
            // if the attribute is a choice following should do
            boolean ifCheckPrinted = false;
            if (attribs[i].getChoiceElement())
            {
                if (!firstIfWritten)
                {
                    c_writer.write("\tif");
                    firstIfWritten = true;
                } 
                else
                    c_writer.write("\telse if");

                ifCheckPrinted = true;
                c_writer.write("(param->" + attribs[i].getParamNameAsMember() + ")\n\t{\n\t");
            }
            else
                firstIfWritten = false;
 
            //if the attribute is a 'all' following should do
            if (attribs[i].getAllElement())
                if (attribs[i].getMinOccurs() == 0)
                {
                    ifCheckPrinted = true;
                    c_writer.write("\tif(param->" + attribs[i].getParamNameAsMember() + ")\n\t{\n\t");
                }
             
            if (attribs[i].isAnyElement())
            {
                anyCounter += 1;
                String fieldName  = "any" + Integer.toString(anyCounter);
                
                if (!ifCheckPrinted && attribs[i].isOptional())
                    c_writer.write("\tif (param->" + fieldName + " != NULL)\n");
                                
                c_writer.write("\t\tpSZ->serializeAnyObject(param->" + fieldName +");\n");
            }
            else if (attribs[i].isArray())
            {
                if (attribs[i].isSimpleType() || attribs[i].getType().isSimpleType())
                {
                    String baseTypeName = null;
                    if (!attribs[i].isSimpleType() && attribs[i].getType().isSimpleType())
                        baseTypeName = CUtils.getSimpleType(attribs[i].getType().getBaseType());
                    else
                        baseTypeName = attribs[i].getTypeName();
                    
                    c_writer.write("\tpSZ->serializeBasicArray(param->" + attribs[i].getParamNameAsMember()
                        + ", " + namespace + ","
                        + CUtils.getXSDEnumeratorForType(baseTypeName) + ", \""
                        + attribs[i].getParamNameAsSOAPString() + "\");\n");
                }
                else
                {
                    arrayType = attribs[i].getTypeName();
                    
                    c_writer.write("\tpSZ->serializeCmplxArray(param->" + attribs[i].getParamNameAsMember() 
                            + ", (void*)Axis_Serialize_" + arrayType 
                            + ", (void*)Axis_Delete_" + arrayType 
                            + ", \"" + attribs[i].getElementNameAsSOAPString() + "\", " + namespace + ");\n");
                }
            }
            else if (attribs[i].isSimpleType() || attribs[i].getType().isSimpleType())
            {
                String typeName = attribs[i].getTypeName();
                String baseTypeName = null;
                if (attribs[i].getType().isSimpleType())
                    baseTypeName = CUtils.getSimpleType (attribs[i].getType().getBaseType ());
                else
                    baseTypeName = typeName;
                
                if (!ifCheckPrinted && attribs[i].isOptional())
                    c_writer.write("\tif (param->" + attribs[i].getParamNameAsMember() + " != NULL)\n\t");
                
                // If the simple type is a choice it is handled
                // as a pointer variable.  This is the same in 'all' element and nillable elements.
                String ampersand = "&";
                if (CUtils.isPointerType(baseTypeName)
                            || attribs[i].getChoiceElement()
                            || attribs[i].getAllElement()
                            || isElementNillable(i) || isElementOptional(i))
                    ampersand = "";

                c_writer.write("\tpSZ->serializeAsElement(\""
                        + attribs[i].getElementNameAsSOAPString() + "\", " + namespace
                        + ", (void*)" + ampersand + "(param->" + attribs[i].getParamNameAsMember() + "), " 
                        + CUtils.getXSDEnumeratorForType(baseTypeName) + ");\n");
            }
            else
            {
                //if complex type
                
                String tab = "";
                if (ifCheckPrinted)
                    tab = "\t";
                else if (attribs[i].isOptional())
                {
                    tab = "\t";
                    c_writer.write("\tif (param->" + attribs[i].getParamNameAsMember() + " != NULL)\n\t{\n\t");
                }
                
                String elm = attribs[i].getParamNameAsSOAPString();
                if (attribs[i].isReference())
                    elm = attribs[i].getTypeName();

                if (attribs[i].getNsQualified())
                {
                    namespace = type.getName().getNamespaceURI();
                    QName elementName = attribs[i].getElementName();
                    if (elementName != null)
                       namespace = elementName.getNamespaceURI();

                    c_writer.write("\tsPrefix = pSZ->getNamespacePrefix(\"" + namespace + "\", blnIsNewSubElemPrefix);\n");
                    c_writer.write(tab + "\tpSZ->serialize(\"<\", sPrefix, \":\", \"" + elm + "\", 0);\n");
                    c_writer.write(tab + "\tif (blnIsNewSubElemPrefix)\n");
                    c_writer.write(tab + "\t\tpSZ->serialize(\" xmlns:\", sPrefix, \"=\\\"\", \"" + namespace + "\", \"\\\"\", 0);\n");
                    c_writer.write(tab + "\tAxis_Serialize_" + attribs[i].getTypeName() + "(param->" + attribs[i].getParamNameAsMember() + ", pSZ);\n");
                    c_writer.write(tab + "\tpSZ->serialize(\"</\", sPrefix, \":\", \"" + elm + "\", \">\", 0);\n");
                    c_writer.write(tab + "\tif (blnIsNewSubElemPrefix)\n");
                    c_writer.write(tab + "\t\tpSZ->removeNamespacePrefix(\""  + namespace + "\");\n");
                }
                else
                {
                    c_writer.write("\tpSZ->serialize(\"<" + elm + "\", 0);\n");
                    c_writer.write(tab + "\tAxis_Serialize_" + attribs[i].getTypeName()
                            + "(param->" + attribs[i].getParamNameAsMember() + ", pSZ);\n");
                    c_writer.write(tab + "\tpSZ->serialize(\"</" + elm + "\", \">\", 0);\n");
                }
                
                if (!ifCheckPrinted && attribs[i].isOptional())
                    c_writer.write("\t}\n");
            }

            //end if choice element

            if (ifCheckPrinted)
                c_writer.write("\t}\n");
        }

        //=============================================================================
        // End of attribute and element serialization
        //=============================================================================                
                
        if (wscontext.getWrapperInfo().getBindingStyle().equals("rpc"))
        {
            c_writer.write("\n\tpSZ->serialize(\"</\", Axis_TypeName_" + c_classname
                    + ", \">\", NULL);\n");
        }

        CUtils.printBlockComment(c_writer, "Remove namespace, if new.");        
        
        c_writer.write("\tif (!bArray && blnIsNewPrefix)\n");
        c_writer.write("\t\tpSZ->removeNamespacePrefix(Axis_URI_" + c_classname + ");\n");
        c_writer.write("\n");
        
        c_writer.write("\treturn AXIS_SUCCESS;\n");
        c_writer.write("}\n");
    }