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