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