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.component.file.FileExchange;
021 import org.apache.camel.language.IllegalSyntaxException;
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</li>
030 * <li><tt>file:name.noext</tt> to access the file name with no extension</li>
031 * <li><tt>file:parent</tt> to access the parent file name</li>
032 * <li><tt>file:path</tt> to access the file path name</li>
033 * <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
034 * <li><tt>file:canonical.path</tt> to access the canonical path name</li>
035 * <li><tt>file:length</tt> to access the file length as a Long type</li>
036 * <li><tt>date:<command>:<pattern></tt> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
037 * Additional Supported commands are: <tt>file</tt> for the last modified timestamp of the file.
038 * All the commands from {@link SimpleLanguage} is also avaiable.
039 * </li>
040 * </ul>
041 * All the simple expression is also available so you can eg use <tt>${in.header.foo}</tt> to access the foo header.
042 *
043 * @see org.apache.camel.language.simple.SimpleLanguage
044 * @see org.apache.camel.language.bean.BeanLanguage
045 */
046 public class FileLanguage extends AbstractSimpleLanguage {
047
048 public static Expression file(String expression) {
049 FileLanguage language = new FileLanguage();
050 return language.createExpression(expression);
051 }
052
053 protected Expression<FileExchange> createSimpleExpression(String expression) {
054
055 // file: prefix
056 String remainder = ifStartsWithReturnRemainder("file:", expression);
057 if (remainder != null) {
058 if (ObjectHelper.equal(remainder, "name")) {
059 return FileExpressionBuilder.fileNameExpression();
060 } else if (ObjectHelper.equal(remainder, "name.noext")) {
061 return FileExpressionBuilder.fileNameNoExtensionExpression();
062 } else if (ObjectHelper.equal(remainder, "ext")) {
063 return FileExpressionBuilder.fileExtensionExpression();
064 } else if (ObjectHelper.equal(remainder, "parent")) {
065 return FileExpressionBuilder.fileParentExpression();
066 } else if (ObjectHelper.equal(remainder, "path")) {
067 return FileExpressionBuilder.filePathExpression();
068 } else if (ObjectHelper.equal(remainder, "absolute.path")) {
069 return FileExpressionBuilder.fileAbsolutePathExpression();
070 } else if (ObjectHelper.equal(remainder, "canonical.path")) {
071 return FileExpressionBuilder.fileCanoicalPathExpression();
072 } else if (ObjectHelper.equal(remainder, "length")) {
073 return FileExpressionBuilder.fileSizeExpression();
074 }
075 }
076
077 // date: prefix
078 remainder = ifStartsWithReturnRemainder("date:", expression);
079 if (remainder != null) {
080 String[] parts = remainder.split(":");
081 if (parts.length != 2) {
082 throw new IllegalSyntaxException(this, expression + " ${date:command:pattern} is the correct syntax.");
083 }
084 String command = parts[0];
085 String pattern = parts[1];
086 return FileExpressionBuilder.dateExpression(command, pattern);
087 }
088
089 // fallback to simple language if not file specific
090 return FileExpressionBuilder.simpleExpression(expression);
091 }
092
093 }