kward       02/04/03 17:17:20

  Modified:    java/scratchpad/src/org/apache/xindice/server/rpc
                        RPCDefaultMessage.java
               java/scratchpad/src/org/apache/xindice/server/rpc/messages
                        Query.java
  Log:
  added initial query method to rpc
  
  Revision  Changes    Path
  1.4       +3 -1      
xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCDefaultMessage.java
  
  Index: RPCDefaultMessage.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/RPCDefaultMessage.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RPCDefaultMessage.java    2 Apr 2002 22:57:05 -0000       1.3
  +++ RPCDefaultMessage.java    4 Apr 2002 01:17:20 -0000       1.4
  @@ -56,7 +56,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: RPCDefaultMessage.java,v 1.3 2002/04/02 22:57:05 kstaken Exp $
  + * $Id: RPCDefaultMessage.java,v 1.4 2002/04/04 01:17:20 kward Exp $
    */
   
   import java.util.Hashtable;
  @@ -76,6 +76,8 @@
      public static final String MAXKEYSIZE = "maxkeysize";
      public static final String PAGESIZE = "pagesize";
      public static final String TYPE = "type";
  +   public static final String QUERY = "query";
  +   public static final String NAMESPACES = "namespaces";
      
      public Hashtable execute(Hashtable message) throws Exception {
         return null;
  
  
  
  1.2       +79 -5     
xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/messages/Query.java
  
  Index: Query.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/scratchpad/src/org/apache/xindice/server/rpc/messages/Query.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Query.java        2 Apr 2002 22:57:05 -0000       1.1
  +++ Query.java        4 Apr 2002 01:17:20 -0000       1.2
  @@ -56,28 +56,102 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * $Id: Query.java,v 1.1 2002/04/02 22:57:05 kstaken Exp $
  + * $Id: Query.java,v 1.2 2002/04/04 01:17:20 kward Exp $
    */
   
  +import java.util.Enumeration;
   import java.util.Hashtable;
   import java.io.*;
   
   import org.w3c.dom.*;
   
   import org.apache.xindice.core.*;
  +import org.apache.xindice.core.data.*;
   import org.apache.xindice.xml.*;
  +import org.apache.xindice.xml.dom.*;
   import org.apache.xindice.server.rpc.*;
   
  -/**
  - *
  - */
  +   /**
  +    * Executes a query against a document or collection
  +    * @return The result of the query as XML.
  +    */
  +
   public class Query extends RPCDefaultMessage {   
      
      public Hashtable execute(Hashtable message) throws Exception {
  +
         Collection col = getCollection( (String) message.get(COLLECTION) );
  +      NodeSet ns = null;
  +
  +      if (message.containsKey( NAME )) {
  +
  +         ns = col.queryDocument( (String)message.get(TYPE), 
(String)message.get(QUERY), mapNamespaces( (Hashtable)message.get(NAMESPACES) 
), (String)message.get(NAME) );
  +
  +      } else {
  +
  +         ns = col.queryCollection( (String)message.get(TYPE), 
(String)message.get(QUERY), mapNamespaces( (Hashtable)message.get(NAMESPACES) ) 
);
  +      }
   
         Hashtable result = new Hashtable();
  -      result.put(RESULT, null);
  +      result.put(RESULT, queryWrapper( ns ));
         return result;
      }
  +
  +   /**
  +    * Maps a Hashtable containing namespace definitions into a Xindice
  +    * NamespaceMap.
  +    *
  +    * @param namespaces
  +    * @return A NamespaceMap
  +    */
  +   protected NamespaceMap mapNamespaces( Hashtable namespaces ) {
  +      NamespaceMap nsMap = null;
  +      if ( namespaces.size() > 0 ) {
  +         nsMap = new NamespaceMap();
  +
  +         Enumeration keys = namespaces.keys();
  +         while ( keys.hasMoreElements() ) {
  +            String key = ( String ) keys.nextElement();
  +            nsMap.setNamespace( key, ( String ) namespaces.get( key ) );
  +         }
  +      }
  +
  +      return nsMap;
  +   }
  +
  +   /**
  +    * Adds additional meta data to the query response and turns it into a
  +    * Document.
  +    *
  +    * @param ns The NodeSet containing the query results.
  +    * @return the result set as an XML document
  +    * @exception Exception
  +    */
  +   private String queryWrapper( NodeSet ns ) throws Exception {
  +      // Turn the NodeSet into a document.
  +      DocumentImpl doc = new DocumentImpl();
  +
  +      Element root = doc.createElement( "result" );
  +      doc.appendChild( root );
  +      int count = 0;
  +      while ( ns != null && ns.hasMoreNodes() ) {
  +         Node n = ns.getNextNode();
  +
  +         if ( n.getNodeType() == Node.DOCUMENT_NODE ) {
  +            n = ( ( Document ) n ).getDocumentElement();
  +         }
  +
  +         if ( n instanceof DBNode ) {
  +            ( ( DBNode ) n ).expandSource();
  +         }
  +
  +         root.appendChild( doc.importNode( n, true ) );
  +         count++;
  +      }
  +
  +      root.setAttribute( "count", Integer.toString( count ) );
  +
  +      return TextWriter.toString( doc );
  +   }
  +
   }
  
  
  

Reply via email to