[prev in list] [next in list] [prev in thread] [next in thread] 

List:       xindice-dev
Subject:    cvs commit: xml-xindice/java/src/org/apache/xindice/client/xmldb/embed CollectionImpl.java
From:       vgritsenko () apache ! org
Date:       2004-02-24 3:20:28
Message-ID: 20040224032028.82199.qmail () minotaur ! apache ! org
[Download RAW message or body]

vgritsenko    2004/02/23 19:20:27

  Modified:    java/src/org/apache/xindice/xml NamespaceMap.java
               java/src/org/apache/xindice/server/rpc/messages Query.java
               java/src/org/apache/xindice/client/xmldb/embed
                        CollectionImpl.java
  Added:       java/src/org/apache/xindice/core/query QueryUtil.java
  Log:
  Reduce code duplication in handling query results
  
  Revision  Changes    Path
  1.1                  \
xml-xindice/java/src/org/apache/xindice/core/query/QueryUtil.java  
  Index: QueryUtil.java
  ===================================================================
  /*
   * 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: QueryUtil.java,v 1.1 2004/02/24 03:20:27 vgritsenko Exp $
   */
  
  package org.apache.xindice.core.query;
  
  import org.apache.xindice.core.data.NodeSet;
  import org.apache.xindice.util.XindiceRuntimeException;
  import org.apache.xindice.xml.NamespaceMap;
  import org.apache.xindice.xml.TextWriter;
  import org.apache.xindice.xml.dom.DBNode;
  import org.apache.xindice.xml.dom.DocumentImpl;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  
  import java.util.Hashtable;
  
  /**
   * Helper to convert NodeSet query result into the Document
   *
   * @version CVS $Revision: 1.1 $, $Date: 2004/02/24 03:20:27 $
   */
  public class QueryUtil {
  
      /**
       * Maps a Hashtable containing namespace definitions into a Xindice
       * NamespaceMap.
       */
      public static NamespaceMap mapNamespaces(Hashtable namespaces) {
          if (namespaces != null && namespaces.size() > 0) {
              return new NamespaceMap(namespaces);
          }
  
          return null;
      }
  
      /**
       * Adds additional meta data to the query results,
       * and turns it into a DOM document.
       * @param expandSource if true, source meta attributes will be added
       */
      public static Document queryResultsToDOM(NodeSet nodeSet, boolean expandSource) \
{  DocumentImpl doc = new DocumentImpl();
  
          Element root = doc.createElement("result");
          doc.appendChild(root);
          int count = 0;
          while (nodeSet != null && nodeSet.hasMoreNodes()) {
              final Object element = nodeSet.getNextNode();
              if (element instanceof Node) {
                  Node n = (Node) element;
  
                  if (n.getNodeType() == Node.DOCUMENT_NODE) {
                      n = ((Document) n).getDocumentElement();
                  }
  
                  if (expandSource && n instanceof DBNode) {
                      ((DBNode) n).expandSource();
                  }
  
                  root.appendChild(doc.importNode(n, true));
              } else if (element instanceof Boolean || element instanceof Double) {
                  root.appendChild(doc.createTextNode(element.toString()));
              } else if (element instanceof String) {
                  root.appendChild(doc.createTextNode((String) element));
              } else {
                  throw new XindiceRuntimeException("Unknown result type (" + \
element.getClass().getName() + ") in nodeset");  }
  
              count++;
          }
  
          root.setAttribute("count", Integer.toString(count));
          return doc;
      }
  
      /**
       * Adds additional meta data to the query results,
       * and turns it into a DOM document.
       */
      public static Document queryResultsToDOM(NodeSet nodeSet) {
          return queryResultsToDOM(nodeSet, true);
      }
  
      /**
       * Adds additional meta data to the query results,
       * and turns it into a String containing XML document.
       * @param expandSource if true, source meta attributes will be added
       */
      public static String queryResultsToString(NodeSet nodeSet, boolean \
                expandSource) {
          return TextWriter.toString(queryResultsToDOM(nodeSet, expandSource));
      }
  
      /**
       * Adds additional meta data to the query results,
       * and turns it into a String containing XML document.
       */
      public static String queryResultsToString(NodeSet nodeSet) {
          return TextWriter.toString(queryResultsToDOM(nodeSet, true));
      }
  }
  
  
  
  1.10      +14 -2     xml-xindice/java/src/org/apache/xindice/xml/NamespaceMap.java
  
  Index: NamespaceMap.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/NamespaceMap.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- NamespaceMap.java	8 Feb 2004 03:50:13 -0000	1.9
  +++ NamespaceMap.java	24 Feb 2004 03:20:27 -0000	1.10
  @@ -27,6 +27,8 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  +import java.util.Enumeration;
  +import java.util.Hashtable;
   
   /**
    * NamespaceMap is just a HashMap extension that provides some useful
  @@ -36,6 +38,16 @@
    */
   public final class NamespaceMap extends HashMap {
       private Element elem = null;
  +
  +    public NamespaceMap() {
  +    }
  +
  +    public NamespaceMap(Hashtable namespaces) {
  +        for (Enumeration keys = namespaces.keys(); keys.hasMoreElements(); ) {
  +            final String key = (String) keys.nextElement();
  +            setNamespace(key, (String) namespaces.get(key));
  +        }
  +    }
   
       public Node getContextNode() {
           if (elem == null) {
  
  
  
  1.11      +12 -100   \
xml-xindice/java/src/org/apache/xindice/server/rpc/messages/Query.java  
  Index: Query.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/server/rpc/messages/Query.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Query.java	19 Feb 2004 02:44:42 -0000	1.10
  +++ Query.java	24 Feb 2004 03:20:27 -0000	1.11
  @@ -20,16 +20,9 @@
   
   import org.apache.xindice.core.Collection;
   import org.apache.xindice.core.data.NodeSet;
  +import org.apache.xindice.core.query.QueryUtil;
   import org.apache.xindice.server.rpc.RPCDefaultMessage;
  -import org.apache.xindice.util.XindiceRuntimeException;
   import org.apache.xindice.xml.NamespaceMap;
  -import org.apache.xindice.xml.TextWriter;
  -import org.apache.xindice.xml.dom.DBNode;
  -import org.apache.xindice.xml.dom.DocumentImpl;
  -
  -import org.w3c.dom.Document;
  -import org.w3c.dom.Element;
  -import org.w3c.dom.Node;
   
   import java.util.Enumeration;
   import java.util.Hashtable;
  @@ -47,109 +40,28 @@
               throw new Exception(MISSING_COLLECTION_PARAM);
           }
   
  -        if (!message.containsKey(TYPE)) {
  +        final String type = (String) message.get(TYPE);
  +        if (type == null) {
               throw new Exception(MISSING_TYPE_PARAM);
           }
   
  -        if (!message.containsKey(QUERY)) {
  +        final String query = (String) message.get(QUERY);
  +        if (query == null) {
               throw new Exception(MISSING_QUERY_PARAM);
           }
   
           Collection col = getCollection((String) message.get(COLLECTION));
  -        NodeSet ns = null;
  +        NamespaceMap nsMap = QueryUtil.mapNamespaces((Hashtable) \
message.get(NAMESPACES));  
  +        NodeSet ns;
           if (message.containsKey(NAME)) {
  -            ns = col.queryDocument((String) message.get(TYPE),
  -                                   (String) message.get(QUERY),
  -                                   mapNamespaces((Hashtable) \
                message.get(NAMESPACES)),
  -                                   message.get(NAME));
  +            ns = col.queryDocument(type, query, nsMap, message.get(NAME));
           } else {
  -            ns = col.queryCollection((String) message.get(TYPE),
  -                                     (String) message.get(QUERY),
  -                                     mapNamespaces((Hashtable) \
message.get(NAMESPACES)));  +            ns = col.queryCollection(type, query, \
nsMap);  }
   
           Hashtable result = new Hashtable();
  -        result.put(RESULT, queryWrapper(ns));
  +        result.put(RESULT, QueryUtil.queryResultsToString(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
  -     */
  -    private static String queryWrapper(NodeSet ns) {
  -
  -        //
  -        // FIXME: There is a copy of this same code in \
                org.apache.xindice.client.xmldb.embed.CollectionImpl#nodesetToDocument
                
  -        //        It should be refactored into some common place. May be, \
                NodeSetUtil?
  -        //
  -
  -        // 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()) {
  -            final Object element = ns.getNextNode();
  -            if (element instanceof Node)
  -            {
  -                Node n = (Node) element;
  -
  -                if (n.getNodeType() == Node.DOCUMENT_NODE) {
  -                    n = ((Document) n).getDocumentElement();
  -                }
  -
  -                if (n instanceof DBNode) {
  -                    ((DBNode) n).expandSource();
  -                }
  -
  -                root.appendChild(doc.importNode(n, true));
  -            }
  -            else if (element instanceof Boolean || element instanceof Double)
  -            {
  -                root.appendChild(doc.createTextNode(element.toString()));
  -            }
  -            else if (element instanceof String)
  -            {
  -                root.appendChild(doc.createTextNode((String) element));
  -            }
  -            else
  -            {
  -                throw new XindiceRuntimeException("Unknown result type (" + \
                element.getClass().getName() + ") in nodeset");
  -            }
  -
  -            count++;
  -
  -        }
  -
  -        root.setAttribute("count", Integer.toString(count));
  -        return TextWriter.toString(doc);
       }
   }
  
  
  
  1.33      +11 -73    \
xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java  
  Index: CollectionImpl.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/client/xmldb/embed/CollectionImpl.java,v
  retrieving revision 1.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- CollectionImpl.java	19 Feb 2004 02:50:44 -0000	1.32
  +++ CollectionImpl.java	24 Feb 2004 03:20:27 -0000	1.33
  @@ -18,10 +18,6 @@
   
   package org.apache.xindice.client.xmldb.embed;
   
  -import java.util.Enumeration;
  -import java.util.Hashtable;
  -import java.util.StringTokenizer;
  -
   import org.apache.xindice.client.xmldb.ResourceSetImpl;
   import org.apache.xindice.client.xmldb.XindiceCollection;
   import org.apache.xindice.client.xmldb.resources.BinaryResourceImpl;
  @@ -31,11 +27,10 @@
   import org.apache.xindice.core.FaultCodes;
   import org.apache.xindice.core.data.NodeSet;
   import org.apache.xindice.core.meta.MetaData;
  +import org.apache.xindice.core.query.QueryUtil;
   import org.apache.xindice.util.Configuration;
  -import org.apache.xindice.util.XindiceRuntimeException;
  -import org.apache.xindice.xml.NamespaceMap;
  -import org.apache.xindice.xml.dom.DBNode;
   import org.apache.xindice.xml.dom.DocumentImpl;
  +
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   import org.w3c.dom.Node;
  @@ -46,6 +41,9 @@
   import org.xmldb.api.modules.BinaryResource;
   import org.xmldb.api.modules.XMLResource;
   
  +import java.util.Hashtable;
  +import java.util.StringTokenizer;
  +
   /**
    * Implementation of XML:DB's <code>Collection</code> interface using
    * direct access to interact with database server
  @@ -429,14 +427,14 @@
   
           checkOpen();
           try {
  -            NodeSet result;
  +            NodeSet nodeSet;
               if (name != null) {
  -                result = col.queryDocument(queryLang, query, \
createNamespaceMap(nsMap), name);  +                nodeSet = \
col.queryDocument(queryLang, query, QueryUtil.mapNamespaces(nsMap), name);  } else {
  -                result = col.queryCollection(queryLang, query, \
createNamespaceMap(nsMap));  +                nodeSet = \
col.queryCollection(queryLang, query, QueryUtil.mapNamespaces(nsMap));  }
   
  -            return new ResourceSetImpl(this, nodesetToDocument(result));
  +            return new ResourceSetImpl(this, \
QueryUtil.queryResultsToDOM(nodeSet));  } catch (Exception e) {
               throw FaultCodes.createXMLDBException(FaultCodes.QRY_PROCESSING_ERROR,
                                                     "Query error: " + \
e.getMessage(), e);  @@ -556,66 +554,6 @@
           } catch (Exception e) {
               throw FaultCodes.createXMLDBException(e);
           }
  -    }
  -
  -    // search result handling
  -
  -    private static Document nodesetToDocument(NodeSet ns) {
  -
  -        //
  -        // FIXME: There is a copy of this same code in \
                org.apache.xindice.server.xmldb.rpc.messages.Query#queryWrapper
  -        //        It should be refactored into some common place. May be, \
                NodeSetUtil?
  -        //
  -
  -        // 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()) {
  -            final Object element = ns.getNextNode();
  -            if (element instanceof Node) {
  -                Node n = (Node) element;
  -
  -                if (n.getNodeType() == Node.DOCUMENT_NODE) {
  -                    n = ((Document) n).getDocumentElement();
  -                }
  -
  -                if (n instanceof DBNode) {
  -                    ((DBNode) n).expandSource();
  -                }
  -
  -                root.appendChild(doc.importNode(n, true));
  -            } else if (element instanceof Boolean || element instanceof Double) {
  -                root.appendChild(doc.createTextNode(element.toString()));
  -            } else if (element instanceof String) {
  -                root.appendChild(doc.createTextNode((String) element));
  -            } else {
  -                throw new XindiceRuntimeException("Unknown result type (" + \
                element.getClass().getName() + ") in nodeset");
  -            }
  -
  -            count++;
  -        }
  -
  -        root.setAttribute("count", Integer.toString(count));
  -        return doc;
  -    }
  -
  -    private NamespaceMap createNamespaceMap(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;
       }
   
       public MetaData getMetaData(String id) throws XMLDBException {
  
  
  


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic