/************************************************************************** /* Command-line application for document indexing. /* /* 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 java.util.*; import gnu.getopt.*; import de.bablokb.luala.lib.*; /** Command-line application for document indexing. @version $Revision: 1.8 $ @author $Author: bablokb $ */ public class CmdIndexer { private String iIndexDir = null; private String iSourceDir = null; private boolean iUpdate = false; private boolean iDebug = false; //////////////////////////////////////////////////////////////////////////// /** Display Usage information and exit. */ private static void usage() { System.out.println("\nusage: java " + CmdIndexer.class.getName() + " -s sourceDir -i indexDir [-udh]\n\n" + "valid options:\n" + "\t-s sourceDir root-directory of files to index\n" + "\t (or pass filenames to stdin)\n" + "\t-i indexDir index-directory (required)\n" + "\t-u update existing index\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:s:udh"); int c; while ((c = g.getopt()) != -1) { switch(c) { case 'i': iIndexDir = g.getOptarg(); break; case 's': iSourceDir = g.getOptarg(); break; case 'u': iUpdate = true; break; case 'd': iDebug = true; break; case 'h': case ':': case '?': usage(); break; default: break; } } } //////////////////////////////////////////////////////////////////////////// /** Check arguments. */ private void checkArguments() { if (iIndexDir == null) usage(); try { if (iSourceDir == null && System.in.available() < 1) usage(); } catch (IOException e) { } if (iSourceDir != null && ! new File(iSourceDir).isDirectory()) { System.err.println("error: " + iSourceDir + " is not a directory!"); usage(); } if (iUpdate && ! new File(iIndexDir).exists()) iUpdate = false; } //////////////////////////////////////////////////////////////////////////// /** Create or update the index. */ private void createIndex() throws Exception { IndexEngine indexer = new IndexEngine(iIndexDir); indexer.setCreateFlag(!iUpdate); indexer.open(); if (iSourceDir != null) indexer.indexDir(new File(iSourceDir)); if (System.in.available() > 0) { // working as a pipe BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = reader.readLine()) != null) { indexer.indexFile(line); } reader.close(); } indexer.close(); } //////////////////////////////////////////////////////////////////////////// /** Main-program. Currently, the program just supports two command-line arguments:
  1. The source directory
  2. The index directory
*/ public static void main(String[] args) { CmdIndexer indexer = new CmdIndexer(); try { indexer.parseArguments(args); indexer.checkArguments(); indexer.createIndex(); } catch (Exception e) { System.err.println("error: " + e.getMessage()); if (indexer.iDebug) e.printStackTrace(); } } }