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.text.SimpleDateFormat;
021 import java.util.Date;
022
023 import org.apache.camel.Exchange;
024 import org.apache.camel.Expression;
025 import org.apache.camel.builder.ExpressionBuilder;
026 import org.apache.camel.language.IllegalSyntaxException;
027 import org.apache.camel.language.constant.ConstantLanguage;
028
029 /**
030 * A helper class for working with <a href="http://activemq.apache.org/camel/expression.html">expressions</a> based
031 * on files.
032 * <p/>
033 * This expression expects the headers from the {@link FileLanguage} on the <b>IN</b> message.
034 *
035 * @see org.apache.camel.language.simple.FileLanguage
036 */
037 public final class FileExpressionBuilder {
038
039 private FileExpressionBuilder() {
040 // Helper class
041 }
042
043 public static <E extends Exchange> Expression<E> fileNameExpression() {
044 return new Expression<E>() {
045 public Object evaluate(E exchange) {
046 return exchange.getIn().getHeader("CamelFileName", String.class);
047 }
048
049 @Override
050 public String toString() {
051 return "file:name";
052 }
053 };
054 }
055
056 public static <E extends Exchange> Expression<E> fileNameNoExtensionExpression() {
057 return new Expression<E>() {
058 public Object evaluate(E exchange) {
059 String name = exchange.getIn().getHeader("CamelFileName", String.class);
060 if (name != null) {
061 return name.substring(0, name.lastIndexOf('.'));
062 } else {
063 return null;
064 }
065 }
066
067 @Override
068 public String toString() {
069 return "file:name.noext";
070 }
071 };
072 }
073
074 public static <E extends Exchange> Expression<E> fileParentExpression() {
075 return new Expression<E>() {
076 public Object evaluate(E exchange) {
077 return exchange.getIn().getHeader("CamelFileParent", String.class);
078 }
079
080 @Override
081 public String toString() {
082 return "file:parent";
083 }
084 };
085 }
086
087 public static <E extends Exchange> Expression<E> filePathExpression() {
088 return new Expression<E>() {
089 public Object evaluate(E exchange) {
090 return exchange.getIn().getHeader("CamelFilePath", String.class);
091 }
092
093 @Override
094 public String toString() {
095 return "file:path";
096 }
097 };
098 }
099
100 public static <E extends Exchange> Expression<E> fileAbsolutePathExpression() {
101 return new Expression<E>() {
102 public Object evaluate(E exchange) {
103 return exchange.getIn().getHeader("CamelFileAbsolutePath", String.class);
104 }
105
106 @Override
107 public String toString() {
108 return "file:absolute.path";
109 }
110 };
111 }
112
113 public static <E extends Exchange> Expression<E> fileCanoicalPathExpression() {
114 return new Expression<E>() {
115 public Object evaluate(E exchange) {
116 return exchange.getIn().getHeader("CamelFileCanonicalPath", String.class);
117 }
118
119 @Override
120 public String toString() {
121 return "file:canonical.path";
122 }
123 };
124 }
125
126 public static <E extends Exchange> Expression<E> fileSizeExpression() {
127 return new Expression<E>() {
128 public Object evaluate(E exchange) {
129 return exchange.getIn().getHeader("CamelFileLength", Long.class);
130 }
131
132 @Override
133 public String toString() {
134 return "file:length";
135 }
136 };
137 }
138
139 public static <E extends Exchange> Expression<E> dateExpression(final String command, final String pattern) {
140 return new Expression<E>() {
141 public Object evaluate(E exchange) {
142 if ("file".equals(command)) {
143 Date date = exchange.getIn().getHeader("CamelFileLastModified", Date.class);
144 if (date != null) {
145 SimpleDateFormat df = new SimpleDateFormat(pattern);
146 return df.format(date);
147 } else {
148 return null;
149 }
150 }
151 // must call evaluate to return the nested language evaluate when evaluating
152 // stacked expressions
153 return ExpressionBuilder.dateExpression(command, pattern).evaluate(exchange);
154 }
155
156 @Override
157 public String toString() {
158 return "date(" + command + ":" + pattern + ")";
159 }
160 };
161 }
162
163 public static <E extends Exchange> Expression<E> simpleExpression(final String simple) {
164 return new Expression<E>() {
165 public Object evaluate(E exchange) {
166 // must call evaluate to return the nested language evaluate when evaluating
167 // stacked expressions
168 try {
169 return SimpleLanguage.simple(simple).evaluate(exchange);
170 } catch (IllegalSyntaxException e) {
171 // fallback to constant so end users can enter a fixed filename
172 return ConstantLanguage.constant(simple).evaluate(exchange);
173 }
174 }
175
176 @Override
177 public String toString() {
178 return "simple(" + simple + ")";
179 }
180 };
181 }
182
183 }