1 /*
2 * $Id: XPathExpressionEvaluator.java 11231 2008-03-06 21:17:37Z rossmason $
3 * --------------------------------------------------------------------------------------
4 * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5 *
6 * The software in this package is published under the terms of the CPAL v1.0
7 * license, a copy of which has been included with this distribution in the
8 * LICENSE.txt file.
9 */
10
11 package org.mule.module.xml.expression;
12
13 import org.mule.module.xml.i18n.XmlMessages;
14
15 import org.dom4j.Node;
16 import org.jaxen.JaxenException;
17 import org.jaxen.XPath;
18 import org.jaxen.dom.DOMXPath;
19 import org.jaxen.dom4j.Dom4jXPath;
20 import org.w3c.dom.Document;
21
22 /**
23 * Will select the text of a single node based on the property name
24 */
25 public class XPathExpressionEvaluator extends AbstractXPathExpressionEvaluator
26 {
27 public static final String NAME = "xpath";
28
29 protected XPath createXPath(String expression, Object object) throws JaxenException
30 {
31 if(object instanceof Document)
32 {
33 return new DOMXPath(expression);
34 }
35 else if (object instanceof org.dom4j.Document)
36 {
37 return new Dom4jXPath(expression);
38 }
39 // else if (object instanceof nu.xom.Document)
40 // {
41 // return new XOMXPath(expression);
42 // }
43 // else if (object instanceof org.jdom.Document)
44 // {
45 // return new JDOMXPath(expression);
46 // }
47 else
48 {
49 throw new IllegalArgumentException(XmlMessages.domTypeNotSupported(object.getClass()).getMessage());
50 }
51 }
52
53 protected Object extractResultFromNode(Object result)
54 {
55 if(result instanceof Node)
56 {
57 return ((Node)result).getText();
58 }
59 else if(result instanceof org.w3c.dom.Node)
60 {
61 return ((org.w3c.dom.Node)result).getFirstChild().getNodeValue();
62 }
63 // else if(result instanceof nu.xom.Node)
64 // {
65 // return ((nu.xom.Node)result).getText();
66 // }
67 // else if(result instanceof org.jdom.Node)
68 // {
69 // return ((org.jdom.Node)result).getText();
70 // }
71 else
72 {
73 throw new IllegalArgumentException(XmlMessages.domTypeNotSupported(result.getClass()).getMessage());
74 }
75 }
76
77 /** {@inheritDoc} */
78 public String getName()
79 {
80 return NAME;
81 }
82 }