Package org.apache.xindice.tools.command

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

/*
* 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: TextQuery.java 578602 2007-09-23 20:33:31Z natalia $
*/

package org.apache.xindice.tools.command;

import org.apache.xindice.tools.XMLTools;
import org.apache.xindice.client.TextQueryService;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.DatabaseManager;

/**
* TextQuery is designed to enable the user to query a Collection for
* Documents that match Lucene query.
*
* @version $Revision: 578602 $, $Date: 2007-09-23 16:33:31 -0400 (Sun, 23 Sep 2007) $
*/
public class TextQuery 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;
            }

            TextQueryService service = (TextQueryService) col.getService("TextQueryService", "1.0");

            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;
    }

    public void usage() {
        System.out.println("Format: xindice text -c <context> [-l [-d <path>]] [-v] [parameters...]");
        System.out.println();
        System.out.println("Searches collection for documents matching given text query");
        System.out.println();
        System.out.println("Command-specific switches:");
        System.out.println("    -q|--query <query>");
        System.out.println("                 Lucene text query");
        System.out.println();
    }
}
TOP

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

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.