axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r358255 - /webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java
Date Wed, 21 Dec 2005 10:23:33 GMT
Author: sanka
Date: Wed Dec 21 02:23:27 2005
New Revision: 358255

URL: http://svn.apache.org/viewcvs?rev=358255&view=rev
Log:
Added: Generating wrapper elements for Messages with more than one Part

Modified:
    webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java

Modified: webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java?rev=358255&r1=358254&r2=358255&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java
(original)
+++ webservices/axis2/trunk/java/modules/core/src/org/apache/axis2/deployment/AxisServiceBuilder.java
Wed Dec 21 02:23:27 2005
@@ -16,6 +16,33 @@
 
 package org.apache.axis2.deployment;
 
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.wsdl.Binding;
+import javax.wsdl.BindingOperation;
+import javax.wsdl.Definition;
+import javax.wsdl.Input;
+import javax.wsdl.Message;
+import javax.wsdl.Operation;
+import javax.wsdl.Output;
+import javax.wsdl.Part;
+import javax.wsdl.Port;
+import javax.wsdl.Service;
+import javax.wsdl.Types;
+import javax.wsdl.WSDLException;
+import javax.wsdl.extensions.ExtensibilityElement;
+import javax.wsdl.extensions.schema.Schema;
+import javax.wsdl.extensions.soap.SOAPBinding;
+import javax.wsdl.factory.WSDLFactory;
+import javax.wsdl.xml.WSDLReader;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.description.AxisMessage;
 import org.apache.axis2.description.AxisOperation;
@@ -30,18 +57,6 @@
 import org.w3c.dom.Element;
 import org.xml.sax.SAXException;
 
-import javax.wsdl.*;
-import javax.wsdl.extensions.ExtensibilityElement;
-import javax.wsdl.extensions.schema.Schema;
-import javax.wsdl.extensions.soap.SOAPBinding;
-import javax.wsdl.factory.WSDLFactory;
-import javax.wsdl.xml.WSDLReader;
-import javax.xml.parsers.ParserConfigurationException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Iterator;
-import java.util.Map;
-
 /**
  * AxisServiceBuilder builds an AxisService using a WSDL document which is feed
  * as a javax.wsdl.Definition or as an InputStream. If there are multiple
@@ -51,6 +66,26 @@
  */
 public class AxisServiceBuilder {
 
+    private static final String XMLSCHEMA_NAMESPACE_URI = "http://www.w3.org/2001/XMLSchema";
+
+    private static final String XMLSCHEMA_NAMESPACE_PREFIX = "xs";
+
+    private static final String XML_SCHEMA_SEQUENCE_LOCAL_NAME = "sequence";
+
+    private static final String XML_SCHEMA_COMPLEX_TYPE_LOCAL_NAME = "complexType";
+
+    private static final String XML_SCHEMA_ELEMENT_LOCAL_NAME = "element";
+
+    private static final String XML_SCHEMA_IMPORT_LOCAL_NAME = "import";
+
+    private static final String XSD_NAME = "name";
+
+    private static final String XSD_TYPE = "type";
+
+    private static final String XSD_REF = "ref";
+
+    private int nsCount = 1;
+
     public AxisService getAxisService(InputStream wsdlInputStream)
             throws WSDLException {
         WSDLReader reader = WSDLFactory.newInstance().newWSDLReader();
@@ -127,21 +162,48 @@
                 AxisOperation axisOperation = AxisOperationFactory
                         .getAxisOperation(getMessageExchangePattern(bindingOperation));
 
+                // setting operation name
+                axisOperation.setName(new QName(operation.getName()));
+
                 // Input
                 Input input = operation.getInput();
                 AxisMessage inAxisMsg = axisOperation
                         .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
                 Message inMsg = input.getMessage();
-                inAxisMsg.setElementQName(inMsg.getQName());
+
+                if (wrapperble(inMsg)) {
+                    Element wrapper = getWrappedElement(definition, inMsg);
+                    inAxisMsg.setElementQName(new QName(wrapper
+                            .getNamespaceURI(), wrapper.getLocalName(), wrapper
+                            .getPrefix()));
+
+                    // TODO
+                    // axisService.addSchema(getXmlSchema(wrapper));
+
+                } else {
+                    inAxisMsg.setElementQName(inMsg.getQName());
+                }
 
                 // Output
                 Output output = operation.getOutput();
 
                 if (output != null) {
+
                     AxisMessage outAxisMsg = axisOperation
                             .getMessage(WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
                     Message outMsg = output.getMessage();
-                    outAxisMsg.setElementQName(outMsg.getQName());
+
+                    if (wrapperble(outMsg)) {
+                        Element wrapper = getWrappedElement(definition, outMsg);
+                        outAxisMsg.setElementQName(new QName(wrapper
+                                .getNamespaceURI(), wrapper.getLocalName(),
+                                wrapper.getPrefix()));
+                        // TODO
+                        // axisService.addSchema(getXmlSchema(wrapper));
+
+                    } else {
+                        outAxisMsg.setElementQName(outMsg.getQName());
+                    }
                 }
 
                 axisService.addOperation(axisOperation);
@@ -151,6 +213,7 @@
                 throw new WSDLProcessingException(axisFault.getMessage());
             }
         }
+        getXmlSchemaPrefix(definition);
 
         return axisService;
     }
