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.Exchange;
020    import org.apache.camel.Expression;
021    import org.apache.camel.Predicate;
022    import org.apache.camel.builder.ExpressionBuilder;
023    import org.apache.camel.builder.PredicateBuilder;
024    import org.apache.camel.language.IllegalSyntaxException;
025    import org.apache.camel.spi.Language;
026    import org.apache.camel.util.ObjectHelper;
027    
028    /**
029     * A <a href="http://activemq.apache.org/camel/simple.html>simple language</a>
030     * which maps simple property style notations to acces headers and bodies.
031     * Examples of supported expressions are <p/>
032     * <ul>
033     * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
034     * <li>in.body or body to access the inbound body</li>
035     * <li>out.header.foo to access an outbound header called 'foo'</li>
036     * <li>out.body to access the inbound body</li>
037     * <li>property.foo to access the exchange property called 'foo'</li>
038     * <li>sys.foo to access the system property called 'foo'</li>
039     * </ul>
040     * 
041     * @version $Revision: $
042     */
043    public class SimpleLanguage implements Language {
044    
045        public Predicate<Exchange> createPredicate(String expression) {
046            return PredicateBuilder.toPredicate(createExpression(expression));
047        }
048    
049        public Expression<Exchange> createExpression(String expression) {
050            if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
051                return ExpressionBuilder.bodyExpression();
052            } else if (ObjectHelper.equals(expression, "out.body")) {
053                return ExpressionBuilder.outBodyExpression();
054            }
055            String remainder = ifStartsWithReturnRemainder("in.header.", expression);
056            if (remainder == null) {
057                remainder = ifStartsWithReturnRemainder("header.", expression);
058            }
059            if (remainder != null) {
060                return ExpressionBuilder.headerExpression(remainder);
061            }
062            remainder = ifStartsWithReturnRemainder("out.header.", expression);
063            if (remainder != null) {
064                return ExpressionBuilder.outHeaderExpression(remainder);
065            }
066            remainder = ifStartsWithReturnRemainder("property.", expression);
067            if (remainder != null) {
068                return ExpressionBuilder.propertyExpression(remainder);
069            }
070            remainder = ifStartsWithReturnRemainder("sys.", expression);
071            if (remainder != null) {
072                return ExpressionBuilder.propertyExpression(remainder);
073            }
074            throw new IllegalSyntaxException(this, expression);
075        }
076    
077        protected String ifStartsWithReturnRemainder(String prefix, String text) {
078            if (text.startsWith(prefix)) {
079                String remainder = text.substring(prefix.length());
080                if (remainder.length() > 0) {
081    
082                    return remainder;
083                }
084            }
085            return null;
086        }
087    }