1
2
3
4
5
6
7 package org.mule.module.xml.expression;
8
9 import org.mule.module.xml.i18n.XmlMessages;
10
11 import org.dom4j.Node;
12 import org.jaxen.JaxenException;
13 import org.jaxen.XPath;
14 import org.jaxen.dom.DOMXPath;
15 import org.jaxen.dom4j.Dom4jXPath;
16
17
18
19
20 public class XPathExpressionEvaluator extends AbstractXPathExpressionEvaluator
21 {
22 public static final String NAME = "xpath";
23
24 @Override
25 protected XPath createXPath(String expression, Object object) throws JaxenException
26 {
27 if(createDOMXPath(object))
28 {
29 return new DOMXPath(expression);
30 }
31 else if (createDom4jXPath(object))
32 {
33 return new Dom4jXPath(expression);
34 }
35 else
36 {
37 throw new IllegalArgumentException(XmlMessages.domTypeNotSupported(object.getClass()).getMessage());
38 }
39 }
40
41 @Override
42 protected String getXPathClassName(Object object)
43 {
44 if(createDOMXPath(object))
45 {
46 return DOMXPath.class.getName();
47 }
48 if(createDom4jXPath(object))
49 {
50 return Dom4jXPath.class.getName();
51 }
52 return super.getXPathClassName(object);
53 }
54
55 private boolean createDOMXPath(Object object)
56 {
57 return object instanceof org.w3c.dom.Document || object instanceof org.w3c.dom.Element;
58 }
59
60 private boolean createDom4jXPath(Object object)
61 {
62 return object instanceof org.dom4j.Document || object instanceof org.dom4j.Element;
63 }
64
65 protected Object extractResultFromNode(Object result)
66 {
67 if(result instanceof Node)
68 {
69 return ((Node)result).getText();
70 }
71 else if(result instanceof org.w3c.dom.Node)
72 {
73 return ((org.w3c.dom.Node)result).getFirstChild().getNodeValue();
74 }
75 else
76 {
77 return result;
78 }
79 }
80
81
82 public String getName()
83 {
84 return NAME;
85 }
86 }