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 593399 2007-11-09 02:27:03Z natalia $
*/

package org.apache.xindice.tools.command;

import org.apache.xindice.tools.XMLTools;

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.StringTokenizer;

/**
* SingleDocumentQuery is designed to enable the user/admin to XPathQuery
* a Collection for a Single Document.
*
* @version $Revision: 593399 $, $Date: 2007-11-08 21:27:03 -0500 (Thu, 08 Nov 2007) $
*/
public class XPathQuery extends Command {

    public boolean execute(XMLTools.Config table) throws Exception {

        if (table.getString(XMLTools.COLLECTION) == null) {
            System.out.println("ERROR : Collection name and switch required");
            return false;
        }

        if ("".equals(table.getString(XMLTools.QUERY))) {
            System.out.println("ERROR : Query and switch required");
            return false;
        }

        Collection col = null;
        try {
            String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
                                                      table.getBoolean(XMLTools.LOCAL));
            String querystring = table.getString(XMLTools.QUERY);

            col = DatabaseManager.getCollection(colstring);

            if (col == null) {
                System.out.println("ERROR : Collection not found!");
                return false;
            }

            XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
            addNamespaces(service, table.getString(XMLTools.NAMESPACES));

            ResourceSet resultSet = service.query(querystring);
            ResourceIterator results = resultSet.getIterator();

            while (results.hasMoreResources()) {
                XMLResource resource = (XMLResource) results.nextResource();
                String documentstr = (String) resource.getContent();
                System.out.println(documentstr);
            }
        } 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());
            }
        }
    }

    public void usage() {
        System.out.println("Format: xindice xpath -c <context> [-l [-d <path>]] [-v] [parameters...]");
        System.out.println();
        System.out.println("Searches collection for documents matching given XPath query");
        System.out.println();
        System.out.println("Command-specific switches:");
        System.out.println("    -q|--query <query>");
        System.out.println("                 XPath query");
        System.out.println("    -s|--namespaces <namespaces>");
        System.out.println("                 Semicolon delimited list of namespaces for query");
        System.out.println("                 in the form prefix=namespace-uri");
        System.out.println();
        System.out.println("Examples:");
        System.out.println("    xindice xpath -c /db/test/ocs -q test");
        System.out.println("    xindice xpath -c /db/test -s a=http://somedomain.com/schema.xsd -q /a:foo");
        System.out.println();
    }
}
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.