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/*
1072N/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
289N/A */
289N/Apackage org.opensolaris.opengrok.search;
289N/A
816N/Aimport java.io.BufferedReader;
289N/Aimport java.io.File;
816N/Aimport java.io.InputStreamReader;
289N/Aimport java.util.ArrayList;
289N/Aimport java.util.List;
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
1470N/A private static final String usage =
1470N/A "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n" +
1470N/A "\t -R <configuration.xml> Read configuration from the specified file\n" +
1470N/A "\t -d Symbol Definitions\n" +
1470N/A "\t -r Symbol References\n" +
1470N/A "\t -p Path\n" +
1470N/A "\t -h History\n" +
1470N/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 {
1470N/A RuntimeEnvironment.readConfig(new File(getopt.getOptarg()));
289N/A } catch (Exception e) {
289N/A System.err.println("Failed to read config file: ");
1072N/A System.err.println(e.getMessage());
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() {
1470N/A if (RuntimeEnvironment.getConfig().getDataRoot() == 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()) {
1470N/A System.err.println("Your search \"" + engine.getQuery()
1470N/A + "\" did not match any files.");
536N/A } else {
1470N/A String root = RuntimeEnvironment.getConfig().getSourceRoot();
1470N/A System.out.println("Printing results 1 - " + nhits +" of "
1470N/A + totalResults + " total matching documents collected.");
536N/A for (Hit hit : results) {
908N/A File file = new File(root, hit.getPath());
1470N/A System.out.println(file.getAbsolutePath() + ":" + hit.getLineno()
1470N/A + " [" + hit.getLine() + "]");
816N/A }
816N/A
816N/A if (nhits<totalResults) {
1470N/A System.out.println("Printed results 1 - " + nhits +" of "
1470N/A + totalResults + " total matching documents collected.");
816N/A System.out.println("Collect the rest (y/n) ?");
1190N/A BufferedReader in=null;
816N/A try {
1190N/A in = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
819N/A String line = in.readLine();
857N/A if (null == line || line.length() == 0 || line.charAt(0) == 'n') {
819N/A return;
819N/A }
816N/A } catch (Exception ex) {
1072N/A System.err.println(ex.getMessage());
1072N/A }
816N/A engine.results(nhits, totalResults, results);
816N/A for (Hit hit : results) {
908N/A File file = new File(root, hit.getPath());
1470N/A System.out.println(file.getAbsolutePath() + ":" + hit.getLineno()
1470N/A + " [" + 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}