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 java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Expression;
024    import org.apache.camel.Predicate;
025    import org.apache.camel.builder.ExpressionBuilder;
026    import org.apache.camel.builder.PredicateBuilder;
027    import org.apache.camel.spi.Language;
028    
029    /**
030     * Abstract base class for Simple languages.
031     */
032    public abstract class AbstractSimpleLanguage implements Language {
033        
034        public Predicate<Exchange> createPredicate(String expression) {
035            return PredicateBuilder.toPredicate(createExpression(expression));
036        }
037    
038        public Expression<Exchange> createExpression(String expression) {
039            if (expression.indexOf("${") >= 0) {
040                return createComplexExpression(expression);
041            }
042            return createSimpleExpression(expression);
043        }
044    
045        protected Expression<Exchange> createComplexExpression(String expression) {
046            List<Expression> results = new ArrayList<Expression>();
047    
048            int pivot = 0;
049            int size = expression.length();
050            while (pivot < size) {
051                int idx = expression.indexOf("${", pivot);
052                if (idx < 0) {
053                    results.add(createConstantExpression(expression, pivot, size));
054                    break;
055                } else {
056                    if (pivot < idx) {
057                        results.add(createConstantExpression(expression, pivot, idx));
058                    }
059                    pivot = idx + 2;
060                    int endIdx = expression.indexOf("}", pivot);
061                    if (endIdx < 0) {
062                        throw new IllegalArgumentException("Expecting } but found end of string for simple expression: " + expression);
063                    }
064                    String simpleText = expression.substring(pivot, endIdx);
065    
066                    Expression simpleExpression = createSimpleExpression(simpleText);
067                    results.add(simpleExpression);
068                    pivot = endIdx + 1;
069                }
070            }
071            return ExpressionBuilder.concatExpression(results, expression);
072        }
073    
074        protected Expression createConstantExpression(String expression, int start, int end) {
075            return ExpressionBuilder.constantExpression(expression.substring(start, end));
076        }
077    
078        /**
079         * Creates the simple expression based on the extracted content from the ${ } place holders
080         *
081         * @param expression  the content between ${ and }
082         * @return the expression
083         */
084        protected abstract <E extends Exchange> Expression<Exchange> createSimpleExpression(String expression);
085    
086        protected String ifStartsWithReturnRemainder(String prefix, String text) {
087            if (text.startsWith(prefix)) {
088                String remainder = text.substring(prefix.length());
089                if (remainder.length() > 0) {
090                    return remainder;
091                }
092            }
093            return null;
094        }
095    }