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