Search in sources :

Example 1 with SimpleIllegalSyntaxException

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;
        }
    };
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Example 2 with SimpleIllegalSyntaxException

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;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) Matcher(java.util.regex.Matcher) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Predicate(org.apache.camel.Predicate)

Example 3 with SimpleIllegalSyntaxException

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;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Predicate(org.apache.camel.Predicate)

Aggregations

Exchange (org.apache.camel.Exchange)3 Expression (org.apache.camel.Expression)3 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)3 Predicate (org.apache.camel.Predicate)2 Matcher (java.util.regex.Matcher)1 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)1 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)1