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