001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.language.simple;
018    
019    import org.apache.camel.Expression;
020    import org.apache.camel.Predicate;
021    import org.apache.camel.language.simple.ast.SimpleFunctionExpression;
022    import org.apache.camel.language.simple.types.SimpleToken;
023    import org.apache.camel.language.simple.types.SimpleTokenType;
024    import org.apache.camel.language.simple.types.TokenType;
025    import org.apache.camel.util.ExpressionToPredicateAdapter;
026    
027    /**
028     * A backwards compatible parser, which supports the old simple language
029     * syntax by which simple functions can be given without using start and
030     * end tokens.
031     * <p/>
032     * For example "body" would be parsed as the body function, where as the
033     * new parser would require that to be entered as "${body}".
034     * <p/>
035     * This parser is to be removed when the old syntax is no longer supported.
036     *
037     * @deprecated will be removed in Camel 3.0
038     */
039    @Deprecated
040    public final class SimpleBackwardsCompatibleParser {
041    
042        private SimpleBackwardsCompatibleParser() {
043            // static methods
044        }
045    
046        public static Expression parseExpression(String expression, boolean allowEscape) {
047            return doParseExpression(expression, allowEscape);
048        }
049    
050        public static Predicate parsePredicate(String expression, boolean allowEscape) {
051            Expression answer = doParseExpression(expression, allowEscape);
052            if (answer != null) {
053                return ExpressionToPredicateAdapter.toPredicate(answer);
054            } else {
055                return null;
056            }
057        }
058    
059        private static Expression doParseExpression(String expression, boolean allowEscape) {
060            // should have no function tokens
061            for (int i = 0; i < expression.length(); i++) {
062                SimpleToken token = SimpleTokenizer.nextToken(expression, i, allowEscape, TokenType.functionStart, TokenType.functionEnd);
063                if (token.getType().getType() == TokenType.functionStart || token.getType().getType() == TokenType.functionEnd) {
064                    return null;
065                }
066            }
067    
068            // okay there is no function tokens, then try to parse it as a simple function expression
069            SimpleToken token = new SimpleToken(new SimpleTokenType(TokenType.functionStart, expression), 0);
070            SimpleFunctionExpression function = new SimpleFunctionExpression(token);
071            function.addText(expression);
072            return function.createExpression(expression, false);
073        }
074    
075    }