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