1 | /** |
---|
2 | * This software is copyright (c) 2011-2013 by |
---|
3 | * - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
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 | * |
---|
12 | * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
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 | |
---|
19 | import java.util.Collections; |
---|
20 | import java.util.List; |
---|
21 | import java.util.concurrent.CopyOnWriteArrayList; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * Class for registering query parsers. The query parser for CQL will be |
---|
26 | * automatically registered. |
---|
27 | * |
---|
28 | * @see SRUQueryParser |
---|
29 | */ |
---|
30 | public class SRUQueryParserRegistry { |
---|
31 | private CopyOnWriteArrayList<SRUQueryParser<?>> queryParsers = |
---|
32 | new CopyOnWriteArrayList<SRUQueryParser<?>>(); |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * Constructor. |
---|
37 | */ |
---|
38 | public SRUQueryParserRegistry() { |
---|
39 | queryParsers.add(new CQLQueryParser()); |
---|
40 | queryParsers.add(new SearchTermsQueryParser()); |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | /** |
---|
45 | * Register a new query parser |
---|
46 | * |
---|
47 | * @param parser |
---|
48 | * the query parser instance to be registered |
---|
49 | * @throws SRUConfigException |
---|
50 | * if a query parser for the same query type was already |
---|
51 | * registered |
---|
52 | */ |
---|
53 | public void registerQueryParser(SRUQueryParser<?> parser) |
---|
54 | throws SRUConfigException { |
---|
55 | if (parser == null) { |
---|
56 | throw new NullPointerException("parser == null"); |
---|
57 | } |
---|
58 | if (parser.getQueryType() == null) { |
---|
59 | throw new NullPointerException("parser.getQueryType() == null"); |
---|
60 | } |
---|
61 | synchronized (this) { |
---|
62 | final String queryType = parser.getQueryType(); |
---|
63 | for (SRUQueryParser<?> queryParser : queryParsers) { |
---|
64 | if (queryType.equals(queryParser.getQueryType())) { |
---|
65 | throw new SRUConfigException( |
---|
66 | "query parser for queryType '" + queryType + |
---|
67 | "' is already registered"); |
---|
68 | } |
---|
69 | } // for |
---|
70 | queryParsers.add(parser); |
---|
71 | } // synchronized (this) |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * Find a query parser by query type. |
---|
77 | * |
---|
78 | * @param queryType |
---|
79 | * the query type to search for |
---|
80 | * @return the matching {@link SRUQueryParser} instance or <code>null</code> |
---|
81 | * if no matching parser was found. |
---|
82 | */ |
---|
83 | public SRUQueryParser<?> findQueryParser(String queryType) { |
---|
84 | if (queryType == null) { |
---|
85 | throw new NullPointerException("queryType == null"); |
---|
86 | } |
---|
87 | for (SRUQueryParser<?> queryParser : queryParsers) { |
---|
88 | if (queryType.equals(queryParser.getQueryType())) { |
---|
89 | return queryParser; |
---|
90 | } |
---|
91 | } // for |
---|
92 | return null; |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | public List<SRUQueryParser<?>> getQueryParsers() { |
---|
97 | return Collections.unmodifiableList(queryParsers); |
---|
98 | } |
---|
99 | |
---|
100 | } // class SRUQueryParserRegistry |
---|