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 018 package org.apache.camel.language.simple; 019 020 import java.io.IOException; 021 import java.text.SimpleDateFormat; 022 import java.util.Date; 023 024 import org.apache.camel.Expression; 025 import org.apache.camel.RuntimeCamelException; 026 import org.apache.camel.component.file.FileExchange; 027 import org.apache.camel.language.bean.BeanLanguage; 028 029 /** 030 * A helper class for working with <a href="http://activemq.apache.org/camel/expression.html">expressions</a> based 031 * on FileExchange. 032 */ 033 public final class FileExpressionBuilder { 034 private FileExpressionBuilder() { 035 // Helper class 036 } 037 038 public static <E extends FileExchange> Expression<E> fileNameExpression() { 039 return new Expression<E>() { 040 public Object evaluate(E exchange) { 041 if (exchange.getFile() == null) { 042 return null; 043 } 044 return exchange.getFile().getName(); 045 } 046 047 @Override 048 public String toString() { 049 return "file:name"; 050 } 051 }; 052 } 053 054 public static <E extends FileExchange> Expression<E> fileNameNoExtensionExpression() { 055 return new Expression<E>() { 056 public Object evaluate(E exchange) { 057 if (exchange.getFile() == null) { 058 return null; 059 } 060 String name = exchange.getFile().getName(); 061 return name.substring(0, name.lastIndexOf('.')); 062 } 063 064 @Override 065 public String toString() { 066 return "file:name.noext"; 067 } 068 }; 069 } 070 071 public static <E extends FileExchange> Expression<E> fileParentExpression() { 072 return new Expression<E>() { 073 public Object evaluate(E exchange) { 074 if (exchange.getFile() == null) { 075 return null; 076 } 077 return exchange.getFile().getParent(); 078 } 079 080 @Override 081 public String toString() { 082 return "file:parent"; 083 } 084 }; 085 } 086 087 public static <E extends FileExchange> Expression<E> filePathExpression() { 088 return new Expression<E>() { 089 public Object evaluate(E exchange) { 090 if (exchange.getFile() == null) { 091 return null; 092 } 093 return exchange.getFile().getPath(); 094 } 095 096 @Override 097 public String toString() { 098 return "file:path"; 099 } 100 }; 101 } 102 103 public static <E extends FileExchange> Expression<E> fileAbsoluteExpression() { 104 return new Expression<E>() { 105 public Object evaluate(E exchange) { 106 if (exchange.getFile() == null) { 107 return null; 108 } 109 return exchange.getFile().getAbsolutePath(); 110 } 111 112 @Override 113 public String toString() { 114 return "file:absolute"; 115 } 116 }; 117 } 118 119 public static <E extends FileExchange> Expression<E> fileCanoicalPathExpression() { 120 return new Expression<E>() { 121 public Object evaluate(E exchange) { 122 if (exchange.getFile() == null) { 123 return null; 124 } 125 try { 126 return exchange.getFile().getCanonicalPath(); 127 } catch (IOException e) { 128 throw new RuntimeCamelException("Could not get the canonical path for file: " + exchange.getFile(), e); 129 } 130 } 131 132 @Override 133 public String toString() { 134 return "file:canonical.path"; 135 } 136 }; 137 } 138 139 public static <E extends FileExchange> Expression<E> dateExpression(final String command, final String pattern) { 140 return new Expression<E>() { 141 public Object evaluate(E exchange) { 142 Date date; 143 if ("file".equals(command)) { 144 if (exchange.getFile() == null) { 145 return null; 146 } 147 date = new Date(exchange.getFile().lastModified()); 148 } else if ("now".equals(command)) { 149 date = new Date(); 150 } else if (command.startsWith("header.") || command.startsWith("in.header.")) { 151 String key = command.substring(command.lastIndexOf(".") + 1); 152 date = exchange.getIn().getHeader(key, Date.class); 153 if (date == null) { 154 throw new IllegalArgumentException("Could not find java.util.Date object at " + command); 155 } 156 } else if (command.startsWith("out.header.")) { 157 String key = command.substring(command.lastIndexOf(".") + 1); 158 date = exchange.getOut().getHeader(key, Date.class); 159 if (date == null) { 160 throw new IllegalArgumentException("Could not find java.util.Date object at " + command); 161 } 162 } else { 163 throw new IllegalArgumentException("Command not supported for dateExpression: " + command); 164 } 165 166 SimpleDateFormat df = new SimpleDateFormat(pattern); 167 return df.format(date); 168 } 169 170 @Override 171 public String toString() { 172 return "date(" + command + ":" + pattern + ")"; 173 } 174 }; 175 } 176 177 public static <E extends FileExchange> Expression<E> simpleExpression(final String simple) { 178 return new Expression<E>() { 179 public Object evaluate(E exchange) { 180 // must call evalute to return the nested langauge evaluate when evaluating 181 // stacked expressions 182 return SimpleLanguage.simple(simple).evaluate(exchange); 183 } 184 185 @Override 186 public String toString() { 187 return "simple(" + simple + ")"; 188 } 189 }; 190 } 191 192 public static <E extends FileExchange> Expression<E> beanExpression(final String bean) { 193 return new Expression<E>() { 194 public Object evaluate(E exchange) { 195 // must call evalute to return the nested langauge evaluate when evaluating 196 // stacked expressions 197 return BeanLanguage.bean(bean).evaluate(exchange); 198 } 199 200 @Override 201 public String toString() { 202 return "bean(" + bean + ")"; 203 } 204 }; 205 } 206 207 }