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.ExpressionIllegalSyntaxException;
021    import org.apache.camel.builder.ExpressionBuilder;
022    import org.apache.camel.util.ObjectHelper;
023    
024    /**
025     * A <a href="http://camel.apache.org/simple.html">simple language</a>
026     * which maps simple property style notations to access headers and bodies.
027     * Examples of supported expressions are:
028     * <ul>
029     * <li>id to access the inbound message id</li>
030     * <li>in.body or body to access the inbound body</li>
031     * <li>out.body to access the inbound body</li>
032     * <li>in.header.foo or header.foo to access an inbound header called 'foo'</li>
033     * <li>out.header.foo to access an outbound header called 'foo'</li>
034     * <li>property.foo to access the exchange property called 'foo'</li>
035     * <li>sys.foo to access the system property called 'foo'</li>
036     * <li>exception.messsage to access the exception message</li>
037     * <li>date:&lt;command&gt;:&lt;pattern&gt; for date formatting using the {@link java.text.SimpleDateFormat} patterns.
038     *     Supported commands are: <tt>now</tt> for current timestamp,
039     *     <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header.
040     *     <tt>out.header.xxx</tt> to use the Date object in the out header.
041     * </li>
042     * <li>bean:&lt;bean expression&gt; to invoke a bean using the
043     * {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li>
044     * </ul>
045     *
046     * @version $Revision: 752893 $
047     */
048    public class SimpleLanguage extends SimpleLanguageSupport {
049    
050        public static Expression simple(String expression) {
051            SimpleLanguage language = new SimpleLanguage();
052            return language.createExpression(expression);
053        }
054    
055        protected Expression createSimpleExpression(String expression) {
056            if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
057                return ExpressionBuilder.bodyExpression();
058            } else if (ObjectHelper.equal(expression, "out.body")) {
059                return ExpressionBuilder.outBodyExpression();
060            } else if (ObjectHelper.equal(expression, "id")) {
061                return ExpressionBuilder.messageIdExpression();
062            } else if (ObjectHelper.equal(expression, "exception.message")) {
063                return ExpressionBuilder.exchangeExceptionMessageExpression();
064            }
065    
066            // in header expression
067            String remainder = ifStartsWithReturnRemainder("in.header.", expression);
068            if (remainder == null) {
069                remainder = ifStartsWithReturnRemainder("header.", expression);
070            }
071            if (remainder == null) {
072                remainder = ifStartsWithReturnRemainder("headers.", expression);
073            }
074            if (remainder == null) {
075                remainder = ifStartsWithReturnRemainder("in.headers.", expression);
076            }
077            if (remainder != null) {
078                return ExpressionBuilder.headerExpression(remainder);
079            }
080    
081            // out header expression
082            remainder = ifStartsWithReturnRemainder("out.header.", expression);
083            if (remainder == null) {
084                remainder = ifStartsWithReturnRemainder("out.headers.", expression);
085            }
086            if (remainder != null) {
087                return ExpressionBuilder.outHeaderExpression(remainder);
088            }
089    
090            // property
091            remainder = ifStartsWithReturnRemainder("property.", expression);
092            if (remainder != null) {
093                return ExpressionBuilder.propertyExpression(remainder);
094            }
095    
096            // system property
097            remainder = ifStartsWithReturnRemainder("sys.", expression);
098            if (remainder != null) {
099                return ExpressionBuilder.systemPropertyExpression(remainder);
100            }
101    
102            // date: prefix
103            remainder = ifStartsWithReturnRemainder("date:", expression);
104            if (remainder != null) {
105                String[] parts = remainder.split(":");
106                if (parts.length != 2) {
107                    throw new ExpressionIllegalSyntaxException("Valid syntax: ${date:command:pattern} was: " + expression);
108                }
109                String command = parts[0];
110                String pattern = parts[1];
111                return ExpressionBuilder.dateExpression(command, pattern);
112            }
113    
114            // bean: prefix
115            remainder = ifStartsWithReturnRemainder("bean:", expression);
116            if (remainder != null) {
117                return ExpressionBuilder.beanExpression(remainder);
118            }
119    
120            throw new ExpressionIllegalSyntaxException(expression);
121        }
122    
123        public boolean isSingleton() {
124            return true;
125        }
126    }