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