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

Last change on this file since 6848 was 6848, checked in by Oliver Schonefeld, 9 years ago
  • use a Builder pattern for SRuQueryParserRegistry
  • Property svn:eol-style set to native
File size: 5.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.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 =
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
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.