1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2007
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: org.eclipse.jdt.ui.prefs 1178 2006-11-06 12:48:02Z dmsmith $
21   */
22  package org.crosswire.jsword.index.lucene;
23  
24  import java.io.IOException;
25  
26  import org.apache.lucene.document.Document;
27  import org.apache.lucene.index.IndexReader;
28  import org.apache.lucene.search.Collector;
29  import org.apache.lucene.search.Scorer;
30  import org.apache.lucene.search.Searcher;
31  import org.crosswire.jsword.passage.Key;
32  import org.crosswire.jsword.passage.NoSuchVerseException;
33  import org.crosswire.jsword.passage.VerseFactory;
34  import org.crosswire.jsword.versification.Versification;
35  
36  /**
37   * A simple collector of verses that stores the verses in a Key.
38   * 
39   * @see gnu.lgpl.License for license details.<br>
40   *      The copyright to this program is held by it's authors.
41   * @author DM Smith [dmsmith555 at yahoo dot com]
42   */
43  public class VerseCollector extends Collector {
44  
45      /**
46       * Create a collector for the searcher that populates results.
47       */
48      public VerseCollector(Versification refSystem, Searcher searcher, Key results) {
49          this.v11n = refSystem;
50          this.searcher = searcher;
51          this.results = results;
52      }
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see org.apache.lucene.search.Collector#acceptsDocsOutOfOrder()
58       */
59      @Override
60      public boolean acceptsDocsOutOfOrder() {
61          // Order is unimportant
62          return true;
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see org.apache.lucene.search.Collector#collect(int)
69       */
70      @Override
71      public void collect(int docId) throws IOException {
72          Document doc = searcher.doc(docBase + docId);
73          try {
74              Key key = VerseFactory.fromString(v11n, doc.get(LuceneIndex.FIELD_KEY));
75              results.addAll(key);
76          } catch (NoSuchVerseException e) {
77              // Wrap the NoSuchVerseException in an IOException so it can be
78              // gotten.
79              IOException ioe = new IOException();
80              ioe.initCause(e);
81              throw ioe;
82              // JDK6: Change to:
83              // throw new IOException(e);
84          }
85      }
86  
87      /*
88       * (non-Javadoc)
89       * 
90       * @see
91       * org.apache.lucene.search.Collector#setNextReader(org.apache.lucene.index
92       * .IndexReader, int)
93       */
94      @Override
95      public void setNextReader(IndexReader reader, int docBase) throws IOException {
96          this.docBase = docBase;
97      }
98  
99      /*
100      * (non-Javadoc)
101      * 
102      * @see
103      * org.apache.lucene.search.Collector#setScorer(org.apache.lucene.search
104      * .Scorer)
105      */
106     @Override
107     public void setScorer(Scorer scorer) throws IOException {
108         // This collector does no scoring. It collects all hits.
109     }
110 
111     private int docBase;
112     private Versification v11n;
113     private Searcher searcher;
114     private Key results;
115 }
116