use of org.apache.camel.Predicate in project camel by apache.
the class SimplePredicateParser method createPredicates.
/**
* Creates the {@link Predicate}s from the AST nodes.
*
* @return the created {@link Predicate}s, is never <tt>null</tt>.
*/
private List<Predicate> createPredicates() {
List<Predicate> answer = new ArrayList<Predicate>();
for (SimpleNode node : nodes) {
Expression exp = node.createExpression(expression);
if (exp != null) {
Predicate predicate = ExpressionToPredicateAdapter.toPredicate(exp);
answer.add(predicate);
}
}
return answer;
}
use of org.apache.camel.Predicate in project camel by apache.
the class BinaryExpression method createRangeExpression.
private Expression createRangeExpression(final String expression, final Expression leftExp, final Expression rightExp) {
return new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
Predicate predicate;
String range = rightExp.evaluate(exchange, String.class);
Matcher matcher = RANGE_PATTERN.matcher(range);
if (matcher.matches()) {
// wrap as constant expression for the from and to values
Expression from = ExpressionBuilder.constantExpression(matcher.group(1));
Expression to = ExpressionBuilder.constantExpression(matcher.group(3));
// build a compound predicate for the range
predicate = PredicateBuilder.isGreaterThanOrEqualTo(leftExp, from);
predicate = PredicateBuilder.and(predicate, PredicateBuilder.isLessThanOrEqualTo(leftExp, to));
} else {
throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator is not valid. Valid syntax:'from..to' (where from and to are numbers).");
}
if (operator == BinaryOperatorType.NOT_RANGE) {
predicate = PredicateBuilder.not(predicate);
}
boolean answer = predicate.matches(exchange);
return exchange.getContext().getTypeConverter().convertTo(type, answer);
}
@Override
public String toString() {
return left + " " + token.getText() + " " + right;
}
};
}
use of org.apache.camel.Predicate in project camel by apache.
the class BinaryExpression method createIsExpression.
private Expression createIsExpression(final String expression, final Expression leftExp, final Expression rightExp) {
return new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
Predicate predicate;
String name = rightExp.evaluate(exchange, String.class);
if (name == null || "null".equals(name)) {
throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator cannot accept null. A class type must be provided.");
}
Class<?> rightType = exchange.getContext().getClassResolver().resolveClass(name);
if (rightType == null) {
throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator cannot find class with name: " + name);
}
predicate = PredicateBuilder.isInstanceOf(leftExp, rightType);
if (operator == BinaryOperatorType.NOT_IS) {
predicate = PredicateBuilder.not(predicate);
}
boolean answer = predicate.matches(exchange);
return exchange.getContext().getTypeConverter().convertTo(type, answer);
}
@Override
public String toString() {
return left + " " + token.getText() + " " + right;
}
};
}
use of org.apache.camel.Predicate in project camel by apache.
the class SimpleLanguage method createPredicate.
public Predicate createPredicate(String expression) {
ObjectHelper.notNull(expression, "expression");
expression = loadResource(expression);
// support old simple language syntax
@SuppressWarnings("deprecation") Predicate answer = SimpleBackwardsCompatibleParser.parsePredicate(expression, allowEscape);
if (answer == null) {
// use the new parser
SimplePredicateParser parser = new SimplePredicateParser(expression, allowEscape);
answer = parser.parsePredicate();
}
return answer;
}
use of org.apache.camel.Predicate in project camel by apache.
the class FilterBeforeSplitTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
Predicate goodWord = body().contains("World");
from("direct:start").to("mock:before").filter(goodWord).to("mock:good").end().split(body().tokenize(" "), new MyAggregationStrategy()).to("mock:split").end().to("mock:result");
}
};
}
Aggregations