1 | /** |
---|
2 | * This software is copyright (c) 2013-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 | */ |
---|
17 | package eu.clarin.sru.server.fcs; |
---|
18 | |
---|
19 | import java.util.Arrays; |
---|
20 | import java.util.Collections; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import eu.clarin.sru.server.SRUConstants; |
---|
25 | import eu.clarin.sru.server.SRUDiagnosticList; |
---|
26 | import eu.clarin.sru.server.SRUQuery; |
---|
27 | import eu.clarin.sru.server.SRUQueryBase; |
---|
28 | import eu.clarin.sru.server.SRUQueryParser; |
---|
29 | import eu.clarin.sru.server.SRUVersion; |
---|
30 | import eu.clarin.sru.server.fcs.parser.QueryNode; |
---|
31 | import eu.clarin.sru.server.fcs.parser.QueryParser; |
---|
32 | import eu.clarin.sru.server.fcs.parser.QueryParserException; |
---|
33 | |
---|
34 | public class FCSQueryParser implements SRUQueryParser<QueryNode> { |
---|
35 | private static final String PARAM_QUERY = "query"; |
---|
36 | private static final List<String> QUERY_PARAMETER_NAMES = |
---|
37 | Collections.unmodifiableList(Arrays.asList(PARAM_QUERY)); |
---|
38 | private final QueryParser parser = new QueryParser(); |
---|
39 | |
---|
40 | |
---|
41 | @Override |
---|
42 | public String getQueryType() { |
---|
43 | return Constants.FCS_QUERY_TYPE_FCS; |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | @Override |
---|
48 | public boolean supportsVersion(SRUVersion version) { |
---|
49 | if (version == null) { |
---|
50 | throw new NullPointerException("version == null"); |
---|
51 | } |
---|
52 | /* |
---|
53 | * FCS-QL is only supported by SRU 2.0 |
---|
54 | */ |
---|
55 | return version.compareTo(SRUVersion.VERSION_2_0) >= 0; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | @Override |
---|
60 | public String getQueryTypeDefintion() { |
---|
61 | return null; |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | @Override |
---|
66 | public List<String> getQueryParameterNames() { |
---|
67 | return QUERY_PARAMETER_NAMES; |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | @Override |
---|
72 | public SRUQuery<QueryNode> parseQuery(SRUVersion version, |
---|
73 | Map<String, String> parameters, SRUDiagnosticList diagnostics) { |
---|
74 | FCSQuery result = null; |
---|
75 | |
---|
76 | final String rawQuery = parameters.get(PARAM_QUERY); |
---|
77 | if (rawQuery == null) { |
---|
78 | diagnostics.addDiagnostic( |
---|
79 | SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
80 | null, |
---|
81 | "No query passed to query parser."); |
---|
82 | return null; |
---|
83 | } |
---|
84 | |
---|
85 | try { |
---|
86 | QueryNode parsedQuery = parser.parse(rawQuery); |
---|
87 | result = new FCSQuery(rawQuery, parsedQuery); |
---|
88 | } catch (QueryParserException e) { |
---|
89 | diagnostics.addDiagnostic( |
---|
90 | Constants.FCS_DIAGNOSTIC_GENERAL_QUERY_SYNTAX_ERROR, |
---|
91 | null, |
---|
92 | e.getMessage()); |
---|
93 | } catch (Exception e) { |
---|
94 | diagnostics.addDiagnostic( |
---|
95 | SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
96 | null, |
---|
97 | "Unexpected error while parsing query."); |
---|
98 | } |
---|
99 | return result; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | public static final class FCSQuery extends SRUQueryBase<QueryNode> { |
---|
104 | |
---|
105 | private FCSQuery(String rawQuery, QueryNode parsedQuery) { |
---|
106 | super(rawQuery, parsedQuery); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | @Override |
---|
111 | public String getQueryType() { |
---|
112 | return Constants.FCS_QUERY_TYPE_FCS; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | } // class FCSQueryParser |
---|
117 | |
---|