/************************************************************************** /* Command-line application for document search. /* /* Copyright (c) 2003-2004 by Bernhard Bablok (mail@bablokb.de) /* /* This library is free software; you can redistribute it and/or modify /* it under the terms of the GNU Lesser General Public License as published /* by the Free Software Foundation; either version 2 of the License or /* (at your option) any later version. /* /* This library is distributed in the hope that it will be useful, but /* WITHOUT ANY WARRANTY; without even the implied warranty of /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /* GNU Lesser General Public License for more details. /* /* You should have received a copy of the GNU Lesser General Public License /* along with this library; see the file COPYING.LESSER. If not, write to /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, /* Boston, MA 02111-1307 USA /**************************************************************************/ package de.bablokb.luala.app; import java.io.*; import org.apache.lucene.document.*; import gnu.getopt.*; import de.bablokb.luala.lib.*; /** Command-line application for document search. @version $Revision: 1.11 $ @author $Author: bablokb $ */ public class CmdSearcher { private String iIndexDir = null; private String iPathPrefix = ""; private String iQueryString = null; private boolean iUseHtml=false; private boolean iDebug=false; //////////////////////////////////////////////////////////////////////////// /** Display Usage information and exit. */ private void usage() { System.out.println("\nusage: java " + CmdSearcher.class.getName() + " -i indexDir [-p path-prefix] [-Hdh] query\n\n" + "valid options:\n" + "\t-i indexDir directory with the index-files (required)\n" + "\t-p path-prefix a prefix to prepend to the results\n" + "\t-H output in html-format\n" + "\t-d display debug-output\n" + "\t-h display usage-information and exit\n" ); System.exit(1); } //////////////////////////////////////////////////////////////////////////// /** Parse commandline arguments. @param args The cmd-line arguments. */ private void parseArguments(String[] args) { Getopt g = new Getopt(CmdSearcher.class.getName(),args, ":i:p:Hdh"); int c; while ((c = g.getopt()) != -1) { switch(c) { case 'i': iIndexDir = g.getOptarg(); break; case 'p': iPathPrefix = g.getOptarg(); break; case 'H': iUseHtml = true; break; case 'd': iDebug = true; break; case 'h': case ':': case '?': usage(); break; default: break; } } StringBuffer buffer = new StringBuffer(); for (int i=g.getOptind();i 0) iQueryString = buffer.toString(); } //////////////////////////////////////////////////////////////////////////// /** Check arguments. */ private void checkArguments() { if (iIndexDir == null) usage(); if (! new File(iIndexDir).isDirectory()) { System.err.println("error: " + iIndexDir + " does not exist!"); usage(); } if (iQueryString == null) usage(); } //////////////////////////////////////////////////////////////////////////// /** Process the query and print results. */ private void processQuery() throws Exception { SearchEngine se = new SearchEngine(iIndexDir); Document[] results = se.search(iQueryString); BasicResultRenderer renderer; if (iUseHtml) renderer = new HtmlResultRenderer(iPathPrefix); else renderer = new TextResultRenderer(iPathPrefix); String output = (String) renderer.render(results); System.out.println(output); } //////////////////////////////////////////////////////////////////////////// /** Main-program. Currently, the program just supports three command-line arguments:
  1. The index directory
  2. The query string
  3. The optional path-prefix for results
@param args The cmd-line arguments. */ public static void main(String[] args) { CmdSearcher searcher = new CmdSearcher(); try { searcher.parseArguments(args); searcher.checkArguments(); searcher.processQuery(); } catch (Exception e) { System.err.println("error: " + e.getMessage()); if (searcher.iDebug) e.printStackTrace(); } } }