Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ExpressionFilenameParser |
|
| 1.75;1.75 | ||||
ExpressionFilenameParser$1 |
|
| 1.75;1.75 |
1 | /* | |
2 | * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com | |
3 | * The software in this package is published under the terms of the CPAL v1.0 | |
4 | * license, a copy of which has been included with this distribution in the | |
5 | * LICENSE.txt file. | |
6 | */ | |
7 | package org.mule.transport.file; | |
8 | ||
9 | import org.mule.api.MuleContext; | |
10 | import org.mule.api.MuleMessage; | |
11 | import org.mule.api.context.MuleContextAware; | |
12 | import org.mule.api.expression.ExpressionManager; | |
13 | import org.mule.util.TemplateParser; | |
14 | ||
15 | import java.text.MessageFormat; | |
16 | ||
17 | /** | |
18 | * <code>ExpressionFilenameParser</code> can use any expression language supported by Mule | |
19 | * to construct a file name for the current message. Expressions can be xpath, xquery, ognl, mvel, | |
20 | * header, function and more. For more information see http://www.mulesoft.org/documentation/display/MULE3USER/Using+Expressions. | |
21 | * <p/> | |
22 | * For example an xpath expression can be defined to pull a message id out of an xml message and | |
23 | * use that as the file name - | |
24 | * <code> | |
25 | * #[xpath:/message/header/@id] | |
26 | * </code> | |
27 | * <p/> | |
28 | * This parser superseeds the (now removed) <code>org.mule.transport.file.SimpleFilenameParser</code> | |
29 | * which has been kept in Mule 2 for compatibility. The following demonstrates how to achieve the | |
30 | * same results when using the <code>ExpressionFilenameParser</code> over the | |
31 | * <code>SimpleFilenameParser</code> | |
32 | * <ul> | |
33 | * <li>#[DATE] : #[function:datestamp]</li> | |
34 | * <li>#[DATE:yy-MM-dd] : #[function:datestamp-yy-MM-dd]</li> | |
35 | * <li>#[SYSTIME] : #[function:systime]</li> | |
36 | * <li>#[UUID] : #[function:uuid]</li> | |
37 | * <li>#[ORIGINALNAME] : #[header:originalFilename]</li> | |
38 | * <li>#[COUNT] : #[function:count] - note that this is a global counter.</li> | |
39 | * <li>#[<Message Property Name>] : #[header:<Message Property Name>]</li> | |
40 | * </ul> | |
41 | */ | |
42 | 0 | public class ExpressionFilenameParser implements FilenameParser, MuleContextAware |
43 | { | |
44 | public static final String DEFAULT_DATE_FORMAT = "dd-MM-yy_HH-mm-ss.SSS"; | |
45 | 0 | public static final String DEFAULT_EXPRESSION = MessageFormat.format("{0}function:uuid{1}.dat", |
46 | ExpressionManager.DEFAULT_EXPRESSION_PREFIX, | |
47 | ExpressionManager.DEFAULT_EXPRESSION_POSTFIX); | |
48 | ||
49 | 0 | private final TemplateParser wigglyMuleParser = TemplateParser.createMuleStyleParser(); |
50 | 0 | private final TemplateParser squareParser = TemplateParser.createSquareBracesStyleParser(); |
51 | ||
52 | protected MuleContext muleContext; | |
53 | ||
54 | public void setMuleContext(MuleContext context) | |
55 | { | |
56 | 0 | this.muleContext = context; |
57 | 0 | } |
58 | ||
59 | public String getFilename(MuleMessage message, String expression) | |
60 | { | |
61 | 0 | if (expression == null) |
62 | { | |
63 | 0 | expression = DEFAULT_EXPRESSION; |
64 | } | |
65 | ||
66 | 0 | if (expression.indexOf(ExpressionManager.DEFAULT_EXPRESSION_PREFIX) > -1) |
67 | { | |
68 | 0 | return getFilename(message, expression, wigglyMuleParser); |
69 | } | |
70 | else | |
71 | { | |
72 | 0 | return getFilename(message, expression, squareParser); |
73 | } | |
74 | } | |
75 | ||
76 | protected String getFilename(final MuleMessage message, String expression, TemplateParser parser) | |
77 | { | |
78 | 0 | return parser.parse(new TemplateParser.TemplateCallback() |
79 | 0 | { |
80 | public Object match(String token) | |
81 | { | |
82 | 0 | return muleContext.getExpressionManager().evaluate(token, message); |
83 | } | |
84 | }, expression); | |
85 | } | |
86 | } |