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