source: SRUServer/trunk/src/main/java/eu/clarin/sru/server/SRUQueryParserRegistry.java @ 6842

Last change on this file since 6842 was 6842, checked in by Oliver Schonefeld, 9 years ago
  • add 'searchTerms' query parser (SRU 2.0)
  • Property svn:eol-style set to native
File size: 3.1 KB
Line 
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 */
17package eu.clarin.sru.server;
18
19import java.util.Collections;
20import java.util.List;
21import 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 */
30public 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
Note: See TracBrowser for help on using the repository browser.