Thinktecture.Tools.Web.Services.CodeGeneration.R2DMessage.Translate C# (CSharp) Method

Translate() public method

public Translate ( string operationName, string operationNS, bool isInput ) : void
operationName string
operationNS string
isInput bool
return void
        public void Translate(string operationName, string operationNS, bool isInput)
        {
            elem.ParentNode.InsertBefore(elem.OwnerDocument.CreateComment(elem.OuterXml), elem);
            XmlElement schemaElem = (XmlElement) elem.OwnerDocument.SelectSingleNode("wsdl:definitions/wsdl:types/xsd:schema[@targetNamespace='"+operationNS+"']", nsmgr);

            if (schemaElem == null )
            {
                XmlElement typesElem = (XmlElement) elem.OwnerDocument.SelectSingleNode("wsdl:definitions/wsdl:types", nsmgr);
                if (typesElem == null)
                {
                    throw new Rpc2DocumentLiteralTranslationException("<wsdl:types> not found.");
                }
                schemaElem = typesElem.OwnerDocument.CreateElement("xsd", "schema", "http://www.w3.org/2001/XMLSchema");
                schemaElem.Attributes.Append(typesElem.OwnerDocument.CreateAttribute("targetNamespace"));
                schemaElem.Attributes["targetNamespace"].Value = operationNS;
                typesElem.AppendChild(schemaElem);
            }

            string targetNamespace = schemaElem.Attributes["targetNamespace"].Value;
            string targetNamespacePrefix = "r2dtns";
            StringBuilder targetNamespacePrefixSb = new StringBuilder(targetNamespacePrefix);
            XmlAttribute tnsAttr = schemaElem.Attributes[targetNamespacePrefix];

            while (tnsAttr != null)
            {
                if (tnsAttr.Value != targetNamespace)
                {
                    //targetNamespacePrefix += "x";
                    targetNamespacePrefixSb.Append("x");
                    tnsAttr = schemaElem.Attributes[targetNamespacePrefixSb.ToString()];
                }
            }

            targetNamespacePrefix = targetNamespacePrefixSb.ToString();

            if (tnsAttr == null)
            {
                tnsAttr = schemaElem.OwnerDocument.CreateAttribute("xmlns:"+ targetNamespacePrefix);
                tnsAttr.Value = targetNamespace;
                schemaElem.Attributes.Append(tnsAttr);
            }

            XmlElement elemElem = elem.OwnerDocument.CreateElement("xsd", "element", "http://www.w3.org/2001/XMLSchema");
            XmlElement typeElem = elem.OwnerDocument.CreateElement("xsd", "complexType", "http://www.w3.org/2001/XMLSchema");
            XmlAttribute nameAttribute = typeElem.OwnerDocument.CreateAttribute("name");
            XmlAttribute typeAttribute = typeElem.OwnerDocument.CreateAttribute("type");
            nameAttribute.Value = GetSchemaElementName(operationName, isInput);

            typeAttribute.Value = targetNamespacePrefix + ":" + nameAttribute.Value;

            typeElem.Attributes.Append(nameAttribute);
            elemElem.Attributes.Append((XmlAttribute)nameAttribute.CloneNode(true));
            elemElem.Attributes.Append(typeAttribute);
            schemaElem.InsertBefore(elemElem, schemaElem.FirstChild);
            schemaElem.InsertAfter(typeElem, elemElem);
            XmlElement sequenceElem = elem.OwnerDocument.CreateElement("xsd", "sequence", "http://www.w3.org/2001/XMLSchema");
            typeElem.InsertBefore(sequenceElem, typeElem.FirstChild);
            schemaElem.InsertBefore(elem.OwnerDocument.CreateComment("Begin of generated message type"), elemElem);
            schemaElem.InsertAfter(elem.OwnerDocument.CreateComment("End of generated message type"), typeElem);

            XmlElement part = (XmlElement) elem.FirstChild;

            foreach (R2DType p in parameters)
            {
                p.Translate(sequenceElem, part.Attributes["name"].Value, targetNamespacePrefix);
                part = (XmlElement) part.NextSibling;
            }

            XmlNode n = elem.LastChild;

            while (n!= null)
            {
                XmlNode toDelete = n;
                n = n.PreviousSibling;
                elem.RemoveChild(toDelete);
            }

            XmlElement paramElem = elem.OwnerDocument.CreateElement("wsdl", "part", "http://schemas.xmlsoap.org/wsdl/");
            elem.AppendChild(paramElem);
            XmlAttribute typeNsAttr = elem.OwnerDocument.CreateAttribute("xmlns:typens");
            typeNsAttr.Value = targetNamespace;
            elem.Attributes.Append(typeNsAttr);
            paramElem.Attributes.Append(paramElem.OwnerDocument.CreateAttribute("", "name", ""));
            paramElem.Attributes["name"].Value="parameter";
            paramElem.Attributes.Append(paramElem.OwnerDocument.CreateAttribute("", "element", "" ));
            paramElem.Attributes["element"].Value="typens:" + GetSchemaElementName(operationName, isInput);
        }

Usage Example

 public void Translate()
 {
     if (translated == false)
     {
         inputMsg.Translate(name, inNS, true);
         if (outputMsg != null)
         {
             outputMsg.Translate(name, outNS, false);
         }
     }
     translated = true;
 }