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