001: /*
002: ******************************************************************
003: Copyright (c) 2006-2007, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.jaxp13;
038:
039: import org.custommonkey.xmlunit.NamespaceContext;
040: import org.custommonkey.xmlunit.XMLUnit;
041: import org.custommonkey.xmlunit.XpathEngine;
042: import org.custommonkey.xmlunit.exceptions.ConfigurationException;
043: import org.custommonkey.xmlunit.exceptions.XpathException;
044:
045: import javax.xml.xpath.XPath;
046: import javax.xml.xpath.XPathConstants;
047: import javax.xml.xpath.XPathExpressionException;
048: import javax.xml.xpath.XPathFactory;
049:
050: import org.w3c.dom.Document;
051: import org.w3c.dom.NodeList;
052:
053: /**
054: * XPath engine based on javax.xml.xpath.
055: */
056: public class Jaxp13XpathEngine implements XpathEngine {
057:
058: private final XPath xpath;
059:
060: public Jaxp13XpathEngine() throws ConfigurationException {
061: try {
062: XPathFactory f = null;
063: if (XMLUnit.getXPathFactory() != null) {
064: f = (XPathFactory) Class.forName(
065: XMLUnit.getXPathFactory()).newInstance();
066: } else {
067: f = XPathFactory.newInstance();
068: }
069:
070: xpath = f.newXPath();
071: } catch (Exception ex) {
072: throw new ConfigurationException(ex);
073: }
074: }
075:
076: /**
077: * Execute the specified xpath syntax <code>select</code> expression
078: * on the specified document and return the list of nodes (could have
079: * length zero) that match
080: * @param select
081: * @param document
082: * @return list of matching nodes
083: */
084: public NodeList getMatchingNodes(String select, Document document)
085: throws XpathException {
086: try {
087: return (NodeList) xpath.evaluate(select, document,
088: XPathConstants.NODESET);
089: } catch (XPathExpressionException ex) {
090: throw new XpathException(ex);
091: }
092: }
093:
094: /**
095: * Evaluate the result of executing the specified xpath syntax
096: * <code>select</code> expression on the specified document
097: * @param select
098: * @param document
099: * @return evaluated result
100: * @throws TransformerException
101: * @throws TransformerConfigurationException
102: */
103: public String evaluate(String select, Document document)
104: throws XpathException {
105: try {
106: return xpath.evaluate(select, document);
107: } catch (XPathExpressionException ex) {
108: throw new XpathException(ex);
109: }
110: }
111:
112: public void setNamespaceContext(NamespaceContext ctx) {
113: xpath.setNamespaceContext(new XMLUnitNamespaceContext2Jaxp13(
114: ctx));
115: }
116: }
|