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.lastIndexOf(".") != -1) {
061 return name.substring(0, name.lastIndexOf('.'));
062 } else {
063 // name does not have extension
064 return name;
065 }
066 }
067
068 @Override
069 public String toString() {
070 return "file:name.noext";
071 }
072 };
073 }
074
075 public static <E extends Exchange> Expression<E> fileParentExpression() {
076 return new Expression<E>() {
077 public Object evaluate(E exchange) {
078 return exchange.getIn().getHeader("CamelFileParent", String.class);
079 }
080
081 @Override
082 public String toString() {
083 return "file:parent";
084 }
085 };
086 }
087
088 public static <E extends Exchange> Expression<E> filePathExpression() {
089 return new Expression<E>() {
090 public Object evaluate(E exchange) {
091 return exchange.getIn().getHeader("CamelFilePath", String.class);
092 }
093
094 @Override
095 public String toString() {
096 return "file:path";
097 }
098 };
099 }
100
101 public static <E extends Exchange> Expression<E> fileAbsolutePathExpression() {
102 return new Expression<E>() {
103 public Object evaluate(E exchange) {
104 return exchange.getIn().getHeader("CamelFileAbsolutePath", String.class);
105 }
106
107 @Override
108 public String toString() {
109 return "file:absolute.path";
110 }
111 };
112 }
113
114 public static <E extends Exchange> Expression<E> fileCanoicalPathExpression() {
115 return new Expression<E>() {
116 public Object evaluate(E exchange) {
117 return exchange.getIn().getHeader("CamelFileCanonicalPath", String.class);
118 }
119
120 @Override
121 public String toString() {
122 return "file:canonical.path";
123 }
124 };
125 }
126
127 public static <E extends Exchange> Expression<E> fileSizeExpression() {
128 return new Expression<E>() {
129 public Object evaluate(E exchange) {
130 return exchange.getIn().getHeader("CamelFileLength", Long.class);
131 }
132
133 @Override
134 public String toString() {
135 return "file:length";
136 }
137 };
138 }
139
140 public static <E extends Exchange> Expression<E> dateExpression(final String command, final String pattern) {
141 return new Expression<E>() {
142 public Object evaluate(E exchange) {
143 if ("file".equals(command)) {
144 Date date = exchange.getIn().getHeader("CamelFileLastModified", Date.class);
145 if (date != null) {
146 SimpleDateFormat df = new SimpleDateFormat(pattern);
147 return df.format(date);
148 } else {
149 return null;
150 }
151 }
152 // must call evaluate to return the nested language evaluate when evaluating
153 // stacked expressions
154 return ExpressionBuilder.dateExpression(command, pattern).evaluate(exchange);
155 }
156
157 @Override
158 public String toString() {
159 return "date(" + command + ":" + pattern + ")";
160 }
161 };
162 }
163
164 public static <E extends Exchange> Expression<E> simpleExpression(final String simple) {
165 return new Expression<E>() {
166 public Object evaluate(E exchange) {
167 // must call evaluate to return the nested language evaluate when evaluating
168 // stacked expressions
169 try {
170 return SimpleLanguage.simple(simple).evaluate(exchange);
171 } catch (IllegalSyntaxException e) {
172 // fallback to constant so end users can enter a fixed filename
173 return ConstantLanguage.constant(simple).evaluate(exchange);
174 }
175 }
176
177 @Override
178 public String toString() {
179 return "simple(" + simple + ")";
180 }
181 };
182 }
183
184 }