1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.module.xml.expression; |
11 | |
|
12 | |
import org.mule.api.MuleMessage; |
13 | |
import org.mule.api.MuleRuntimeException; |
14 | |
import org.mule.api.lifecycle.Disposable; |
15 | |
import org.mule.module.xml.i18n.XmlMessages; |
16 | |
import org.mule.util.expression.ExpressionEvaluator; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.WeakHashMap; |
23 | |
|
24 | |
import org.jaxen.JaxenException; |
25 | |
import org.jaxen.XPath; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 464 | public abstract class AbstractXPathExpressionEvaluator implements ExpressionEvaluator, Disposable |
32 | |
{ |
33 | 464 | private Map cache = new WeakHashMap(8); |
34 | |
|
35 | |
|
36 | |
public Object evaluate(String expression, Object message) |
37 | |
{ |
38 | |
try |
39 | |
{ |
40 | 0 | if(message instanceof MuleMessage) |
41 | |
{ |
42 | 0 | message = ((MuleMessage)message).getPayload(); |
43 | |
} |
44 | 0 | XPath xpath = getXPath(expression, message); |
45 | |
|
46 | 0 | List result = xpath.selectNodes(message); |
47 | 0 | result = extractResultsFromNodes(result); |
48 | 0 | if(result.size()==1) |
49 | |
{ |
50 | 0 | return result.get(0); |
51 | |
} |
52 | 0 | else if(result.size()==0) |
53 | |
{ |
54 | 0 | return null; |
55 | |
} |
56 | |
else |
57 | |
{ |
58 | 0 | return result; |
59 | |
} |
60 | |
} |
61 | 0 | catch (JaxenException e) |
62 | |
{ |
63 | 0 | throw new MuleRuntimeException(XmlMessages.failedToProcessXPath(expression), e); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
|
68 | |
public final void setName(String name) |
69 | |
{ |
70 | 0 | throw new UnsupportedOperationException("setName"); |
71 | |
} |
72 | |
|
73 | |
protected XPath getXPath(String expression, Object object) throws JaxenException |
74 | |
{ |
75 | 0 | XPath xpath = (XPath)cache.get(expression + getClass().getName()); |
76 | 0 | if(xpath==null) |
77 | |
{ |
78 | 0 | xpath = createXPath(expression, object); |
79 | 0 | cache.put(expression + getClass().getName(), xpath); |
80 | |
} |
81 | 0 | return xpath; |
82 | |
} |
83 | |
|
84 | |
protected abstract XPath createXPath(String expression, Object object) throws JaxenException; |
85 | |
|
86 | |
protected List extractResultsFromNodes(List results) |
87 | |
{ |
88 | 0 | if(results==null) |
89 | |
{ |
90 | 0 | return null; |
91 | |
} |
92 | 0 | List newResults = new ArrayList(results.size()); |
93 | 0 | for (Iterator iterator = results.iterator(); iterator.hasNext();) |
94 | |
{ |
95 | 0 | Object o = iterator.next(); |
96 | 0 | newResults.add(extractResultFromNode(o)); |
97 | 0 | } |
98 | 0 | return newResults; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void dispose() |
107 | |
{ |
108 | 464 | cache.clear(); |
109 | 464 | } |
110 | |
|
111 | |
protected abstract Object extractResultFromNode(Object result); |
112 | |
} |