You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by js...@apache.org on 2007/05/02 17:32:21 UTC

svn commit: r534515 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/xml/ test/java/org/apache/camel/processor/

Author: jstrachan
Date: Wed May  2 08:32:20 2007
New Revision: 534515

URL: http://svn.apache.org/viewvc?view=rev&rev=534515
Log:
added an XPath namespaces builder to make it easier to create namespaces and reuse them in multiple xpaths

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java   (with props)
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java   (with props)

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java?view=auto&rev=534515
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java Wed May  2 08:32:20 2007
@@ -0,0 +1,59 @@
+/**
+ *
+ * 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.builder.xml;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Set;
+
+/**
+ * A helper class for creating namespaces which can then be used to create XPath expressions
+ *
+ * @version $Revision: 1.1 $
+ */
+public class NamespaceBuilder {
+    private Map<String,String> namespaces = new HashMap<String, String>();
+
+    public static NamespaceBuilder namespaceContext() {
+        return new NamespaceBuilder();
+    }
+
+    public static NamespaceBuilder namespaceContext(String prefix, String uri) {
+        return new NamespaceBuilder().namespace(prefix, uri);
+    }
+
+    public NamespaceBuilder namespace(String prefix, String uri) {
+        namespaces.put(prefix, uri);
+        return this;
+    }
+
+    /**
+     * Creates a new XPath expression using the current namespaces
+     *
+     * @param xpath the XPath expression
+     * @return a new XPath expression
+     */
+    public XPathBuilder xpath(String xpath) {
+        XPathBuilder answer = XPathBuilder.xpath(xpath);
+        Set<Map.Entry<String,String>> entries = namespaces.entrySet();
+        for (Map.Entry<String, String> entry : entries) {
+          answer.namespace(entry.getKey(), entry.getValue());
+        }
+        return answer;
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xml/NamespaceBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java?view=auto&rev=534515
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java Wed May  2 08:32:20 2007
@@ -0,0 +1,73 @@
+/**
+ *
+ * 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.processor;
+
+import static org.apache.camel.builder.xml.XPathBuilder.xpath;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class XPathFilterTest extends ContextTestSupport {
+    protected Endpoint<Exchange> startEndpoint;
+    protected MockEndpoint resultEndpoint;
+
+    public void testSendMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(1);
+
+        client.sendBody("direct:start", "<person name='James' city='London'/>");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public void testSendNotMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(0);
+
+        client.sendBody("direct:start", "<person name='Hiram' city='Tampa'/>");
+
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        startEndpoint = resolveMandatoryEndpoint("direct:start");
+        resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                from("direct:start").filter(
+                        xpath("/person[@name='James']")
+                ).to("mock:result");
+                // END SNIPPET: example
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java?view=auto&rev=534515
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java Wed May  2 08:32:20 2007
@@ -0,0 +1,78 @@
+/**
+ *
+ * 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.processor;
+
+import static org.apache.camel.builder.xml.NamespaceBuilder.namespaceContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.builder.xml.NamespaceBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class XPathWithNamespaceBuilderFilterTest extends ContextTestSupport {
+    protected Endpoint<Exchange> startEndpoint;
+    protected MockEndpoint resultEndpoint;
+
+    public void testSendMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(1);
+
+        client.sendBody("direct:start", "<person xmlns='http://acme.com/cheese' name='James' city='London'/>");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public void testSendNotMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(0);
+
+        client.sendBody("direct:start", "<person xmlns='http://acme.com/cheese'  name='Hiram' city='Tampa'/>");
+
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        startEndpoint = resolveMandatoryEndpoint("direct:start");
+        resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                // lets define the namespaces we'll need in our filters
+                NamespaceBuilder ns = namespaceContext("c", "http://acme.com/cheese")
+                            .namespace("xsd", "http://www.w3.org/2001/XMLSchema");
+
+                from("direct:start").filter(
+                        ns.xpath("/c:person[@name='James']")
+                ).to("mock:result");
+                // END SNIPPET: example
+            }
+        };
+    }
+
+
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespaceBuilderFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java?view=auto&rev=534515
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java Wed May  2 08:32:20 2007
@@ -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.processor;
+
+import static org.apache.camel.builder.xml.XPathBuilder.xpath;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Endpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.builder.xml.XPathBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class XPathWithNamespacesFilterTest extends ContextTestSupport {
+    protected Endpoint<Exchange> startEndpoint;
+    protected MockEndpoint resultEndpoint;
+
+    public void testSendMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(1);
+
+        client.sendBody("direct:start", "<person xmlns='http://acme.com/cheese' name='James' city='London'/>");
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    public void testSendNotMatchingMessage() throws Exception {
+        resultEndpoint.expectedMessageCount(0);
+
+        client.sendBody("direct:start", "<person xmlns='http://acme.com/cheese'  name='Hiram' city='Tampa'/>");
+
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        startEndpoint = resolveMandatoryEndpoint("direct:start");
+        resultEndpoint = (MockEndpoint) resolveMandatoryEndpoint("mock:result");
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                from("direct:start").filter(
+                        xpath("/c:person[@name='James']").namespace("c", "http://acme.com/cheese")
+                ).to("mock:result");
+                // END SNIPPET: example
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/XPathWithNamespacesFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native