/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: XPathQueryImpl.java,v 1.8 2004/02/08 02:50:54 vgritsenko Exp $
*/
package org.apache.xindice.core.xupdate;
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.
*
* @version CVS $Revision: 1.8 $, $Date: 2004/02/08 02:50:54 $
*/
public final class XPathQueryImpl implements XPathQuery {
private String qstring;
private Node rootNode;
private Node namespace;
private NodeFilter filter;
private XPath xpath;
/**
* 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));
}
}