use of org.apache.camel.language.simple.types.SimpleIllegalSyntaxException in project camel by apache.
the class SimpleFunctionStart method doCreateCompositeExpression.
private Expression doCreateCompositeExpression(final String expression) {
final SimpleToken token = getToken();
return new Expression() {
@Override
public <T> T evaluate(Exchange exchange, Class<T> type) {
StringBuilder sb = new StringBuilder();
boolean quoteEmbeddedFunctions = false;
// we need to concat the block so we have the expression
for (SimpleNode child : block.getChildren()) {
// whether a nested function should be lazy evaluated or not
boolean lazy = true;
if (child instanceof SimpleFunctionStart) {
lazy = ((SimpleFunctionStart) child).lazyEval(child);
}
if (child instanceof LiteralNode) {
String text = ((LiteralNode) child).getText();
sb.append(text);
quoteEmbeddedFunctions |= ((LiteralNode) child).quoteEmbeddedNodes();
// if its quoted literal then embed that as text
} else if (!lazy || child instanceof SingleQuoteStart || child instanceof DoubleQuoteStart) {
try {
// pass in null when we evaluate the nested expressions
Expression nested = child.createExpression(null);
String text = nested.evaluate(exchange, String.class);
if (text != null) {
if (quoteEmbeddedFunctions && !StringHelper.isQuoted(text)) {
sb.append("'").append(text).append("'");
} else {
sb.append(text);
}
}
} catch (SimpleParserException e) {
// must rethrow parser exception as illegal syntax with details about the location
throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
}
// if its an inlined function then embed that function as text so it can be evaluated lazy
} else if (child instanceof SimpleFunctionStart) {
sb.append(child);
}
}
// we have now concat the block as a String which contains the function expression
// which we then need to evaluate as a function
String exp = sb.toString();
SimpleFunctionExpression function = new SimpleFunctionExpression(token);
function.addText(exp);
try {
return function.createExpression(exp).evaluate(exchange, type);
} catch (SimpleParserException e) {
// must rethrow parser exception as illegal syntax with details about the location
throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
}
}
@Override
public String toString() {
return expression;
}
};
}
use of org.apache.camel.language.simple.types.SimpleIllegalSyntaxException 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.language.simple.types.SimpleIllegalSyntaxException 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;
}
};
}
Aggregations