forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XpathFilterElement.java
309 lines (283 loc) · 10.8 KB
/
XpathFilterElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.filters;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import com.puppycrawl.tools.checkstyle.TreeWalkerAuditEvent;
import com.puppycrawl.tools.checkstyle.TreeWalkerFilter;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.xpath.AbstractNode;
import com.puppycrawl.tools.checkstyle.xpath.RootNode;
import net.sf.saxon.Configuration;
import net.sf.saxon.om.Item;
import net.sf.saxon.sxpath.XPathDynamicContext;
import net.sf.saxon.sxpath.XPathEvaluator;
import net.sf.saxon.sxpath.XPathExpression;
import net.sf.saxon.trans.XPathException;
/**
* This filter element is immutable and processes {@link TreeWalkerAuditEvent}
* objects based on the criteria of file, check, module id, xpathQuery.
*
*/
public class XpathFilterElement implements TreeWalkerFilter {
/** The regexp to match file names against. */
private final Pattern fileRegexp;
/** The pattern for file names. */
private final String filePattern;
/** The regexp to match check names against. */
private final Pattern checkRegexp;
/** The pattern for check class names. */
private final String checkPattern;
/** The regexp to match message names against. */
private final Pattern messageRegexp;
/** The pattern for message names. */
private final String messagePattern;
/** Module id filter. */
private final String moduleId;
/** Xpath expression. */
private final XPathExpression xpathExpression;
/** Xpath query. */
private final String xpathQuery;
/** Indicates if all properties are set to Null. */
private final boolean areAllPropertiesNull;
/**
* Creates a {@code XpathElement} instance.
*
* @param files regular expression for names of filtered files
* @param checks regular expression for filtered check classes
* @param message regular expression for messages.
* @param moduleId the module id
* @param query the xpath query
* @throws IllegalArgumentException if the xpath query is not expected.
*/
public XpathFilterElement(String files, String checks,
String message, String moduleId, String query) {
filePattern = files;
if (files == null) {
fileRegexp = null;
}
else {
fileRegexp = Pattern.compile(files);
}
checkPattern = checks;
if (checks == null) {
checkRegexp = null;
}
else {
checkRegexp = CommonUtil.createPattern(checks);
}
messagePattern = message;
if (message == null) {
messageRegexp = null;
}
else {
messageRegexp = Pattern.compile(message);
}
this.moduleId = moduleId;
xpathQuery = query;
if (xpathQuery == null) {
xpathExpression = null;
}
else {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(
Configuration.newConfiguration());
try {
xpathExpression = xpathEvaluator.createExpression(xpathQuery);
}
catch (XPathException ex) {
throw new IllegalArgumentException("Unexpected xpath query: " + xpathQuery, ex);
}
}
areAllPropertiesNull = fileRegexp == null
&& checkRegexp == null
&& messageRegexp == null
&& moduleId == null
&& xpathExpression == null;
}
/**
* Creates a {@code XpathElement} instance.
*
* @param files regular expression for names of filtered files
* @param checks regular expression for filtered check classes
* @param message regular expression for messages.
* @param moduleId the module id
* @param query the xpath query
* @throws IllegalArgumentException if the xpath query is not correct.
*/
public XpathFilterElement(Pattern files, Pattern checks, Pattern message,
String moduleId, String query) {
if (files == null) {
filePattern = null;
fileRegexp = null;
}
else {
filePattern = files.pattern();
fileRegexp = files;
}
if (checks == null) {
checkPattern = null;
checkRegexp = null;
}
else {
checkPattern = checks.pattern();
checkRegexp = checks;
}
if (message == null) {
messagePattern = null;
messageRegexp = null;
}
else {
messagePattern = message.pattern();
messageRegexp = message;
}
this.moduleId = moduleId;
xpathQuery = query;
if (xpathQuery == null) {
xpathExpression = null;
}
else {
final XPathEvaluator xpathEvaluator = new XPathEvaluator(
Configuration.newConfiguration());
try {
xpathExpression = xpathEvaluator.createExpression(xpathQuery);
}
catch (XPathException ex) {
throw new IllegalArgumentException("Incorrect xpath query: " + xpathQuery, ex);
}
}
areAllPropertiesNull = fileRegexp == null
&& checkRegexp == null
&& messageRegexp == null
&& moduleId == null
&& xpathExpression == null;
}
@Override
public boolean accept(TreeWalkerAuditEvent event) {
return !isFileNameAndModuleAndModuleNameMatching(event)
|| !isMessageNameMatching(event)
|| !isXpathQueryMatching(event)
|| areAllPropertiesNull();
}
/**
* Check if all properties are set to null.
*
* @return true if all properties are set to null
*/
private boolean areAllPropertiesNull() {
return areAllPropertiesNull;
}
/**
* Is matching by file name, module id and Check name.
*
* @param event event
* @return true if it is matching
*/
private boolean isFileNameAndModuleAndModuleNameMatching(TreeWalkerAuditEvent event) {
return event.getFileName() != null
&& (fileRegexp == null || fileRegexp.matcher(event.getFileName()).find())
&& event.getViolation() != null
&& (moduleId == null || moduleId.equals(event.getModuleId()))
&& (checkRegexp == null || checkRegexp.matcher(event.getSourceName()).find());
}
/**
* Is matching by message.
*
* @param event event
* @return true if it is matching or not set.
*/
private boolean isMessageNameMatching(TreeWalkerAuditEvent event) {
return messageRegexp == null || messageRegexp.matcher(event.getMessage()).find();
}
/**
* Is matching by xpath query.
*
* @param event event
* @return true if it is matching or not set.
*/
private boolean isXpathQueryMatching(TreeWalkerAuditEvent event) {
boolean isMatching;
if (xpathExpression == null) {
isMatching = true;
}
else {
isMatching = false;
final List<AbstractNode> nodes = getItems(event)
.stream().map(AbstractNode.class::cast).collect(Collectors.toList());
for (AbstractNode abstractNode : nodes) {
isMatching = abstractNode.getTokenType() == event.getTokenType()
&& abstractNode.getLineNumber() == event.getLine()
&& abstractNode.getColumnNumber() == event.getColumnCharIndex();
if (isMatching) {
break;
}
}
}
return isMatching;
}
/**
* Returns list of nodes matching xpath expression given event.
*
* @param event {@code TreeWalkerAuditEvent} object
* @return list of nodes matching xpath expression given event
* @throws IllegalStateException if the xpath query could not be evaluated.
*/
private List<Item> getItems(TreeWalkerAuditEvent event) {
final RootNode rootNode;
if (event.getRootAst() == null) {
rootNode = null;
}
else {
rootNode = new RootNode(event.getRootAst());
}
final List<Item> items;
try {
final XPathDynamicContext xpathDynamicContext =
xpathExpression.createDynamicContext(rootNode);
items = xpathExpression.evaluate(xpathDynamicContext);
}
catch (XPathException ex) {
throw new IllegalStateException("Cannot initialize context and evaluate query: "
+ xpathQuery, ex);
}
return items;
}
@Override
public int hashCode() {
return Objects.hash(filePattern, checkPattern, messagePattern,
moduleId, xpathQuery, areAllPropertiesNull);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
final XpathFilterElement xpathFilter = (XpathFilterElement) other;
return Objects.equals(filePattern, xpathFilter.filePattern)
&& Objects.equals(checkPattern, xpathFilter.checkPattern)
&& Objects.equals(messagePattern, xpathFilter.messagePattern)
&& Objects.equals(moduleId, xpathFilter.moduleId)
&& Objects.equals(xpathQuery, xpathFilter.xpathQuery)
&& Objects.equals(areAllPropertiesNull, xpathFilter.areAllPropertiesNull);
}
}