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