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    package org.apache.camel.builder;
018    
019    import java.text.SimpleDateFormat;
020    import java.util.Date;
021    
022    import org.apache.camel.Exchange;
023    import org.apache.camel.Expression;
024    import org.apache.camel.impl.ExpressionAdapter;
025    import org.apache.camel.spi.Language;
026    import org.apache.camel.util.FileUtil;
027    
028    /**
029     * A helper class for working with <a href="http://camel.apache.org/expression.html">expressions</a> based
030     * on files.
031     * <p/>
032     * This expression expects the headers from the {@link org.apache.camel.language.simple.FileLanguage} on the <b>IN</b> message.
033     *
034     * @see org.apache.camel.language.simple.FileLanguage
035     * @deprecated will be removed in Camel 2.3
036     */
037    @Deprecated
038    public final class FileExpressionBuilder {
039    
040        private FileExpressionBuilder() {
041            // Helper class
042        }
043    
044        public static Expression fileNameExpression() {
045            return new ExpressionAdapter() {
046                public Object evaluate(Exchange exchange) {
047                    return exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
048                }
049    
050                @Override
051                public String toString() {
052                    return "file:name";
053                }
054            };
055        }
056    
057        public static Expression fileOnlyNameExpression() {
058            return new ExpressionAdapter() {
059                public Object evaluate(Exchange exchange) {
060                    String answer = exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class);
061                    if (answer == null) {
062                        answer = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
063                        answer = FileUtil.stripPath(answer);
064                    }
065                    return answer;
066                }
067    
068                @Override
069                public String toString() {
070                    return "file:onlyname";
071                }
072            };
073        }
074    
075        public static Expression fileNameNoExtensionExpression() {
076            return new ExpressionAdapter() {
077                public Object evaluate(Exchange exchange) {
078                    String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
079                    return FileUtil.stripExt(name);
080                }
081    
082                @Override
083                public String toString() {
084                    return "file:name.noext";
085                }
086            };
087        }
088    
089        public static Expression fileOnlyNameNoExtensionExpression() {
090            return new ExpressionAdapter() {
091                public Object evaluate(Exchange exchange) {
092                    String name = fileOnlyNameExpression().evaluate(exchange, String.class);
093                    return FileUtil.stripExt(name);
094                }
095    
096                @Override
097                public String toString() {
098                    return "file:onlyname.noext";
099                }
100            };
101        }
102    
103        public static Expression fileExtensionExpression() {
104            return new ExpressionAdapter() {
105                public Object evaluate(Exchange exchange) {
106                    String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
107                    if (name != null) {
108                        return name.substring(name.lastIndexOf('.') + 1);
109                    } else {
110                        return null;
111                    }
112                }
113    
114                @Override
115                public String toString() {
116                    return "file:ext";
117                }
118            };
119        }
120    
121        public static Expression fileParentExpression() {
122            return new ExpressionAdapter() {
123                public Object evaluate(Exchange exchange) {
124                    return exchange.getIn().getHeader("CamelFileParent", String.class);
125                }
126    
127                @Override
128                public String toString() {
129                    return "file:parent";
130                }
131            };
132        }
133    
134        public static Expression filePathExpression() {
135            return new ExpressionAdapter() {
136                public Object evaluate(Exchange exchange) {
137                    return exchange.getIn().getHeader("CamelFilePath", String.class);
138                }
139    
140                @Override
141                public String toString() {
142                    return "file:path";
143                }
144            };
145        }
146    
147        public static Expression fileAbsolutePathExpression() {
148            return new ExpressionAdapter() {
149                public Object evaluate(Exchange exchange) {
150                    return exchange.getIn().getHeader("CamelFileAbsolutePath", String.class);
151                }
152    
153                @Override
154                public String toString() {
155                    return "file:absolute.path";
156                }
157            };
158        }
159    
160        public static Expression fileAbsoluteExpression() {
161            return new ExpressionAdapter() {
162                public Object evaluate(Exchange exchange) {
163                    return exchange.getIn().getHeader("CamelFileAbsolute", Boolean.class);
164                }
165    
166                @Override
167                public String toString() {
168                    return "file:absolute";
169                }
170            };
171        }
172    
173        public static Expression fileSizeExpression() {
174            return new ExpressionAdapter() {
175                public Object evaluate(Exchange exchange) {
176                    return exchange.getIn().getHeader("CamelFileLength", Long.class);
177                }
178    
179                @Override
180                public String toString() {
181                    return "file:length";
182                }
183            };
184        }
185    
186        public static Expression fileLastModifiedExpression() {
187            return new ExpressionAdapter() {
188                public Object evaluate(Exchange exchange) {
189                    return exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Date.class);
190                }
191    
192                @Override
193                public String toString() {
194                    return "file:modified";
195                }
196            };
197        }
198    
199    
200        public static Expression dateExpression(final String command, final String pattern) {
201            return new ExpressionAdapter() {
202                public Object evaluate(Exchange exchange) {
203                    if ("file".equals(command)) {
204                        Date date = exchange.getIn().getHeader(Exchange.FILE_LAST_MODIFIED, Date.class);
205                        if (date != null) {
206                            SimpleDateFormat df = new SimpleDateFormat(pattern);
207                            return df.format(date);
208                        } else {
209                            return null;
210                        }
211                    }
212                    // must call evaluate to return the nested language evaluate when evaluating
213                    // stacked expressions
214                    return ExpressionBuilder.dateExpression(command, pattern).evaluate(exchange, Object.class);
215                }
216    
217                @Override
218                public String toString() {
219                    return "date(" + command + ":" + pattern + ")";
220                }
221            };
222        }
223    
224        public static Expression simpleExpression(final String expression) {
225            return new ExpressionAdapter() {
226                public Object evaluate(Exchange exchange) {
227                    // must call evaluate to return the nested language evaluate when evaluating
228                    // stacked expressions
229                    Language simple = exchange.getContext().resolveLanguage("simple");
230                    return simple.createExpression(expression).evaluate(exchange, Object.class);
231                }
232    
233                @Override
234                public String toString() {
235                    return "simple(" + expression + ")";
236                }
237            };
238        }
239    
240    }