001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 23/07/2007 15:58:30
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.search;
045:
046: import java.io.IOException;
047: import java.text.SimpleDateFormat;
048: import java.util.Date;
049:
050: import org.apache.lucene.analysis.Analyzer;
051: import org.apache.lucene.index.IndexReader;
052: import org.apache.lucene.index.IndexWriter;
053: import org.apache.lucene.store.Directory;
054: import org.apache.lucene.store.FSDirectory;
055: import org.apache.lucene.store.RAMDirectory;
056:
057: /**
058: * @author Rafael Steil
059: * @version $Id: LuceneSettings.java,v 1.9 2007/08/06 23:04:50 rafaelsteil Exp $
060: */
061: public class LuceneSettings {
062: private Analyzer analyzer;
063: private Directory directory;
064:
065: public LuceneSettings(Analyzer analyzer) {
066: this .analyzer = analyzer;
067: }
068:
069: public void useRAMDirectory() throws Exception {
070: this .directory = new RAMDirectory();
071: IndexWriter writer = new IndexWriter(this .directory,
072: this .analyzer, true);
073: writer.close();
074: }
075:
076: public void useFSDirectory(String indexDirectory) throws Exception {
077: if (!IndexReader.indexExists(indexDirectory)) {
078: this .createIndexDirectory(indexDirectory);
079: }
080:
081: this .directory = FSDirectory.getDirectory(indexDirectory);
082: }
083:
084: public void createIndexDirectory(String directoryPath)
085: throws IOException {
086: FSDirectory fsDir = FSDirectory.getDirectory(directoryPath);
087: IndexWriter writer = new IndexWriter(fsDir, this .analyzer, true);
088: writer.close();
089: }
090:
091: public Directory directory() {
092: return this .directory;
093: }
094:
095: public Analyzer analyzer() {
096: return this .analyzer;
097: }
098:
099: public String formatDateTime(Date date) {
100: return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
101: }
102: }
|