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

Last change on this file since 7282 was 7282, checked in by Oliver Schonefeld, 2 years ago
  • cleanup
  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1/**
2 * This software is copyright (c) 2011-2022 by
3 *  - Leibniz-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 Leibniz-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.ArrayList;
20import java.util.Collections;
21import java.util.List;
22
23
24/**
25 * A registry to keep track of registered {@link SRUQueryParser} to be used by
26 * the {@link SRUServer}.
27 *
28 * @see SRUQueryParser
29 */
30public class SRUQueryParserRegistry {
31    private final List<SRUQueryParser<?>> parsers;
32
33
34    /**
35     * Constructor.
36     */
37    private SRUQueryParserRegistry(List<SRUQueryParser<?>> parsers) {
38        if (parsers == null) {
39            throw new NullPointerException("parsers == null");
40        }
41        if (parsers.isEmpty()) {
42            throw new IllegalArgumentException("parsers is empty!");
43        }
44        this.parsers = Collections.unmodifiableList(parsers);
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        }
60        return findParser(parsers, queryType);
61    }
62
63
64    /**
65     * Get a list of all registered query parsers.
66     *
67     * @return a list of registered query parsers
68     */
69    public List<SRUQueryParser<?>> getQueryParsers() {
70        return parsers;
71    }
72
73
74    /**
75     * Builder for creating {@link SRUQueryParserRegistry} instances.
76     */
77    public static class Builder {
78        private final List<SRUQueryParser<?>> parsers = new ArrayList<>();
79
80
81        /**
82         * Constructor.
83         *
84         * @param registerDefaults
85         *            if <code>true</code>, register SRU/CQL standard query
86         *            parsers (queryType <em>cql</em> and <em>searchTerms</em>),
87         *            otherwise do nothing
88         */
89        public Builder(boolean registerDefaults) {
90            if (registerDefaults) {
91                registerDefaults();
92            }
93        }
94
95
96        /**
97         * Constructor. Automatically registers registers SRU/CQL standard query
98         * parsers (queryType <em>cql</em> and <em>searchTerms</em>).
99         */
100        public Builder() {
101            this(true);
102        }
103
104
105        public Builder registerDefaults() {
106            if (findParser(parsers, SRUConstants.SRU_QUERY_TYPE_CQL) == null) {
107                try {
108                    register(new CQLQueryParser());
109                } catch (SRUConfigException e) {
110                    /* IGNORE */
111                }
112            }
113            if (findParser(parsers, SRUConstants.SRU_QUERY_TYPE_SEARCH_TERMS) == null) {
114                try {
115                    register(new SearchTermsQueryParser());
116                } catch (SRUConfigException e) {
117                    /* IGNORE */
118                }
119            }
120            return this;
121        }
122
123
124        /**
125         * Register a new query parser
126         *
127         * @param parser
128         *            the query parser instance to be registered
129         * @return this {@link Builder} instance (to allow chaining)
130         * @throws SRUConfigException
131         *             if a query parser for the same query type was already
132         *             registered
133         */
134        public Builder register(SRUQueryParser<?> parser) throws SRUConfigException {
135            if (parser == null) {
136                throw new NullPointerException("parser == null");
137            }
138            if (parser.getQueryType() == null) {
139                throw new NullPointerException("parser.getQueryType() == null");
140            }
141
142            // duplicate-save add ...
143            if (findParser(parsers, parser.getQueryType()) == null) {
144                parsers.add(parser);
145            } else {
146                throw new SRUConfigException("query parser for queryType '" +
147                        parser.getQueryType() + "' is already registered");
148            }
149
150            return this;
151        }
152
153
154        /**
155         * Create a configured {@link SRUQueryParserRegistry} instance from this
156         * builder.
157         *
158         * @return a {@link SRUQueryParserRegistry} instance
159         */
160        public SRUQueryParserRegistry build() {
161            return new SRUQueryParserRegistry(parsers);
162        }
163    }
164
165
166    private static final SRUQueryParser<?> findParser(
167            List<SRUQueryParser<?>> parsers, String queryType) {
168        for (SRUQueryParser<?> parser : parsers) {
169            if (queryType.equals(parser.getQueryType())) {
170                return parser;
171            }
172        }
173        return null;
174    }
175
176} // class SRUQueryParserRegistry
Note: See TracBrowser for help on using the repository browser.