[6825] | 1 | /** |
---|
[7269] | 2 | * This software is copyright (c) 2011-2022 by |
---|
| 3 | * - Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[6825] | 4 | * This is free software. You can redistribute it |
---|
| 5 | * and/or modify it under the terms described in |
---|
| 6 | * the GNU General Public License v3 of which you |
---|
| 7 | * should have received a copy. Otherwise you can download |
---|
| 8 | * it from |
---|
| 9 | * |
---|
| 10 | * http://www.gnu.org/licenses/gpl-3.0.txt |
---|
| 11 | * |
---|
[7269] | 12 | * @copyright Leibniz-Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
[6825] | 13 | * |
---|
| 14 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
| 15 | * GNU General Public License v3 |
---|
| 16 | */ |
---|
| 17 | package eu.clarin.sru.server; |
---|
| 18 | |
---|
[6848] | 19 | import java.util.ArrayList; |
---|
[6825] | 20 | import java.util.Collections; |
---|
| 21 | import java.util.List; |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | /** |
---|
[6848] | 25 | * A registry to keep track of registered {@link SRUQueryParser} to be used by |
---|
| 26 | * the {@link SRUServer}. |
---|
[6825] | 27 | * |
---|
| 28 | * @see SRUQueryParser |
---|
| 29 | */ |
---|
| 30 | public class SRUQueryParserRegistry { |
---|
[6848] | 31 | private final List<SRUQueryParser<?>> parsers; |
---|
[6825] | 32 | |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * Constructor. |
---|
| 36 | */ |
---|
[6848] | 37 | private SRUQueryParserRegistry(List<SRUQueryParser<?>> parsers) { |
---|
| 38 | if (parsers == null) { |
---|
| 39 | throw new NullPointerException("parsers == null"); |
---|
[6825] | 40 | } |
---|
[6848] | 41 | if (parsers.isEmpty()) { |
---|
| 42 | throw new IllegalArgumentException("parsers is empty!"); |
---|
[6825] | 43 | } |
---|
[6848] | 44 | this.parsers = Collections.unmodifiableList(parsers); |
---|
[6825] | 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Find a query parser by query type. |
---|
| 50 | * |
---|
| 51 | * @param queryType |
---|
| 52 | * the query type to search for |
---|
| 53 | * @return the matching {@link SRUQueryParser} instance or <code>null</code> |
---|
| 54 | * if no matching parser was found. |
---|
| 55 | */ |
---|
| 56 | public SRUQueryParser<?> findQueryParser(String queryType) { |
---|
| 57 | if (queryType == null) { |
---|
| 58 | throw new NullPointerException("queryType == null"); |
---|
| 59 | } |
---|
[6848] | 60 | return findParser(parsers, queryType); |
---|
[6825] | 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
[6848] | 64 | /** |
---|
| 65 | * Get a list of all registered query parsers. |
---|
| 66 | * |
---|
| 67 | * @return a list of registered query parsers |
---|
| 68 | */ |
---|
[6825] | 69 | public List<SRUQueryParser<?>> getQueryParsers() { |
---|
[6848] | 70 | return parsers; |
---|
[6825] | 71 | } |
---|
| 72 | |
---|
[6848] | 73 | |
---|
| 74 | /** |
---|
| 75 | * Builder for creating {@link SRUQueryParserRegistry} instances. |
---|
| 76 | */ |
---|
| 77 | public static class Builder { |
---|
| 78 | private final List<SRUQueryParser<?>> parsers = |
---|
| 79 | new ArrayList<SRUQueryParser<?>>(); |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Constructor. |
---|
| 84 | * |
---|
| 85 | * @param registerDefaults |
---|
| 86 | * if <code>true</code>, register SRU/CQL standard query |
---|
| 87 | * parsers (queryType <em>cql</em> and <em>searchTerms</em>), |
---|
| 88 | * otherwise do nothing |
---|
| 89 | */ |
---|
| 90 | public Builder(boolean registerDefaults) { |
---|
| 91 | if (registerDefaults) { |
---|
| 92 | registerDefaults(); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Constructor. Automaticaly registers registers SRU/CQL standard query |
---|
| 99 | * parsers (queryType <em>cql</em> and <em>searchTerms</em>). |
---|
| 100 | */ |
---|
| 101 | public Builder() { |
---|
| 102 | this(true); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | public Builder registerDefaults() { |
---|
| 107 | if (findParser(parsers, SRUConstants.SRU_QUERY_TYPE_CQL) == null) { |
---|
| 108 | try { |
---|
| 109 | register(new CQLQueryParser()); |
---|
| 110 | } catch (SRUConfigException e) { |
---|
| 111 | /* IGNORE */ |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | if (findParser(parsers, SRUConstants.SRU_QUERY_TYPE_SEARCH_TERMS) == null) { |
---|
| 115 | try { |
---|
| 116 | register(new SearchTermsQueryParser()); |
---|
| 117 | } catch (SRUConfigException e) { |
---|
| 118 | /* IGNORE */ |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | return this; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Register a new query parser |
---|
| 127 | * |
---|
| 128 | * @param parser |
---|
| 129 | * the query parser instance to be registered |
---|
[6934] | 130 | * @return this {@link Builder} instance (to allow chaining) |
---|
[6848] | 131 | * @throws SRUConfigException |
---|
| 132 | * if a query parser for the same query type was already |
---|
| 133 | * registered |
---|
| 134 | */ |
---|
| 135 | public Builder register(SRUQueryParser<?> parser) throws SRUConfigException { |
---|
| 136 | if (parser == null) { |
---|
| 137 | throw new NullPointerException("parser == null"); |
---|
| 138 | } |
---|
| 139 | if (parser.getQueryType() == null) { |
---|
| 140 | throw new NullPointerException("parser.getQueryType() == null"); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // duplicate-save add ... |
---|
| 144 | if (findParser(parsers, parser.getQueryType()) == null) { |
---|
| 145 | parsers.add(parser); |
---|
| 146 | } else { |
---|
| 147 | throw new SRUConfigException("query parser for queryType '" + |
---|
| 148 | parser.getQueryType() + "' is already registered"); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return this; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | /** |
---|
| 156 | * Create a configured {@link SRUQueryParserRegistry} instance from this |
---|
| 157 | * builder. |
---|
| 158 | * |
---|
| 159 | * @return a {@link SRUQueryParserRegistry} instance |
---|
| 160 | */ |
---|
| 161 | public SRUQueryParserRegistry build() { |
---|
| 162 | return new SRUQueryParserRegistry(parsers); |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | private static final SRUQueryParser<?> findParser( |
---|
| 168 | List<SRUQueryParser<?>> parsers, String queryType) { |
---|
| 169 | for (SRUQueryParser<?> parser : parsers) { |
---|
| 170 | if (queryType.equals(parser.getQueryType())) { |
---|
| 171 | return parser; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | return null; |
---|
| 175 | } |
---|
| 176 | |
---|
[6825] | 177 | } // class SRUQueryParserRegistry |
---|