You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/06/10 16:05:16 UTC

svn commit: r783363 - in /camel/trunk/components/camel-cxf/src: main/java/org/apache/camel/component/cxf/util/CxfUtils.java test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java

Author: ningjiang
Date: Wed Jun 10 14:05:16 2009
New Revision: 783363

URL: http://svn.apache.org/viewvc?rev=783363&view=rev
Log:
MR-161 added a util method to write the namespaces at the first element

Added:
    camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java   (with props)
Modified:
    camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java

Modified: camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java?rev=783363&r1=783362&r2=783363&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java (original)
+++ camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java Wed Jun 10 14:05:16 2009
@@ -17,10 +17,31 @@
 
 package org.apache.camel.component.cxf.util;
 
+
+
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
+import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.io.CachedOutputStream;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.staxutils.W3CDOMStreamWriter;
 
 
 public final class CxfUtils {
@@ -36,5 +57,141 @@
         bos.close();
         return bos.getOut().toString();
     }
+    
+    public static String elementToString(Element element) throws Exception {
+        Map<String, String> namespaces = new HashMap<String, String>();
+        visitNodesForNameSpace(element, namespaces);
+        W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
+        writeElement(element, writer, namespaces);
+        return getStringFromDoc(writer.getDocument());
+        
+    }
+    
+    private static void writeElement(Element e,
+                                    XMLStreamWriter writer,                                    
+                                    Map<String, String> namespaces)
+        throws XMLStreamException {
+        String prefix = e.getPrefix();
+        String ns = e.getNamespaceURI();
+        String localName = e.getLocalName();
+
+        if (prefix == null) {
+            prefix = "";
+        }
+        if (localName == null) {
+            localName = e.getNodeName();
+
+            if (localName == null) {
+                throw new IllegalStateException("Element's local name cannot be null!");
+            }
+        }
+
+        String decUri = writer.getNamespaceContext().getNamespaceURI(prefix);
+        boolean declareNamespace = decUri == null || !decUri.equals(ns);
+
+        if (ns == null || ns.length() == 0) {
+            writer.writeStartElement(localName);
+            if (StringUtils.isEmpty(decUri)) {
+                declareNamespace = false;
+            }
+        } else {
+            writer.writeStartElement(prefix, localName, ns);
+        }
+
+        NamedNodeMap attrs = e.getAttributes();
+        for (int i = 0; i < attrs.getLength(); i++) {
+            Node attr = attrs.item(i);
+
+            String name = attr.getLocalName();
+            String attrPrefix = attr.getPrefix();
+            if (attrPrefix == null) {
+                attrPrefix = "";
+            }
+            if (name == null) {
+                name = attr.getNodeName();
+            }
+     
+            if ("xmlns".equals(attrPrefix)) {
+                writer.writeNamespace(name, attr.getNodeValue());
+                if (name.equals(prefix) && attr.getNodeValue().equals(ns)) {
+                    declareNamespace = false;
+                }
+            } else {
+                if ("xmlns".equals(name) && "".equals(attrPrefix)) {
+                    writer.writeNamespace("", attr.getNodeValue());
+                    if (attr.getNodeValue().equals(ns)) {
+                        declareNamespace = false;
+                    } else if (StringUtils.isEmpty(attr.getNodeValue())
+                        && StringUtils.isEmpty(ns)) {
+                        declareNamespace = false;
+                    }
+                } else {
+                    String attns = attr.getNamespaceURI();
+                    String value = attr.getNodeValue();
+                    if (attns == null || attns.length() == 0) {
+                        writer.writeAttribute(name, value);
+                    } else if (attrPrefix == null || attrPrefix.length() == 0) {
+                        writer.writeAttribute(attns, name, value);
+                    } else {
+                        writer.writeAttribute(attrPrefix, attns, name, value);
+                    }                    
+                }
+            }
+        }
+
+        if (declareNamespace) {
+            if (ns == null) {
+                writer.writeNamespace(prefix, "");
+            } else {
+                writer.writeNamespace(prefix, ns);
+            }
+        }
+        
+        if (namespaces != null && namespaces.size() > 0) {
+            for (String key : namespaces.keySet()) {
+                String namespaceURI = namespaces.get(key);
+                writer.writeNamespace(key, namespaceURI);
+            }
+        }
+
+        Node nd = e.getFirstChild();
+        while (nd != null) {
+            StaxUtils.writeNode(nd, writer, false);
+            nd = nd.getNextSibling();
+        }       
+
+        writer.writeEndElement();
+        
+    }
+
+    private static String getStringFromDoc(Document document) throws IOException {
+        //Serialize DOM
+        OutputFormat format    = new OutputFormat(document);
+        format.setOmitXMLDeclaration(true);
+        // as a String
+        StringWriter stringOut = new StringWriter();    
+        XMLSerializer serial   = new XMLSerializer(stringOut, 
+                                                    format);
+        serial.serialize(document);
+       
+        return stringOut.toString(); 
+       
+    }
+
+    private static void visitNodesForNameSpace(Node node, Map<String, String> namespaces) {
+        if (node instanceof Element) {
+            Element element = (Element)node;
+            if (element.getPrefix() != null && element.getNamespaceURI() != null) {
+                namespaces.put(element.getPrefix(), element.getNamespaceURI());
+            }
+            if (node.getChildNodes() != null) {
+                NodeList nodelist = node.getChildNodes();
+                for (int i = 0; i < nodelist.getLength(); i++) {
+                    visitNodesForNameSpace(nodelist.item(i), namespaces);
+                }
+            }
+        }
+
+    }
 
 }

