1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.xml.expression; |
12 | |
|
13 | |
import org.mule.api.transport.MessageAdapter; |
14 | |
import org.mule.module.xml.util.XMLUtils; |
15 | |
import org.mule.util.expression.ExpressionEvaluator; |
16 | |
|
17 | |
import org.apache.commons.jxpath.JXPathContext; |
18 | |
import org.apache.commons.logging.Log; |
19 | |
import org.apache.commons.logging.LogFactory; |
20 | |
import org.dom4j.Document; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 234 | public class JXPathExpressionEvaluator implements ExpressionEvaluator |
26 | |
{ |
27 | |
public static final String NAME = "jxpath"; |
28 | |
|
29 | |
|
30 | |
|
31 | 234 | protected transient Log logger = LogFactory.getLog(getClass()); |
32 | |
|
33 | |
public Object evaluate(String name, Object message) |
34 | |
{ |
35 | |
|
36 | 6 | Object result = null; |
37 | 6 | Object payload = message; |
38 | 6 | if (message instanceof MessageAdapter) |
39 | |
{ |
40 | 4 | payload = ((MessageAdapter) message).getPayload(); |
41 | |
} |
42 | |
|
43 | |
Document dom4jDoc; |
44 | |
try |
45 | |
{ |
46 | 6 | dom4jDoc = XMLUtils.toDocument(payload); |
47 | |
} |
48 | 0 | catch (Exception e) |
49 | |
{ |
50 | 0 | logger.error(e); |
51 | 0 | return null; |
52 | 6 | } |
53 | |
|
54 | |
|
55 | 6 | if (dom4jDoc != null) |
56 | |
{ |
57 | 0 | result = dom4jDoc.valueOf(name); |
58 | |
} |
59 | |
|
60 | |
else |
61 | |
{ |
62 | 6 | JXPathContext context = JXPathContext.newContext(payload); |
63 | |
try |
64 | |
{ |
65 | 6 | result = context.getValue(name); |
66 | |
} |
67 | 2 | catch (Exception e) |
68 | |
{ |
69 | |
|
70 | 4 | } |
71 | |
} |
72 | 6 | return result; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
public String getName() |
77 | |
{ |
78 | 232 | return NAME; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
public void setName(String name) |
83 | |
{ |
84 | 0 | throw new UnsupportedOperationException("setName"); |
85 | |
} |
86 | |
} |