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 public abstract class AbstractXPathExpressionEvaluator implements ExpressionEvaluator, Disposable
32 {
33 private Map cache = new WeakHashMap(8);
34
35
36 public Object evaluate(String expression, Object message)
37 {
38 try
39 {
40 if(message instanceof MuleMessage)
41 {
42 message = ((MuleMessage)message).getPayload();
43 }
44 XPath xpath = getXPath(expression, message);
45
46 List result = xpath.selectNodes(message);
47 result = extractResultsFromNodes(result);
48 if(result.size()==1)
49 {
50 return result.get(0);
51 }
52 else if(result.size()==0)
53 {
54 return null;
55 }
56 else
57 {
58 return result;
59 }
60 }
61 catch (JaxenException e)
62 {
63 throw new MuleRuntimeException(XmlMessages.failedToProcessXPath(expression), e);
64 }
65 }
66
67
68 public final void setName(String name)
69 {
70 throw new UnsupportedOperationException("setName");
71 }
72
73 protected XPath getXPath(String expression, Object object) throws JaxenException
74 {
75 XPath xpath = (XPath)cache.get(expression + getClass().getName());
76 if(xpath==null)
77 {
78 xpath = createXPath(expression, object);
79 cache.put(expression + getClass().getName(), xpath);
80 }
81 return xpath;
82 }
83
84 protected abstract XPath createXPath(String expression, Object object) throws JaxenException;
85
86 protected List extractResultsFromNodes(List results)
87 {
88 if(results==null)
89 {
90 return null;
91 }
92 List newResults = new ArrayList(results.size());
93 for (Iterator iterator = results.iterator(); iterator.hasNext();)
94 {
95 Object o = iterator.next();
96 newResults.add(extractResultFromNode(o));
97 }
98 return newResults;
99 }
100
101
102
103
104
105
106 public void dispose()
107 {
108 cache.clear();
109 }
110
111 protected abstract Object extractResultFromNode(Object result);
112 }