Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 155   Methods: 4
NCLOC: 107   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XPathSearchCollectionViewer.java 0% 0% 0% 0%
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: XPathSearchCollectionViewer.java 711958 2008-11-06 20:12:54Z natalia $
 18    */
 19   
 20    package org.apache.xindice.webadmin.viewer.components;
 21   
 22    import java.io.IOException;
 23    import java.io.Writer;
 24   
 25    import javax.servlet.ServletException;
 26    import javax.servlet.http.HttpServletRequest;
 27    import javax.servlet.http.HttpServletResponse;
 28   
 29    import org.w3c.dom.Node;
 30   
 31    import org.apache.xindice.xml.NamespaceMap;
 32    import org.apache.xindice.core.Collection;
 33    import org.apache.xindice.core.DBException;
 34    import org.apache.xindice.core.data.NodeSet;
 35    import org.apache.xindice.webadmin.viewer.*;
 36    import org.apache.xindice.webadmin.viewer.HtmlViewerComponent;
 37    import org.apache.xindice.xml.Xml2HtmlWriter;
 38    import org.apache.commons.logging.Log;
 39    import org.apache.commons.logging.LogFactory;
 40   
 41    /**
 42    * Xindice Html Viewer for collection xpath searches.
 43    *
 44    * @author <a href="mailto:jmetzner@apache.org">Jan Metzner</a>
 45    * @version $Revision: 711958 $, $Date: 2008-11-06 20:12:54 +0000 (Thu, 06 Nov 2008) $
 46    */
 47    public class XPathSearchCollectionViewer extends HtmlViewerComponent implements HtmlCollectionViewer {
 48   
 49    private static final Log log = LogFactory.getLog(XPathSearchCollectionViewer.class);
 50   
 51  0 public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
 52    throws ServletException, IOException {
 53   
 54  0 String query = req.getParameter("query");
 55  0 if (query == null) {
 56  0 query = "";
 57    }
 58   
 59  0 String path = col.getCanonicalName();
 60  0 String title = "XPATH Query for " + path;
 61  0 Writer output = startViewer(title, path, res);
 62   
 63    // get namespaces
 64  0 String[] ns = {"", ""};
 65  0 int nsSize = 1;
 66  0 String rawNsSize = req.getParameter("nssize");
 67  0 if (rawNsSize != null) {
 68  0 nsSize = Integer.parseInt(rawNsSize);
 69  0 ns = new String[nsSize * 2];
 70  0 int index = 0;
 71  0 for (int i = 1; i <= nsSize; i++) {
 72  0 String prefix = req.getParameter("prefix" + i);
 73  0 String uri = req.getParameter("uri" + i);
 74  0 if (prefix != null) {
 75  0 ns[index] = prefix;
 76    } else {
 77  0 ns[index] = "";
 78    }
 79  0 index++;
 80  0 if (uri != null) {
 81  0 ns[index] = uri;
 82    } else {
 83  0 ns[index] = "";
 84    }
 85  0 index++;
 86    }
 87    }
 88   
 89  0 printQueryForm(output, query, path, ns);
 90  0 output.write("<br />\n");
 91  0 output.flush();
 92   
 93  0 if (query.length() > 0) { // print result
 94  0 NamespaceMap nsMap = new NamespaceMap();
 95  0 int index = 0;
 96  0 for (int i = 0; i < nsSize; i++) {
 97  0 nsMap.setNamespace(ns[index++], ns[index++]);
 98    }
 99  0 try {
 100  0 NodeSet result = col.queryCollection("XPath", query, nsMap);
 101  0 printQuery(result, output, query, path);
 102    } catch (DBException e) {
 103  0 log.error(e);
 104  0 printStartTable("Error while XPath Query", output);
 105  0 printStartSingleTableBox(output);
 106  0 output.write("<b>Error while XPath Query:</b><br />");
 107  0 output.write(e.getMessage());
 108  0 printEndSingleTableBox(output);
 109  0 printEndTable(output);
 110    }
 111    }
 112   
 113  0 finishViewer(output);
 114    }
 115   
 116  0 protected String getName() {
 117  0 return "xpath";
 118    }
 119   
 120  0 private void printQuery(NodeSet result, Writer output, String query, String path)
 121    throws IOException {
 122   
 123  0 String resultTitle = "Result for Query " + query;
 124  0 printStartTable(resultTitle, output);
 125   
 126  0 if (!result.hasMoreNodes()) {
 127  0 printStartSingleTableBox(output);
 128  0 output.write("No result for query " + query + "");
 129  0 printEndSingleTableBox(output);
 130    }
 131   
 132  0 while (result.hasMoreNodes()) {
 133  0 Object node = result.getNextNode();
 134   
 135  0 printStartSingleTableBox(output);
 136  0 if (node instanceof Node) {
 137  0 Xml2HtmlWriter.write((Node) node, output);
 138    } else {
 139    // TODO print something, that makes sense
 140  0 output.write("Node is no Node");
 141    }
 142  0 printEndSingleTableBox(output);
 143    }
 144  0 printEndTable(output);
 145    }
 146   
 147  0 private void printQueryForm(Writer output, String query, String path, String[] ns)
 148    throws IOException {
 149  0 String formTitle = "XPath Query for Collection " + path;
 150  0 printStartTable(formTitle, output);
 151  0 printSearchFromRow("xpath", query, ns, output);
 152  0 printEndTable(output);
 153    }
 154   
 155    }