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:33:36 UTC

svn commit: r783368 - in /camel/branches/camel-1.x: ./ components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java

Author: ningjiang
Date: Wed Jun 10 14:33:36 2009
New Revision: 783368

URL: http://svn.apache.org/viewvc?rev=783368&view=rev
Log:
Merged revisions 783363 via svnmerge from 
https://svn.apache.org/repos/asf/camel/trunk

........
  r783363 | ningjiang | 2009-06-10 22:05:16 +0800 (Wed, 10 Jun 2009) | 1 line
  
  MR-161 added a util method to write the namespaces at the first element
........

Added:
    camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java   (with props)
    camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java
      - copied, changed from r783363, camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java
Modified:
    camel/branches/camel-1.x/   (props changed)

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jun 10 14:33:36 2009
@@ -1 +1 @@
-/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193,773446,773781,774192,774383,774658-774659,776198,776289,776504,776975,778102,778689-778701,779121,779143,779489,781314-781349,781775,781
 923,781974,781993,782557,782594,782681,782886,782918-782923,783204
+/camel/trunk:736980,739733,739904,740251,740295,740306,740596,740663,741848,742231,742705,742739,742854,742856,742898,742906,743613,743762,743773,743920,743959-743960,744123,745105,745367,745541,745751,745826,745978,746269,746872,746895,746962,747258,747678-747704,748392,748436,748821,749563-749564,749574,749628-749629,749936,749956,750017,750334,750396,750761,750796,752068,752117,752418,752751-752755,752764-752773,752956,753087,753101,753175,755136,755487,756313,756348,756870,756939,757636,757693,757743,757865,758539,758563,758600,758617,758692,758990,759362,759453,759887,759931,760003,760890,760909,760937,761194,761536,761583,761607,762047,762633,762650,762935,763095,763484,763551,765154,765686,765729,765743,765824,766016,766289,766584,766588,766590,766602,766673,767403,767824,768342,769239,769346,769368,769434,770172,770906,771303,773193,773446,773781,774192,774383,774658-774659,776198,776289,776504,776975,778102,778689-778701,779121,779143,779489,781314-781349,781775,781
 923,781974,781993,782557,782594,782681,782886,782918-782923,783204,783363

Propchange: camel/branches/camel-1.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Added: camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java?rev=783368&view=auto
==============================================================================
--- camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java (added)
+++ camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java Wed Jun 10 14:33:36 2009
@@ -0,0 +1,197 @@
+/**
+ * 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.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 {
+    
+    private CxfUtils() {
+        // helper class
+    }
+    
+    public static String getStringFromInputStream(InputStream in) throws Exception {
+        CachedOutputStream bos = new CachedOutputStream();
+        IOUtils.copy(in, bos);
+        in.close();
+        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);
+                }
+            }
+        }
+
+    }
+
+}

Propchange: camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/branches/camel-1.x/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/util/CxfUtils.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Copied: camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java (from r783363, camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java)
URL: http://svn.apache.org/viewvc/camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java?p2=camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java&p1=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java&r1=783363&r2=783368&rev=783368&view=diff
==============================================================================
--- camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java (original)
+++ camel/branches/camel-1.x/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/util/SplitterWithXqureyTest.java Wed Jun 10 14:33:36 2009
@@ -68,7 +68,7 @@
             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");
+                from("direct:endpoint").splitter().xpath("//one:other", namespaces).to("mock:result");
             }
         };
     }