1
2
3
4
5
6
7
8
9
10
11 package org.mule.expression;
12
13 import org.mule.api.MuleMessage;
14 import org.mule.api.expression.ExpressionEvaluator;
15 import org.mule.api.expression.RequiredValueException;
16 import org.mule.config.i18n.CoreMessages;
17 import org.mule.util.StringUtils;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22
23
24
25
26 public class MapPayloadExpressionEvaluator implements ExpressionEvaluator, ExpressionConstants
27 {
28 public static final String NAME = "map-payload";
29
30 public Object evaluate(String expression, MuleMessage message)
31 {
32 Object payload = message.getPayload();
33
34 if (!(payload instanceof Map))
35 {
36 return null;
37 }
38
39 if (expression.indexOf(DELIM) > -1)
40 {
41
42 String[] strings = StringUtils.splitAndTrim(expression, DELIM);
43 Map<String, Object> result = new HashMap<String, Object>(strings.length);
44
45 for (String s : strings)
46 {
47 Object val = getValue(s, (Map) payload);
48 if (val != null)
49 {
50 if (s.endsWith(OPTIONAL_ARGUMENT))
51 {
52 s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length());
53 }
54 result.put(s, val);
55 }
56 }
57 return result;
58 }
59 else
60 {
61 return getValue(expression, (Map)payload);
62 }
63 }
64
65 protected Object getValue(String key, Map map)
66 {
67 boolean required;
68 if (key.endsWith(OPTIONAL_ARGUMENT))
69 {
70 key = key.substring(0, key.length() - OPTIONAL_ARGUMENT.length());
71 required = false;
72 }
73 else
74 {
75 required = true;
76 }
77 Object val = map.get(key);
78 if (val != null)
79 {
80 return val;
81 }
82 else if (required)
83 {
84 throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, key));
85 }
86 return null;
87 }
88
89
90
91
92 public String getName()
93 {
94 return NAME;
95 }
96
97
98
99
100 public void setName(String name)
101 {
102 throw new UnsupportedOperationException();
103 }
104
105 }