| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| StrongsNumberFilter |
|
| 2.2;2.2 |
| 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 or later | |
| 5 | * as published by the Free Software Foundation. This program is distributed | |
| 6 | * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even | |
| 7 | * the 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 | * © CrossWire Bible Society, 2007 - 2016 | |
| 18 | * | |
| 19 | */ | |
| 20 | package org.crosswire.jsword.index.lucene.analysis; | |
| 21 | ||
| 22 | import java.io.IOException; | |
| 23 | ||
| 24 | import org.apache.lucene.analysis.TokenStream; | |
| 25 | import org.apache.lucene.analysis.tokenattributes.TermAttribute; | |
| 26 | import org.crosswire.jsword.JSMsg; | |
| 27 | import org.crosswire.jsword.book.Book; | |
| 28 | import org.crosswire.jsword.book.study.StrongsNumber; | |
| 29 | import org.slf4j.Logger; | |
| 30 | import org.slf4j.LoggerFactory; | |
| 31 | ||
| 32 | /** | |
| 33 | * A StrongsNumberFilter normalizes Strong's Numbers. | |
| 34 | * | |
| 35 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
| 36 | * @author DM Smith | |
| 37 | */ | |
| 38 | public class StrongsNumberFilter extends AbstractBookTokenFilter { | |
| 39 | ||
| 40 | /** | |
| 41 | * Construct filtering <i>in</i>. | |
| 42 | * | |
| 43 | * @param in | |
| 44 | */ | |
| 45 | public StrongsNumberFilter(TokenStream in) { | |
| 46 | 0 | this(null, in); |
| 47 | 0 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Construct filtering <i>in</i>. | |
| 51 | * | |
| 52 | * @param book the book | |
| 53 | * @param in | |
| 54 | */ | |
| 55 | public StrongsNumberFilter(Book book, TokenStream in) { | |
| 56 | 0 | super(book, in); |
| 57 | 0 | termAtt = addAttribute(TermAttribute.class); |
| 58 | 0 | } |
| 59 | ||
| 60 | /* | |
| 61 | * (non-Javadoc) | |
| 62 | * | |
| 63 | * @see org.apache.lucene.analysis.TokenStream#incrementToken() | |
| 64 | */ | |
| 65 | @Override | |
| 66 | public boolean incrementToken() throws IOException { | |
| 67 | // If the term is suffixed with '!a' or 'a', where 'a' is a sequence of | |
| 68 | // 1 or more letters | |
| 69 | // then create a token without the suffix and also for the whole. | |
| 70 | 0 | if (number == null) { |
| 71 | // Need to loop over invalid tokens | |
| 72 | 0 | while (input.incrementToken()) { |
| 73 | 0 | String tokenText = termAtt.term(); |
| 74 | ||
| 75 | 0 | number = new StrongsNumber(tokenText); |
| 76 | ||
| 77 | // Skip invalid Strong's Numbers. | |
| 78 | // Still need to return true as there may be more tokens to filter. | |
| 79 | 0 | if (!number.isValid()) { |
| 80 | // TRANSLATOR: User error condition: Indicates that what was given is not a Strong's Number. {0} is a placeholder for the bad Strong's Number. | |
| 81 | 0 | log.warn(JSMsg.gettext("Not a valid Strong's Number \"{0}\"", tokenText)); |
| 82 | ||
| 83 | // Go get the next token | |
| 84 | 0 | continue; |
| 85 | } | |
| 86 | ||
| 87 | 0 | String s = number.getStrongsNumber(); |
| 88 | 0 | termAtt.setTermBuffer(s); |
| 89 | ||
| 90 | // If the number had a part keep it around for the next call | |
| 91 | // TODO(DMS): if there is a part, then treat as a synonym, | |
| 92 | // setting the same position increment. | |
| 93 | 0 | if (!number.isPart()) { |
| 94 | 0 | number = null; |
| 95 | } | |
| 96 | ||
| 97 | // incrementToken returned a value. There may be more input. | |
| 98 | 0 | return true; |
| 99 | } | |
| 100 | ||
| 101 | // There was no more input | |
| 102 | 0 | return false; |
| 103 | } | |
| 104 | ||
| 105 | // Process the Strong's number with the !a | |
| 106 | 0 | termAtt.setTermBuffer(number.getFullStrongsNumber()); |
| 107 | // We are done with the Strong's Number so mark it as used | |
| 108 | 0 | number = null; |
| 109 | // We are working on a value returned by incrementToken. | |
| 110 | // There may be more input. | |
| 111 | 0 | return true; |
| 112 | } | |
| 113 | ||
| 114 | /* Define to quite FindBugs */ | |
| 115 | @Override | |
| 116 | public boolean equals(Object obj) { | |
| 117 | 0 | return super.equals(obj); |
| 118 | } | |
| 119 | ||
| 120 | /* Define to quite FindBugs */ | |
| 121 | @Override | |
| 122 | public int hashCode() { | |
| 123 | 0 | return super.hashCode(); |
| 124 | } | |
| 125 | ||
| 126 | private TermAttribute termAtt; | |
| 127 | private StrongsNumber number; | |
| 128 | ||
| 129 | /** | |
| 130 | * The log stream | |
| 131 | */ | |
| 132 | 0 | private static final Logger log = LoggerFactory.getLogger(StrongsNumberFilter.class); |
| 133 | } |