| StrongsNumberFilter.java |
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: StrongsNumberFilter.java 2221 2012-01-25 21:32:57Z dmsmith $
21 */
22 package org.crosswire.jsword.index.lucene.analysis;
23
24 import java.io.IOException;
25
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.analysis.tokenattributes.TermAttribute;
28 import org.crosswire.common.util.Logger;
29 import org.crosswire.jsword.book.Book;
30 import org.crosswire.jsword.book.BookException;
31 import org.crosswire.jsword.book.study.StrongsNumber;
32
33 /**
34 * A StrongsNumberFilter normalizes Strong's Numbers.
35 *
36 * @see gnu.lgpl.License for license details.<br>
37 * The copyright to this program is held by it's authors.
38 * @author DM Smith [dmsmith555 at yahoo dot com]
39 */
40 public class StrongsNumberFilter extends AbstractBookTokenFilter {
41
42 /**
43 * Construct filtering <i>in</i>.
44 */
45 public StrongsNumberFilter(TokenStream in) {
46 this(null, in);
47 }
48
49 /**
50 * Construct filtering <i>in</i>.
51 */
52 public StrongsNumberFilter(Book book, TokenStream in) {
53 super(book, in);
54 termAtt = addAttribute(TermAttribute.class);
55 }
56
57 /*
58 * (non-Javadoc)
59 *
60 * @see org.apache.lucene.analysis.TokenStream#incrementToken()
61 */
62 @Override
63 public boolean incrementToken() throws IOException {
64 // If the term is suffixed with '!a' or 'a', where 'a' is a sequence of
65 // 1 or more letters
66 // then create a token without the suffix and also for the whole.
67 if (number == null) {
68 if (input.incrementToken()) {
69 try {
70 String tokenText = termAtt.term();
71
72 number = new StrongsNumber(tokenText);
73 String s = number.getStrongsNumber();
74
75 // Was it a Strong's Number? If so it transformed.
76 if (!s.equals(tokenText)) {
77 termAtt.setTermBuffer(s);
78
79 // If the number had a part keep it around for the next
80 // call
81 if (!number.isPart()) {
82 number = null;
83 }
84 }
85 } catch (BookException e) {
86 log.error(e.getDetailedMessage());
87 }
88
89 // We are providing a term
90 return true;
91 }
92
93 // There was no more input
94 return false;
95 }
96
97 // Process the Strong's number with the !a
98 termAtt.setTermBuffer(number.getFullStrongsNumber());
99 // We are done with the Strong's Number so mark it as used
100 number = null;
101 // We are providing a term
102 return true;
103 }
104
105 /* Define to quite FindBugs */
106 @Override
107 public boolean equals(Object obj) {
108 return super.equals(obj);
109 }
110
111 /* Define to quite FindBugs */
112 @Override
113 public int hashCode() {
114 return super.hashCode();
115 }
116
117 private TermAttribute termAtt;
118 private StrongsNumber number;
119
120 /**
121 * The log stream
122 */
123 private static final Logger log = Logger.getLogger(StrongsNumberFilter.class);
124 }
125