@@ -159,12 +222,12 @@
         Iterator ports = service.getPorts().values().iterator();
         Port port;
         Binding binding;
-
+        Iterator extElements;
+        
         while (ports.hasNext()) {
             port = (Port) ports.next();
             binding = port.getBinding();
-            Iterator extElements = binding.getExtensibilityElements()
-                    .iterator();
+            extElements = binding.getExtensibilityElements().iterator();
 
             while (extElements.hasNext()) {
 
@@ -188,5 +251,138 @@
 
     private XmlSchema getXMLSchema(Element element) {
         return (new XmlSchemaCollection()).read(element);
+    }
+
+    private String getXmlSchemaPrefix(Definition definition) {
+        Map namespaces = definition.getNamespaces();
+
+        if (namespaces.containsValue(XMLSCHEMA_NAMESPACE_URI)) {
+
+            Iterator prefixes = namespaces.keySet().iterator();
+            String prefix;
+
+            while (prefixes.hasNext()) {
+                prefix = (String) prefixes.next();
+
+                if (XMLSCHEMA_NAMESPACE_URI.equals(namespaces.get(prefix))) {
+                    return prefix;
+                }
+            }
+        }
+        return XMLSCHEMA_NAMESPACE_PREFIX; //default prefix
+    }
+
+    private boolean wrapperble(Message message) {
+        Map parts = message.getParts();
+
+        if (parts.size() > 1) {
+            return true;
+        }
+
+        Iterator iterator = parts.values().iterator();
+        Part part;
+
+        while (iterator.hasNext()) {
+            part = (Part) iterator.next();
+            if (part.getTypeName() != null) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private Document getDOMDocument() {
+        try {
+            DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
+            fac.setNamespaceAware(true);
+            return fac.newDocumentBuilder().newDocument();
+
+        } catch (ParserConfigurationException ex) {
+            throw new WSDLProcessingException(ex.getMessage());
+        }
+    }
+
+    private Element getWrappedElement(Definition definition, Message message) {
+        Document document = getDOMDocument();
+        String xsdPrefix = getXmlSchemaPrefix(definition);
+        Map namespaceImports = new HashMap();
+        Map namespacePrefixes = new HashMap();
+        Map parts = message.getParts();
+
+        String name = message.getQName().getLocalPart();
+        Element nComplexType = document.createElementNS(
+                XMLSCHEMA_NAMESPACE_URI, xsdPrefix + ":"
+                        + XML_SCHEMA_COMPLEX_TYPE_LOCAL_NAME);
+        nComplexType.setAttribute(XSD_NAME, name);
+
+        Element contextSequence = document.createElementNS(
+                XMLSCHEMA_NAMESPACE_URI, xsdPrefix + ":"
+                        + XML_SCHEMA_SEQUENCE_LOCAL_NAME);
+
+        Element childElement;
+        Iterator iterator = parts.values().iterator();
+
+        while (iterator.hasNext()) {
+            Part part = (Part) iterator.next();
+            String elementName = part.getName();
+            boolean isTyped = true;
+
+            QName schemaTypeName;
+
+            if (part.getTypeName() != null) {
+                schemaTypeName = part.getTypeName();
+
+            } else if (part.getElementName() != null) {
+                schemaTypeName = part.getElementName();
+
+            } else {
+                throw new WSDLProcessingException("unqualified message part");
+            }
+
+            childElement = document.createElementNS(XMLSCHEMA_NAMESPACE_URI,
+                    xsdPrefix + ":" + XML_SCHEMA_ELEMENT_LOCAL_NAME);
+            String prefix;
+
+            if (!XMLSCHEMA_NAMESPACE_URI.equals(schemaTypeName
+                    .getNamespaceURI())) {
+                String namespace = schemaTypeName.getNamespaceURI();
+
+                if (namespaceImports.containsKey(namespace)) {
+                    Element namespaceImport = document.createElementNS(
+                            XMLSCHEMA_NAMESPACE_URI, xsdPrefix + ":"
+                                    + XML_SCHEMA_IMPORT_LOCAL_NAME);
+                    namespaceImport.setAttribute("namespace", namespace);
+                    namespaceImports.put(namespace, namespaceImport);
+                    prefix = getTempPrefix();
+                    namespacePrefixes.put(namespace, prefix);
+
+                } else {
+                    prefix = (String) namespacePrefixes.get(namespace);
+                }
+
+            } else {
+                prefix = xsdPrefix;
+            }
+
+            if (isTyped) {
+                childElement.setAttribute(XSD_NAME, elementName);
+                childElement.setAttribute(XSD_TYPE, prefix + ":"
+                        + schemaTypeName.getLocalPart());
+
+            } else {
+                childElement.setAttribute(XSD_REF, prefix + ":"
+                        + schemaTypeName.getLocalPart());
+            }
+
+            contextSequence.appendChild(childElement);
+        }
+
+        nComplexType.appendChild(contextSequence);
+        return nComplexType;
+
+    }
+
+    private String getTempPrefix() {
+        return "ns" + nsCount++;
     }
 }



Mime
View raw message