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