Search.java revision 38115c792a2142d264ca2617b11962301f079c0a
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye/*
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * CDDL HEADER START
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye *
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * The contents of this file are subject to the terms of the
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * Common Development and Distribution License (the "License").
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * You may not use this file except in compliance with the License.
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye *
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * See LICENSE.txt included in this distribution for the specific
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * language governing permissions and limitations under the License.
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye *
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * When distributing Covered Code, include this CDDL HEADER in each
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * file and include the License file at LICENSE.txt.
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * If applicable, add the following below this CDDL HEADER, with the
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * fields enclosed by brackets "[]" replaced with your own identifying
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * information: Portions Copyright [yyyy] [name of copyright owner]
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye *
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * CDDL HEADER END
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye */
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye/*
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * Use is subject to license terms.
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye */
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyepackage org.opensolaris.opengrok.search;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.io.File;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.util.ArrayList;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.util.List;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport org.opensolaris.opengrok.search.scope.SearchEngine;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport org.opensolaris.opengrok.util.Getopt;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye/**
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * Search and list the matching files
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye */
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeclass Search {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye /**
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye * usage Search index "query" prunepath
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye */
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye public static void main(String[] argv) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -R <configuration.xml> Read configuration from the specified file\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -d Symbol Definitions\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -r Symbol References\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -p Path\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -h History\n"
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye + "\t -f Full text";
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye SearchEngine engine = new SearchEngine();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye boolean config = false;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye try {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye getopt.parse();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye } catch (Exception e) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println(e.getMessage());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println(usage);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.exit(1);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye int cmd;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye while ((cmd = getopt.getOpt()) != -1) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye switch (cmd) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'R':
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye try {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye RuntimeEnvironment.getInstance().readConfiguration(new File(getopt.getOptarg()));
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye config = true;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye } catch (Exception e) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println("Failed to read config file: ");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye e.printStackTrace();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.exit(1);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'd':
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.setDefinition(getopt.getOptarg());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'r':
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.setSymbol(getopt.getOptarg());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'p':
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.setFile(getopt.getOptarg());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'h':
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.setHistory(getopt.getOptarg());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye case 'f' :
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.setFreetext(getopt.getOptarg());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye break;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye default:
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println("Unknown option: " + (char) cmd);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println(usage);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.exit(1);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye if (!config) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println("You must specify a configuration file");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println(usage);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.exit(1);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye if (!engine.isValidQuery()) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println("You did not specify a valid query");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println(usage);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.exit(1);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye int nhits = engine.search();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye if (nhits == 0) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.err.println("Your search \"" + engine.getQuery() + "\" did not match any files.");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye } else {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye List<Hit> hits = new ArrayList<Hit>();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye engine.more(0, nhits, hits);
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye String root = RuntimeEnvironment.getInstance().getSourceRootPath();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye for (Hit hit : hits) {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye File file = new File(root, hit.getFilename());
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye System.out.println(file.getAbsolutePath() + ": [" + hit.getLine() + "]");
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye private Search() {
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye }
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye}