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:<command>:<pattern></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 private static final SimpleLanguage SIMPLE = new SimpleLanguage(); 058 059 public static Expression file(String expression) { 060 FileLanguage language = new FileLanguage(); 061 return language.createExpression(expression); 062 } 063 064 protected Expression createSimpleExpression(String expression, boolean strict) { 065 066 // file: prefix 067 String remainder = ifStartsWithReturnRemainder("file:", expression); 068 if (remainder != null) { 069 if (ObjectHelper.equal(remainder, "name")) { 070 return FileExpressionBuilder.fileNameExpression(); 071 } else if (ObjectHelper.equal(remainder, "name.noext")) { 072 return FileExpressionBuilder.fileNameNoExtensionExpression(); 073 } else if (ObjectHelper.equal(remainder, "onlyname")) { 074 return FileExpressionBuilder.fileOnlyNameExpression(); 075 } else if (ObjectHelper.equal(remainder, "onlyname.noext")) { 076 return FileExpressionBuilder.fileOnlyNameNoExtensionExpression(); 077 } else if (ObjectHelper.equal(remainder, "ext")) { 078 return FileExpressionBuilder.fileExtensionExpression(); 079 } else if (ObjectHelper.equal(remainder, "parent")) { 080 return FileExpressionBuilder.fileParentExpression(); 081 } else if (ObjectHelper.equal(remainder, "path")) { 082 return FileExpressionBuilder.filePathExpression(); 083 } else if (ObjectHelper.equal(remainder, "absolute")) { 084 return FileExpressionBuilder.fileAbsoluteExpression(); 085 } else if (ObjectHelper.equal(remainder, "absolute.path")) { 086 return FileExpressionBuilder.fileAbsolutePathExpression(); 087 } else if (ObjectHelper.equal(remainder, "length")) { 088 return FileExpressionBuilder.fileSizeExpression(); 089 } else if (ObjectHelper.equal(remainder, "modified")) { 090 return FileExpressionBuilder.fileLastModifiedExpression(); 091 } 092 } 093 094 // date: prefix 095 remainder = ifStartsWithReturnRemainder("date:", expression); 096 if (remainder != null) { 097 String[] parts = remainder.split(":"); 098 if (parts.length != 2) { 099 throw new ExpressionIllegalSyntaxException("Valid syntax: ${date:command:pattern} was: " + expression); 100 } 101 String command = parts[0]; 102 String pattern = parts[1]; 103 return FileExpressionBuilder.dateExpression(command, pattern); 104 } 105 106 // fallback to simple language if not file specific 107 return SIMPLE.createSimpleExpression(expression, strict); 108 } 109 110 public boolean isSingleton() { 111 return true; 112 } 113 }