Package org.apache.xindice.tools.command

Source Code of org.apache.xindice.tools.command.XPathQuery

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*
* $Id: XPathQuery.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/

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 $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
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 (table.get(XMLTools.COLLECTION) == null) {
                System.out.println("ERROR : Collection name and switch required");
                return false;
            }

            if ("".equals(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 != null && namespacesString.length() > 0) {
            StringTokenizer st = new StringTokenizer(namespacesString, "=;");
            if (st.countTokens() % 2 != 0) {
                throw new XMLDBException(0, "mismatched namespace prefixes and uris in '" + namespacesString + "'");
            }
            while (st.hasMoreTokens()) {
                service.setNamespace(st.nextToken(), st.nextToken());
            }
        }
    }
}
TOP

Related Classes of org.apache.xindice.tools.command.XPathQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.