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/03/22 10:25:14 UTC
svn commit: r521162 - in /activemq/camel/trunk/camel-core/src:
main/java/org/apache/camel/ main/java/org/apache/camel/builder/
main/java/org/apache/camel/builder/xpath/
main/java/org/apache/camel/processor/ main/java/org/apache/camel/util/
test/java/or...
Author: jstrachan
Date: Thu Mar 22 02:25:12 2007
New Revision: 521162
URL: http://svn.apache.org/viewvc?view=rev&rev=521162
Log:
added XPath predicate support; also refactored the Predicate interface so a single expression class can implement predicate and expression
Added:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java (with props)
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java (with props)
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java (with props)
Modified:
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/ExchangeXPathExpression.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/FilterProcessor.java
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xpath/XPathTest.java
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Predicate.java Thu Mar 22 02:25:12 2007
@@ -24,10 +24,11 @@
public interface Predicate<E> {
/**
- * Evaluates the predicate on the message exchange
+ * Evaluates the predicate on the message exchange and returns true
+ * if this exchange matches the predicate
*
* @param exchange the message exchange
* @return true if the predicate matches
*/
- boolean evaluate(E exchange);
+ boolean matches(E exchange);
}
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java?view=auto&rev=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java Thu Mar 22 02:25:12 2007
@@ -0,0 +1,31 @@
+/**
+ *
+ * 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;
+
+/**
+ * A provider of newly constructed objects
+ *
+ * @version $Revision$
+ */
+public interface Provider<T> {
+
+ /**
+ * Returns the newly constructed instance
+ */
+ T get();
+}
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/Provider.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java Thu Mar 22 02:25:12 2007
@@ -27,6 +27,8 @@
* @version $Revision: $
*/
public class ExpressionBuilder {
+
+
/**
* Returns an expression for the header value with the given name
*
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java?view=auto&rev=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java Thu Mar 22 02:25:12 2007
@@ -0,0 +1,36 @@
+/**
+ *
+ * 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;
+
+import org.apache.camel.Expression;
+import org.apache.camel.Exchange;
+
+/**
+ * A Factory of {@link Expression} objects typically implemented by a builder such as @{XPathBuilder}
+ *
+ * @version $Revision$
+ */
+public interface ExpressionFactory<E extends Exchange> {
+
+ /**
+ * Creates an expression object
+ *
+ * @return the newly created expression object
+ */
+ public Expression<E> createExpression();
+}
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/ExpressionFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java Thu Mar 22 02:25:12 2007
@@ -35,8 +35,8 @@
notNull(left, "left");
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
- return left.evaluate(exchange) && right.evaluate(exchange);
+ public boolean matches(E exchange) {
+ return left.matches(exchange) && right.matches(exchange);
}
@Override
@@ -53,8 +53,8 @@
notNull(left, "left");
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
- return left.evaluate(exchange) || right.evaluate(exchange);
+ public boolean matches(E exchange) {
+ return left.matches(exchange) || right.matches(exchange);
}
@Override
@@ -69,7 +69,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.equals(value1, value2);
@@ -87,7 +87,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return !ObjectHelper.equals(value1, value2);
@@ -105,7 +105,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.compare(value1, value2) < 0;
@@ -123,7 +123,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.compare(value1, value2) <= 0;
@@ -141,7 +141,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.compare(value1, value2) > 0;
@@ -159,7 +159,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.compare(value1, value2) >= 0;
@@ -177,7 +177,7 @@
notNull(type, "type");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value = expression.evaluate(exchange);
return type.isInstance(value);
}
@@ -193,7 +193,7 @@
notNull(expression, "expression");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value = expression.evaluate(exchange);
return value == null;
}
@@ -209,7 +209,7 @@
notNull(expression, "expression");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value = expression.evaluate(exchange);
return value != null;
}
@@ -226,7 +226,7 @@
notNull(right, "right");
return new Predicate<E>() {
- public boolean evaluate(E exchange) {
+ public boolean matches(E exchange) {
Object value1 = left.evaluate(exchange);
Object value2 = right.evaluate(exchange);
return ObjectHelper.contains(value1, value2);
Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java?view=auto&rev=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java Thu Mar 22 02:25:12 2007
@@ -0,0 +1,37 @@
+/**
+ *
+ * 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;
+
+import org.apache.camel.Expression;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+
+/**
+ * A Factory of {@link Predicate} objects typically implemented by a builder such as @{XPathBuilder}
+ *
+ * @version $Revision$
+ */
+public interface PredicateFactory<E extends Exchange> {
+
+ /**
+ * Creates a predicate object
+ *
+ * @return the newly created expression object
+ */
+ public Predicate<E> createPredicate();
+}
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/PredicateFactory.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/ExchangeXPathExpression.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/ExchangeXPathExpression.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/ExchangeXPathExpression.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/ExchangeXPathExpression.java Thu Mar 22 02:25:12 2007
@@ -20,11 +20,14 @@
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Message;
+import org.apache.camel.Predicate;
+import org.apache.camel.util.ObjectHelper;
import org.xml.sax.InputSource;
import javax.xml.namespace.QName;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathConstants;
import java.io.StringReader;
/**
@@ -32,7 +35,7 @@
*
* @version $Revision$
*/
-public class ExchangeXPathExpression<E extends Exchange> implements Expression<E> {
+public class ExchangeXPathExpression<E extends Exchange> implements Expression<E>, Predicate<E> {
private final XPathExpression expression;
private final MessageVariableResolver variableResolver;
private Class documentType;
@@ -45,6 +48,10 @@
this.documentType = builder.getDocumentType();
this.text = builder.getText();
this.resultType = builder.getResultType();
+ }
+
+ public boolean matches(E exchange) {
+ return ObjectHelper.toBoolean(evaluateAs(exchange, XPathConstants.BOOLEAN));
}
public Object evaluate(E exchange) {
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/xpath/XPathBuilder.java Thu Mar 22 02:25:12 2007
@@ -20,6 +20,10 @@
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.RuntimeExpressionException;
+import org.apache.camel.Provider;
+import org.apache.camel.Predicate;
+import org.apache.camel.builder.PredicateFactory;
+import org.apache.camel.builder.ExpressionFactory;
import org.w3c.dom.Document;
import javax.xml.namespace.QName;
@@ -37,7 +41,7 @@
*
* @version $Revision$
*/
-public class XPathBuilder<E extends Exchange> {
+public class XPathBuilder<E extends Exchange> implements ExpressionFactory<E>, PredicateFactory<E> {
private final String text;
private XPathFactory xpathFactory;
private Class documentType = Document.class;
@@ -55,16 +59,14 @@
}
public Expression<E> createExpression() {
- try {
- MessageVariableResolver variableResolver = new MessageVariableResolver();
- XPathExpression expression = createXPathExpression(variableResolver);
- return new ExchangeXPathExpression<E>(this, expression, variableResolver);
- }
- catch (XPathException e) {
- throw new InvalidXPathExpression(text, e);
- }
+ return createExchangeXPathExpression();
}
+ public Predicate<E> createPredicate() {
+ return createExchangeXPathExpression();
+ }
+
+
// Builder methods
//-------------------------------------------------------------------------
@@ -208,6 +210,17 @@
// Implementation methods
//-------------------------------------------------------------------------
+ protected ExchangeXPathExpression<E> createExchangeXPathExpression() {
+ try {
+ MessageVariableResolver variableResolver = new MessageVariableResolver();
+ XPathExpression expression = createXPathExpression(variableResolver);
+ return new ExchangeXPathExpression<E>(this, expression, variableResolver);
+ }
+ catch (XPathException e) {
+ throw new InvalidXPathExpression(text, e);
+ }
+ }
+
protected XPathExpression createXPathExpression(MessageVariableResolver variableResolver) throws XPathExpressionException, XPathFactoryConfigurationException {
XPath xPath = getXPathFactory().newXPath();
xPath.setNamespaceContext(getNamespaceContext());
@@ -217,4 +230,5 @@
}
return xPath.compile(text);
}
+
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/FilterProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/FilterProcessor.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/FilterProcessor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/FilterProcessor.java Thu Mar 22 02:25:12 2007
@@ -33,7 +33,7 @@
}
public void onExchange(E exchange) {
- if (predicate.evaluate(exchange)) {
+ if (predicate.matches(exchange)) {
processor.onExchange(exchange);
}
}
Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java Thu Mar 22 02:25:12 2007
@@ -123,4 +123,17 @@
return false;
}
}
+
+ /**
+ * Converts the given value to a boolean, handling strings or Boolean objects; otherwise returning true if non-null
+ */
+ public static boolean toBoolean(Object value) {
+ if (value instanceof Boolean) {
+ return (Boolean) value;
+ }
+ if (value instanceof String) {
+ return "true".equalsIgnoreCase(value.toString());
+ }
+ return value != null;
+ }
}
Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xpath/XPathTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xpath/XPathTest.java?view=diff&rev=521162&r1=521161&r2=521162
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xpath/XPathTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/builder/xpath/XPathTest.java Thu Mar 22 02:25:12 2007
@@ -22,6 +22,7 @@
import org.apache.camel.Exchange;
import org.apache.camel.Expression;
import org.apache.camel.Message;
+import org.apache.camel.Predicate;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.commons.logging.Log;
@@ -35,13 +36,20 @@
public class XPathTest extends TestCase {
private static final transient Log log = LogFactory.getLog(XPathTest.class);
- public void testXPath() throws Exception {
+ public void testXPathExpressions() throws Exception {
assertExpression("/foo/bar/@xyz", "cheese", "<foo><bar xyz='cheese'/></foo>");
assertExpression("$name", "James", "<foo><bar xyz='cheese'/></foo>");
assertExpression("foo/bar", "cheese", "<foo><bar>cheese</bar></foo>");
assertExpression("foo/bar/text()", "cheese", "<foo><bar>cheese</bar></foo>");
}
+ public void testXPathPredicates() throws Exception {
+ assertPredicate("/foo/bar/@xyz", true, "<foo><bar xyz='cheese'/></foo>");
+ assertPredicate("$name = 'James'", true, "<foo><bar xyz='cheese'/></foo>");
+ assertPredicate("$name = 'Hiram'", false, "<foo><bar xyz='cheese'/></foo>");
+ assertPredicate("/foo/notExist", false, "<foo><bar xyz='cheese'/></foo>");
+ }
+
public void testUsingJavaExtensions() throws Exception {
Object instance = null;
@@ -79,14 +87,33 @@
}
protected Object assertExpression(Expression expression, String expected, String xml) {
+ Exchange exchange = createExchange(xml);
+ Object value = expression.evaluate(exchange);
+ assertEquals("Expression: " + expression, expected, value);
+ return value;
+ }
+
+ protected void assertPredicate(String xpath, boolean expected, String xml) {
+ Predicate predicate = XPathBuilder.xpath(xpath).createPredicate();
+ boolean value = assertPredicate(predicate, expected, xml);
+
+ log.debug("Evaluated xpath: " + xpath + " on XML: " + xml + " result: " + value);
+
+ }
+
+ protected boolean assertPredicate(Predicate predicate, boolean expected, String xml) {
+ Exchange exchange = createExchange(xml);
+ boolean value = predicate.matches(exchange);
+ assertEquals("Predicate: " + predicate, expected, value);
+ return value;
+ }
+
+ protected Exchange createExchange(String xml) {
CamelContext context = new DefaultCamelContext();
Exchange exchange = new DefaultExchange(context);
Message message = exchange.getIn();
message.getHeaders().setHeader("name", "James");
message.setBody(xml);
-
- Object value = expression.evaluate(exchange);
- assertEquals("Expression: " + expression, expected, value);
- return value;
+ return exchange;
}
}