You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by jb...@apache.org on 2002/05/06 16:43:02 UTC

cvs commit: xml-xindice/java/scratchpad/src/org/apache/xindice/tools XMLTools.java

jbates      02/05/06 07:43:02

  Modified:    java/scratchpad/src/org/apache/xindice/tools XMLTools.java
  Added:       java/scratchpad/src/org/apache/xindice/client/rpc/base
                        CollectionImpl.java CommonConfigurable.java
                        DatabaseImpl.java ResourceIteratorImpl.java
                        ResourceSetImpl.java
               java/scratchpad/src/org/apache/xindice/client/rpc/modules
                        QueryService.java XMLResourceImpl.java
                        XPathQueryServiceImpl.java
                        XUpdateQueryServiceImpl.java
  Removed:     java/scratchpad/src/org/apache/xindice/client/rpc
                        CollectionImpl.java CommonConfigurable.java
                        DatabaseImpl.java XMLResourceImpl.java
  Log:
  Query services for Xindice XML:DB client
  
  Revision  Changes    Path
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/base/CollectionImpl.java
  
  Index: CollectionImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: CollectionImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.base;
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.xmldb.api.base.Service;
  import org.apache.xmlrpc.XmlRpc;
  import org.apache.xmlrpc.XmlRpcClient;
  import org.xmldb.api.base.Resource;
  import org.xmldb.api.modules.XMLResource;
  import org.apache.xindice.server.rpc.RPCDefaultMessage;
  import java.util.Hashtable;
  import java.util.Vector;
  import java.net.MalformedURLException;
  import org.apache.xindice.client.rpc.modules.XMLResourceImpl;
  import org.apache.xindice.xml.TextWriter;
  import org.xmldb.api.base.ResourceSet;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  import java.io.StringReader;
  
  /**
   * Implementation of XML:DB's <code>Collection</code> interface using
   * XML-RPC to interact with database server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  public class CollectionImpl extends CommonConfigurable implements Collection {
  
      /* Named service classes map to instantiate for each collection */
      private static String[] serviceClassesMap =
      {  "org.apache.xindice.client.modules.XPathQueryServiceImpl",
        "org.apache.xindice.client.modules.XUpdateQueryServiceImpl" };
        
      /* Instantiated named services map */
      private Hashtable servicesMap = new Hashtable();
      
      /* path to XML-RPC service on database */
      private static String XINDICE_SERVICE_LOCATION = "/RPC2";
  
      /* Xindice query result meta-info namespace */
      public static final String QUERY_NS = "http://xml.apache.org/xindice/Query";
      
      /* host and port number of server */
      private String hostPort;
      
      /* path to collection on target server */
      private String collPath;
      
      /* the XML-RPC client stub, connected to server */
      private XmlRpcClient client = null;
  
  
      /**
      /* Creates new <code>CollectionImpl</code> instance representing connection
       * to server collection.
       *
       * @param hostPort hostname and port number in <code>host:port</code> format.
       *        Port no is optional, in which case HTTP default is assumed.
       * @exception XMLDBException thrown if a connection could not be established,
       *            because of URL syntax errors, or connection failure, or if no
       *            collection with path <code>collPath</code> could be located.
       */
      public CollectionImpl(String hostPort, String collPath) throws XMLDBException {
          
          this.hostPort = hostPort;
          this.collPath = collPath;
          String xmlrpcURI = "http://" + hostPort + XINDICE_SERVICE_LOCATION;
          XmlRpc.setEncoding("UTF8");
          try {
              
              XmlRpc.setDriver("xerces");
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Xerces needed");
          }
          
          try {
              
              client = new XmlRpcClient(xmlrpcURI);
              
              /* Just check the collection does actually exist */
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              runRemoteCommand("GetCollectionConfiguration", params);
          } catch (MalformedURLException e) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION, collPath);
          }
          
          for (int i=0; i < serviceClassesMap.length; i++) {
  
              try {
              
                  /*
                   * Look up service constructor that accepts CollectionImpl
                   * argument
                   */
                  Service s = (Service) 
                          Class.forName(serviceClassesMap[i]).newInstance();
                  s.setCollection(this);
                  
                  servicesMap.put(s.getName() + s.getVersion(), s);
              } catch (InstantiationException e) {
              
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Coudln't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              } catch (ClassNotFoundException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Coudln't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              } catch (IllegalAccessException e) {
                  
                  throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "Coudln't create "
                         + serviceClassesMap[i] + " service: " + e.getMessage());
              }
          } 
      }
      
      /**
       * Submits a command for RPC to database server
       *
       * @param cmdName command name
       * @param params hashtable containing named parameters to send to server
       * @return the return value from the server. Type of return value depends on
       *         command.
       *
       * @exception Exception thrown if XML-RPC reports an exception.
       */
      private Object runRemoteCommand(String cmdName, Hashtable params) throws Exception {
          
          params.put("message", cmdName);
          
          Vector v = new Vector();
          v.add(params);
          return ((Hashtable) client.execute("run",v)).get(RPCDefaultMessage.RESULT);
      }
          
          
      /**
       * Returns the available services for this collection
       *
       * @return an array with one entry per available service
       */
      public Service[] getServices() {
  
          return (Service[]) servicesMap.entrySet().toArray(
                  new Service[servicesMap.size()]);
      }
      
      /**
       * Returns the named resource in this collection
       *
       * @param resourceName resource name
       * @return the resource
       * @exception XMLDBException thrown if there is no resource named
       *            <code>resourceName</code> or if some other error condition
       *            occurs.
       */
      public Resource getResource(String resourceName) throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resourceName);
              
              String xml = (String) runRemoteCommand("GetDocument", params);
              
              return new XMLResourceImpl(resourceName, xml, this);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, e.getMessage());
          }
      }
      
      /**
       * Returns number of resources in this collection.
       *
       * @return the number of resources
       * @exception XMLDBException thrown if count could not be obtained for
       *            some unexpected reason
       */
      public int getResourceCount() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              return ((Long) runRemoteCommand(
                      "GetDocumentCount", params)).intValue();
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Stores contents of reource back to database
       *
       * @param resource the resource to store in database
       * @exception XMLDBException thrown if resource was of incompatible type,
       *            or if some other error condition arises.
       */
      public void storeResource(Resource resource) throws XMLDBException {
          
          if (!(resource instanceof XMLResource)) {
           
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "Only XML resources supported");
          }
          
          if (resource.getContent() == null) {
              
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "no resource data");
          }
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resource.getId());
              params.put(RPCDefaultMessage.DOCUMENT, resource.getContent());
              String name = (String) runRemoteCommand("InsertDocument", params);
              ((XMLResourceImpl) resource).setId(name);
              
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Looks up named service for this collection.
       *
       * @param name service name
       * @param version service version
       * @exception XMLDBException thrown if no mactching service
       *            could be located.
       */
      public Service getService(String name, String version) throws XMLDBException {
          
          Service s = (Service) servicesMap.get(name + version);
          if (s == null) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_SERVICE);
          }
          
          return s;
      }
      
      /**
       * Checks if the collection is still open. Only open collections are safe
       * to work with.
       *
       * @return whether the collection is still open
       */
      public boolean isOpen() {
  
         return (client != null);
      }
      
      /**
       * Throws an exception if collection is no longer open
       *
       * @exception XMLDBException thrown if collection is closed
       */
      private void checkOpen() throws XMLDBException {
         
          if (!isOpen()) {
              
              throw new XMLDBException(ErrorCodes.COLLECTION_CLOSED);
          }
      }
      
      /**
       * Returns this collection's name
       *
       * @return collection name
       */
      public String getName() {
  
          return collPath.substring(collPath.lastIndexOf('/') + 1);
      }
          
      /**
       * Returns complete path to collection
       *
       * @return the collection path
       */
      public String getPath() {
          
          return collPath;
      }
      
      /**
       * Returns XML:DB URI that would retrieve this collection
       *
       * @return a complete XML:DB URI
       */
      public String getURI() {
          
          return "xmldb:" + DatabaseImpl.DRIVER_NAME + "://"
                  + hostPort + collPath;
      }
      
      /**
       * Returns child collection of this collection with specified name
       *
       * @param colName child's name
       * @return the child collection
       * @exception XMLDBException thrown if there is no such child, or some
       *            other connection error occurred
       */
      public Collection getChildCollection(String collName) throws XMLDBException {
  
          if (collName.indexOf('/') != -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_COLLECTION);
          }
          
          return new CollectionImpl(hostPort, collPath + "/" + collName);
      }
          
      /**
       * Creates a new identifier that can be used as name for a resource in this
       * collection.
       *
       * @return the new identifier name
       * @exception XMLDBException thrown in case of some unknwon error condition
       */
      public String createId() throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              return (String) runRemoteCommand("CreateNewOID", params);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      /**
       * Closes physical connection to server containing this collection.
       *
       * After calling this method, most methods will throw a <code>XMLDBException</code>
       * exception with code <code>ErrorCodes.COLLECTION_CLOSED</code>. The collection
       * object will in effect become useless.
       */
      public void close() throws org.xmldb.api.base.XMLDBException {
      
          client = null;
      }
      
      /**
       * Constructs a new resource that will belong in this collection.
       *
       * Only XML resources are supported. To save the resource to the database, you
       * must first set the resource's XML data, and then call
       * <code>storeResource()</code>.
       *
       * @param name name for new resource. If empty or <code>null</code>, a name
       *        will be assigned when storing the resource in the database.
       * @param type must be <code>XMLResource</code>.
       * @exception XMLDBException thrown in case of an invalid resource type or name
       */
      public Resource createResource(String name, String type) throws XMLDBException {
          
          if (name.indexOf('/') != -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, 
                      "Name cannot contain '/'");
          }
          
          if (!"XMLResource".equals(type)) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_RESOURCE_TYPE,
                      "only XMLResources supported");
          }
          
          if (name == null) {
              
              name = "";
          }
          
          return new XMLResourceImpl(name, this);
      }
      
      /**
       * Returns the parent collection, if any, of this collection
       *
       * @return the parent collectionm
       * @exception XMLDBException thrown if this is the root collection (which
       *            has no parent), or in case of some other unknown error.
       */
      public Collection getParentCollection() throws XMLDBException {
          
          if (collPath == "/") {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_COLLECTION,
                      "root collection has no parent");
          }
          
          return new CollectionImpl(hostPort, collPath.substring(0,
                  collPath.lastIndexOf('/')));
      }
      
      /**
       * Removes a (XML) resource from this collection.
       *
       * The resource object itself is still valid after removal, and can be used
       * to modify the resource and possibly store it back to the database again.
       *
       * @param resource resource to remove from the databasew
       * @exception XMLDBException thrown if the resource couldn't be located, or
       *            in case of some other error
       */
      public void removeResource(Resource resource) throws XMLDBException {
          
          if (!(resource instanceof XMLResource)) {
           
              throw new XMLDBException(ErrorCodes.INVALID_RESOURCE,
                      "Only XML resources supported");
          }
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.NAME, resource.getId());
              runRemoteCommand("RemoveDocument", params);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE, e.getMessage());
          }
      }
      
      /**
       * Lists the available child collections of this collection
       *
       * @return array containing child collection names
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public String[] listChildCollections() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              Vector list = (Vector) runRemoteCommand("ListCollections", params);
              
              return (String[]) list.toArray(new String[list.size()]);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }
      
      /**
       * Returns number of child collections in this collection
       *
       * @return the number of child collections
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public int getChildCollectionCount() throws XMLDBException {
  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              return ((Long) runRemoteCommand("GetCollectionsCount", params)).intValue();
              
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }
      
      /**
       * Lists the available documents of this collection
       *
       * @return array containing resource names
       * @exception XMLDBException thrown in case of unknwon error condition
       */
      public String[] listResources() throws XMLDBException {
          
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              Vector list = (Vector) runRemoteCommand("ListDocuments", params);
              
              return (String[]) list.toArray(new String[list.size()]);
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }   
      }  
  
      /**
       * Queries the entire collection and resturns the result
       *
       * @param name name of document to query
       * @param queryLang <code>XUpdate</code> or <code>XPath</code> 
       * @param query the text of the query statement
       * @param nsMap namespace bindings to use when evaluating query
       * @return set containing result of query
       * @throws XMLDBException thrown in case of invalid query or other error
       */
      public ResourceSet query(String queryLang, String query, Hashtable nsMap)
              throws XMLDBException {
                  
          return query(null, queryLang, query, nsMap);
      }
  
      /**
       * Queries a specific document or the entire collection and returns result
       *
       * @param name name of document to query, or <code>null</code> to query
       *        entire collection.
       * @param queryLang <code>XUpdate</code> or <code>XPath</code>.
       * @param query the text of the query statement
       * @param nsMap namespace bindings to use when evaluating query
       * @return set containing result of query
       * @throws XMLDBException thrown in case of invalid query or other error
       */
      public ResourceSet query(String name, String queryLang, String query, Hashtable nsMap)
              throws XMLDBException {
                  
          checkOpen();
          try {
              
              Hashtable params = new Hashtable();
              params.put(RPCDefaultMessage.COLLECTION, collPath);
              params.put(RPCDefaultMessage.TYPE, queryLang);
              params.put(RPCDefaultMessage.NAMESPACES, nsMap);
              if (name != null) {
                  
                  params.put(RPCDefaultMessage.NAME, name);
              }
              
              String result = (String) runRemoteCommand("Query", params);
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              dbf.setNamespaceAware(true);
              Document resultDoc = dbf.newDocumentBuilder().parse(
                  new InputSource(new StringReader(result)));
              
              NodeList nodes = resultDoc.getDocumentElement().getChildNodes();
              ResourceSetImpl rs = new ResourceSetImpl();
              
              for (int i = 0; i < nodes.getLength(); i++) {
                  
                  Node n = nodes.item(i);
              
                  String documentId = ((Element) n).getAttributeNS(
                          QUERY_NS, "key");
              
                  XMLResourceImpl resource = new XMLResourceImpl(documentId, 
                          TextWriter.toString(n), this);
                  rs.addResource(resource);
              }
           
              return rs;
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, 
                      "Query error: " + e.getMessage());
          }
      }
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/base/CommonConfigurable.java
  
  Index: CommonConfigurable.java
  ===================================================================
  package org.apache.xindice.client.rpc.base;
  
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: CommonConfigurable.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  import java.util.*;
  import org.xmldb.api.base.*;
  
  /**
   * Base class to handle property managment within the API implementation.
   */
  public abstract class CommonConfigurable implements Configurable {
     Hashtable config = null;
  
     /**
      * Constructor for the CommonConfigurable object
      */
     public CommonConfigurable() {
        config = new Hashtable();
     }
  
     /**
      * Sets a property value. If the property doesn't exist it is created, if it
      * does exist it is overwritten.
      *
      * @param name The Property name
      * @param value The new Property value
      * @exception XMLDBException
      */
     public void setProperty(String name, String value) throws XMLDBException {
        config.put(name, value);
     }
  
     /**
      * Gets the Property associated with name
      *
      * @param name The name of the property to retrieve.
      * @return The Property value
      * @exception XMLDBException
      */
     public String getProperty(String name) throws XMLDBException {
        return (String) config.get(name);
     }
  }
  
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/base/DatabaseImpl.java
  
  Index: DatabaseImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: DatabaseImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.base;
  
  import org.xmldb.api.base.Database;
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  
  /**
   * implements XML:DB's <code>Database</code> interface using XML-RPC to
   * communicate with the Xindice server.
   *
   * Note this class is a database <em>driver</em>, and one class of this database
   * could be used to connect to <em>many</em> different databases.
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  public class DatabaseImpl extends CommonConfigurable implements Database {
      
      /* prefix used to denote XML:DB URI's that should use this driver */
      static String DRIVER_NAME = "xindice-rpc";
      
      /* XML:DB conformance level of this driver */
      private String CONFORMANCE_LEVEL = "0";
      
      /**
       * Creates new <code>DatabaseImpl</code> instance
       */
      public DatabaseImpl() {
  
      }
  
      /**
       * Checks whether this driver can handle the <code>xmldbURI</code> collection
       * URI.
       *
       * @param xmldbURI XML:DB URI to check, without the 'xmldb:' prefix
       * @return whether this driver can handle it
       */
      public boolean acceptsURI(String xmldbURI) throws XMLDBException {
  
          return ((xmldbURI != null) && xmldbURI.startsWith(getName() + "://"));
      }
      
      /**
       * Returns the collection referred to by <code>xmldbURI</code>, attempting
       * to authenticate access to it using <code>userName</code> and
       * <code>password</code> credentials.
       *
       * @param xmldbURI XML:DB URI to collection, without the 'xmldb:' prefix
       * @param userName username used to log into database server
       * @param password password used to log into database server
       * @exception XMLDBException thrown in case a connection could not be
       *            established
       */
      public Collection getCollection(String xmldbURI, String userName, String password)
              throws XMLDBException {
                  
          /* TODO: introduce authentication some day */
                  
          if (!acceptsURI(xmldbURI)) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          }
  
          /* Chop off driver prefix, and '://' */
          xmldbURI = xmldbURI.substring(getName().length() + 3);
          
          /* Extract host name & port, if present */
          int firstSlash = xmldbURI.indexOf('/');
          if (firstSlash == -1) {
              
              throw new XMLDBException(ErrorCodes.INVALID_URI);
          }
          
          String hostPort = xmldbURI.substring(0, firstSlash);
          String collPath = xmldbURI.substring(firstSlash);
  
          /* Absent host defaults to localhost and standard HTTP port */
          if (hostPort.equals("")) {
              
              hostPort = "127.0.0.1";
          }
          
          return new CollectionImpl(hostPort, collPath);
      }
      
      /**
       * Returns the prefix used in XML:DB to denote URI's that this driver can
       * handle.
       *
       * @return the prefix driver name
       */
      public String getName() throws XMLDBException {
  
          return DRIVER_NAME;
      }
          
      public String getConformanceLevel() throws XMLDBException {
  
          return CONFORMANCE_LEVEL;
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/base/ResourceIteratorImpl.java
  
  Index: ResourceIteratorImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: ResourceIteratorImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.base;
  
  import java.util.Iterator;
  import org.xmldb.api.base.ResourceIterator;
  import org.xmldb.api.base.Resource;
  
  /**
   * Implements XML:DB's <code>ResourceIterator</code> interface using 
   * a <code>java.util.Iterator</java>
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  public class ResourceIteratorImpl implements ResourceIterator {
  
      /* the iterator used as a basis for this object */
      private Iterator iterator;
      
      /**
       * Creates new ResourceInteratorImpl using
       * <code>it</code> to extract the resource items
       */
      ResourceIteratorImpl(Iterator it) {
  
          this.iterator = it;
      }
      
      /**
       * More resources to come?
       */
      public boolean hasMoreResources() {
          
          return iterator.hasNext();
      }    
      
      /**
       * Next resource
       *
       * @exception ClassCastException thrown if some idiot initializes this
       *            object with an iterator containing non-resources.
       */
      public Resource nextResource() {
          
          return (Resource) iterator.next();
      }    
  
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/base/ResourceSetImpl.java
  
  Index: ResourceSetImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: ResourceSetImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.base;
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.ResourceSet;
  import org.xmldb.api.base.Resource;
  import org.xmldb.api.base.ResourceIterator;
  import org.xmldb.api.base.XMLDBException;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.apache.xindice.client.rpc.base.CollectionImpl;
  import org.apache.xindice.client.rpc.base.ResourceIteratorImpl;
  import org.apache.xindice.client.rpc.modules.XMLResourceImpl;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.ParserConfigurationException;
  import org.xmldb.api.base.ErrorCodes;
  
  /**
   * Implements XML:DB's <code>ResourceSet</code> interface. This class
   * actually <em>contains</em> a set of XML resources (the only kind supported
   * by Xindice).
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  
  public class ResourceSetImpl extends java.util.Vector implements ResourceSet {
  
      public static final String RESOURCE_SET_NS
              = "http://www.xmldb.org/xapi/ResourceSet";
      
      /* collection that resources in this set belong to. */
      private Collection col = null;
      
      /**
       * Creates new empty <code>ResourceSetImpl</code>
       */
      public ResourceSetImpl() {
  
          super();
      } 
  
      /**
       * Constructs a new resource set from the data
       * in the resourceColl collection.
       *
       * @param resourceColl the resources to include in this set, as a java
       *        collection. Only <code>XMLResourceImpl</code> should be in the
       *        collection. In addition the XML resources should be all from 
       *        the same XML:DB collection.
       */
      public ResourceSetImpl(java.util.Collection resourceColl) throws XMLDBException {
       
          super(resourceColl);
          setCollection();
      }
      
      private void setCollection() throws XMLDBException {
  
          if (size() > 0) {
              
              col = ((XMLResourceImpl) get(0)).getParentCollection();
          }
      }
      
      /**
       * Constructs a new resource set from the supplied resource array.
       *
       * @param resourceArray the resources to include in this set
       */
      public ResourceSetImpl(XMLResourceImpl[] resourceArray) {
       
          super(java.util.Arrays.asList(resourceArray));
      }
      
      /**
       * Removes <code>index</code>'th item from resource set
       *
       * @param index index of item to remove 
       */
      public void removeResource(long index) throws XMLDBException {
  
          remove((int) index);
      }
      
      
      public ResourceIterator getIterator() throws XMLDBException {
  
          return new ResourceIteratorImpl(iterator());
      }
      
      public Resource getMembersAsResource() throws XMLDBException {
  
          if (col == null) {
              
              /* This is an empty set => can't build wrapper resource */
              return null;
          }
          
          /*
           * Code shamefully copied from CORBA client without any kind of regard
           * for copyright laws...
           */
          
          // This impl works but it would be nice if we just got the result set from
          // the server in this format instead of having to build it. Right now it's
          // pretty innefficient
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          dbf.setNamespaceAware(true);
          Document doc = null;
          try {
              
              doc = dbf.newDocumentBuilder().newDocument();
          } catch (ParserConfigurationException e) {
              
              throw new XMLDBException(ErrorCodes.VENDOR_ERROR, "XML parser "
                      + "error: " + e.getMessage());
          }
          
          Element set = doc.createElementNS(RESOURCE_SET_NS, "xapi:resourceSet");
          set.setAttributeNS(RESOURCE_SET_NS, "xapi:collectionURI",
                  ((CollectionImpl) col).getURI());
          set.setAttribute("xmlns:xapi", RESOURCE_SET_NS);
          doc.appendChild(set);
  
          for (int i = 0; i < size(); i++) {
              
              XMLResourceImpl res = (XMLResourceImpl) get(i);
              Element resource = doc.createElementNS(RESOURCE_SET_NS,
              "xapi:resource");
              resource.setAttributeNS(RESOURCE_SET_NS, "xapi:documentID", res.getId());
              resource.appendChild(doc.importNode(
                      ((Document) res.getContentAsDOM()).getDocumentElement(), true));
  
              set.appendChild(resource);
          }
  
          XMLResourceImpl result = new XMLResourceImpl("", (CollectionImpl) col);
          result.setContentAsDOM(doc);
  
          return result;
      }
          
      /** 
       * Returns number of resources in set
       */
      public long getSize() {
  
          return size();
      }
      
      public void addResource(Resource resource) throws XMLDBException {
  
          add(resource);
          setCollection();
      }
      
      /**
       * Returns resource from this set
       *
       * @param index index of resource to return
       * @return the resource
       * @exception ClassCastException thrown if for some reason, this
       *            object was wrongly intialized
       */
      public Resource getResource(long index) {
  
          return (XMLResourceImpl) get((int) index);
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/modules/QueryService.java
  
  Index: QueryService.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: QueryService.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.modules;
  
  import org.xmldb.api.base.Service;
  import org.xmldb.api.modules.XPathQueryService;
  import org.xmldb.api.base.Collection;
  import org.apache.xindice.client.rpc.base.CommonConfigurable;
  import org.xmldb.api.base.ResourceSet;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.apache.xindice.client.rpc.base.CollectionImpl;
  import java.util.Hashtable;
  
  /**
   * Abstract query service.
   * 
   * Used to implemented both XUpdate and XPath query
   * service, as the interface for these queries on the XML-RPC API is more
   * or less identical.
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public abstract class QueryService extends CommonConfigurable implements Service {
  
      /* XPath or XUpdate. This property should be set by subclasses */
      protected String queryLang;
      
      /* Collection that this query service should query in */
      Collection collection;
      
      /* namespace bindings in use for this query */
      Hashtable nsMap = new Hashtable();
      
      public String getVersion() {
          
          return "1.0";
      }    
      
      public String getName() {
          
          return queryLang + "QueryService";
      }    
      
      public void setCollection(Collection collection) {
          
          this.collection = collection;
      }
      
      public ResourceSet queryResource(String name, String query)
              throws XMLDBException {
          
          return ((CollectionImpl) collection).query(name, queryLang, query, nsMap);
      }    
  
      public void clearNamespaces() {
          
          nsMap.clear();
      }
      
      public String getNamespace(String prefix) {
          
          return (String) nsMap.get(prefix);
      }
      
      public void setNamespace(String prefix, String namespaceURI) {
          
          nsMap.put(prefix, namespaceURI);
      }
      
      public ResourceSet query(String query) throws XMLDBException {
          
          return ((CollectionImpl) collection).query(queryLang, query, nsMap);
      }
      
      public void removeNamespace(String prefix) throws XMLDBException {
          
          nsMap.remove(prefix);
      }
      
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/modules/XMLResourceImpl.java
  
  Index: XMLResourceImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XMLResourceImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.modules;
  
  import org.apache.xindice.client.rpc.base.CommonConfigurable;
  import org.apache.xindice.client.rpc.base.CollectionImpl;
  import org.xmldb.api.modules.XMLResource;
  import org.w3c.dom.Node;
  import org.xml.sax.ContentHandler;
  import org.xmldb.api.base.XMLDBException;
  import javax.xml.parsers.*;
  import org.xml.sax.InputSource;
  import java.io.StringReader;
  import org.xmldb.api.base.ErrorCodes;
  import org.xml.sax.XMLReader;
  import org.xml.sax.helpers.DefaultHandler;
  import org.apache.xindice.xml.TextWriter;
          
  /**
   * Implementation of XML:DB's <code>XMLResource</code> that uses XML-RPC
   * to dialogue with the server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1
   */
  public class XMLResourceImpl extends CommonConfigurable implements XMLResource {
      
      private String xml;
      
      private String name;
      
      private CollectionImpl coll;
      
      /**
       * Creates new XMLResourceImpl
       *
       * @param name for resource. May be empty, in which case a name can later be
       *       assigned using setId()
       * @param content XML data for resource. May be <code>null</code> in which
       *       case content can be set later with <code>setContent()</code>
       */
      public XMLResourceImpl(String name, String content, CollectionImpl coll) {
          
          this.name = name;
          this.xml = content;
          this.coll = coll;
      }
      
      /**
       * Creates new XMLResourceImpl with <code>null</code> content
       *
       * @param name for resource. May be empty, in which case a name can later be
       *       assigned using setId()
       * @param content XML data for resource. May be <code>null</code> in which
       *       case content can be set later with <code>setContent()</code>
       */
      public XMLResourceImpl(String name, CollectionImpl coll) {
          
          this(name, null, coll);
      }
      
      public void setContentAsDOM(Node node) throws XMLDBException {
          
          setContent(TextWriter.toString(node));
      }
      
      public String getResourceType() {
          
          return "XMLResource";
      }
      
      public org.xml.sax.ContentHandler setContentAsSAX() throws org.xmldb.api.base.XMLDBException {
          
          /* TODO: return string serializer that sets content in
           * <code>endDocument</code>
           */
          return null;
      }
      
      public Object getContent() {
          
          return xml;
      }
      
      public org.w3c.dom.Node getContentAsDOM() throws org.xmldb.api.base.XMLDBException {
          
          try {
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              factory.setNamespaceAware(true);
              DocumentBuilder db = factory.newDocumentBuilder();
              
              return db.parse(new InputSource(new StringReader(xml)));
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      public void getContentAsSAX(ContentHandler contentHandler) throws XMLDBException {
          
          try {
              
              SAXParserFactory factory = SAXParserFactory.newInstance();
              factory.setNamespaceAware(true);
              XMLReader rd = factory.newSAXParser().getXMLReader();
              rd.setContentHandler(contentHandler);
              rd.parse(new InputSource(new StringReader(xml)));
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      public String getDocumentId() {
          
          return name;
      }
      
      public String getId() {
          
          return name;
      }
      
      public void setContent(Object xml) throws org.xmldb.api.base.XMLDBException {
          
          if (!(xml instanceof String)) {
              
              throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE,
              "XMLResource only accept String content");
          }
          
          try {
              
              SAXParserFactory factory = SAXParserFactory.newInstance();
              factory.setNamespaceAware(true);
              XMLReader rd = factory.newSAXParser().getXMLReader();
              rd.setContentHandler(new DefaultHandler());
              rd.parse(new InputSource(new StringReader((String) xml)));
              
              /* No problems => ok set content */
              this.xml = (String) xml;
          } catch (Exception e) {
              
              throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
          }
      }
      
      public org.xmldb.api.base.Collection getParentCollection() throws org.xmldb.api.base.XMLDBException {
          
          return coll;
      }
      
      public void setId(String name) {
          
          this.name = name;
      }
          
  }
  
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/modules/XPathQueryServiceImpl.java
  
  Index: XPathQueryServiceImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XPathQueryServiceImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.modules;
  
  import org.xmldb.api.modules.XPathQueryService;
  import org.xmldb.api.base.XMLDBException;
  
  /**
   * XML:DB XPathQueryService implementation that uses XML-RPC communication
   * with server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public class XPathQueryServiceImpl extends QueryService implements XPathQueryService {
  
      /**
       * Creates new XPathQueryService
       */
      public XPathQueryServiceImpl() {
          
          super();
          queryLang = "XPath";
      }
  }
  
  
  
  1.1                  xml-xindice/java/scratchpad/src/org/apache/xindice/client/rpc/modules/XUpdateQueryServiceImpl.java
  
  Index: XUpdateQueryServiceImpl.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xindice" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999-2001, The dbXML
   * Group, L.L.C., http://www.dbxmlgroup.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XUpdateQueryServiceImpl.java,v 1.1 2002/05/06 14:43:02 jbates Exp $
   */
  
  package org.apache.xindice.client.rpc.modules;
  
  import org.xmldb.api.modules.XUpdateQueryService;
  import org.xmldb.api.base.XMLDBException;
  import org.xmldb.api.base.ErrorCodes;
  import org.xmldb.api.modules.XMLResource;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  
  /**
   * XML:DB XUpdateQueryService implementation that uses XML-RPC communication
   * with server
   *
   * @author James Bates <ja...@amplexor.com>
   * @version 1.0
   */
  public class XUpdateQueryServiceImpl extends QueryService implements XUpdateQueryService {
  
      /**
       * Creates new XUpdateQueryService
       */
      public XUpdateQueryServiceImpl() {
          
          super();
          queryLang = "XUpdate";
      }
      
      /**
       * Extracts modified count from the pseudo-result returned from query
       */
      private long getResultCount(XMLResource resource) throws XMLDBException {
          
          Document doc = (Document) resource.getContentAsDOM();
        
          // Get the src:modified element
          Node node = doc.getDocumentElement().getFirstChild();
        
          // The count is a text node within the element.
          String count = node.getFirstChild().getNodeValue();
        
          try {
             
              return Long.parseLong(count);
         } catch (Exception e) {
           
             throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
         }
      }
     
      /**
       * Performs an XUpdate operation on the entire collection
       */
      public long update(String query) throws XMLDBException {
          
          return getResultCount((XMLResource) query(query).getResource(0));
      }
      
      /**
       * Preforms an XUpdate operation on the specified document
       */
      public long updateResource(String name, String query) throws XMLDBException {
          
          return getResultCount((XMLResource) queryResource(name, query).getResource(0));
      }
  }
  
  
  
  1.2       +2 -2      xml-xindice/java/scratchpad/src/org/apache/xindice/tools/XMLTools.java
  
  Index: XMLTools.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/tools/XMLTools.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLTools.java	3 May 2002 16:35:11 -0000	1.1
  +++ XMLTools.java	6 May 2002 14:43:02 -0000	1.2
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: XMLTools.java,v 1.1 2002/05/03 16:35:11 jbates Exp $
  + * $Id: XMLTools.java,v 1.2 2002/05/06 14:43:02 jbates Exp $
    */
   
   import org.xmldb.api.base.*;
  @@ -327,7 +327,7 @@
                  // Also register XML-RPC driver so it is available to the
                  // cmd-line tools.
                  db = (Database) Class.forName(
  -                        "org.apache.xindice.client.rpc.DatabaseImpl").newInstance();
  +                        "org.apache.xindice.client.rpc.base.DatabaseImpl").newInstance();
                  DatabaseManager.registerDatabase( db );
              }