Search.java revision 4db6dbfe971f3c68c0945e5da3b6d5a3248054da
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
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.io.File;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.util.ArrayList;
38115c792a2142d264ca2617b11962301f079c0aTrond Norbyeimport java.util.List;
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
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye private final SearchEngine engine = new SearchEngine();
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye protected final List<Hit> results = new ArrayList<Hit>();
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye protected boolean parseCmdLine(String[] argv) {
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
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye if (!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();
38115c792a2142d264ca2617b11962301f079c0aTrond Norbye int nhits = engine.search();
5e0ccd8bb84a766f65680b4cc1e43b356d1d78b8Trond Norbye if (nhits > 0) {
5e0ccd8bb84a766f65680b4cc1e43b356d1d78b8Trond Norbye engine.more(0, nhits, results);
5e0ccd8bb84a766f65680b4cc1e43b356d1d78b8Trond Norbye }
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();
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye for (Hit hit : results) {
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye File file = new File(root, hit.getFilename());
a90c307564052f582357f820eadec4390e6a86c6Trond Norbye System.out.println(file.getAbsolutePath() + ": [" + hit.getLine() + "]");
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}