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