KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > search > ChannelIndexer


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: ChannelIndexer.java,v 1.7 2003/09/24 13:49:06 niko_schmuck Exp $
28

29 package de.nava.informa.search;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 import org.apache.lucene.analysis.Analyzer;
36 import org.apache.lucene.analysis.standard.StandardAnalyzer;
37 import org.apache.lucene.index.IndexWriter;
38 import org.apache.commons.logging.Log;
39 import org.apache.commons.logging.LogFactory;
40
41 import de.nava.informa.core.ItemIF;
42 import de.nava.informa.core.ChannelIF;
43
44 /**
45  * Utility class which allows to generate respective maintain a
46  * fulltext index for news items.
47  *
48  * @author Niko Schmuck (niko@nava.de)
49  */

50 public final class ChannelIndexer {
51
52   private static Log logger = LogFactory.getLog(ChannelIndexer.class);
53
54   private String JavaDoc indexDir;
55   private int nrOfIndexedItems;
56   private Analyzer analyzer;
57   
58   /**
59    * Constructor which allows to specify the index directory.
60    * For the full-text indexing process the lucene
61    * {@link org.apache.lucene.analysis.standard.StandardAnalyzer}
62    * is used.
63    *
64    * @param indexDir - The directory in which the index files are stored.
65    */

66   public ChannelIndexer(String JavaDoc indexDir) {
67     this.indexDir = indexDir;
68     this.nrOfIndexedItems = 0;
69     this.analyzer = new StandardAnalyzer();
70   }
71   
72   /**
73    * Index all news items contained in the given channels.
74    *
75    * @param createNewIndex - wether a new index should be generated or
76    * an existant one should be taken into account
77    * @param channels - a collection of ChannelIF objects
78    */

79   public void indexChannels(boolean createNewIndex, Collection JavaDoc channels)
80     throws java.io.IOException JavaDoc {
81
82     Collection JavaDoc items = new ArrayList JavaDoc();
83     Iterator JavaDoc itC = channels.iterator();
84     while (itC.hasNext()) {
85       ChannelIF channel = (ChannelIF) itC.next();
86       if (logger.isDebugEnabled()) {
87         logger.debug("Searching channel " + channel + " for items.");
88       }
89       items.addAll(channel.getItems());
90     }
91     if (!items.isEmpty()) {
92       indexItems(createNewIndex, items);
93     } else {
94       logger.info("No items found for indexing.");
95     }
96   }
97
98   /**
99    * Index all given news items.
100    *
101    * @param createNewIndex - Wether a new index should be generated or
102    * an existant one should be taken into account.
103    * @param items - A collection of ItemIF objects.
104    */

105   public void indexItems(boolean createNewIndex, Collection JavaDoc items)
106     throws java.io.IOException JavaDoc {
107     
108     logger.info("Start writing index.");
109     IndexWriter writer = new IndexWriter(indexDir, analyzer, createNewIndex);
110     Iterator JavaDoc itI = items.iterator();
111     while (itI.hasNext()) {
112       ItemIF item = (ItemIF) itI.next();
113       if (logger.isDebugEnabled()) {
114         logger.debug("Add item " + item + " to index.");
115       }
116       writer.addDocument(ItemDocument.makeDocument(item));
117     }
118     writer.optimize();
119     nrOfIndexedItems = writer.docCount();
120     writer.close();
121     logger.info("Finished writing index.");
122   }
123
124   /**
125    * Returns the number of documents that were in the index last time
126    * the index operation was performed.
127    *
128    * Note: Use only directly after the indexing process, otherwise the
129    * return value may be wrong.
130    */

131   public int getNrOfIndexedItems() {
132     return nrOfIndexedItems;
133   }
134
135   public void setIndexDir(String JavaDoc indexDir) {
136     this.indexDir = indexDir;
137   }
138
139   public String JavaDoc getIndexDir() {
140     return indexDir;
141   }
142   
143 }
144
Popular Tags