axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ami...@apache.org
Subject svn commit: r608128 - in /webservices/axis2/trunk/java/modules/adb: src/org/apache/axis2/databinding/utils/MultirefHelper.java test/org/apache/axis2/databinding/utils/MultirefHelperTest.java
Date Wed, 02 Jan 2008 15:35:20 GMT
Author: amilas
Date: Wed Jan  2 07:35:18 2008
New Revision: 608128

URL: http://svn.apache.org/viewvc?rev=608128&view=rev
Log:
Add helper methods to process the multirefs

Added:
    webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/MultirefHelperTest.java
Modified:
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/MultirefHelper.java

Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/MultirefHelper.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/MultirefHelper.java?rev=608128&r1=608127&r2=608128&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/MultirefHelper.java
(original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/MultirefHelper.java
Wed Jan  2 07:35:18 2008
@@ -19,10 +19,10 @@
 
 package org.apache.axis2.databinding.utils;
 
-import org.apache.axiom.om.OMAttribute;
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.*;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPBody;
 import org.apache.axis2.AxisFault;
 import org.apache.axis2.databinding.typemapping.SimpleTypeMapper;
 import org.apache.axis2.engine.ObjectSupplier;
@@ -30,6 +30,7 @@
 import javax.xml.namespace.QName;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 public class MultirefHelper {
 
@@ -168,6 +169,138 @@
     public void clean() {
         elementMap.clear();
         objectmap.clear();
+    }
+
+    /**
+     * this method is used to process the href attributes which may comes with the incomming
soap mesaage
+     * <soap:body>
+     * <operation>
+     * <arg1 href="#obj1"/>
+     * </operation>
+     * <multiref id="obj1">
+     * <name>the real argument</name>
+     * <color>blue</color>
+     * </multiref>
+     * </soap:body>
+     * here we assume first child of the soap body has the main object structure and others
contain the
+     * multiref parts.
+     * Soap spec says that those multiref parts must be top level elements.
+     *
+     * @param soapEnvelope
+     */
+
+    public static void processHrefAttributes(SOAPEnvelope soapEnvelope)
+            throws AxisFault {
+        // first populate the multiref parts to a hash table.
+        SOAPBody soapBody = soapEnvelope.getBody();
+        // first build the whole tree
+        soapBody.build();
+        OMElement omElement = null;
+        OMAttribute idAttribute = null;
+        Map idAndOMElementMap = new HashMap();
+        for (Iterator iter = soapBody.getChildElements(); iter.hasNext();) {
+            omElement = (OMElement) iter.next();
+            // the attribute id is an unqualified attribute
+            idAttribute = omElement.getAttribute(new QName(null, "id"));
+            if (idAttribute != null) {
+                // for the first element there may not have an id
+                idAndOMElementMap.put(idAttribute.getAttributeValue(), omElement);
+            }
+        }
+
+        // start processing from the first child
+        processHrefAttributes(idAndOMElementMap, soapBody.getFirstElement(), OMAbstractFactory.getOMFactory());
+
+    }
+
+    public static void processHrefAttributes(Map idAndOMElementMap,
+                                         OMElement elementToProcess,
+                                         OMFactory omFactory)
+            throws AxisFault {
+
+        // first check whether this element has an href value.
+        // href is also an unqualifed attribute
+        OMAttribute hrefAttribute = elementToProcess.getAttribute(new QName(null, "href"));
+        if (hrefAttribute != null) {
+            // i.e this has an href attribute
+            String hrefAttributeValue = hrefAttribute.getAttributeValue();
+            if (!hrefAttributeValue.startsWith("#")) {
+                throw new AxisFault("In valid href ==> " + hrefAttributeValue + " does
not starts with #");
+            } else {
+                OMElement referedOMElement =
+                        (OMElement) idAndOMElementMap.get(hrefAttributeValue.substring(1));
+                if (referedOMElement == null) {
+                    throw new AxisFault("In valid href ==> " + hrefAttributeValue + "
can not find" +
+                            "the matching element");
+                } else {
+                    // now we have to remove the hrefAttribute and add all the child elements
to the
+                    // element being proccesed
+                    elementToProcess.removeAttribute(hrefAttribute);
+                    OMElement clonedReferenceElement = getClonedOMElement(referedOMElement,
omFactory);
+                    OMNode omNode = null;
+                    for (Iterator iter = clonedReferenceElement.getChildren(); iter.hasNext();)
{
+                        omNode = (OMNode) iter.next();
+                        elementToProcess.addChild(omNode.detach());
+                    }
+
+                    // add attributes
+                    OMAttribute omAttribute = null;
+                    for (Iterator iter = clonedReferenceElement.getAllAttributes(); iter.hasNext();)
{
+                        omAttribute = (OMAttribute) iter.next();
+                        // we do not have to populate the id attribute
+                        if (!omAttribute.getLocalName().equals("id")) {
+                            elementToProcess.addAttribute(omAttribute);
+                        }
+                    }
+                }
+            }
+        }
+
+        // call recursively to proces all elements
+        OMElement childOMElement = null;
+        for (Iterator iter = elementToProcess.getChildElements(); iter.hasNext();) {
+            childOMElement = (OMElement) iter.next();
+            processHrefAttributes(idAndOMElementMap, childOMElement, omFactory);
+        }
+    }
+
+    /**
+     * returns an cloned om element for this OMElement
+     *
+     * @param omElement
+     * @return cloned omElement
+     */
+    public static OMElement getClonedOMElement(OMElement omElement, OMFactory omFactory)
throws AxisFault {
+
+        OMElement newOMElement = omFactory.createOMElement(omElement.getQName());
+
+        // copying attributes
+        OMAttribute omAttribute = null;
+        OMAttribute newOMAttribute = null;
+        for (Iterator iter = omElement.getAllAttributes(); iter.hasNext();) {
+            omAttribute = (OMAttribute) iter.next();
+            if (!omAttribute.getAttributeValue().equals("id")) {
+                newOMAttribute = omFactory.createOMAttribute(
+                        omAttribute.getLocalName(),
+                        omAttribute.getNamespace(),
+                        omAttribute.getAttributeValue());
+                newOMElement.addAttribute(newOMAttribute);
+            }
+        }
+        OMNode omNode = null;
+        OMText omText = null;
+        for (Iterator iter = omElement.getChildren(); iter.hasNext();) {
+            omNode = (OMNode) iter.next();
+            if (omNode instanceof OMText) {
+                omText = (OMText) omNode;
+                newOMElement.addChild(omFactory.createOMText(omText.getText()));
+            } else if (omNode instanceof OMElement) {
+                newOMElement.addChild(getClonedOMElement((OMElement) omNode, omFactory));
+            } else {
+                throw new AxisFault("Unknown child element type ==> " + omNode.getClass().getName());
+            }
+        }
+        return newOMElement;
     }
 
 }

Added: webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/MultirefHelperTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/MultirefHelperTest.java?rev=608128&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/MultirefHelperTest.java
(added)
+++ webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/MultirefHelperTest.java
Wed Jan  2 07:35:18 2008
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.axis2.databinding.utils;
+
+import junit.framework.TestCase;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axis2.AxisFault;
+
+import java.io.ByteArrayInputStream;
+import java.util.Iterator;
+
+
+public class MultirefHelperTest extends TestCase {
+     public void testProcessHrefAttributes1(){
+        String bodyElement = "<soap:body xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+
+                "    <operation>\n" +
+                "        <arg1 href=\"#obj1\"/>\n" +
+                "    </operation>\n" +
+                "    <multiref id=\"obj1\">\n" +
+                "        <name>the real argument</name>\n" +
+                "        <color>blue</color>\n" +
+                "    </multiref>\n" +
+                "</soap:body>";
+        testProcessHrefAttributes(bodyElement);
+    }
+
+    public void testProcessHrefAttribute2(){
+        String bodyElement = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+
+                "    <ns1:operation xmlns:ns1=\"http:temp1.org\">\n" +
+                "        <ns1:arg1 href=\"#obj1\"/>\n" +
+                "    </ns1:operation>\n" +
+                "    <ns2:multiref id=\"obj1\"  xmlns:ns2=\"http:temp1.org\">\n" +
+                "        <ns2:name>the real argument</ns2:name>\n" +
+                "        <ns2:color>blue</ns2:color>\n" +
+                "    </ns2:multiref>\n" +
+                "</soapenv:Body>";
+        testProcessHrefAttributes(bodyElement);
+    }
+
+    public void testProcessHrefAttribute3(){
+        String bodyElement = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+
+                "        <ns1:operation xmlns:ns1=\"http:temp1.org\">\n" +
+                "            <ns1:arg1 href=\"#obj1\"/>\n" +
+                "            <ns1:operation2>\n" +
+                "                <ns1:arg1 href=\"#obj1\"/>\n" +
+                "            </ns1:operation2>\n" +
+                "        </ns1:operation>\n" +
+                "        <ns2:multiref xmlns:ns2=\"http:temp1.org\" id=\"obj1\">\n"
+
+                "            <ns2:name>the real argument</ns2:name>\n" +
+                "            <ns2:color>blue</ns2:color>\n" +
+                "        </ns2:multiref>\n" +
+                "    </soapenv:Body>";
+        testProcessHrefAttributes(bodyElement);
+    }
+
+    public void testProcessHrefAttribute4(){
+        String bodyElement = "<soapenv:Body xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
+
+                "    <ns1:operation xmlns:ns1=\"http:temp1.org\">\n" +
+                "        <ns1:arg1 href=\"#obj1\"/>\n" +
+                "        <ns1:operation2>\n" +
+                "            <ns1:arg1 href=\"#obj1\"/>\n" +
+                "            <ns1:test href=\"#obj2\"/>\n" +
+                "        </ns1:operation2>\n" +
+                "    </ns1:operation>\n" +
+                "    <ns2:multiref xmlns:ns2=\"http:temp1.org\" id=\"obj1\">\n" +
+                "        <ns2:name>the real argument</ns2:name>\n" +
+                "        <ns2:color>blue</ns2:color>\n" +
+                "        <ns2:person href=\"#obj2\"/>\n" +
+                "    </ns2:multiref>\n" +
+                "    <multiref id=\"obj2\">\n" +
+                "        <age>23</age>\n" +
+                "        <name>amila</name>\n" +
+                "    </multiref>\n" +
+                "</soapenv:Body>";
+        testProcessHrefAttributes(bodyElement);
+    }
+
+    private void testProcessHrefAttributes(String bodyElement){
+
+        try {
+            XMLStreamReader xmlReader =
+                    StAXUtils.createXMLStreamReader(new ByteArrayInputStream(bodyElement.getBytes()));
+            StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(xmlReader);
+            OMElement generatedElement = stAXOMBuilder.getDocumentElement();
+            SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
+            SOAPEnvelope soapEnvelope =  soapFactory.getDefaultEnvelope();
+            OMElement omElement = null;
+            for (Iterator iter = generatedElement.getChildElements();iter.hasNext();){
+                omElement = (OMElement) iter.next();
+                soapEnvelope.getBody().addChild(omElement);
+            }
+
+            String omElementString;
+            omElementString = soapEnvelope.toString();
+            System.out.println("OM Element before processing ==> " + omElementString);
+            MultirefHelper.processHrefAttributes(soapEnvelope);
+            omElementString = soapEnvelope.toString();
+            System.out.println("OM Element after processing ==> " + omElementString);
+
+
+        } catch (XMLStreamException e) {
+            fail();
+        } catch (AxisFault axisFault) {
+            fail();
+        }
+
+    }
+}



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


Mime
View raw message