Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 116   Methods: 3
NCLOC: 75   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XPathQuery.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: XPathQuery.java 593399 2007-11-09 02:27:03Z natalia $
 18    */
 19   
 20    package org.apache.xindice.tools.command;
 21   
 22    import org.apache.xindice.tools.XMLTools;
 23   
 24    import org.xmldb.api.DatabaseManager;
 25    import org.xmldb.api.base.Collection;
 26    import org.xmldb.api.base.ResourceIterator;
 27    import org.xmldb.api.base.ResourceSet;
 28    import org.xmldb.api.base.XMLDBException;
 29    import org.xmldb.api.modules.XMLResource;
 30    import org.xmldb.api.modules.XPathQueryService;
 31   
 32    import java.util.StringTokenizer;
 33   
 34    /**
 35    * SingleDocumentQuery is designed to enable the user/admin to XPathQuery
 36    * a Collection for a Single Document.
 37    *
 38    * @version $Revision: 593399 $, $Date: 2007-11-09 02:27:03 +0000 (Fri, 09 Nov 2007) $
 39    */
 40    public class XPathQuery extends Command {
 41   
 42  0 public boolean execute(XMLTools.Config table) throws Exception {
 43   
 44  0 if (table.getString(XMLTools.COLLECTION) == null) {
 45  0 System.out.println("ERROR : Collection name and switch required");
 46  0 return false;
 47    }
 48   
 49  0 if ("".equals(table.getString(XMLTools.QUERY))) {
 50  0 System.out.println("ERROR : Query and switch required");
 51  0 return false;
 52    }
 53   
 54  0 Collection col = null;
 55  0 try {
 56  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 57    table.getBoolean(XMLTools.LOCAL));
 58  0 String querystring = table.getString(XMLTools.QUERY);
 59   
 60  0 col = DatabaseManager.getCollection(colstring);
 61   
 62  0 if (col == null) {
 63  0 System.out.println("ERROR : Collection not found!");
 64  0 return false;
 65    }
 66   
 67  0 XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
 68  0 addNamespaces(service, table.getString(XMLTools.NAMESPACES));
 69   
 70  0 ResourceSet resultSet = service.query(querystring);
 71  0 ResourceIterator results = resultSet.getIterator();
 72   
 73  0 while (results.hasMoreResources()) {
 74  0 XMLResource resource = (XMLResource) results.nextResource();
 75  0 String documentstr = (String) resource.getContent();
 76  0 System.out.println(documentstr);
 77    }
 78    } finally {
 79  0 if (col != null) {
 80  0 col.close();
 81    }
 82    }
 83   
 84  0 return true;
 85    }
 86   
 87  0 private void addNamespaces(XPathQueryService service, String namespacesString) throws XMLDBException {
 88  0 if (namespacesString != null && namespacesString.length() > 0) {
 89  0 StringTokenizer st = new StringTokenizer(namespacesString, "=;");
 90  0 if (st.countTokens() % 2 != 0) {
 91  0 throw new XMLDBException(0, "mismatched namespace prefixes and uris in '" + namespacesString + "'");
 92    }
 93  0 while (st.hasMoreTokens()) {
 94  0 service.setNamespace(st.nextToken(), st.nextToken());
 95    }
 96    }
 97    }
 98   
 99  0 public void usage() {
 100  0 System.out.println("Format: xindice xpath -c <context> [-l [-d <path>]] [-v] [parameters...]");
 101  0 System.out.println();
 102  0 System.out.println("Searches collection for documents matching given XPath query");
 103  0 System.out.println();
 104  0 System.out.println("Command-specific switches:");
 105  0 System.out.println(" -q|--query <query>");
 106  0 System.out.println(" XPath query");
 107  0 System.out.println(" -s|--namespaces <namespaces>");
 108  0 System.out.println(" Semicolon delimited list of namespaces for query");
 109  0 System.out.println(" in the form prefix=namespace-uri");
 110  0 System.out.println();
 111  0 System.out.println("Examples:");
 112  0 System.out.println(" xindice xpath -c /db/test/ocs -q test");
 113  0 System.out.println(" xindice xpath -c /db/test -s a=http://somedomain.com/schema.xsd -q /a:foo");
 114  0 System.out.println();
 115    }
 116    }