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.parser; |
---|
18 | |
---|
19 | import java.util.Collections; |
---|
20 | import java.util.Set; |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | * A FCS-QL expression tree SIMPLE expression node. |
---|
25 | */ |
---|
26 | public class Expression extends QueryNode { |
---|
27 | private final String qualifier; |
---|
28 | private final String identifier; |
---|
29 | private final Operator operator; |
---|
30 | private final String regex; |
---|
31 | private final Set<RegexFlag> regex_flags; |
---|
32 | |
---|
33 | |
---|
34 | /** |
---|
35 | * Constructor. |
---|
36 | * |
---|
37 | * @param qualifier |
---|
38 | * the layer identifier qualifier or <code>null</code> if none |
---|
39 | * @param identifier |
---|
40 | * the layer identifier |
---|
41 | * @param operator |
---|
42 | * the operator |
---|
43 | * @param regex |
---|
44 | * the regular expression |
---|
45 | * @param regex_flags |
---|
46 | * the regular expression flags or <code>null</code> of none |
---|
47 | */ |
---|
48 | Expression(String qualifier, String identifier, Operator operator, |
---|
49 | String regex, Set<RegexFlag> regex_flags) { |
---|
50 | super(QueryNodeType.EXPRESSION); |
---|
51 | if ((qualifier != null) && !qualifier.isEmpty()) { |
---|
52 | this.qualifier = qualifier; |
---|
53 | } else { |
---|
54 | this.qualifier = null; |
---|
55 | } |
---|
56 | this.identifier = identifier; |
---|
57 | this.operator = operator; |
---|
58 | this.regex = regex; |
---|
59 | if ((regex_flags != null) && !regex_flags.isEmpty()) { |
---|
60 | this.regex_flags = Collections.unmodifiableSet(regex_flags); |
---|
61 | } else { |
---|
62 | this.regex_flags = null; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | * Get the layer identifier. |
---|
69 | * |
---|
70 | * @return the layer identifier |
---|
71 | */ |
---|
72 | public String getLayerIdentifier() { |
---|
73 | return identifier; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /** |
---|
78 | * Check if the expression used a given <em>Layer Type Identifier</em>. |
---|
79 | * |
---|
80 | * @param identifier |
---|
81 | * the Layer Type Identifier to check against |
---|
82 | * @return <code>true</code> if this identifier was used, <code>false</code> |
---|
83 | * otherwise |
---|
84 | */ |
---|
85 | public boolean hasLayerIdentifier(String identifier) { |
---|
86 | if (identifier == null) { |
---|
87 | throw new NullPointerException("identifier == null"); |
---|
88 | } |
---|
89 | return this.identifier.equals(identifier); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | /** |
---|
94 | * Get the Layer Type Identifier qualifier. |
---|
95 | * |
---|
96 | * @return the Layer Type Identifier qualifier or <code>null</code> if none |
---|
97 | * was used in this expression |
---|
98 | */ |
---|
99 | public String getLayerQualifier() { |
---|
100 | return qualifier; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | /** |
---|
105 | * Check if the Layer Type Identifier qualifier is empty. |
---|
106 | * |
---|
107 | * @return <code>true</code> if no Layer Type Identifier qualifier was set, |
---|
108 | * <code>false</code> otherwise |
---|
109 | */ |
---|
110 | public boolean isLayerQualifierEmpty() { |
---|
111 | return (qualifier == null); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /** |
---|
116 | * Check if the expression used a given qualifier for the Layer Type |
---|
117 | * Identifier. |
---|
118 | * |
---|
119 | * @param qualifier |
---|
120 | * the qualifier to check against |
---|
121 | * @return <code>true</code> if this identifier was used, <code>false</code> |
---|
122 | * otherwise |
---|
123 | */ |
---|
124 | public boolean hasLayerQualifier(String qualifier) { |
---|
125 | if (qualifier == null) { |
---|
126 | throw new NullPointerException("qualifier == null"); |
---|
127 | } |
---|
128 | if (this.qualifier != null) { |
---|
129 | return this.qualifier.equals(qualifier); |
---|
130 | } else { |
---|
131 | return false; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | * Get the operator. |
---|
138 | * |
---|
139 | * @return the operator |
---|
140 | */ |
---|
141 | public Operator getOperator() { |
---|
142 | return operator; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | /** |
---|
147 | * Check if expression used a given operator. |
---|
148 | * |
---|
149 | * @param operator |
---|
150 | * the operator to check |
---|
151 | * @return <code>true</code> if the given operator was used, |
---|
152 | * <code>false</code> otherwise |
---|
153 | */ |
---|
154 | public boolean hasOperator(Operator operator) { |
---|
155 | if (operator == null) { |
---|
156 | throw new NullPointerException("operator == null"); |
---|
157 | } |
---|
158 | return this.operator == operator; |
---|
159 | } |
---|
160 | |
---|
161 | |
---|
162 | /** |
---|
163 | * Get the regex value. |
---|
164 | * |
---|
165 | * @return the regex value |
---|
166 | */ |
---|
167 | public String getRegexValue() { |
---|
168 | return regex; |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | /** |
---|
173 | * Get the regex flags set. |
---|
174 | * |
---|
175 | * @return the regex flags set or <code>null</code> if no flags were used |
---|
176 | * in this expression |
---|
177 | */ |
---|
178 | public Set<RegexFlag> getRegexFlags() { |
---|
179 | return regex_flags; |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * Check if a regex flag set is empty. |
---|
185 | * |
---|
186 | * @return <code>true</code> if no regex flags where set, <code>false</code> |
---|
187 | * otherwise |
---|
188 | */ |
---|
189 | public boolean isRegexFlagsEmpty() { |
---|
190 | return (regex_flags == null); |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | /** |
---|
195 | * Check if a regex flag is set. |
---|
196 | * |
---|
197 | * @param flag |
---|
198 | * the flag to be checked |
---|
199 | * @return <code>true</code> if the flag is set, <code>false</code> |
---|
200 | * otherwise |
---|
201 | */ |
---|
202 | public boolean hasRegexFlag(RegexFlag flag) { |
---|
203 | if (flag == null) { |
---|
204 | throw new NullPointerException("flag == null"); |
---|
205 | } |
---|
206 | if (regex_flags != null) { |
---|
207 | return regex_flags.contains(flag); |
---|
208 | } else { |
---|
209 | return false; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | @Override |
---|
215 | public String toString() { |
---|
216 | StringBuilder sb = new StringBuilder(); |
---|
217 | sb.append("("); |
---|
218 | sb.append(nodeType.toDisplayString()); |
---|
219 | sb.append(" "); |
---|
220 | if (qualifier != null) { |
---|
221 | sb.append(qualifier).append(":"); |
---|
222 | } |
---|
223 | sb.append(identifier); |
---|
224 | sb.append(" "); |
---|
225 | sb.append(operator.toDisplayString()); |
---|
226 | sb.append(" \""); |
---|
227 | for (int i = 0; i < regex.length(); i++) { |
---|
228 | char ch = regex.charAt(i); |
---|
229 | switch (ch) { |
---|
230 | case '\n': |
---|
231 | sb.append("\\n"); |
---|
232 | break; |
---|
233 | case '\r': |
---|
234 | sb.append("\\r"); |
---|
235 | break; |
---|
236 | case '\t': |
---|
237 | sb.append("\\t"); |
---|
238 | break; |
---|
239 | default: |
---|
240 | sb.append(ch); |
---|
241 | } |
---|
242 | } |
---|
243 | sb.append("\""); |
---|
244 | if (regex_flags != null) { |
---|
245 | sb.append("/"); |
---|
246 | if (regex_flags.contains(RegexFlag.CASE_INSENSITIVE)) { |
---|
247 | sb.append("i"); |
---|
248 | } |
---|
249 | if (regex_flags.contains(RegexFlag.CASE_SENSITIVE)) { |
---|
250 | sb.append("I"); |
---|
251 | } |
---|
252 | if (regex_flags.contains(RegexFlag.LITERAL_MATCHING)) { |
---|
253 | sb.append("l"); |
---|
254 | } |
---|
255 | if (regex_flags.contains(RegexFlag.IGNORE_DIACRITICS)) { |
---|
256 | sb.append("d"); |
---|
257 | } |
---|
258 | } |
---|
259 | sb.append(")"); |
---|
260 | return sb.toString(); |
---|
261 | } |
---|
262 | |
---|
263 | |
---|
264 | @Override |
---|
265 | public void accept(QueryVisitor visitor) { |
---|
266 | visitor.visit(this); |
---|
267 | } |
---|
268 | |
---|
269 | } // class Expression |
---|