package com.dbxml.db.common.xupdate;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: XPathQueryImpl.java,v 1.2 2004/02/12 00:17:56 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));
   }
}

