001: /* * LIUS - Lucene Index Update and Search
002: * http://sourceforge.net/projects/lius/
003: *
004: * Copyright (c) 2005, Laval University Library. All rights reserved.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */
020: package ca.ulaval.bibl.lius.main.samples;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.StringTokenizer;
025:
026: import org.apache.lucene.analysis.Analyzer;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.index.IndexWriter;
029:
030: import ca.ulaval.bibl.lius.LiusLogger;
031: import ca.ulaval.bibl.lius.Exception.LiusException;
032: import ca.ulaval.bibl.lius.Lucene.AnalyzerFactory;
033: import ca.ulaval.bibl.lius.Lucene.LuceneActions;
034: import ca.ulaval.bibl.lius.config.LiusConfig;
035: import ca.ulaval.bibl.lius.config.LiusConfigBuilder;
036: import ca.ulaval.bibl.lius.index.Indexer;
037: import ca.ulaval.bibl.lius.index.IndexerFactory;
038:
039: /**
040: * *
041: *
042: * @author Rida Benjelloun (rida.benjelloun@bibl.ulaval.ca)
043: */
044: public class IndexingFiles {
045: public static void main(String[] args) throws IOException {
046: String sep = File.separator;
047: StringTokenizer st = new StringTokenizer(System
048: .getProperty("java.class.path"), File.pathSeparator);
049: File classDir = new File(st.nextToken());
050: String indexDir = classDir.getParent() + sep + "luceneIndex";
051: String liusConfig = classDir.getParent() + sep + "Config" + sep
052: + "liusFilesConfig.xml";
053: String log4j = classDir.getParent() + sep + "Config" + sep
054: + "log4j" + sep + "log4j.properties";
055: String toIndex = classDir.getParent() + sep + "ExempleFiles"
056: + sep + "meta" + sep + "DublinCore.xml";
057: LiusConfig lc = LiusConfigBuilder.getSingletonInstance()
058: .getLiusConfig(liusConfig);
059: Analyzer analyzer = AnalyzerFactory.getAnalyzer(lc);
060: IndexWriter writer = null;
061: try {
062: LiusLogger.setLoggerConfigFile(log4j);
063: LuceneActions.getSingletonInstance().newIndex(indexDir);
064: boolean createIndex = LuceneActions.getSingletonInstance()
065: .createIndexValue(lc.getCreateIndex(), indexDir);
066: writer = new IndexWriter(indexDir, analyzer, createIndex);
067: LuceneActions.getSingletonInstance().setIndexWriterProps(
068: writer, lc);
069: Indexer indexer = IndexerFactory.getIndexer(toIndex);
070: Document luceneDoc = indexer.createLuceneDocument(toIndex,
071: liusConfig);
072: LuceneActions.getSingletonInstance().save(luceneDoc,
073: writer, lc);
074: } catch (LiusException e) {
075: e.printStackTrace();
076: } finally {
077: writer.close();
078: LuceneActions.getSingletonInstance().unLock(indexDir);
079: }
080: /*
081: * //Il est possible d'indexer en utilisant uniquement cette méthode qui
082: * regroupe ce qui est définit en haut try {
083: * LuceneActions.getSingletonInstance().index(toIndex, indexDir,
084: * liusConfig); } catch (IOException ex) { ex.printStackTrace(); } catch
085: * (LiusException ex) { ex.printStackTrace();
086: */
087: }
088: }
089:
090: //classDir.getParent() + sep + "ExempleFiles" + sep + "meta"+sep
091: // +"DublinCore.xml"; LiusConfig lc =
092: // LiusConfigBuilder.getSingletonInstance(). getLiusConfig(liusConfig);
093: // Analyzer analyzer = AnalyzerFactory.getAnalyzer(lc); IndexWriter
094: // writer = null; try { LiusLogger.setLoggerConfigFile(log4j);
095: // LuceneActions.getSingletonInstance().newIndex(indexDir); boolean
096: // createIndex = LuceneActions.getSingletonInstance().
097: // createIndexValue(lc.getCreateIndex(), indexDir); writer = new
098: // IndexWriter(indexDir, analyzer, createIndex);
099: // LuceneActions.getSingletonInstance().setIndexWriterProps(writer,lc);
100: // Indexer indexer = IndexerFactory.getIndexer(toIndex); Document
101: // luceneDoc = indexer.createLuceneDocument(toIndex, liusConfig);
102: // LuceneActions.getSingletonInstance().save(luceneDoc, writer, lc); }
103: // catch (LiusException e) { e.printStackTrace(); } finally {
104: // writer.close();
105: // LuceneActions.getSingletonInstance().unLock(indexDir); } /* //Il est
106: // possible d'indexer en utilisant uniquement cette méthode qui regroupe
107: // ce qui est définit en haut try {
108: // LuceneActions.getSingletonInstance().index(toIndex, indexDir,
109: // liusConfig); } catch (IOException ex) { ex.printStackTrace(); } catch
110: // (LiusException ex) { ex.printStackTrace(); } */ }}
|