<%@ Page Language="vb" %>
<%
    Response.Write(ProcessWSDLDocument())
%>

<Script runat="server">

    Dim htmlDoc As New Msxml2.DOMDocument()
    Dim Null as System.DBNull
   
    Function ProcessWSDLDocument() 
         Dim xmlDoc As New Msxml2.DOMDocument()
         xmlDoc.async = false
         xmlDoc.load ("Path to Listing1.wsdl file\Listing1.wsdl")
    
         Dim html = htmlDoc.createElement("html")
         Dim body = htmlDoc.createElement("body")
         
         Dim para = htmlDoc.createElement("p")
         para.setAttribute ("align", "center")

         body.appendChild(para)
         html.appendChild(body)
         htmlDoc.appendChild(html)

          If xmlDoc.parseError.errorCode <> 0 Then
             para.appendChild(htmlDoc.createTextNode("WSDL File parsing error occurred"))
             Return htmlDoc.xml
         Else
             Dim definitionsElement = xmlDoc.documentElement
             Dim definitionsNamespacePrefix = getPrefix(definitionsElement.nodeName)
             Dim definitionsNamespaceURI = getNamespaceURI(definitionsElement, definitionsNamespacePrefix)

             If ((definitionsNamespaceURI <> "http://schemas.xmlsoap.org/wsdl/") Or + _
                 (getLocalName(definitionsElement.nodeName)<>"definitions")) Then
                  para.appendChild(htmlDoc.createTextNode("Invalid WSDL.. [either namespace or <definitions> element is incorrect]"))
             Else 
                 Dim serviceElement
                 Dim definitionChildren = definitionsElement.childNodes

                 Dim i 
                 For i=0 To definitionChildren.length -1
                     If ((getLocalName(definitionChildren.item(i).nodeName) = "service")And + _
                         (definitionChildren.item(i).nodeType = 1)) Then
                         para.appendChild(getServiceTable(definitionChildren.item(i)))
                     End If    
         
                 Next
             End If

             Return htmlDoc.xml

         End If

    End Function  'ProcessWSDLDocument()
    

    Function getServiceTable(serviceElement)
        Dim table = htmlDoc.createElement("table")

        table.setAttribute("border","6")
        table.setAttribute("bgColor", "#F0F8FF")
        table.setAttribute("cellPadding","4")
        table.setAttribute("cellSpacing","5")
        table.setAttribute("borderColor","#D2B48C")

        Dim tBody = htmlDoc.createElement("tbody")
        table.appendChild(tBody)

        tBody.appendChild(createRow("Name of Service", serviceElement.getAttribute("name")))
        Dim serviceChildren = serviceElement.childNodes
        Dim documentationElement

        Dim i
        For i=0 To serviceChildren.length -1
            If ((getLocalName(serviceChildren.item(i).nodeName) = "documentation") And + _
                (serviceChildren.item(i).nodeType = 1)) Then  '1 means the Element node
                 documentationElement = serviceChildren.item(i)
                 Dim documentation = documentationElement.firstChild.nodeValue
                 tBody.appendChild(createRow ("Service Description", documentation))
                 Exit For 'break
            End If
        Next

        Dim portElement = getChild(serviceElement, "port")

        If  portElement Is Null Then
            tBody.appendChild(createRow("Error !! ","No <port> element found!!"))

        Else
            Dim portName = portElement.getAttribute("name")
            Dim serviceSiblings = serviceElement.parentNode.childNodes

            Dim j
            For j=0 To serviceSiblings.length -1
                If ((getLocalName(serviceSiblings.item(j).tagName) = "portType") And + _
                    (serviceSiblings.item(j).getAttribute("name") = portName)) Then
                     Dim portTypeElement = serviceSiblings.item(j)
                     attachPortTypeSetOfRows(tBody, portTypeElement)
                End If
            Next
        End If

        Return table

             End Function 'getServiceTable


    Function attachPortTypeSetOfRows (tBody, portTypeElement)
       
       Dim OperationsList = portTypeElement.childNodes

       Dim i        
       For i=0 To OperationsList.length -1
           If (getLocalName(OperationsList.item(i).tagName) = "operation") Then
               attachOperationSetOfRows(tBody, OperationsList.item(i))
           End If    
       Next
     
    End Function 'getPortTypeSetOfRows 


    Function attachOperationSetOfRows(tBody, OperationElement)
        tBody.appendChild(createRow("Name of Operation",OperationElement.getAttribute("name")))
            Dim documentationElement = getChild(OperationElement, "documentation")

        If Not (documentationElement Is Null) Then
            tBody.appendChild(createRow ("Description of Operation", documentationElement.firstChild.nodeValue))
        End If

        Dim inputElement = getChild(OperationElement, "input")
        Dim messageElement

        If Not (inputElement Is Null) Then
            Dim inputMessage = inputElement.getAttribute("message")
            
            Dim operationParentSiblings = OperationElement.parentNode.parentNode.childNodes

            Dim i
            For i=0  To operationParentSiblings.length -1
                If ((getLocalName(operationParentSiblings.item(i).tagName)= "message") And + _
                    (operationParentSiblings.item(i).getAttribute("name")= inputMessage)) Then
                    attachMessageRows(tBody, operationParentSiblings.item(i))
                    Exit For 'break
                End If
            Next
        End If
          
    End Function 'attachOperationSetOfRows


    Function attachMessageRows(tBody, messageElement)
       Dim form = htmlDoc.createElement("form")
       form.setAttribute("action", "http://localhost/DOM4WS/SoapRequestGenerator")
       form.setAttribute ("method","GET")

       Dim partElements = messageElement.childNodes

       Dim i
       For i=0 To partElements.length -1
           If (getLocalName(partElements.item(i).tagName) = "part") Then
               attachPartRow(form, partElements.item(i))
           End If
       Next

       Dim input = htmlDoc.createElement("input")
       Dim emptycell = htmlDoc.createElement("td")
       Dim cell = htmlDoc.createElement("td")
       input.setAttribute("size", "25")
       input.setAttribute("type", "submit")
       input.setAttribute("value","Submit Query")
       cell.appendChild(input)

       Dim row = htmlDoc.createElement("tr")
       row.setAttribute("bgColor","#D2B48C")
       row.appendChild(emptycell)
       row.appendChild(cell)
       form.appendChild(row)
       tBody.appendChild(form)

   End Function 'attachMessageRows


   Function attachPartRow(form, partElement)
       Dim row = htmlDoc.createElement("tr")
       row.setAttribute("bgColor","#D2B48C")

       Dim cell = htmlDoc.createElement("td")
       Dim bold = htmlDoc.createElement("b")
       bold.appendChild(htmlDoc.createTextNode("Enter "+partElement.getAttribute("name")))
       cell.appendChild(bold)
       row.appendChild(cell)

       cell = htmlDoc.createElement("td")
       Dim input = htmlDoc.createElement("input")
       input.setAttribute("size", "25")
       input.setAttribute("name",partElement.getAttribute("name"))
       input.setAttribute("type","text")
       cell.appendChild(input)
       row.appendChild(cell)

       form.appendChild(row)

   End Function 'attachPartRow


   Function createRow( label, text ) 
       Dim row = htmlDoc.createElement("tr")
       row.setAttribute("bgColor", "#FDF5E6")

       Dim cell = htmlDoc.createElement("td")
       Dim bold = htmlDoc.createElement("b")
       Dim labelTextNode = htmlDoc.createTextNode(label)
       bold.appendChild(labelTextNode)
       cell.appendChild(bold)
       row.appendChild(cell)

       cell = htmlDoc.createElement("td")
       cell.appendChild(htmlDoc.createTextNode(text))
       row.appendChild(cell)

      Return row
 
   End Function ' createRow


   Function getChild(parentElement, childName)
       Dim childElement
       Dim children = parentElement.childNodes

       Dim i 
       For i=0 To children.length -1
           if (getLocalName(children.item(i).tagName) = childName)
               childElement = children.item(i)
               Exit For 'break
           End if
       Next
       
       Return childElement

    End Function 'getChild


    Function getNamespaceURI(elementNode, namespacePrefix)
        Dim namespaceURI

        If elementNode.nodeType = 9 Then '9 means the Document node 
            Return Null
        Else 
            namespaceURI = findNamespace (elementNode, namespacePrefix)
        End If
 
        If namespaceURI Is Null Then
            namespaceURI = getNamespaceURI(elementNode.parentNode, namespacePrefix)
        Else
            Return namespaceURI 
        End If
  
       Return namespaceURI

   End Function 'getNamespaceURI(elementNode, namespacePrefix)


   Function findNamespace(elementNode, namespacePrefix)

       Dim attributes = elementNode.attributes

       If Not (attributes Is Null) And (attributes.length > 0) Then 
           Dim x
           For x=0 To attributes.length -1
               Dim attributeNamespacePrefix = getPrefix(attributes.item(x).nodeName)
               Dim attributeNamespaceSuffix = getLocalName(attributes.item(x).nodeName)

               If  (attributeNamespacePrefix Is Null) And + _
                   (attributeNamespaceSuffix = "xmlns") Then
                    Return attributes.item(x).nodeValue
               Else If ((attributeNamespacePrefix = "xmlns") And + _
                   (attributeNamespaceSuffix = namespacePrefix)) Then
                    Return attributes.item(x).nodeValue
               End If
                
           Next

           Return Null
        End if
    End Function 'addNamespace (elementNode)


    Function getPrefix(tagName) 
        Dim prefix as String
        Dim prefixIndex = tagName.indexOf(":")

        If prefixIndex = -1 Then
            Return Null
        Else 
            prefix = tagName.substring(0, prefixIndex)
            Return prefix
        End If    

    End Function 'getPrefix(tagName)


    Function getLocalName(tagName)

       Dim suffix as String
       Dim prefixIndex = tagName.indexOf(":")

       If  prefixIndex = -1 Then
           Return tagName
       Else 
           suffix = tagName.Substring(prefixIndex+1, tagName.Length-(prefixIndex+1))
           Return suffix
       End If    

    End Function 'getLocalName(tagName)
       
</Script>