Search.java revision 816
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
816N/Aimport java.io.BufferedReader;
289N/Aimport java.io.File;
816N/Aimport java.io.IOException;
816N/Aimport java.io.InputStreamReader;
816N/Aimport java.io.UnsupportedEncodingException;
289N/Aimport java.util.ArrayList;
289N/Aimport java.util.List;
816N/Aimport java.util.logging.Level;
816N/Aimport java.util.logging.Logger;
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
552N/A private SearchEngine engine;
538N/A final List<Hit> results = new ArrayList<Hit>();
816N/A int totalResults =0;
816N/A int nhits=0;
536N/A
538N/A @SuppressWarnings({"PMD.SwitchStmtsShouldHaveDefault"})
536N/A protected boolean parseCmdLine(String[] argv) {
552N/A engine = new SearchEngine();
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
552N/A if (engine == null || !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();
816N/A nhits = engine.search();
535N/A if (nhits > 0) {
816N/A engine.results(0, nhits, results);
535N/A }
816N/A totalResults = engine.totalHits;
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();
816N/A System.out.println("Printing results 1 - " + nhits +" of " + totalResults + " total matching documents collected.");
536N/A for (Hit hit : results) {
536N/A File file = new File(root, hit.getFilename());
816N/A System.out.println(file.getAbsolutePath() + ":"+hit.getLineno()+" [" + hit.getLine() + "]");
816N/A }
816N/A
816N/A if (nhits<totalResults) {
816N/A System.out.println("Printed results 1 - " + nhits +" of " + totalResults + " total matching documents collected.");
816N/A System.out.println("Collect the rest (y/n) ?");
816N/A BufferedReader in=null;
816N/A String line="";
816N/A try {
816N/A in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
816N/A line = in.readLine();
816N/A } catch (Exception ex) {
816N/A Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
816N/A }
816N/A if (line.length() == 0 || line.charAt(0) == 'n') {
816N/A return;
816N/A }
816N/A engine.results(nhits, totalResults, results);
816N/A for (Hit hit : results) {
816N/A File file = new File(root, hit.getFilename());
816N/A System.out.println(file.getAbsolutePath() + ":"+hit.getLineno()+" [" + hit.getLine() + "]");
816N/A }
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}