Search.java revision 387
1194N/A/*
1194N/A * CDDL HEADER START
1194N/A *
1194N/A * The contents of this file are subject to the terms of the
1194N/A * Common Development and Distribution License (the "License").
1194N/A * You may not use this file except in compliance with the License.
1194N/A *
1194N/A * See LICENSE.txt included in this distribution for the specific
1194N/A * language governing permissions and limitations under the License.
1194N/A *
1194N/A * When distributing Covered Code, include this CDDL HEADER in each
1194N/A * file and include the License file at LICENSE.txt.
1194N/A * If applicable, add the following below this CDDL HEADER, with the
1194N/A * fields enclosed by brackets "[]" replaced with your own identifying
1194N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1194N/A *
1194N/A * CDDL HEADER END
1194N/A */
1194N/A
1194N/A/*
1194N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1194N/A * Use is subject to license terms.
1194N/A */
1194N/Apackage org.opensolaris.opengrok.search;
1194N/A
1194N/Aimport java.io.File;
1194N/Aimport java.util.ArrayList;
1194N/Aimport java.util.List;
1195N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1195N/Aimport org.opensolaris.opengrok.util.Getopt;
1194N/A
1194N/A/**
1194N/A * Search and list the matching files
1194N/A */
1194N/Afinal class Search {
1194N/A
1194N/A /**
1194N/A * usage Search index "query" prunepath
1194N/A */
1194N/A public static void main(String[] argv) {
1194N/A String usage = "USAGE: Search -R <configuration.xml> [-d | -r | -p | -h | -f] 'query string' ..\n"
1194N/A + "\t -R <configuration.xml> Read configuration from the specified file\n"
1194N/A + "\t -d Symbol Definitions\n"
1194N/A + "\t -r Symbol References\n"
1194N/A + "\t -p Path\n"
1194N/A + "\t -h History\n"
1194N/A + "\t -f Full text";
1194N/A
1194N/A SearchEngine engine = new SearchEngine();
1194N/A boolean config = false;
1194N/A
1194N/A Getopt getopt = new Getopt(argv, "R:d:r:p:h:f:");
1194N/A try {
1194N/A getopt.parse();
1194N/A } catch (Exception e) {
1194N/A System.err.println(e.getMessage());
1194N/A System.err.println(usage);
1194N/A System.exit(1);
1194N/A }
1194N/A
1194N/A int cmd;
1194N/A while ((cmd = getopt.getOpt()) != -1) {
1194N/A switch (cmd) {
1194N/A case 'R':
1194N/A try {
1194N/A RuntimeEnvironment.getInstance().readConfiguration(new File(getopt.getOptarg()));
1194N/A config = true;
1194N/A } catch (Exception e) {
1194N/A System.err.println("Failed to read config file: ");
1194N/A e.printStackTrace();
1194N/A System.exit(1);
1194N/A }
1194N/A break;
1194N/A case 'd':
1194N/A engine.setDefinition(getopt.getOptarg());
1194N/A break;
1194N/A case 'r':
1194N/A engine.setSymbol(getopt.getOptarg());
1194N/A break;
1194N/A case 'p':
1194N/A engine.setFile(getopt.getOptarg());
1194N/A break;
1194N/A case 'h':
1194N/A engine.setHistory(getopt.getOptarg());
1194N/A break;
1194N/A case 'f' :
1195N/A engine.setFreetext(getopt.getOptarg());
1195N/A break;
1195N/A
1195N/A default:
1195N/A System.err.println("Unknown option: " + (char) cmd);
1195N/A System.err.println(usage);
1195N/A System.exit(1);
1195N/A }
1195N/A }
1195N/A
1195N/A if (!config) {
1195N/A System.err.println("You must specify a configuration file");
1195N/A System.err.println(usage);
1195N/A System.exit(1);
1195N/A }
1195N/A
1195N/A if (!engine.isValidQuery()) {
1195N/A System.err.println("You did not specify a valid query");
1195N/A System.err.println(usage);
1195N/A System.exit(1);
1194N/A }
int nhits = engine.search();
if (nhits == 0) {
System.err.println("Your search \"" + engine.getQuery() + "\" did not match any files.");
} else {
List<Hit> hits = new ArrayList<Hit>();
engine.more(0, nhits, hits);
String root = RuntimeEnvironment.getInstance().getSourceRootPath();
for (Hit hit : hits) {
File file = new File(root, hit.getFilename());
System.out.println(file.getAbsolutePath() + ": [" + hit.getLine() + "]");
}
}
}
private Search() {
}
}