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