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