Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 154   Methods: 5
NCLOC: 85   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
QueryUtil.java 63.6% 88.6% 80% 80.3%
coverage coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: QueryUtil.java 595817 2007-11-16 20:49:03Z vgritsenko $
 18    */
 19   
 20    package org.apache.xindice.core.query;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24    import org.apache.xindice.client.xmldb.XindiceCollection;
 25    import org.apache.xindice.core.data.NodeSet;
 26    import org.apache.xindice.util.XindiceRuntimeException;
 27    import org.apache.xindice.xml.NamespaceMap;
 28    import org.apache.xindice.xml.TextWriter;
 29    import org.apache.xindice.xml.dom.DBNode;
 30    import org.apache.xindice.xml.dom.DocumentImpl;
 31    import org.apache.xindice.xml.dom.NodeImpl;
 32   
 33    import org.w3c.dom.Attr;
 34    import org.w3c.dom.Comment;
 35    import org.w3c.dom.Document;
 36    import org.w3c.dom.Element;
 37    import org.w3c.dom.Node;
 38    import org.w3c.dom.ProcessingInstruction;
 39    import org.w3c.dom.Text;
 40   
 41    import java.util.Map;
 42   
 43    /**
 44    * Helper to convert NodeSet query result into the Document
 45    *
 46    * @version $Revision: 595817 $, $Date: 2007-11-16 20:49:03 +0000 (Fri, 16 Nov 2007) $
 47    */
 48    public class QueryUtil {
 49   
 50    private static final Log log = LogFactory.getLog(QueryUtil.class);
 51   
 52    /**
 53    * Maps a Hashtable containing namespace definitions into a Xindice
 54    * NamespaceMap.
 55    */
 56  357 public static NamespaceMap mapNamespaces(Map namespaces) {
 57  357 if (namespaces != null && namespaces.size() > 0) {
 58  9 return new NamespaceMap(namespaces);
 59    }
 60   
 61  348 return null;
 62    }
 63   
 64    /**
 65    * Adds additional meta data to the query results,
 66    * and turns it into a DOM document.
 67    * @param expandSource if true, source meta attributes will be added
 68    */
 69  357 public static Document queryResultsToDOM(NodeSet nodeSet, boolean expandSource) {
 70  357 DocumentImpl doc = new DocumentImpl();
 71   
 72  357 Element root = doc.createElement("result");
 73  357 doc.appendChild(root);
 74  357 int count = 0;
 75  357 while (nodeSet != null && nodeSet.hasMoreNodes()) {
 76  531 final Object element = nodeSet.getNextNode();
 77  531 if (element instanceof Attr) {
 78  27 Attr n = (Attr) element;
 79   
 80  27 Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
 81  27 holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
 82  27 holder.setAttributeNode((Attr) doc.importNode(n, true));
 83   
 84  27 if (expandSource && n instanceof DBNode) {
 85  27 ((DBNode) holder).setSource(((DBNode) n).getSource());
 86  27 ((DBNode) holder).expandSource();
 87    }
 88   
 89  27 root.appendChild(holder);
 90  504 } else if (element instanceof Text || element instanceof Comment) {
 91  36 Node n = (Node) element;
 92   
 93  36 Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
 94  36 holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
 95  36 holder.appendChild(doc.importNode(n, true));
 96   
 97  36 if (expandSource && n instanceof DBNode) {
 98  36 ((DBNode) holder).setSource(((DBNode) n).getSource());
 99  36 ((DBNode) holder).expandSource();
 100    }
 101   
 102  36 root.appendChild(holder);
 103  468 } else if (element instanceof ProcessingInstruction) {
 104  0 if (log.isWarnEnabled()) {
 105  0 log.warn("XPath query with ProcessingInstruction result is not supported");
 106    }
 107  468 } else if (element instanceof Node) {
 108  468 Node n = (Node) element;
 109   
 110  468 if (n.getNodeType() == Node.DOCUMENT_NODE) {
 111  0 n = ((Document) n).getDocumentElement();
 112    }
 113   
 114  468 if (expandSource && n instanceof DBNode) {
 115  468 ((DBNode) n).expandSource();
 116    }
 117   
 118  468 root.appendChild(doc.importNode(n, true));
 119    } else {
 120  0 throw new XindiceRuntimeException("Unknown result type (" + element.getClass().getName() + ") in nodeset");
 121    }
 122   
 123  531 count++;
 124    }
 125   
 126  357 root.setAttribute("count", Integer.toString(count));
 127  357 return doc;
 128    }
 129   
 130    /**
 131    * Adds additional meta data to the query results,
 132    * and turns it into a DOM document.
 133    */
 134  238 public static Document queryResultsToDOM(NodeSet nodeSet) {
 135  238 return queryResultsToDOM(nodeSet, true);
 136    }
 137   
 138    /**
 139    * Adds additional meta data to the query results,
 140    * and turns it into a String containing XML document.
 141    * @param expandSource if true, source meta attributes will be added
 142    */
 143  0 public static String queryResultsToString(NodeSet nodeSet, boolean expandSource) {
 144  0 return TextWriter.toString(queryResultsToDOM(nodeSet, expandSource));
 145    }
 146   
 147    /**
 148    * Adds additional meta data to the query results,
 149    * and turns it into a String containing XML document.
 150    */
 151  119 public static String queryResultsToString(NodeSet nodeSet) {
 152  119 return TextWriter.toString(queryResultsToDOM(nodeSet, true));
 153    }
 154    }