Search in sources :

Example 11 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class SimpleFunctionExpression method createSimpleExpression.

private Expression createSimpleExpression(String function, boolean strict) {
    // return the function directly if we can create function without analyzing the prefix
    Expression answer = createSimpleExpressionDirectly(function);
    if (answer != null) {
        return answer;
    }
    // body and headers first
    answer = createSimpleExpressionBodyOrHeader(function, strict);
    if (answer != null) {
        return answer;
    }
    // camelContext OGNL
    String remainder = ifStartsWithReturnRemainder("camelContext", function);
    if (remainder != null) {
        boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
        if (invalid) {
            throw new SimpleParserException("Valid syntax: ${camelContext.OGNL} was: " + function, token.getIndex());
        }
        return ExpressionBuilder.camelContextOgnlExpression(remainder);
    }
    // Exception OGNL
    remainder = ifStartsWithReturnRemainder("exception", function);
    if (remainder != null) {
        boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
        if (invalid) {
            throw new SimpleParserException("Valid syntax: ${exception.OGNL} was: " + function, token.getIndex());
        }
        return ExpressionBuilder.exchangeExceptionOgnlExpression(remainder);
    }
    // property
    remainder = ifStartsWithReturnRemainder("property", function);
    if (remainder == null) {
        remainder = ifStartsWithReturnRemainder("exchangeProperty", function);
    }
    if (remainder != null) {
        // remove leading character (dot or ?)
        if (remainder.startsWith(".") || remainder.startsWith("?")) {
            remainder = remainder.substring(1);
        }
        // remove starting and ending brackets
        if (remainder.startsWith("[") && remainder.endsWith("]")) {
            remainder = remainder.substring(1, remainder.length() - 1);
        }
        // validate syntax
        boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
        if (invalid) {
            throw new SimpleParserException("Valid syntax: ${exchangeProperty.OGNL} was: " + function, token.getIndex());
        }
        if (OgnlHelper.isValidOgnlExpression(remainder)) {
            // ognl based property
            return ExpressionBuilder.propertyOgnlExpression(remainder);
        } else {
            // regular property
            return ExpressionBuilder.exchangePropertyExpression(remainder);
        }
    }
    // system property
    remainder = ifStartsWithReturnRemainder("sys.", function);
    if (remainder != null) {
        return ExpressionBuilder.systemPropertyExpression(remainder);
    }
    remainder = ifStartsWithReturnRemainder("sysenv.", function);
    if (remainder != null) {
        return ExpressionBuilder.systemEnvironmentExpression(remainder);
    }
    // exchange OGNL
    remainder = ifStartsWithReturnRemainder("exchange", function);
    if (remainder != null) {
        boolean invalid = OgnlHelper.isInvalidValidOgnlExpression(remainder);
        if (invalid) {
            throw new SimpleParserException("Valid syntax: ${exchange.OGNL} was: " + function, token.getIndex());
        }
        return ExpressionBuilder.exchangeOgnlExpression(remainder);
    }
    // file: prefix
    remainder = ifStartsWithReturnRemainder("file:", function);
    if (remainder != null) {
        Expression fileExpression = createSimpleFileExpression(remainder, strict);
        if (fileExpression != null) {
            return fileExpression;
        }
    }
    // date: prefix
    remainder = ifStartsWithReturnRemainder("date:", function);
    if (remainder != null) {
        String[] parts = remainder.split(":", 2);
        if (parts.length == 1) {
            return ExpressionBuilder.dateExpression(parts[0]);
        } else if (parts.length == 2) {
            return ExpressionBuilder.dateExpression(parts[0], parts[1]);
        }
    }
    // date-with-timezone: prefix
    remainder = ifStartsWithReturnRemainder("date-with-timezone:", function);
    if (remainder != null) {
        String[] parts = remainder.split(":", 3);
        if (parts.length < 3) {
            throw new SimpleParserException("Valid syntax: ${date-with-timezone:command:timezone:pattern} was: " + function, token.getIndex());
        }
        return ExpressionBuilder.dateExpression(parts[0], parts[1], parts[2]);
    }
    // bean: prefix
    remainder = ifStartsWithReturnRemainder("bean:", function);
    if (remainder != null) {
        return ExpressionBuilder.beanExpression(remainder);
    }
    // properties: prefix
    remainder = ifStartsWithReturnRemainder("properties:", function);
    if (remainder != null) {
        String[] parts = remainder.split(":");
        if (parts.length > 2) {
            throw new SimpleParserException("Valid syntax: ${properties:key[:default]} was: " + function, token.getIndex());
        }
        return ExpressionBuilder.propertiesComponentExpression(remainder, null, null);
    }
    // properties-location: prefix
    remainder = ifStartsWithReturnRemainder("properties-location:", function);
    if (remainder != null) {
        String[] parts = remainder.split(":");
        if (parts.length > 3) {
            throw new SimpleParserException("Valid syntax: ${properties-location:location:key[:default]} was: " + function, token.getIndex());
        }
        String locations = null;
        String key = remainder;
        if (parts.length >= 2) {
            locations = ObjectHelper.before(remainder, ":");
            key = ObjectHelper.after(remainder, ":");
        }
        return ExpressionBuilder.propertiesComponentExpression(key, locations, null);
    }
    // ref: prefix
    remainder = ifStartsWithReturnRemainder("ref:", function);
    if (remainder != null) {
        return ExpressionBuilder.refExpression(remainder);
    }
    // const: prefix
    remainder = ifStartsWithReturnRemainder("type:", function);
    if (remainder != null) {
        Expression exp = ExpressionBuilder.typeExpression(remainder);
        // we want to cache this expression so we wont re-evaluate it as the type/constant wont change
        return ExpressionBuilder.cacheExpression(exp);
    }
    // miscellaneous functions
    Expression misc = createSimpleExpressionMisc(function);
    if (misc != null) {
        return misc;
    }
    if (strict) {
        throw new SimpleParserException("Unknown function: " + function, token.getIndex());
    } else {
        return null;
    }
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) Expression(org.apache.camel.Expression)

Example 12 with SimpleParserException

use of org.apache.camel.language.simple.types.SimpleParserException in project camel by apache.

the class BaseSimpleParser method prepareUnaryExpressions.

/**
     * Prepares unary expressions.
     * <p/>
     * This process prepares the unary expressions in the AST. This is done
     * by linking the unary operator with the left hand side node,
     * to have the AST graph updated and prepared properly.
     * <p/>
     * So when the AST node is later used to create the {@link org.apache.camel.Predicate}s
     * or {@link org.apache.camel.Expression}s to be used by Camel then the AST graph
     * has a linked and prepared graph of nodes which represent the input expression.
     */
protected void prepareUnaryExpressions() {
    Stack<SimpleNode> stack = new Stack<SimpleNode>();
    for (SimpleNode node : nodes) {
        if (node instanceof UnaryExpression) {
            UnaryExpression token = (UnaryExpression) node;
            // remember the logical operator
            String operator = token.getOperator().toString();
            SimpleNode previous = stack.isEmpty() ? null : stack.pop();
            if (previous == null) {
                throw new SimpleParserException("Unary operator " + operator + " has no left hand side token", token.getToken().getIndex());
            } else {
                token.acceptLeft(previous);
            }
        }
        stack.push(node);
    }
    // replace nodes from the stack
    nodes.clear();
    nodes.addAll(stack);
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) UnaryExpression(org.apache.camel.language.simple.ast.UnaryExpression) Stack(java.util.Stack) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Aggregations

SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)12 Expression (org.apache.camel.Expression)5 SimpleNode (org.apache.camel.language.simple.ast.SimpleNode)5 Stack (java.util.Stack)4 SimpleToken (org.apache.camel.language.simple.types.SimpleToken)2 ArrayList (java.util.ArrayList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Exchange (org.apache.camel.Exchange)1 BinaryExpression (org.apache.camel.language.simple.ast.BinaryExpression)1 Block (org.apache.camel.language.simple.ast.Block)1 BlockEnd (org.apache.camel.language.simple.ast.BlockEnd)1 BlockStart (org.apache.camel.language.simple.ast.BlockStart)1 DoubleQuoteStart (org.apache.camel.language.simple.ast.DoubleQuoteStart)1 LiteralExpression (org.apache.camel.language.simple.ast.LiteralExpression)1 LiteralNode (org.apache.camel.language.simple.ast.LiteralNode)1 LogicalExpression (org.apache.camel.language.simple.ast.LogicalExpression)1 SimpleFunctionStart (org.apache.camel.language.simple.ast.SimpleFunctionStart)1 SingleQuoteStart (org.apache.camel.language.simple.ast.SingleQuoteStart)1 UnaryExpression (org.apache.camel.language.simple.ast.UnaryExpression)1 BinaryOperatorType (org.apache.camel.language.simple.types.BinaryOperatorType)1