This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch mock in repository https://gitbox.apache.org/repos/asf/camel.git
commit f4f0c7672467b7cbc0587a2917f0562c52050aad Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Apr 16 15:56:23 2019 +0200 Move mock component out of camel-core. Work in progress. --- .../apache/camel/builder/ExpressionBuilder.java | 1548 +------------------- .../org/apache/camel/builder/PredicateBuilder.java | 524 +------ .../camel/component/mock/MockValueBuilder.java | 26 +- .../support}/builder/BinaryPredicateSupport.java | 2 +- .../camel/support}/builder/ExpressionBuilder.java | 44 +- .../camel/support}/builder/PredicateBuilder.java | 11 +- 6 files changed, 12 insertions(+), 2143 deletions(-) diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java index ed3f8691..f8eb0cd 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java @@ -16,47 +16,13 @@ */ package org.apache.camel.builder; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Collection; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.function.BiFunction; -import java.util.function.Function; -import java.util.regex.Pattern; - -import org.apache.camel.CamelContext; -import org.apache.camel.CamelExecutionException; -import org.apache.camel.Exchange; import org.apache.camel.Expression; -import org.apache.camel.InvalidPayloadException; -import org.apache.camel.Message; -import org.apache.camel.NoSuchLanguageException; -import org.apache.camel.RuntimeCamelException; -import org.apache.camel.RuntimeExchangeException; -import org.apache.camel.spi.Language; -import org.apache.camel.spi.PropertiesComponent; -import org.apache.camel.spi.RouteContext; -import org.apache.camel.spi.UnitOfWork; -import org.apache.camel.support.ExchangeHelper; -import org.apache.camel.support.ExpressionAdapter; -import org.apache.camel.support.GroupIterator; -import org.apache.camel.support.GroupTokenIterator; -import org.apache.camel.support.LanguageSupport; -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.Scanner; import org.apache.camel.util.StringHelper; /** * A helper class for working with <a href="http://camel.apache.org/expression.html">expressions</a>. */ -public final class ExpressionBuilder { - - // TODO: Make this possible to have a base class in camel-support, and then extend it here, so we can have both - // and maybe deprecate this and refer to the builder in camel-support +public final class ExpressionBuilder extends org.apache.camel.support.builder.ExpressionBuilder { /** * Utility classes should not have a public constructor. @@ -65,1157 +31,6 @@ public final class ExpressionBuilder { } /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentObjectsExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachmentObjects(); - } - - @Override - public String toString() { - return "attachmentObjects"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentObjectValuesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachmentObjects().values(); - } - - @Override - public String toString() { - return "attachmentObjects"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentsExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachments(); - } - - @Override - public String toString() { - return "attachments"; - } - }; - } - - /** - * Returns an expression for the inbound message attachments - * - * @return an expression object which will return the inbound message attachments - */ - public static Expression attachmentValuesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getAttachments().values(); - } - - @Override - public String toString() { - return "attachments"; - } - }; - } - - /** - * Returns an expression for the header value with the given name - * <p/> - * Will fallback and look in properties if not found in headers. - * - * @param headerName the name of the header the expression will return - * @return an expression object which will return the header value - */ - public static Expression headerExpression(final String headerName) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String name = simpleExpression(headerName).evaluate(exchange, String.class); - Object header = exchange.getIn().getHeader(name); - if (header == null) { - // fall back on a property - header = exchange.getProperty(name); - } - return header; - } - - @Override - public String toString() { - return "header(" + headerName + ")"; - } - }; - } - - /** - * Returns an expression for the header value with the given name converted to the given type - * <p/> - * Will fallback and look in properties if not found in headers. - * - * @param headerName the name of the header the expression will return - * @param type the type to convert to - * @return an expression object which will return the header value - */ - public static <T> Expression headerExpression(final String headerName, final Class<T> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String name = simpleExpression(headerName).evaluate(exchange, String.class); - Object header = exchange.getIn().getHeader(name, type); - if (header == null) { - // fall back on a property - header = exchange.getProperty(name, type); - } - return header; - } - - @Override - public String toString() { - return "headerAs(" + headerName + ", " + type + ")"; - } - }; - } - - /** - * Returns an expression for the header value with the given name converted to the given type - * <p/> - * Will fallback and look in properties if not found in headers. - * - * @param headerName the name of the header the expression will return - * @param typeName the type to convert to as a FQN class name - * @return an expression object which will return the header value - */ - public static Expression headerExpression(final String headerName, final String typeName) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Class<?> type; - try { - String text = simpleExpression(typeName).evaluate(exchange, String.class); - type = exchange.getContext().getClassResolver().resolveMandatoryClass(text); - } catch (ClassNotFoundException e) { - throw CamelExecutionException.wrapCamelExecutionException(exchange, e); - } - - String text = simpleExpression(headerName).evaluate(exchange, String.class); - Object header = exchange.getIn().getHeader(text, type); - if (header == null) { - // fall back on a property - header = exchange.getProperty(text, type); - } - return header; - } - - @Override - public String toString() { - return "headerAs(" + headerName + ", " + typeName + ")"; - } - }; - } - - /** - * Returns an expression for the inbound message headers - * - * @return an expression object which will return the inbound headers - */ - public static Expression headersExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getHeaders(); - } - - @Override - public String toString() { - return "headers"; - } - }; - } - - /** - * Returns an expression for the out header value with the given name - * <p/> - * Will fallback and look in properties if not found in headers. - * - * @param headerName the name of the header the expression will return - * @return an expression object which will return the header value - */ - public static Expression outHeaderExpression(final String headerName) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (!exchange.hasOut()) { - return null; - } - - String text = simpleExpression(headerName).evaluate(exchange, String.class); - Message out = exchange.getOut(); - Object header = out.getHeader(text); - if (header == null) { - // let's try the exchange header - header = exchange.getProperty(text); - } - return header; - } - - @Override - public String toString() { - return "outHeader(" + headerName + ")"; - } - }; - } - - /** - * Returns an expression for the outbound message headers - * - * @return an expression object which will return the headers, will be <tt>null</tt> if the - * exchange is not out capable. - */ - public static Expression outHeadersExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - // only get out headers if the MEP is out capable - if (ExchangeHelper.isOutCapable(exchange)) { - return exchange.getOut().getHeaders(); - } else { - return null; - } - } - - @Override - public String toString() { - return "outHeaders"; - } - }; - } - - /** - * Returns an expression for the exchange pattern - * - * @see org.apache.camel.Exchange#getPattern() - * @return an expression object which will return the exchange pattern - */ - public static Expression exchangePatternExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getPattern(); - } - - @Override - public String toString() { - return "exchangePattern"; - } - }; - } - - /** - * Returns an expression for an exception set on the exchange - * - * @see Exchange#getException() - * @return an expression object which will return the exception set on the exchange - */ - public static Expression exchangeExceptionExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Exception exception = exchange.getException(); - if (exception == null) { - exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); - } - return exception; - } - - @Override - public String toString() { - return "exchangeException"; - } - }; - } - - /** - * Returns an expression for an exception set on the exchange - * <p/> - * Is used to get the caused exception that typically have been wrapped in some sort - * of Camel wrapper exception - * @param type the exception type - * @see Exchange#getException(Class) - * @return an expression object which will return the exception set on the exchange - */ - public static Expression exchangeExceptionExpression(final Class<Exception> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Exception exception = exchange.getException(type); - if (exception == null) { - exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); - return ObjectHelper.getException(type, exception); - } - return exception; - } - - @Override - public String toString() { - return "exchangeException[" + type + "]"; - } - }; - } - - /** - * Returns an expression for the type converter - * - * @return an expression object which will return the type converter - */ - public static Expression typeConverterExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getContext().getTypeConverter(); - } - - @Override - public String toString() { - return "typeConverter"; - } - }; - } - - /** - * Returns an expression for the {@link org.apache.camel.spi.Registry} - * - * @return an expression object which will return the registry - */ - public static Expression registryExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getContext().getRegistry(); - } - - @Override - public String toString() { - return "registry"; - } - }; - } - - /** - * Returns an expression for lookup a bean in the {@link org.apache.camel.spi.Registry} - * - * @return an expression object which will return the bean - */ - public static Expression refExpression(final String ref) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(ref).evaluate(exchange, String.class); - return exchange.getContext().getRegistry().lookupByName(text); - } - - @Override - public String toString() { - return "ref(" + ref + ")"; - } - }; - } - - /** - * Returns an expression for the {@link org.apache.camel.CamelContext} - * - * @return an expression object which will return the camel context - */ - public static Expression camelContextExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getContext(); - } - - @Override - public String toString() { - return "camelContext"; - } - }; - } - - /** - * Returns an expression for the {@link org.apache.camel.CamelContext} name - * - * @return an expression object which will return the camel context name - */ - public static Expression camelContextNameExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getContext().getName(); - } - - @Override - public String toString() { - return "camelContextName"; - } - }; - } - - /** - * Returns an expression for an exception message set on the exchange - * - * @return an expression object which will return the exception message set on the exchange - */ - public static Expression exchangeExceptionMessageExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Exception exception = exchange.getException(); - if (exception == null) { - exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); - } - return exception != null ? exception.getMessage() : null; - } - - @Override - public String toString() { - return "exchangeExceptionMessage"; - } - }; - } - - /** - * Returns an expression for an exception stacktrace set on the exchange - * - * @return an expression object which will return the exception stacktrace set on the exchange - */ - public static Expression exchangeExceptionStackTraceExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Exception exception = exchange.getException(); - if (exception == null) { - exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class); - } - if (exception != null) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - exception.printStackTrace(pw); - IOHelper.close(pw, sw); - return sw.toString(); - } else { - return null; - } - } - - @Override - public String toString() { - return "exchangeExceptionStackTrace"; - } - }; - } - - /** - * Returns an expression for the property value of exchange with the given name - * - * @param propertyName the name of the property the expression will return - * @return an expression object which will return the property value - */ - public static Expression exchangePropertyExpression(final String propertyName) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(propertyName).evaluate(exchange, String.class); - return exchange.getProperty(text); - } - - @Override - public String toString() { - return "exchangeProperty(" + propertyName + ")"; - } - }; - } - - /** - * Returns an expression for the exchange properties of exchange - * - * @return an expression object which will return the exchange properties - */ - public static Expression exchangePropertiesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getProperties(); - } - - @Override - public String toString() { - return "exchangeProperties"; - } - }; - } - - /** - * Returns an expression for the properties of the camel context - * - * @return an expression object which will return the properties - */ - public static Expression camelContextPropertiesExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getContext().getGlobalOptions(); - } - - @Override - public String toString() { - return "camelContextProperties"; - } - }; - } - - /** - * Returns an expression for the property value of the camel context with the given name - * - * @param propertyName the name of the property the expression will return - * @return an expression object which will return the property value - */ - public static Expression camelContextPropertyExpression(final String propertyName) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(propertyName).evaluate(exchange, String.class); - return exchange.getContext().getGlobalOption(text); - } - - @Override - public String toString() { - return "camelContextProperty(" + propertyName + ")"; - } - }; - } - - /** - * Returns an expression for a system property value with the given name - * - * @param propertyName the name of the system property the expression will return - * @return an expression object which will return the system property value - */ - public static Expression systemPropertyExpression(final String propertyName) { - return systemPropertyExpression(propertyName, null); - } - - /** - * Returns an expression for a system property value with the given name - * - * @param propertyName the name of the system property the expression will return - * @param defaultValue default value to return if no system property exists - * @return an expression object which will return the system property value - */ - public static Expression systemPropertyExpression(final String propertyName, - final String defaultValue) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(propertyName).evaluate(exchange, String.class); - String text2 = simpleExpression(defaultValue).evaluate(exchange, String.class); - return System.getProperty(text, text2); - } - - @Override - public String toString() { - return "systemProperty(" + propertyName + ")"; - } - }; - } - - /** - * Returns an expression for a system environment value with the given name - * - * @param propertyName the name of the system environment the expression will return - * @return an expression object which will return the system property value - */ - public static Expression systemEnvironmentExpression(final String propertyName) { - return systemEnvironmentExpression(propertyName, null); - } - - /** - * Returns an expression for a system environment value with the given name - * - * @param propertyName the name of the system environment the expression will return - * @param defaultValue default value to return if no system environment exists - * @return an expression object which will return the system environment value - */ - public static Expression systemEnvironmentExpression(final String propertyName, - final String defaultValue) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(propertyName).evaluate(exchange, String.class); - String answer = null; - if (text != null) { - // lookup OS env with upper case key - text = text.toUpperCase(); - answer = System.getenv(text); - // some OS do not support dashes in keys, so replace with underscore - if (answer == null) { - String noDashKey = text.replace('-', '_'); - answer = System.getenv(noDashKey); - } - } - - if (answer == null) { - answer = simpleExpression(defaultValue).evaluate(exchange, String.class); - } - return answer; - } - - @Override - public String toString() { - return "systemEnvironment(" + propertyName + ")"; - } - }; - } - - /** - * Returns an expression for the constant value - * - * @param value the value the expression will return - * @return an expression object which will return the constant value - */ - public static Expression constantExpression(final Object value) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return value; - } - - @Override - public String toString() { - return "" + value; - } - }; - } - - /** - * Returns an expression for evaluating the expression/predicate using the given language - * - * @param expression the expression or predicate - * @return an expression object which will evaluate the expression/predicate using the given language - */ - public static Expression languageExpression(final String language, final String expression) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Language lan = exchange.getContext().resolveLanguage(language); - if (lan != null) { - return lan.createExpression(expression).evaluate(exchange, Object.class); - } else { - throw new NoSuchLanguageException(language); - } - } - - @Override - public boolean matches(Exchange exchange) { - Language lan = exchange.getContext().resolveLanguage(language); - if (lan != null) { - return lan.createPredicate(expression).matches(exchange); - } else { - throw new NoSuchLanguageException(language); - } - } - - @Override - public String toString() { - return "language[" + language + ":" + expression + "]"; - } - }; - } - - /** - * Returns the expression for the exchanges inbound message body - */ - public static Expression bodyExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getBody(); - } - - @Override - public String toString() { - return "body"; - } - }; - } - - /** - * Returns a functional expression for the exchanges inbound message body - */ - public static Expression bodyExpression(final Function<Object, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply( - exchange.getIn().getBody() - ); - } - - @Override - public String toString() { - return "bodyExpression"; - } - }; - } - - /** - * Returns a functional expression for the exchanges inbound message body and headers - */ - public static Expression bodyExpression(final BiFunction<Object, Map<String, Object>, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply( - exchange.getIn().getBody(), - exchange.getIn().getHeaders() - ); - } - - @Override - public String toString() { - return "bodyExpression"; - } - }; - } - - /** - * Returns a functional expression for the exchanges inbound message body converted to a desired type - */ - public static <T> Expression bodyExpression(final Class<T> bodyType, final Function<T, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply( - exchange.getIn().getBody(bodyType) - ); - } - - @Override - public String toString() { - return "bodyExpression (" + bodyType + ")"; - } - }; - } - - /** - * Returns a functional expression for the exchanges inbound message body converted to a desired type and headers - */ - public static <T> Expression bodyExpression(final Class<T> bodyType, final BiFunction<T, Map<String, Object>, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply( - exchange.getIn().getBody(bodyType), - exchange.getIn().getHeaders() - ); - } - - @Override - public String toString() { - return "bodyExpression (" + bodyType + ")"; - } - }; - } - - /** - * Returns the expression for the exchanges inbound message body converted - * to the given type - */ - public static <T> Expression bodyExpression(final Class<T> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getBody(type); - } - - @Override - public String toString() { - return "bodyAs[" + type.getName() + "]"; - } - }; - } - - /** - * Returns the expression for the exchanges inbound message body converted - * to the given type - */ - public static Expression bodyExpression(final String name) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(name).evaluate(exchange, String.class); - Class<?> type; - try { - type = exchange.getContext().getClassResolver().resolveMandatoryClass(text); - } catch (ClassNotFoundException e) { - throw CamelExecutionException.wrapCamelExecutionException(exchange, e); - } - return exchange.getIn().getBody(type); - } - - @Override - public String toString() { - return "bodyAs[" + name + "]"; - } - }; - } - - /** - * Returns the expression for the current thread name - */ - public static Expression threadNameExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return Thread.currentThread().getName(); - } - - @Override - public String toString() { - return "threadName"; - } - }; - } - - /** - * Returns the expression for the current step id (if any) - */ - public static Expression stepIdExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getProperty(Exchange.STEP_ID); - } - - @Override - public String toString() { - return "stepId"; - } - }; - } - - /** - * Returns the expression for the exchanges inbound message body converted - * to the given type. - * <p/> - * Does <b>not</b> allow null bodies. - */ - public static <T> Expression mandatoryBodyExpression(final Class<T> type) { - return mandatoryBodyExpression(type, false); - } - - /** - * Returns the expression for the exchanges inbound message body converted - * to the given type - * - * @param type the type - * @param nullBodyAllowed whether null bodies is allowed and if so a null is returned, - * otherwise an exception is thrown - */ - public static <T> Expression mandatoryBodyExpression(final Class<T> type, final boolean nullBodyAllowed) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (nullBodyAllowed) { - if (exchange.getIn().getBody() == null) { - return null; - } - } - - try { - return exchange.getIn().getMandatoryBody(type); - } catch (InvalidPayloadException e) { - throw CamelExecutionException.wrapCamelExecutionException(exchange, e); - } - } - - @Override - public String toString() { - return "mandatoryBodyAs[" + type.getName() + "]"; - } - }; - } - - /** - * Returns the expression for the exchanges inbound message body type - */ - public static Expression bodyTypeExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getBody().getClass(); - } - - @Override - public String toString() { - return "bodyType"; - } - }; - } - - /** - * Returns the expression for the out messages body - */ - public static Expression outBodyExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (exchange.hasOut()) { - return exchange.getOut().getBody(); - } else { - return null; - } - } - - @Override - public String toString() { - return "outBody"; - } - }; - } - - /** - * Returns the expression for the exchanges outbound message body converted - * to the given type - */ - public static <T> Expression outBodyExpression(final Class<T> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (exchange.hasOut()) { - return exchange.getOut().getBody(type); - } else { - return null; - } - } - - @Override - public String toString() { - return "outBodyAs[" + type.getName() + "]"; - } - }; - } - - /** - * Returns the expression for the fault messages body - */ - public static Expression faultBodyExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn(); - return msg.isFault() ? msg.getBody() : null; - } - - @Override - public String toString() { - return "faultBody"; - } - }; - } - - /** - * Returns the expression for the exchanges fault message body converted - * to the given type - */ - public static <T> Expression faultBodyExpression(final Class<T> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Message msg = exchange.hasOut() ? exchange.getOut() : exchange.getIn(); - return msg.isFault() ? msg.getBody(type) : null; - } - - @Override - public String toString() { - return "faultBodyAs[" + type.getName() + "]"; - } - }; - } - - /** - * Returns the expression for the exchange - */ - public static Expression exchangeExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange; - } - - @Override - public String toString() { - return "exchange"; - } - }; - } - - /** - * Returns a functional expression for the exchange - */ - public static Expression exchangeExpression(final Function<Exchange, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply(exchange); - } - - @Override - public String toString() { - return "exchangeExpression"; - } - }; - } - - /** - * Returns the expression for the IN message - */ - public static Expression messageExpression() { - return inMessageExpression(); - } - - /** - * Returns a functional expression for the IN message - */ - public static Expression messageExpression(final Function<Message, Object> function) { - return inMessageExpression(function); - } - - /** - * Returns the expression for the IN message - */ - public static Expression inMessageExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn(); - } - - @Override - public String toString() { - return "inMessage"; - } - }; - } - - /** - * Returns a functional expression for the IN message - */ - public static Expression inMessageExpression(final Function<Message, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply(exchange.getIn()); - } - - @Override - public String toString() { - return "inMessageExpression"; - } - }; - } - - /** - * Returns the expression for the OUT message - */ - public static Expression outMessageExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getOut(); - } - - @Override - public String toString() { - return "outMessage"; - } - }; - } - - /** - * Returns a functional expression for the OUT message - */ - public static Expression outMessageExpression(final Function<Message, Object> function) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return function.apply(exchange.getOut()); - } - - @Override - public String toString() { - return "outMessageExpression"; - } - }; - } - - /** - * Returns an expression which converts the given expression to the given type - */ - public static Expression convertToExpression(final Expression expression, final Class<?> type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (type != null) { - return expression.evaluate(exchange, type); - } else { - return expression; - } - } - - @Override - public String toString() { - return "" + expression; - } - }; - } - - /** - * Returns an expression which converts the given expression to the given type the type - * expression is evaluated to - */ - public static Expression convertToExpression(final Expression expression, final Expression type) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Object result = type.evaluate(exchange, Object.class); - if (result != null) { - return expression.evaluate(exchange, result.getClass()); - } else { - return expression; - } - } - - @Override - public String toString() { - return "" + expression; - } - }; - } - - /** - * Returns a tokenize expression which will tokenize the string with the - * given token - */ - public static Expression tokenizeExpression(final Expression expression, - final String token) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(token).evaluate(exchange, String.class); - Object value = expression.evaluate(exchange, Object.class); - Scanner scanner = ExchangeHelper.getScanner(exchange, value, text); - return scanner; - } - - @Override - public String toString() { - return "tokenize(" + expression + ", " + token + ")"; - } - }; - } - - /** - * Returns an expression that skips the first element - */ - public static Expression skipFirstExpression(final Expression expression) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Object value = expression.evaluate(exchange, Object.class); - Iterator it = exchange.getContext().getTypeConverter().tryConvertTo(Iterator.class, exchange, value); - if (it != null) { - // skip first - it.next(); - return it; - } else { - return value; - } - } - - @Override - public String toString() { - return "skipFirst(" + expression + ")"; - } - }; - } - - /** * Returns an {@link TokenPairExpressionIterator} expression */ public static Expression tokenizePairExpression(String startToken, String endToken, boolean includeTokens) { @@ -1240,366 +55,5 @@ public final class ExpressionBuilder { return new XMLTokenExpressionIterator(path, mode, group); } - /** - * Returns a tokenize expression which will tokenize the string with the - * given regex - */ - public static Expression regexTokenizeExpression(final Expression expression, - final String regexTokenizer) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - Object value = expression.evaluate(exchange, Object.class); - Scanner scanner = ExchangeHelper.getScanner(exchange, value, regexTokenizer); - return scanner; - } - - @Override - public String toString() { - return "regexTokenize(" + expression + ", " + regexTokenizer + ")"; - } - }; - } - - public static Expression groupXmlIteratorExpression(final Expression expression, final String group) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - // evaluate expression as iterator - Iterator<?> it = expression.evaluate(exchange, Iterator.class); - ObjectHelper.notNull(it, "expression: " + expression + " evaluated on " + exchange + " must return an java.util.Iterator"); - // must use GroupTokenIterator in xml mode as we want to concat the xml parts into a single message - // the group can be a simple expression so evaluate it as a number - Integer parts = exchange.getContext().resolveLanguage("simple").createExpression(group).evaluate(exchange, Integer.class); - if (parts == null) { - throw new RuntimeExchangeException("Group evaluated as null, must be evaluated as a positive Integer value from expression: " + group, exchange); - } else if (parts <= 0) { - throw new RuntimeExchangeException("Group must be a positive number, was: " + parts, exchange); - } - return new GroupTokenIterator(exchange, it, null, parts, false); - } - - @Override - public String toString() { - return "group " + expression + " " + group + " times"; - } - }; - } - - public static Expression groupIteratorExpression(final Expression expression, final String token, final String group, final boolean skipFirst) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - // evaluate expression as iterator - Iterator<?> it = expression.evaluate(exchange, Iterator.class); - ObjectHelper.notNull(it, "expression: " + expression + " evaluated on " + exchange + " must return an java.util.Iterator"); - // the group can be a simple expression so evaluate it as a number - Integer parts = exchange.getContext().resolveLanguage("simple").createExpression(group).evaluate(exchange, Integer.class); - if (parts == null) { - throw new RuntimeExchangeException("Group evaluated as null, must be evaluated as a positive Integer value from expression: " + group, exchange); - } else if (parts <= 0) { - throw new RuntimeExchangeException("Group must be a positive number, was: " + parts, exchange); - } - if (token != null) { - return new GroupTokenIterator(exchange, it, token, parts, skipFirst); - } else { - return new GroupIterator(exchange, it, parts, skipFirst); - } - } - - @Override - public String toString() { - return "group " + expression + " " + group + " times"; - } - }; - } - - /** - * Returns a sort expression which will sort the expression with the given comparator. - * <p/> - * The expression is evaluated as a {@link List} object to allow sorting. - */ - @SuppressWarnings({"unchecked", "rawtypes"}) - public static Expression sortExpression(final Expression expression, final Comparator comparator) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - List<?> list = expression.evaluate(exchange, List.class); - list.sort(comparator); - return list; - } - - @Override - public String toString() { - return "sort(" + expression + " by: " + comparator + ")"; - } - }; - } - - /** - * Transforms the expression into a String then performs the regex - * replaceAll to transform the String and return the result - */ - public static Expression regexReplaceAll(final Expression expression, - final String regex, final String replacement) { - final Pattern pattern = Pattern.compile(regex); - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = expression.evaluate(exchange, String.class); - if (text == null) { - return null; - } - return pattern.matcher(text).replaceAll(replacement); - } - - @Override - public String toString() { - return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")"; - } - }; - } - - /** - * Transforms the expression into a String then performs the regex - * replaceAll to transform the String and return the result - */ - public static Expression regexReplaceAll(final Expression expression, - final String regex, final Expression replacementExpression) { - - final Pattern pattern = Pattern.compile(regex); - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = expression.evaluate(exchange, String.class); - String replacement = replacementExpression.evaluate(exchange, String.class); - if (text == null || replacement == null) { - return null; - } - return pattern.matcher(text).replaceAll(replacement); - } - - @Override - public String toString() { - return "regexReplaceAll(" + expression + ", " + pattern.pattern() + ")"; - } - }; - } - - /** - * Appends the String evaluations of the two expressions together - */ - public static Expression append(final Expression left, final Expression right) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return left.evaluate(exchange, String.class) + right.evaluate(exchange, String.class); - } - - @Override - public String toString() { - return "append(" + left + ", " + right + ")"; - } - }; - } - - /** - * Prepends the String evaluations of the two expressions together - */ - public static Expression prepend(final Expression left, final Expression right) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return right.evaluate(exchange, String.class) + left.evaluate(exchange, String.class); - } - - @Override - public String toString() { - return "prepend(" + left + ", " + right + ")"; - } - }; - } - - /** - * Returns an expression which returns the string concatenation value of the various - * expressions - * - * @param expressions the expression to be concatenated dynamically - * @return an expression which when evaluated will return the concatenated values - */ - public static Expression concatExpression(final Collection<Expression> expressions) { - return concatExpression(expressions, null); - } - - /** - * Returns an expression which returns the string concatenation value of the various - * expressions - * - * @param expressions the expression to be concatenated dynamically - * @param description the text description of the expression - * @return an expression which when evaluated will return the concatenated values - */ - public static Expression concatExpression(final Collection<Expression> expressions, final String description) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - StringBuilder buffer = new StringBuilder(); - for (Expression expression : expressions) { - String text = expression.evaluate(exchange, String.class); - if (text != null) { - buffer.append(text); - } - } - return buffer.toString(); - } - - @Override - public String toString() { - if (description != null) { - return description; - } else { - return "concat" + expressions; - } - } - }; - } - - /** - * Returns an Expression for the inbound message id - */ - public static Expression messageIdExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getIn().getMessageId(); - } - - @Override - public String toString() { - return "messageId"; - } - }; - } - - /** - * Returns an Expression for the exchange id - */ - public static Expression exchangeIdExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - return exchange.getExchangeId(); - } - - @Override - public String toString() { - return "exchangeId"; - } - }; - } - - /** - * Returns an Expression for the route id - */ - public static Expression routeIdExpression() { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String answer = null; - UnitOfWork uow = exchange.getUnitOfWork(); - RouteContext rc = uow != null ? uow.getRouteContext() : null; - if (rc != null) { - answer = rc.getRoute().getId(); - } - if (answer == null) { - // fallback and get from route id on the exchange - answer = exchange.getFromRouteId(); - } - return answer; - } - - @Override - public String toString() { - return "routeId"; - } - }; - } - - public static Expression simpleExpression(final String expression) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - if (LanguageSupport.hasSimpleFunction(expression)) { - // resolve language using context to have a clear separation of packages - // must call evaluate to return the nested language evaluate when evaluating - // stacked expressions - Language language = exchange.getContext().resolveLanguage("simple"); - return language.createExpression(expression).evaluate(exchange, Object.class); - } else { - return expression; - } - } - - @Override - public String toString() { - return "simple(" + expression + ")"; - } - }; - } - - public static Expression beanExpression(final String expression) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - // bean is able to evaluate method name if it contains nested functions - // so we should not eager evaluate expression as a string - // resolve language using context to have a clear separation of packages - // must call evaluate to return the nested language evaluate when evaluating - // stacked expressions - Language language = exchange.getContext().resolveLanguage("bean"); - return language.createExpression(expression).evaluate(exchange, Object.class); - } - - @Override - public String toString() { - return "bean(" + expression + ")"; - } - }; - } - - /** - * Returns Simple expression or fallback to Constant expression if expression str is not Simple expression. - */ - public static Expression parseSimpleOrFallbackToConstantExpression(String str, CamelContext camelContext) { - if (StringHelper.hasStartToken(str, "simple")) { - return camelContext.resolveLanguage("simple").createExpression(str); - } - return constantExpression(str); - } - - public static Expression propertiesComponentExpression(final String key, final String locations, final String defaultValue) { - return new ExpressionAdapter() { - public Object evaluate(Exchange exchange) { - String text = simpleExpression(key).evaluate(exchange, String.class); - String text2 = simpleExpression(locations).evaluate(exchange, String.class); - try { - if (text2 != null) { - // the properties component is optional as we got locations - // getComponent will create a new component if none already exists - PropertiesComponent pc = exchange.getContext().getPropertiesComponent(true); - // enclose key with {{ }} to force parsing - String[] paths = text2.split(","); - return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken(), paths); - } else { - // the properties component is mandatory if no locations provided - PropertiesComponent pc = exchange.getContext().getPropertiesComponent(false); - if (pc == null) { - throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" - + " in CamelContext to support property placeholders in expressions"); - } - // enclose key with {{ }} to force parsing - return pc.parseUri(pc.getPrefixToken() + text + pc.getSuffixToken()); - } - } catch (Exception e) { - // property with key not found, use default value if provided - if (defaultValue != null) { - return defaultValue; - } - throw RuntimeCamelException.wrapRuntimeCamelException(e); - } - } - - @Override - public String toString() { - return "properties(" + key + ")"; - } - }; - } } diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java b/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java index eee830e..0b6dd01 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java @@ -16,23 +16,10 @@ */ package org.apache.camel.builder; -import java.util.Arrays; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.camel.Exchange; -import org.apache.camel.Expression; -import org.apache.camel.Predicate; -import org.apache.camel.support.ExpressionToPredicateAdapter; -import org.apache.camel.support.ObjectHelper; - -import static org.apache.camel.util.ObjectHelper.notNull; - /** * A helper class for working with predicates */ -public final class PredicateBuilder { +public final class PredicateBuilder extends org.apache.camel.support.builder.PredicateBuilder { /** * Utility classes should not have a public constructor. @@ -40,513 +27,4 @@ public final class PredicateBuilder { private PredicateBuilder() { } - /** - * Converts the given expression into an {@link Predicate} - */ - public static Predicate toPredicate(final Expression expression) { - return ExpressionToPredicateAdapter.toPredicate(expression); - } - - /** - * A helper method to return the logical not of the given predicate - */ - public static Predicate not(final Predicate predicate) { - notNull(predicate, "predicate"); - return new Predicate() { - public boolean matches(Exchange exchange) { - return !predicate.matches(exchange); - } - - @Override - public String toString() { - return "not (" + predicate + ")"; - } - }; - } - - /** - * A helper method to combine multiple predicates by a logical AND - */ - public static Predicate and(final Predicate left, final Predicate right) { - notNull(left, "left"); - notNull(right, "right"); - return new Predicate() { - public boolean matches(Exchange exchange) { - return left.matches(exchange) && right.matches(exchange); - } - - @Override - public String toString() { - return "(" + left + ") and (" + right + ")"; - } - }; - } - - /** - * A helper method to combine two predicates by a logical OR. - * If you want to combine multiple predicates see {@link #in(Predicate...)} - */ - public static Predicate or(final Predicate left, final Predicate right) { - notNull(left, "left"); - notNull(right, "right"); - return new Predicate() { - public boolean matches(Exchange exchange) { - return left.matches(exchange) || right.matches(exchange); - } - - @Override - public String toString() { - return "(" + left + ") or (" + right + ")"; - } - }; - } - - /** - * Concat the given predicates into a single predicate, which matches - * if at least one predicates matches. - * - * @param predicates predicates - * @return a single predicate containing all the predicates - */ - public static Predicate or(List<Predicate> predicates) { - Predicate answer = null; - for (Predicate predicate : predicates) { - if (answer == null) { - answer = predicate; - } else { - answer = or(answer, predicate); - } - } - return answer; - } - - /** - * Concat the given predicates into a single predicate, which matches - * if at least one predicates matches. - * - * @param predicates predicates - * @return a single predicate containing all the predicates - */ - public static Predicate or(Predicate... predicates) { - return or(Arrays.asList(predicates)); - } - - /** - * A helper method to return true if any of the predicates matches. - */ - public static Predicate in(final Predicate... predicates) { - notNull(predicates, "predicates"); - - return new Predicate() { - public boolean matches(Exchange exchange) { - for (Predicate in : predicates) { - if (in.matches(exchange)) { - return true; - } - } - return false; - } - - @Override - public String toString() { - return "in (" + Arrays.asList(predicates) + ")"; - } - }; - } - - /** - * A helper method to return true if any of the predicates matches. - */ - public static Predicate in(List<Predicate> predicates) { - return in(predicates.toArray(new Predicate[0])); - } - - public static Predicate isEqualTo(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); - } - - protected String getOperationText() { - return "=="; - } - }; - } - - public static Predicate isEqualToIgnoreCase(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue, true); - } - - protected String getOperationText() { - return "=~"; - } - }; - } - - public static Predicate isNotEqualTo(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return false; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return true; - } - - return ObjectHelper.typeCoerceNotEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); - } - - protected String getOperationText() { - return "!="; - } - }; - } - - public static Predicate isLessThan(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) < 0; - } - - protected String getOperationText() { - return "<"; - } - }; - } - - public static Predicate isLessThanOrEqualTo(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) <= 0; - } - - protected String getOperationText() { - return "<="; - } - }; - } - - public static Predicate isGreaterThan(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return false; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) > 0; - } - - protected String getOperationText() { - return ">"; - } - }; - } - - public static Predicate isGreaterThanOrEqualTo(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.typeCoerceCompare(exchange.getContext().getTypeConverter(), leftValue, rightValue) >= 0; - } - - protected String getOperationText() { - return ">="; - } - }; - } - - public static Predicate contains(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.contains(leftValue, rightValue); - } - - protected String getOperationText() { - return "contains"; - } - }; - } - - public static Predicate containsIgnoreCase(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - - return ObjectHelper.containsIgnoreCase(leftValue, rightValue); - } - - protected String getOperationText() { - return "~~"; - } - }; - } - - public static Predicate isNull(final Expression expression) { - return new BinaryPredicateSupport(expression, ExpressionBuilder.constantExpression(null)) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null) { - // the left operator is null so its true - return true; - } - - return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); - } - - protected String getOperationText() { - // leave the operation text as "is not" as Camel will insert right and left expression around it - // so it will be displayed as: XXX is null - return "is"; - } - }; - } - - public static Predicate isNotNull(final Expression expression) { - return new BinaryPredicateSupport(expression, ExpressionBuilder.constantExpression(null)) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue != null) { - // the left operator is not null so its true - return true; - } - - return ObjectHelper.typeCoerceNotEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue); - } - - protected String getOperationText() { - // leave the operation text as "is not" as Camel will insert right and left expression around it - // so it will be displayed as: XXX is not null - return "is not"; - } - }; - } - - public static Predicate isInstanceOf(final Expression expression, final Class<?> type) { - notNull(expression, "expression"); - notNull(type, "type"); - - return new Predicate() { - public boolean matches(Exchange exchange) { - Object value = expression.evaluate(exchange, Object.class); - return type.isInstance(value); - } - - @Override - public String toString() { - return expression + " instanceof " + type.getCanonicalName(); - } - }; - } - - public static Predicate startsWith(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - String leftStr = exchange.getContext().getTypeConverter().convertTo(String.class, leftValue); - String rightStr = exchange.getContext().getTypeConverter().convertTo(String.class, rightValue); - if (leftStr != null && rightStr != null) { - return leftStr.startsWith(rightStr); - } else { - return false; - } - } - - protected String getOperationText() { - return "startsWith"; - } - }; - } - - public static Predicate endsWith(final Expression left, final Expression right) { - return new BinaryPredicateSupport(left, right) { - - protected boolean matches(Exchange exchange, Object leftValue, Object rightValue) { - if (leftValue == null && rightValue == null) { - // they are equal - return true; - } else if (leftValue == null || rightValue == null) { - // only one of them is null so they are not equal - return false; - } - String leftStr = exchange.getContext().getTypeConverter().convertTo(String.class, leftValue); - String rightStr = exchange.getContext().getTypeConverter().convertTo(String.class, rightValue); - if (leftStr != null && rightStr != null) { - return leftStr.endsWith(rightStr); - } else { - return false; - } - } - - protected String getOperationText() { - return "endsWith"; - } - }; - } - - /** - * Returns a predicate which is true if the expression matches the given - * regular expression - * - * @param expression the expression to evaluate - * @param regex the regular expression to match against - * @return a new predicate - */ - public static Predicate regex(final Expression expression, final String regex) { - return regex(expression, Pattern.compile(regex)); - } - - /** - * Returns a predicate which is true if the expression matches the given - * regular expression - * - * @param expression the expression to evaluate - * @param pattern the regular expression to match against - * @return a new predicate - */ - public static Predicate regex(final Expression expression, final Pattern pattern) { - notNull(expression, "expression"); - notNull(pattern, "pattern"); - - return new Predicate() { - public boolean matches(Exchange exchange) { - String value = expression.evaluate(exchange, String.class); - if (value != null) { - Matcher matcher = pattern.matcher(value); - return matcher.matches(); - } - return false; - } - - @Override - public String toString() { - return expression + ".matches('" + pattern + "')"; - } - }; - } - - /** - * Concat the given predicates into a single predicate, which - * only matches if all the predicates matches. - * - * @param predicates predicates - * @return a single predicate containing all the predicates - */ - public static Predicate and(List<Predicate> predicates) { - Predicate answer = null; - for (Predicate predicate : predicates) { - if (answer == null) { - answer = predicate; - } else { - answer = and(answer, predicate); - } - } - return answer; - } - - /** - * Concat the given predicates into a single predicate, which only matches - * if all the predicates matches. - * - * @param predicates predicates - * @return a single predicate containing all the predicates - */ - public static Predicate and(Predicate... predicates) { - return and(Arrays.asList(predicates)); - } - - /** - * A constant predicate. - * - * @param answer the constant matches - * @return a predicate that always returns the given answer. - */ - public static Predicate constant(final boolean answer) { - return new Predicate() { - @Override - public boolean matches(Exchange exchange) { - return answer; - } - - @Override - public String toString() { - return "" + answer; - } - }; - } } diff --git a/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java b/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java index 188bbc6..e0c834a 100644 --- a/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java +++ b/core/camel-core/src/main/java/org/apache/camel/component/mock/MockValueBuilder.java @@ -24,13 +24,11 @@ import java.util.concurrent.atomic.AtomicReference; import org.apache.camel.Exchange; import org.apache.camel.Expression; import org.apache.camel.Predicate; -import org.apache.camel.builder.ExpressionBuilder; import org.apache.camel.builder.ExpressionClause; -import org.apache.camel.builder.PredicateBuilder; -import org.apache.camel.spi.NamespaceAware; import org.apache.camel.support.ExpressionAdapter; import org.apache.camel.support.ExpressionToPredicateAdapter; -import org.apache.camel.support.builder.xml.Namespaces; +import org.apache.camel.support.builder.ExpressionBuilder; +import org.apache.camel.support.builder.PredicateBuilder; /** * A builder of expressions or predicates based on values. @@ -237,26 +235,6 @@ public class MockValueBuilder implements Expression, Predicate { return onNewValueBuilder(newExp); } - public MockValueBuilder tokenizeXML(String tagName, String inheritNamespaceTagName) { - Expression newExp = ExpressionBuilder.tokenizeXMLExpression(tagName, inheritNamespaceTagName); - return onNewValueBuilder(newExp); - } - - public MockValueBuilder xtokenize(String path, Namespaces namespaces) { - return xtokenize(path, 'i', namespaces); - } - - public MockValueBuilder xtokenize(String path, char mode, Namespaces namespaces) { - Expression newExp = ExpressionBuilder.tokenizeXMLAwareExpression(path, mode); - ((NamespaceAware)newExp).setNamespaces(namespaces.getNamespaces()); - return onNewValueBuilder(newExp); - } - - public MockValueBuilder tokenizePair(String startToken, String endToken, boolean includeTokens) { - Expression newExp = ExpressionBuilder.tokenizePairExpression(startToken, endToken, includeTokens); - return onNewValueBuilder(newExp); - } - /** * Tokenizes the string conversion of this expression using the given * regular expression diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/BinaryPredicateSupport.java similarity index 98% rename from core/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java rename to core/camel-support/src/main/java/org/apache/camel/support/builder/BinaryPredicateSupport.java index 3907dd3..8baa57d 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/BinaryPredicateSupport.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/BinaryPredicateSupport.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.builder; +package org.apache.camel.support.builder; import org.apache.camel.BinaryPredicate; import org.apache.camel.Exchange; diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java similarity index 97% copy from core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java copy to core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java index ed3f8691..c5fa4bd 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ExpressionBuilder.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.builder; +package org.apache.camel.support.builder; import java.io.PrintWriter; import java.io.StringWriter; @@ -53,16 +53,7 @@ import org.apache.camel.util.StringHelper; /** * A helper class for working with <a href="http://camel.apache.org/expression.html">expressions</a>. */ -public final class ExpressionBuilder { - - // TODO: Make this possible to have a base class in camel-support, and then extend it here, so we can have both - // and maybe deprecate this and refer to the builder in camel-support - - /** - * Utility classes should not have a public constructor. - */ - private ExpressionBuilder() { - } +public class ExpressionBuilder { /** * Returns an expression for the inbound message attachments @@ -304,7 +295,7 @@ public final class ExpressionBuilder { /** * Returns an expression for the exchange pattern * - * @see org.apache.camel.Exchange#getPattern() + * @see Exchange#getPattern() * @return an expression object which will return the exchange pattern */ public static Expression exchangePatternExpression() { @@ -426,7 +417,7 @@ public final class ExpressionBuilder { } /** - * Returns an expression for the {@link org.apache.camel.CamelContext} + * Returns an expression for the {@link CamelContext} * * @return an expression object which will return the camel context */ @@ -444,7 +435,7 @@ public final class ExpressionBuilder { } /** - * Returns an expression for the {@link org.apache.camel.CamelContext} name + * Returns an expression for the {@link CamelContext} name * * @return an expression object which will return the camel context name */ @@ -1216,31 +1207,6 @@ public final class ExpressionBuilder { } /** - * Returns an {@link TokenPairExpressionIterator} expression - */ - public static Expression tokenizePairExpression(String startToken, String endToken, boolean includeTokens) { - return new TokenPairExpressionIterator(startToken, endToken, includeTokens); - } - - /** - * Returns an {@link TokenXMLExpressionIterator} expression - */ - public static Expression tokenizeXMLExpression(String tagName, String inheritNamespaceTagName) { - StringHelper.notEmpty(tagName, "tagName"); - return new TokenXMLExpressionIterator(tagName, inheritNamespaceTagName); - } - - public static Expression tokenizeXMLAwareExpression(String path, char mode) { - StringHelper.notEmpty(path, "path"); - return new XMLTokenExpressionIterator(path, mode); - } - - public static Expression tokenizeXMLAwareExpression(String path, char mode, int group) { - StringHelper.notEmpty(path, "path"); - return new XMLTokenExpressionIterator(path, mode, group); - } - - /** * Returns a tokenize expression which will tokenize the string with the * given regex */ diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java similarity index 99% copy from core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java copy to core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java index eee830e..302d100 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/PredicateBuilder.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.camel.builder; +package org.apache.camel.support.builder; import java.util.Arrays; import java.util.List; @@ -26,21 +26,14 @@ import org.apache.camel.Expression; import org.apache.camel.Predicate; import org.apache.camel.support.ExpressionToPredicateAdapter; import org.apache.camel.support.ObjectHelper; - import static org.apache.camel.util.ObjectHelper.notNull; /** * A helper class for working with predicates */ -public final class PredicateBuilder { +public class PredicateBuilder { /** - * Utility classes should not have a public constructor. - */ - private PredicateBuilder() { - } - - /** * Converts the given expression into an {@link Predicate} */ public static Predicate toPredicate(final Expression expression) {