Added: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java?rev=783363&view=auto
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java (added)
+++ camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java Wed Jun 10 14:05:16 2009
@@ -0,0 +1,95 @@
+/**
+ * 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.camel.component.cxf.util;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+import com.sun.org.apache.xerces.internal.dom.ElementImpl;
+import com.sun.org.apache.xml.internal.serialize.OutputFormat;
+import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.Namespaces;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.staxutils.StaxUtils;
+import org.apache.cxf.staxutils.W3CDOMStreamWriter;
+
+
+public class SplitterWithXqureyTest extends ContextTestSupport {
+    private static String xmlData = "<workflow id=\"12345\" xmlns=\"http://camel.apache.org/schema/one\" "
+        + "xmlns:two=\"http://camel.apache.org/schema/two\">"
+        + "<person><name>Willem</name></person> "
+        + "<other><two:test name=\"123\">One</two:test></other>"
+        + "<other><two:test name=\"456\">Two</two:test></other>"
+        + "<other><test>Three</test></other>"
+        + "<other><test>Foure</test></other></workflow>";
+    private static String[] verifyStrings = new String[] {
+        "<other xmlns=\"http://camel.apache.org/schema/one\" xmlns:two=\"http://camel.apache.org/schema/two\"><two:test name=\"123\">One</two:test></other>",
+        "<other xmlns=\"http://camel.apache.org/schema/one\" xmlns:two=\"http://camel.apache.org/schema/two\"><two:test name=\"456\">Two</two:test></other>",
+        "<other xmlns=\"http://camel.apache.org/schema/one\"><test>Three</test></other>",
+        "<other xmlns=\"http://camel.apache.org/schema/one\"><test>Foure</test></other>"
+    };
+        
+    
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // split the message with namespaces defined 
+                Namespaces namespaces = new Namespaces("one", "http://camel.apache.org/schema/one");                
+                from("direct:endpoint").split().xpath("//one:other", namespaces).to("mock:result");
+            }
+        };
+    }
+    
+    public void testSenderXmlData() throws Exception {        
+        MockEndpoint result = getMockEndpoint("mock:result");
+        result.reset();
+        result.expectedMessageCount(4);
+        template.sendBody("direct:endpoint", xmlData);
+        assertMockEndpointsSatisfied();
+        int i = 0;
+        for (Exchange exchange : result.getExchanges()) {
+            ElementImpl element = (ElementImpl) exchange.getIn().getBody();           
+            String message = CxfUtils.elementToString(element);            
+            log.info("The splited message is " + message);
+            assertTrue("The splitted message should start with <other", message.indexOf("<other") == 0);
+            assertEquals("Get a wrong message", verifyStrings[i], message);
+            i++;
+        }
+    }
+    
+   
+
+}

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date