Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LuceneQueryBuilder |
|
| 12.0;12 |
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, 2005 - 2016 | |
18 | */ | |
19 | package org.crosswire.jsword.index.lucene; | |
20 | ||
21 | import java.util.regex.Matcher; | |
22 | import java.util.regex.Pattern; | |
23 | ||
24 | import org.crosswire.jsword.index.query.AndNotQuery; | |
25 | import org.crosswire.jsword.index.query.AndQuery; | |
26 | import org.crosswire.jsword.index.query.BaseQuery; | |
27 | import org.crosswire.jsword.index.query.BlurQuery; | |
28 | import org.crosswire.jsword.index.query.NullQuery; | |
29 | import org.crosswire.jsword.index.query.Query; | |
30 | import org.crosswire.jsword.index.query.QueryBuilder; | |
31 | import org.crosswire.jsword.index.query.RangeQuery; | |
32 | ||
33 | /** | |
34 | * A query can have a optional range specifier and an optional blur specifier. | |
35 | * The range specifier can be +[range], -[range] or just [range]. This must | |
36 | * stand at the beginning of the query and may be surrounded by whitespace. The | |
37 | * blur specifier is either ~ or ~n, where ~ means adjacent verses, but ~n means | |
38 | * to blur by n verses. | |
39 | * | |
40 | * @see gnu.lgpl.License The GNU Lesser General Public License for details. | |
41 | * @author DM Smith | |
42 | */ | |
43 | 0 | public final class LuceneQueryBuilder implements QueryBuilder { |
44 | /* | |
45 | * (non-Javadoc) | |
46 | * | |
47 | * @see | |
48 | * org.crosswire.jsword.index.query.QueryBuilder#parse(java.lang.String) | |
49 | */ | |
50 | public Query parse(String aSearch) { | |
51 | 0 | Query query = NULL_QUERY; |
52 | ||
53 | 0 | String sought = aSearch; |
54 | 0 | if (sought == null || sought.length() == 0) { |
55 | 0 | return query; |
56 | } | |
57 | ||
58 | 0 | int i = 0; |
59 | ||
60 | 0 | Query range = null; |
61 | 0 | String rangeModifier = ""; |
62 | // Look for a range +[...], -[...], or [...] | |
63 | 0 | Matcher rangeMatcher = RANGE_PATTERN.matcher(sought); |
64 | 0 | if (rangeMatcher.find()) { |
65 | 0 | rangeModifier = rangeMatcher.group(1); |
66 | 0 | range = new RangeQuery(rangeMatcher.group(2)); |
67 | 0 | sought = sought.replace(rangeMatcher.group(), " "); |
68 | } | |
69 | ||
70 | // Look for a blur ~n | |
71 | 0 | Matcher blurMatcher = BLUR_PATTERN.matcher(sought); |
72 | 0 | if (blurMatcher.find()) { |
73 | // Did we have ~ or ~n? | |
74 | 0 | int blurFactor = 1; |
75 | 0 | String blur = blurMatcher.group(1); |
76 | 0 | if (blur.length() > 0) { |
77 | 0 | blurFactor = Integer.parseInt(blur); |
78 | } | |
79 | 0 | Query left = new BaseQuery(sought.substring(i, blurMatcher.start())); |
80 | 0 | Query right = new BaseQuery(sought.substring(blurMatcher.end())); |
81 | 0 | query = new BlurQuery(left, right, blurFactor); |
82 | 0 | } else if (sought.length() > 0) { |
83 | 0 | query = new BaseQuery(sought); |
84 | } | |
85 | ||
86 | 0 | if (range != null && !NULL_QUERY.equals(query)) { |
87 | 0 | if (rangeModifier.length() == 0 || rangeModifier.charAt(0) == '+') { |
88 | 0 | query = new AndQuery(range, query); |
89 | } else { | |
90 | // AndNot needs to be after what it is restricting | |
91 | 0 | query = new AndNotQuery(query, range); |
92 | } | |
93 | } | |
94 | ||
95 | 0 | return query; |
96 | } | |
97 | ||
98 | /** | |
99 | * The pattern of a range. This is anything that is contained between a | |
100 | * leading [] (but not containing a [ or ]), with a + or - optional prefix, | |
101 | * perhaps surrounded by whitespace. | |
102 | */ | |
103 | 0 | private static final Pattern RANGE_PATTERN = Pattern.compile("\\s*([-+]?)\\[([^\\[\\]]+)\\]\\s*"); |
104 | ||
105 | /** | |
106 | * The pattern of a blur. A '~', optionally followed by a number, | |
107 | * representing the number of verses. | |
108 | */ | |
109 | 0 | private static final Pattern BLUR_PATTERN = Pattern.compile("\\s~(\\d*)?\\s"); |
110 | ||
111 | /** | |
112 | * A query that returns nothing. | |
113 | */ | |
114 | 0 | private static final Query NULL_QUERY = new NullQuery(); |
115 | ||
116 | } |