/*
* 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: XPathQuery.java,v 1.12 2004/02/08 02:57:35 vgritsenko Exp $
*/
package org.apache.xindice.tools.command;
import org.apache.xindice.tools.XMLTools;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.modules.XPathQueryService;
import java.util.Hashtable;
import java.util.StringTokenizer;
/**
* SingleDocumentQuery is designed to enable the user/admin to XPathQuery
* a Collection for a Single Document.
*
* @version CVS $Revision: 1.12 $, $Date: 2004/02/08 02:57:35 $
*/
public class XPathQuery extends Command {
private static final Log log = LogFactory.getLog(XPathQuery.class);
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
try {
if ((String) table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection name and switch required");
return false;
}
if ((String) table.get(XMLTools.QUERY) == "") {
System.out.println("ERROR : Query and switch required");
return false;
}
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION), (String) table.get(XMLTools.LOCAL));
String querystring = (String) table.get(XMLTools.QUERY);
XPathQueryService service = null;
ResourceIterator results = null;
col = DatabaseManager.getCollection(colstring);
if (col == null) {
System.out.println("ERROR : Collection not found!");
return false;
}
service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
addNamespaces(service, (String) table.get("namespaces"));
ResourceSet resultSet = service.query(querystring);
results = resultSet.getIterator();
while (results.hasMoreResources()) {
XMLResource resource = (XMLResource) results.nextResource();
String documentstr = (String) resource.getContent();
System.out.println(documentstr);
}
} catch (Exception e) {
System.out.println("ERROR : " + e.getMessage());
if (table.get(XMLTools.VERBOSE).equals("true")) {
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
return false;
} finally {
if (col != null) {
col.close();
}
}
return true;
}
private void addNamespaces(XPathQueryService service, String namespacesString) throws XMLDBException {
if ((namespacesString != "") && (namespacesString != null)) {
StringTokenizer st = new StringTokenizer(namespacesString, "=;");
if (st.countTokens() % 2 != 0) {
System.out.println("ERROR : mismatched namespace prefixes and uris");
return;
}
while (st.hasMoreTokens()) {
service.setNamespace(st.nextToken(), st.nextToken());
}
}
}
}