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     * <p/>
046     * The simple language now also includes file language out of the box which means the following expression is also
047     * supported:
048     * <ul>
049     *   <li><tt>file:name</tt> to access the file name (is relative, see note below))</li>
050     *   <li><tt>file:name.noext</tt> to access the file name with no extension</li>
051     *   <li><tt>file:ext</tt> to access the file extension</li>
052     *   <li><tt>file:onlyname</tt> to access the file name (no paths)</li>
053     *   <li><tt>file:onlyname.noext</tt> to access the file name (no paths) with no extension </li>
054     *   <li><tt>file:parent</tt> to access the parent file name</li>
055     *   <li><tt>file:path</tt> to access the file path name</li>
056     *   <li><tt>file:absolute</tt> is the file regarded as absolute or relative</li>
057     *   <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
058     *   <li><tt>file:length</tt> to access the file length as a Long type</li>
059     *   <li><tt>file:modified</tt> to access the file last modified as a Date type</li>
060     *   <li><tt>date:&lt;command&gt;:&lt;pattern&gt;</tt> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
061     *     Additional Supported commands are: <tt>file</tt> for the last modified timestamp of the file.
062     *     All the commands from {@link SimpleLanguage} is also available.
063     *   </li>
064     * </ul>
065     * The <b>relative</b> file is the filename with the starting directory clipped, as opposed to <b>path</b> that will
066     * return the full path including the starting directory.
067     * <br/>
068     * The <b>only</b> file is the filename only with all paths clipped.
069     *
070     * @version $Revision: 898656 $
071     */
072    public class SimpleLanguage extends SimpleLanguageSupport {
073    
074        private static final SimpleLanguage SIMPLE = new SimpleLanguage();
075    
076        public static Expression simple(String expression) {
077            return SIMPLE.createExpression(expression);
078        }
079    
080        protected Expression createSimpleExpression(String expression, boolean strict) {
081            if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
082                return ExpressionBuilder.bodyExpression();
083            } else if (ObjectHelper.equal(expression, "out.body")) {
084                return ExpressionBuilder.outBodyExpression();
085            } else if (ObjectHelper.equal(expression, "id")) {
086                return ExpressionBuilder.messageIdExpression();
087            } else if (ObjectHelper.equal(expression, "exception.message")) {
088                return ExpressionBuilder.exchangeExceptionMessageExpression();
089            }
090    
091            // in header expression
092            String remainder = ifStartsWithReturnRemainder("in.header.", expression);
093            if (remainder == null) {
094                remainder = ifStartsWithReturnRemainder("header.", expression);
095            }
096            if (remainder == null) {
097                remainder = ifStartsWithReturnRemainder("headers.", expression);
098            }
099            if (remainder == null) {
100                remainder = ifStartsWithReturnRemainder("in.headers.", expression);
101            }
102            if (remainder != null) {
103                return ExpressionBuilder.headerExpression(remainder);
104            }
105    
106            // out header expression
107            remainder = ifStartsWithReturnRemainder("out.header.", expression);
108            if (remainder == null) {
109                remainder = ifStartsWithReturnRemainder("out.headers.", expression);
110            }
111            if (remainder != null) {
112                return ExpressionBuilder.outHeaderExpression(remainder);
113            }
114    
115            // property
116            remainder = ifStartsWithReturnRemainder("property.", expression);
117            if (remainder != null) {
118                return ExpressionBuilder.propertyExpression(remainder);
119            }
120    
121            // system property
122            remainder = ifStartsWithReturnRemainder("sys.", expression);
123            if (remainder != null) {
124                return ExpressionBuilder.systemPropertyExpression(remainder);
125            }
126    
127            // file: prefix
128            remainder = ifStartsWithReturnRemainder("file:", expression);
129            if (remainder != null) {
130                Expression fileExpression = createSimpleFileExpression(remainder);
131                if (expression != null) {
132                    return fileExpression;
133                }
134            }
135    
136            // date: prefix
137            remainder = ifStartsWithReturnRemainder("date:", expression);
138            if (remainder != null) {
139                String[] parts = remainder.split(":");
140                if (parts.length < 2) {
141                    throw new ExpressionIllegalSyntaxException("Valid syntax: ${date:command:pattern} was: " + expression);
142                }
143                String command = ObjectHelper.before(remainder, ":");
144                String pattern = ObjectHelper.after(remainder, ":");
145                return ExpressionBuilder.dateExpression(command, pattern);
146            }
147    
148            // bean: prefix
149            remainder = ifStartsWithReturnRemainder("bean:", expression);
150            if (remainder != null) {
151                return ExpressionBuilder.beanExpression(remainder);
152            }
153    
154            if (strict) {
155                throw new ExpressionIllegalSyntaxException(expression);
156            } else {
157                return ExpressionBuilder.constantExpression(expression);
158            }
159        }
160        
161        public Expression createSimpleFileExpression(String remainder) {
162            if (ObjectHelper.equal(remainder, "name")) {
163                return ExpressionBuilder.fileNameExpression();
164            } else if (ObjectHelper.equal(remainder, "name.noext")) {
165                return ExpressionBuilder.fileNameNoExtensionExpression();
166            } else if (ObjectHelper.equal(remainder, "onlyname")) {
167                return ExpressionBuilder.fileOnlyNameExpression();
168            } else if (ObjectHelper.equal(remainder, "onlyname.noext")) {
169                return ExpressionBuilder.fileOnlyNameNoExtensionExpression();
170            } else if (ObjectHelper.equal(remainder, "ext")) {
171                return ExpressionBuilder.fileExtensionExpression();
172            } else if (ObjectHelper.equal(remainder, "parent")) {
173                return ExpressionBuilder.fileParentExpression();
174            } else if (ObjectHelper.equal(remainder, "path")) {
175                return ExpressionBuilder.filePathExpression();
176            } else if (ObjectHelper.equal(remainder, "absolute")) {
177                return ExpressionBuilder.fileAbsoluteExpression();
178            } else if (ObjectHelper.equal(remainder, "absolute.path")) {
179                return ExpressionBuilder.fileAbsolutePathExpression();
180            } else if (ObjectHelper.equal(remainder, "length")) {
181                return ExpressionBuilder.fileSizeExpression();
182            } else if (ObjectHelper.equal(remainder, "modified")) {
183                return ExpressionBuilder.fileLastModifiedExpression();
184            }
185            return null;
186        }
187    
188        public boolean isSingleton() {
189            return true;
190        }
191    }