package com.dbxml.db.common.xupdate;
/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: XPathQueryImpl.java,v 1.3 2006/02/02 18:53:52 bradford Exp $
*/
import org.apache.xml.utils.PrefixResolver;
import org.apache.xml.utils.PrefixResolverDefault;
import org.apache.xpath.XPath;
import org.apache.xpath.XPathContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeFilter;
import org.xmldb.common.xml.queries.XObject;
import org.xmldb.common.xml.queries.XPathQuery;
/**
* XPath Query Impl to handle Xalan 2 XPath constructs.
*/
public final class XPathQueryImpl implements XPathQuery {
private String qstring;
private Node rootNode;
private Node namespace;
private NodeFilter filter;
private XPath xpath;
/**
* Constructor for the XPathQueryImpl object
*/
public XPathQueryImpl() {
}
/**
* Sets the QString attribute of the XPathQueryImpl object
*
* @param qstring The new QString value
* @exception Exception Description of Exception
*/
public void setQString(String qstring) throws Exception {
this.qstring = qstring;
}
/**
* Sets the Namespace attribute of the XPathQueryImpl object
*
* @param namespace The new Namespace value
* @exception Exception Description of Exception
*/
public void setNamespace(Node namespace) throws Exception {
this.namespace = namespace;
}
/**
* Sets the NodeFilter attribute of the XPathQueryImpl object
*
* @param filter The new NodeFilter value
* @exception Exception Description of Exception
*/
public void setNodeFilter(NodeFilter filter) throws Exception {
this.filter = filter;
}
/**
* Execute the xpath.
*
* @param rootNode The node from which the query should start or null.
* @return The XObject insulating the query result.
* @exception Exception
*/
public XObject execute(Node rootNode) throws Exception {
if ( rootNode.getNodeType() == Node.DOCUMENT_NODE ) {
rootNode = ((Document)rootNode).getDocumentElement();
}
this.rootNode = rootNode;
// Since we don't have a XML Parser involved here, install some default
// support for things like namespaces, etc.
XPathContext xpathSupport = new XPathContext();
PrefixResolver prefixResolver = null;
// Create an object to resolve namespace prefixes.
if ( namespace != null ) {
if ( namespace.getNodeType() == Node.DOCUMENT_NODE ) {
namespace = ((Document)namespace).getDocumentElement();
}
prefixResolver = new PrefixResolverDefault(namespace);
}
else {
prefixResolver = new PrefixResolverDefault(rootNode);
}
// Create the XPath object.
xpath = new XPath(qstring, null, prefixResolver, XPath.SELECT, null);
// execute the XPath query on the specified root node
return new XObjectImpl(xpath.execute(xpathSupport, rootNode, prefixResolver));
}
}