You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/04/23 13:43:58 UTC
svn commit: r937260 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/converter/jaxp/DomConverter.java
test/java/org/apache/camel/component/file/XPathToFileTest.java
test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
Author: davsclaus
Date: Fri Apr 23 11:43:58 2010
New Revision: 937260
URL: http://svn.apache.org/viewvc?rev=937260&view=rev
Log:
CAMEL-2669: Added more XPath based (NodeList) type converter so you can easily route from xpath to file or whatever.
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java (with props)
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java?rev=937260&r1=937259&r2=937260&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/converter/jaxp/DomConverter.java Fri Apr 23 11:43:58 2010
@@ -16,6 +16,14 @@
*/
package org.apache.camel.converter.jaxp;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.camel.converter.ObjectConverter;
+import org.apache.camel.util.ObjectHelper;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -58,6 +66,27 @@ public final class DomConverter {
return Long.valueOf(s);
}
+ @Converter
+ public static List toList(NodeList nodeList) {
+ List answer = new ArrayList();
+ Iterator it = ObjectHelper.createIterator(nodeList);
+ while (it.hasNext()) {
+ answer.add(it.next());
+ }
+ return answer;
+ }
+
+ @Converter
+ public static InputStream toInputStream(NodeList nodeList) {
+ return new ByteArrayInputStream(toByteArray(nodeList));
+ }
+
+ @Converter
+ public static byte[] toByteArray(NodeList nodeList) {
+ String data = toString(nodeList);
+ return data.getBytes();
+ }
+
private static void append(StringBuilder buffer, NodeList nodeList) {
int size = nodeList.getLength();
for (int i = 0; i < size; i++) {
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java?rev=937260&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java Fri Apr 23 11:43:58 2010
@@ -0,0 +1,71 @@
+/**
+ * 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.file;
+
+import java.io.File;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.w3c.dom.Document;
+
+import static org.apache.camel.builder.xml.XPathBuilder.xpath;
+
+/**
+ * @version $Revision$
+ */
+public class XPathToFileTest extends ContextTestSupport {
+
+ @Override
+ protected void setUp() throws Exception {
+ deleteDirectory("target/xpath");
+ super.setUp();
+ }
+
+ public void testXPathToFile() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(2);
+
+ String xml = "<foo><person>Claus</person><person>Jonathan</person></foo>";
+ Document doc = context.getTypeConverter().convertTo(Document.class, xml);
+
+ template.sendBody("direct:start", doc);
+
+ assertMockEndpointsSatisfied();
+
+ File first = new File("target/xpath/xpath-0.xml").getAbsoluteFile();
+ assertTrue("File xpath-0.xml should exists", first.exists());
+ assertEquals("Claus", context.getTypeConverter().convertTo(String.class, first));
+
+ File second = new File("target/xpath/xpath-1.xml").getAbsoluteFile();
+ assertTrue("File xpath-1.xml should exists", second.exists());
+ assertEquals("Jonathan", context.getTypeConverter().convertTo(String.class, second));
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .split(xpath("/foo/person"))
+ .to("file://target/xpath?fileName=xpath-${property.CamelSplitIndex}.xml")
+ .to("mock:result");
+ }
+ };
+ }
+}
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/XPathToFileTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java?rev=937260&r1=937259&r2=937260&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/jaxp/DomConverterTest.java Fri Apr 23 11:43:58 2010
@@ -16,20 +16,68 @@
*/
package org.apache.camel.converter.jaxp;
+import java.io.InputStream;
+import java.util.List;
+
import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
import org.apache.camel.ContextTestSupport;
+import org.apache.camel.util.ObjectHelper;
/**
* @version $Revision$
*/
public class DomConverterTest extends ContextTestSupport {
- public void testDomConverter() throws Exception {
+ public void testDomConverterToString() throws Exception {
Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
String s = DomConverter.toString(document.getChildNodes());
assertEquals("world!", s);
}
+ public void testDomConverterToBytes() throws Exception {
+ Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
+
+ byte[] bytes = DomConverter.toByteArray(document.getChildNodes());
+ assertTrue("Should be equal", ObjectHelper.equalByteArray("world!".getBytes(), bytes));
+ }
+
+ public void testDomConverterToInteger() throws Exception {
+ Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>47</hello>");
+
+ Integer number = DomConverter.toInteger(document.getChildNodes());
+ assertEquals(47, number.intValue());
+ }
+
+ public void testDomConverterToLong() throws Exception {
+ Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>47</hello>");
+
+ Long number = DomConverter.toLong(document.getChildNodes());
+ assertEquals(47L, number.longValue());
+ }
+
+ public void testDomConverterToList() throws Exception {
+ Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ + "<foo><hello>Hello World</hello><bye>Bye Camel</bye></foo>");
+
+ List list = DomConverter.toList(document.getElementsByTagName("foo"));
+ assertEquals(1, list.size());
+
+ NodeList nl = assertIsInstanceOf(NodeList.class, list.get(0));
+ List sub = DomConverter.toList(nl);
+ assertEquals(2, sub.size());
+
+ assertEquals("Hello World", DomConverter.toString((NodeList) sub.get(0)));
+ assertEquals("Bye Camel", DomConverter.toString((NodeList) sub.get(1)));
+ }
+
+ public void testDomConverterToInputStream() throws Exception {
+ Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>world!</hello>");
+
+ InputStream is = DomConverter.toInputStream(document.getChildNodes());
+ assertEquals("world!", context.getTypeConverter().convertTo(String.class, is));
+ }
+
}