View Javadoc

1   /*
2    * $Id: XPathExpressionEvaluator.java 20321 2010-11-24 15:21:24Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.Element;
16  import org.dom4j.Node;
17  import org.jaxen.JaxenException;
18  import org.jaxen.XPath;
19  import org.jaxen.dom.DOMXPath;
20  import org.jaxen.dom4j.Dom4jXPath;
21  import org.w3c.dom.Document;
22  
23  /**
24   * Will select the text of a single node based on the property name
25   */
26  public class XPathExpressionEvaluator extends AbstractXPathExpressionEvaluator
27  {
28      public static final String NAME = "xpath";
29  
30      protected XPath createXPath(String expression, Object object) throws JaxenException
31      {
32          if(object instanceof Document || object instanceof Element)
33          {
34              return new DOMXPath(expression);
35          }
36          else if (object instanceof org.dom4j.Document || object instanceof org.dom4j.Element)
37          {
38              return new Dom4jXPath(expression);
39          }
40  //        else if (object instanceof nu.xom.Document)
41  //        {
42  //            return new XOMXPath(expression);
43  //        }
44  //        else if (object instanceof org.jdom.Document)
45  //        {
46  //            return new JDOMXPath(expression);
47  //        }
48          else
49          {
50              throw new IllegalArgumentException(XmlMessages.domTypeNotSupported(object.getClass()).getMessage());
51          }
52      }
53  
54      protected Object extractResultFromNode(Object result)
55      {
56          if(result instanceof Node)
57          {
58              return ((Node)result).getText();
59          }
60          else if(result instanceof org.w3c.dom.Node)
61          {
62              return ((org.w3c.dom.Node)result).getFirstChild().getNodeValue();
63          }
64  //        else if(result instanceof nu.xom.Node)
65  //        {
66  //            return ((nu.xom.Node)result).getText();
67  //        }
68  //        else if(result instanceof org.jdom.Node)
69  //        {
70  //            return ((org.jdom.Node)result).getText();
71  //        }
72          else
73          {
74              return result;
75          }
76      }
77  
78      /** {@inheritDoc} */
79      public String getName()
80      {
81          return NAME;
82      }
83  }