Search.java revision 535
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/*
289N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
289N/A * Use is subject to license terms.
289N/A */
289N/Apackage org.opensolaris.opengrok.search;
289N/A
289N/Aimport java.io.File;
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
535N/A private static final String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n" + "\t -R <configuration.xml> Read configuration from the specified file\n" + "\t -d Symbol Definitions\n" + "\t -r Symbol References\n" + "\t -p Path\n" + "\t -h History\n" + "\t -f Full text";
535N/A private final static SearchEngine engine = new SearchEngine();
289N/A
535N/A protected static boolean parseCmdLine(String[] argv) {
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 {
289N/A RuntimeEnvironment.getInstance().readConfiguration(new File(getopt.getOptarg()));
289N/A } catch (Exception e) {
289N/A System.err.println("Failed to read config file: ");
289N/A e.printStackTrace();
289N/A System.exit(1);
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;
535N/A
289N/A default:
289N/A System.err.println("Unknown option: " + (char) cmd);
289N/A System.err.println(usage);
535N/A return false;
289N/A }
289N/A }
289N/A
535N/A if (RuntimeEnvironment.getInstance().getDataRootPath() == 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
535N/A return true;
535N/A }
535N/A
535N/A protected static boolean search(List<Hit> results) {
289N/A if (!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();
289N/A int nhits = engine.search();
535N/A if (nhits > 0) {
535N/A engine.more(0, nhits, results);
535N/A }
535N/A
535N/A return true;
535N/A }
535N/A
535N/A /**
535N/A * usage Search index "query" prunepath
535N/A */
535N/A public static void main(String[] argv) {
535N/A boolean success = false;
535N/A
535N/A if (parseCmdLine(argv)) {
289N/A List<Hit> hits = new ArrayList<Hit>();
535N/A if (search(hits)) {
535N/A success = true;
535N/A if (hits.size() == 0) {
535N/A System.err.println("Your search \"" + engine.getQuery() + "\" did not match any files.");
535N/A } else {
535N/A String root = RuntimeEnvironment.getInstance().getSourceRootPath();
535N/A for (Hit hit : hits) {
535N/A File file = new File(root, hit.getFilename());
535N/A System.out.println(file.getAbsolutePath() + ": [" + hit.getLine() + "]");
535N/A }
535N/A }
289N/A }
289N/A }
535N/A
535N/A System.exit(success ? 0 : 1);
289N/A }
289N/A
289N/A private Search() {
289N/A }
289N/A}