/************************************************************************** /* This class implements a configurable Lucene-index engine. /* /* 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.lib; import java.io.*; import java.util.*; import org.apache.lucene.document.*; import org.apache.lucene.analysis.*; import org.apache.lucene.analysis.de.*; import org.apache.lucene.analysis.ru.*; import org.apache.lucene.analysis.standard.*; import org.apache.lucene.index.*; import org.pdfbox.searchengine.lucene.*; import de.bablokb.luala.lib.*; /** This class implements a configurable Lucene-index engine. @version $Revision: 1.6 $ @author $Author: bablokb $ */ public class IndexEngine { //////////////////////////////////////////////////////////////////////////// /** The index-directory. */ private String iIndexDir; //////////////////////////////////////////////////////////////////////////// /** The analyzer for the index. */ private Analyzer iAnalyzer; //////////////////////////////////////////////////////////////////////////// /** The {@link IndexWriter} used by this engine. */ private IndexWriter iWriter; //////////////////////////////////////////////////////////////////////////// /** The {@link DocumentFactoryManager} used by this engine. */ private DocumentFactoryManager iDocFacManager; //////////////////////////////////////////////////////////////////////////// /** The creation-flag used by this engine. If set to true (default), a new index will be created, otherwise, an existing index will be updated. */ private boolean iCreateFlag; //////////////////////////////////////////////////////////////////////////// /** Default-Constructor. Creates a default analyzer based on the current default locale. */ public IndexEngine() { String currentCountry = Locale.getDefault().getCountry(); if (currentCountry.equals("DE")) iAnalyzer = new GermanAnalyzer(); // else if (currentCountry.equals("RU")) // iAnalyzer = new RussianAnalyzer(???); else iAnalyzer = new StandardAnalyzer(); iDocFacManager = new DocumentFactoryManager(); iCreateFlag = true; } //////////////////////////////////////////////////////////////////////////// /** Construct a IndexEngine with given directory. @param dir The directory with the index. @throws IllegalArgumentException if the argument is not a directory */ public IndexEngine(String dir) throws IllegalArgumentException { this(); setIndexDirectory(dir); } //////////////////////////////////////////////////////////////////////////// /** Sets the index directory. @param dir The directory with the index. @throws IllegalArgumentException if the argument is not a directory */ public void setIndexDirectory(String dir) throws IllegalArgumentException { File d = new File(dir); if (d.exists() && !d.isDirectory()) throw new IllegalArgumentException("not a directory: " + dir); if (d.exists() && !d.canWrite()) throw new IllegalArgumentException("not writable directory: " + dir); iIndexDir = dir; } //////////////////////////////////////////////////////////////////////////// /** Sets the analyzer. @param a The analyzer to use. */ public void setAnalyzer(Analyzer a) { iAnalyzer = a; } //////////////////////////////////////////////////////////////////////////// /** Sets the create-flag. If set to true (default), a new index will be created, otherwise, an existing index will be updated. @param flag true, if a new index should be created (replacing an old index if necessary) */ public void setCreateFlag(boolean flag) { iCreateFlag = flag; } //////////////////////////////////////////////////////////////////////////// /** Open the index. This initializes the {@link IndexWriter}. @throws IOException */ public void open() throws IOException { iWriter = new IndexWriter(iIndexDir,iAnalyzer,iCreateFlag); } //////////////////////////////////////////////////////////////////////////// /** Index a file given a {@link File}.. @param file The file @throws IOException */ public void indexFile(File file) throws IOException { if (file.isDirectory()) indexDir(file); String name = file.getPath(); try { DocumentFactory fac = iDocFacManager.getFactory(name); if (fac == null) { System.out.println("failed to index " + name + ": no DocumentFactory available"); return; } iWriter.addDocument(fac.createDocument(name)); } catch (Exception e) { System.err.println("failed to index " + name + ": " + e.getMessage()); } } //////////////////////////////////////////////////////////////////////////// /** Index a file with given name. @param name The name of the file @throws IOException */ public void indexFile(String name) throws IOException { indexFile(new File(name)); } //////////////////////////////////////////////////////////////////////////// /** Index a directory tree. @throws IOException */ public void indexDir(File file) throws IOException { if (file.isDirectory()) { String[] files = file.list(); for (int i = 0; i < files.length; i++) indexDir(new File(file,files[i])); } else { indexFile(file); } } //////////////////////////////////////////////////////////////////////////// /** Close the index. @throws IOException */ public void close() throws IOException { iWriter.optimize(); iWriter.close(); } }