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.FileExpressionBuilder;
022    import org.apache.camel.util.ObjectHelper;
023    
024    /**
025     * File language is an extension to Simple language to add file specific expressions.
026     *
027     * Examples of supported file expressions are:
028     * <ul>
029     *   <li><tt>file:name</tt> to access the file name (is relative, see note below))</li>
030     *   <li><tt>file:name.noext</tt> to access the file name with no extension</li>
031     *   <li><tt>file:ext</tt> to access the file extension</li>
032     *   <li><tt>file:onlyname</tt> to access the file name (no paths)</li>
033     *   <li><tt>file:onlyname.noext</tt> to access the file name (no paths) with no extension </li>
034     *   <li><tt>file:parent</tt> to access the parent file name</li>
035     *   <li><tt>file:path</tt> to access the file path name</li>
036     *   <li><tt>file:absolute</tt> is the file regarded as absolute or relative</li>
037     *   <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
038     *   <li><tt>file:length</tt> to access the file length as a Long type</li>
039     *   <li><tt>file:modified</tt> to access the file last modified as a Date type</li>
040     *   <li><tt>date:&lt;command&gt;:&lt;pattern&gt;</tt> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
041     *     Additional Supported commands are: <tt>file</tt> for the last modified timestamp of the file.
042     *     All the commands from {@link SimpleLanguage} is also avaiable.
043     *   </li>
044     * </ul>
045     * The <b>relative</b> file is the filename with the starting directory clipped, as opposed to <b>path</b> that will
046     * return the full path including the starting directory.
047     * <br/>
048     * The <b>only</b> file is the filename only with all paths clipped.
049     * <br/>
050      * All the simple expression is also available so you can eg use <tt>${in.header.foo}</tt> to access the foo header.
051     *
052     * @see org.apache.camel.language.simple.SimpleLanguage
053     * @see org.apache.camel.language.bean.BeanLanguage
054     */
055    public class FileLanguage extends SimpleLanguageSupport {
056    
057        public static Expression file(String expression) {
058            FileLanguage language = new FileLanguage();
059            return language.createExpression(expression);
060        }
061    
062        protected Expression createSimpleExpression(String expression) {
063    
064            // file: prefix
065            String remainder = ifStartsWithReturnRemainder("file:", expression);
066            if (remainder != null) {
067                if (ObjectHelper.equal(remainder, "name")) {
068                    return FileExpressionBuilder.fileNameExpression();
069                } else if (ObjectHelper.equal(remainder, "name.noext")) {
070                    return FileExpressionBuilder.fileNameNoExtensionExpression();
071                } else if (ObjectHelper.equal(remainder, "onlyname")) {
072                    return FileExpressionBuilder.fileOnlyNameExpression();
073                } else if (ObjectHelper.equal(remainder, "onlyname.noext")) {
074                    return FileExpressionBuilder.fileOnlyNameNoExtensionExpression();
075                } else if (ObjectHelper.equal(remainder, "ext")) {
076                    return FileExpressionBuilder.fileExtensionExpression();
077                } else if (ObjectHelper.equal(remainder, "parent")) {
078                    return FileExpressionBuilder.fileParentExpression();
079                } else if (ObjectHelper.equal(remainder, "path")) {
080                    return FileExpressionBuilder.filePathExpression();
081                } else if (ObjectHelper.equal(remainder, "absolute")) {
082                    return FileExpressionBuilder.fileAbsoluteExpression();
083                } else if (ObjectHelper.equal(remainder, "absolute.path")) {
084                    return FileExpressionBuilder.fileAbsolutePathExpression();
085                } else if (ObjectHelper.equal(remainder, "length")) {
086                    return FileExpressionBuilder.fileSizeExpression();
087                } else if (ObjectHelper.equal(remainder, "modified")) {
088                    return FileExpressionBuilder.fileLastModifiedExpression();
089                }
090            }
091    
092            // date: prefix
093            remainder = ifStartsWithReturnRemainder("date:", expression);
094            if (remainder != null) {
095                String[] parts = remainder.split(":");
096                if (parts.length != 2) {
097                    throw new ExpressionIllegalSyntaxException("Valid syntax: ${date:command:pattern} was: " + expression);
098                }
099                String command = parts[0];
100                String pattern = parts[1];
101                return FileExpressionBuilder.dateExpression(command, pattern);
102            }
103    
104            // fallback to simple language if not file specific
105            return FileExpressionBuilder.simpleExpression(expression);
106        }
107    
108        public boolean isSingleton() {
109            return true;
110        }
111    }