tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r574961 - in /incubator/tuscany/java/sca/modules/binding-ws-xml/src: main/java/org/apache/tuscany/sca/binding/ws/xml/ test/resources/org/apache/tuscany/sca/binding/ws/xml/
Date Wed, 12 Sep 2007 14:08:01 GMT
Author: antelder
Date: Wed Sep 12 07:08:00 2007
New Revision: 574961

URL: http://svn.apache.org/viewvc?rev=574961&view=rev
Log:
Start work to support wsa:endpointReference. Store the EPR as a DOM Element to avoid dependencies
and leave it to the binding impl to process it

Added:
    incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
  (with props)
Modified:
    incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java
    incubator/tuscany/java/sca/modules/binding-ws-xml/src/test/resources/org/apache/tuscany/sca/binding/ws/xml/CalculatorImpl.componentType

Added: incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java?rev=574961&view=auto
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
(added)
+++ incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
Wed Sep 12 07:08:00 2007
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+
+package org.apache.tuscany.sca.binding.ws.xml;
+
+import static javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+
+import java.io.StringWriter;
+
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamConstants;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+/**
+ * Helper methods to read and write a wsa:endpointReference
+ * TODO: almost direct copy of code for Assembly properties
+ *       must be able to move to a common utility
+ */
+public class EndPointReferenceHelper {
+    
+    /**
+     * Read a wsa:endpointReference into a DOM Element
+     */
+    public static Element readEndPointReference(XMLStreamReader reader) {
+        try {
+
+            return loadElement(reader);
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+    
+    /**
+     * Write a wsa:endpointReference from a DOM Element
+     */
+    public static void writeEndPointReference(Element element, XMLStreamWriter writer)  {
+        try {
+            Source domSource = new DOMSource(element);
+            StreamResult result = new StreamResult(new StringWriter());
+            javax.xml.transform.Transformer transformer = TransformerFactory.newInstance().newTransformer();
+            transformer.transform(domSource , result);
+        } catch (TransformerException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static Element loadElement(XMLStreamReader reader) throws XMLStreamException,
ParserConfigurationException {
+        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+        Node root = document;
+        Node current = root;
+        while (true) {
+            switch (reader.getEventType()) {
+                case XMLStreamConstants.START_ELEMENT:
+                    QName name = reader.getName();
+                    Element child = createElement(document, name);
+
+                    // push the new element and make it the current one
+                    current.appendChild(child);
+                    current = child;
+
+                    int count = reader.getNamespaceCount();
+                    for (int i = 0; i < count; i++) {
+                        String prefix = reader.getNamespacePrefix(i);
+                        String ns = reader.getNamespaceURI(i);
+                        declareNamespace(child, prefix, ns);
+                    }
+
+                    if(!"".equals(name.getNamespaceURI()))
+                    declareNamespace(child, name.getPrefix(), name.getNamespaceURI());
+
+                    // add the attributes for this element
+                    count = reader.getAttributeCount();
+                    for (int i = 0; i < count; i++) {
+                        String ns = reader.getAttributeNamespace(i);
+                        String prefix = reader.getAttributePrefix(i);
+                        String qname = reader.getAttributeLocalName(i);
+                        String value = reader.getAttributeValue(i);
+                        if (prefix != null && prefix.length() != 0) {
+                            qname = prefix + ":" + qname;
+                        }
+                        child.setAttributeNS(ns, qname, value);
+                        if (ns != null) {
+                            declareNamespace(child, prefix, ns);
+                        }
+                    }
+
+                    break;
+                case XMLStreamConstants.CDATA:
+                    current.appendChild(document.createCDATASection(reader.getText()));
+                    break;
+                case XMLStreamConstants.CHARACTERS:
+                    current.appendChild(document.createTextNode(reader.getText()));
+                    break;
+                case XMLStreamConstants.END_ELEMENT:
+                    // if we are back at the root then we are done
+                    if ("EndpointReference".equals(reader.getName().getLocalPart())) {
+                        return document.getDocumentElement();
+                    }
+
+                    // pop the element off the stack
+                    current = current.getParentNode();
+            }
+            if ( reader.hasNext()) reader.next();
+        }
+    }
+
+    private static Element createElement(Document document, QName name) {
+        String prefix = name.getPrefix();
+        String qname =
+            (prefix != null && prefix.length() > 0) ? prefix + ":" + name.getLocalPart()
: name.getLocalPart();
+        return document.createElementNS(name.getNamespaceURI(), qname);
+    }
+
+    private static void declareNamespace(Element element, String prefix, String ns) {
+        if (ns == null) {
+            ns = "";
+        }
+        if (prefix == null) {
+            prefix = "";
+        }
+        String qname = null;
+        if ("".equals(prefix)) {
+            qname = "xmlns";
+        } else {
+            qname = "xmlns:" + prefix;
+        }
+        Node node = element;
+        boolean declared = false;
+        while (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
+            NamedNodeMap attrs = node.getAttributes();
+            if (attrs == null) {
+                break;
+            }
+            Node attr = attrs.getNamedItem(qname);
+            if (attr != null) {
+                declared = ns.equals(attr.getNodeValue());
+                break;
+            }
+            node = node.getParentNode();
+        }
+        if (!declared) {
+            org.w3c.dom.Attr attr = element.getOwnerDocument().createAttributeNS(XMLNS_ATTRIBUTE_NS_URI,
qname);
+            attr.setValue(ns);
+            element.setAttributeNodeNS(attr);
+        }
+    }
+}

Propchange: incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/EndPointReferenceHelper.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java?rev=574961&r1=574960&r2=574961&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java
(original)
+++ incubator/tuscany/java/sca/modules/binding-ws-xml/src/main/java/org/apache/tuscany/sca/binding/ws/xml/WebServiceBindingProcessor.java
Wed Sep 12 07:08:00 2007
@@ -20,6 +20,7 @@
 package org.apache.tuscany.sca.binding.ws.xml;
 
 import static javax.xml.stream.XMLStreamConstants.END_ELEMENT;
+import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
 
 import java.util.Map;
 
@@ -97,6 +98,7 @@
 
         // Read a qname in the form:
         // namespace#wsdl.???(name)
+        Boolean wsdlElementIsBinding = null;
         String wsdlElement = reader.getAttributeValue(null, WSDL_ELEMENT);
         if (wsdlElement != null) {
             int index = wsdlElement.indexOf('#');
@@ -142,6 +144,8 @@
                 // Read a wsdl.service
                 localName = localName.substring("wsdl.binding(".length(), localName.length()
- 1);
                 wsBinding.setBindingName(new QName(namespace, localName));
+                
+                wsdlElementIsBinding = true;
 
             } else {
                 throw new ContributionReadException(
@@ -154,13 +158,23 @@
 
         // Skip to end element
         while (reader.hasNext()) {
-            if (reader.next() == END_ELEMENT && BINDING_WS_QNAME.equals(reader.getName()))
{
+            int event = reader.next();
+            if (event == START_ELEMENT && "EndpointReference".equals(reader.getName().getLocalPart()))
{
+                if (wsdlElementIsBinding != null && wsdlElementIsBinding) {
+                    throw new ContributionReadException(wsdlElement + " must use wsdl.binding
when using wsa:EndpointReference");
+                }
+                wsBinding.setEndPointReference(EndPointReferenceHelper.readEndPointReference(reader));
+            }
+            if (event == END_ELEMENT && BINDING_WS_QNAME.equals(reader.getName()))
{
                 break;
             }
         }
         return wsBinding;
     }
 
+    protected void processEndPointReference(XMLStreamReader reader, WebServiceBinding wsBinding)
{
+    }
+
     public void write(WebServiceBinding wsBinding, XMLStreamWriter writer) throws ContributionWriteException,
XMLStreamException {
 
         // Write a <binding.ws>
@@ -221,6 +235,10 @@
             writer.writeAttribute(WSDLI_NS, WSDL_LOCATION, wsBinding.getLocation());
         }
 
+        if (wsBinding.getEndPointReference() != null) {
+            EndPointReferenceHelper.writeEndPointReference(wsBinding.getEndPointReference(),
writer);
+        }
+        
         writer.writeEndElement();
     }
 

Modified: incubator/tuscany/java/sca/modules/binding-ws-xml/src/test/resources/org/apache/tuscany/sca/binding/ws/xml/CalculatorImpl.componentType
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sca/modules/binding-ws-xml/src/test/resources/org/apache/tuscany/sca/binding/ws/xml/CalculatorImpl.componentType?rev=574961&r1=574960&r2=574961&view=diff
==============================================================================
--- incubator/tuscany/java/sca/modules/binding-ws-xml/src/test/resources/org/apache/tuscany/sca/binding/ws/xml/CalculatorImpl.componentType
(original)
+++ incubator/tuscany/java/sca/modules/binding-ws-xml/src/test/resources/org/apache/tuscany/sca/binding/ws/xml/CalculatorImpl.componentType
Wed Sep 12 07:08:00 2007
@@ -34,6 +34,11 @@
         <binding.ws uri="tempuri.org" wsdlElement="http://sample/calculator#wsdl.binding(DivideBinding)"/>
         <binding.ws uri="tempuri.org" wsdlElement="http://sample/calculator#wsdl.port(DivideService/DividePort)"/>
         <binding.ws uri="tempuri.org" wsdlElement="http://sample/calculator#wsdl.endpoint(DivideService/DivideEndpoint)"/>
+        <binding.ws>
+           <wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing">
+              <wsa:Address>myURI</wsa:Address>
+           </wsa:EndpointReference>
+        </binding.ws>
   </reference>  
 
 </componentType>              



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org


Mime
View raw message