-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ConfigurationLoader.java
613 lines (550 loc) · 25.7 KB
/
ConfigurationLoader.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
///////////////////////////////////////////////////////////////////////////////////////////////
// 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;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
/**
* Loads a configuration from a standard configuration XML file.
*
*/
public final class ConfigurationLoader {
/**
* Enum to specify behaviour regarding ignored modules.
*/
public enum IgnoredModulesOptions {
/**
* Omit ignored modules.
*/
OMIT,
/**
* Execute ignored modules.
*/
EXECUTE,
}
/** Format of message for sax parse exception. */
private static final String SAX_PARSE_EXCEPTION_FORMAT = "%s - %s:%s:%s";
/** The public ID for version 1_0 of the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_0 =
"-//Puppy Crawl//DTD Check Configuration 1.0//EN";
/** The new public ID for version 1_0 of the configuration dtd. */
private static final String DTD_PUBLIC_CS_ID_1_0 =
"-//Checkstyle//DTD Checkstyle Configuration 1.0//EN";
/** The resource for version 1_0 of the configuration dtd. */
private static final String DTD_CONFIGURATION_NAME_1_0 =
"com/puppycrawl/tools/checkstyle/configuration_1_0.dtd";
/** The public ID for version 1_1 of the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_1 =
"-//Puppy Crawl//DTD Check Configuration 1.1//EN";
/** The new public ID for version 1_1 of the configuration dtd. */
private static final String DTD_PUBLIC_CS_ID_1_1 =
"-//Checkstyle//DTD Checkstyle Configuration 1.1//EN";
/** The resource for version 1_1 of the configuration dtd. */
private static final String DTD_CONFIGURATION_NAME_1_1 =
"com/puppycrawl/tools/checkstyle/configuration_1_1.dtd";
/** The public ID for version 1_2 of the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_2 =
"-//Puppy Crawl//DTD Check Configuration 1.2//EN";
/** The new public ID for version 1_2 of the configuration dtd. */
private static final String DTD_PUBLIC_CS_ID_1_2 =
"-//Checkstyle//DTD Checkstyle Configuration 1.2//EN";
/** The resource for version 1_2 of the configuration dtd. */
private static final String DTD_CONFIGURATION_NAME_1_2 =
"com/puppycrawl/tools/checkstyle/configuration_1_2.dtd";
/** The public ID for version 1_3 of the configuration dtd. */
private static final String DTD_PUBLIC_ID_1_3 =
"-//Puppy Crawl//DTD Check Configuration 1.3//EN";
/** The new public ID for version 1_3 of the configuration dtd. */
private static final String DTD_PUBLIC_CS_ID_1_3 =
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN";
/** The resource for version 1_3 of the configuration dtd. */
private static final String DTD_CONFIGURATION_NAME_1_3 =
"com/puppycrawl/tools/checkstyle/configuration_1_3.dtd";
/** Prefix for the exception when unable to parse resource. */
private static final String UNABLE_TO_PARSE_EXCEPTION_PREFIX = "unable to parse"
+ " configuration stream";
/** Dollar sign literal. */
private static final char DOLLAR_SIGN = '$';
/** Dollar sign string. */
private static final String DOLLAR_SIGN_STRING = String.valueOf(DOLLAR_SIGN);
/** The SAX document handler. */
private final InternalLoader saxHandler;
/** Property resolver. **/
private final PropertyResolver overridePropsResolver;
/** The loaded configurations. **/
private final Deque<DefaultConfiguration> configStack = new ArrayDeque<>();
/** Flags if modules with the severity 'ignore' should be omitted. */
private final boolean omitIgnoredModules;
/** The thread mode configuration. */
private final ThreadModeSettings threadModeSettings;
/** The Configuration that is being built. */
private Configuration configuration;
/**
* Creates a new {@code ConfigurationLoader} instance.
*
* @param overrideProps resolver for overriding properties
* @param omitIgnoredModules {@code true} if ignored modules should be
* omitted
* @param threadModeSettings the thread mode configuration
* @throws ParserConfigurationException if an error occurs
* @throws SAXException if an error occurs
*/
private ConfigurationLoader(final PropertyResolver overrideProps,
final boolean omitIgnoredModules,
final ThreadModeSettings threadModeSettings)
throws ParserConfigurationException, SAXException {
saxHandler = new InternalLoader();
overridePropsResolver = overrideProps;
this.omitIgnoredModules = omitIgnoredModules;
this.threadModeSettings = threadModeSettings;
}
/**
* Creates mapping between local resources and dtd ids. This method can't be
* moved to inner class because it must stay static because it is called
* from constructor and inner class isn't static.
*
* @return map between local resources and dtd ids.
*/
private static Map<String, String> createIdToResourceNameMap() {
final Map<String, String> map = new HashMap<>();
map.put(DTD_PUBLIC_ID_1_0, DTD_CONFIGURATION_NAME_1_0);
map.put(DTD_PUBLIC_ID_1_1, DTD_CONFIGURATION_NAME_1_1);
map.put(DTD_PUBLIC_ID_1_2, DTD_CONFIGURATION_NAME_1_2);
map.put(DTD_PUBLIC_ID_1_3, DTD_CONFIGURATION_NAME_1_3);
map.put(DTD_PUBLIC_CS_ID_1_0, DTD_CONFIGURATION_NAME_1_0);
map.put(DTD_PUBLIC_CS_ID_1_1, DTD_CONFIGURATION_NAME_1_1);
map.put(DTD_PUBLIC_CS_ID_1_2, DTD_CONFIGURATION_NAME_1_2);
map.put(DTD_PUBLIC_CS_ID_1_3, DTD_CONFIGURATION_NAME_1_3);
return map;
}
/**
* Parses the specified input source loading the configuration information.
* The stream wrapped inside the source, if any, is NOT
* explicitly closed after parsing, it is the responsibility of
* the caller to close the stream.
*
* @param source the source that contains the configuration data
* @throws IOException if an error occurs
* @throws SAXException if an error occurs
*/
private void parseInputSource(InputSource source)
throws IOException, SAXException {
saxHandler.parseInputSource(source);
}
/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver) throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, IgnoredModulesOptions.EXECUTE);
}
/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param threadModeSettings the thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver, ThreadModeSettings threadModeSettings)
throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver,
IgnoredModulesOptions.EXECUTE, threadModeSettings);
}
/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param ignoredModulesOptions {@code OMIT} if modules with severity
* 'ignore' should be omitted, {@code EXECUTE} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver,
IgnoredModulesOptions ignoredModulesOptions)
throws CheckstyleException {
return loadConfiguration(config, overridePropsResolver, ignoredModulesOptions,
ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
}
/**
* Returns the module configurations in a specified file.
*
* @param config location of config file, can be either a URL or a filename
* @param overridePropsResolver overriding properties
* @param ignoredModulesOptions {@code OMIT} if modules with severity
* 'ignore' should be omitted, {@code EXECUTE} otherwise
* @param threadModeSettings the thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(String config,
PropertyResolver overridePropsResolver,
IgnoredModulesOptions ignoredModulesOptions,
ThreadModeSettings threadModeSettings)
throws CheckstyleException {
// figure out if this is a File or a URL
final URI uri = CommonUtil.getUriByFilename(config);
final InputSource source = new InputSource(uri.toString());
return loadConfiguration(source, overridePropsResolver,
ignoredModulesOptions, threadModeSettings);
}
/**
* Returns the module configurations from a specified input source.
* Note that if the source does wrap an open byte or character
* stream, clients are required to close that stream by themselves
*
* @param configSource the input stream to the Checkstyle configuration
* @param overridePropsResolver overriding properties
* @param ignoredModulesOptions {@code OMIT} if modules with severity
* 'ignore' should be omitted, {@code EXECUTE} otherwise
* @return the check configurations
* @throws CheckstyleException if an error occurs
*/
public static Configuration loadConfiguration(InputSource configSource,
PropertyResolver overridePropsResolver,
IgnoredModulesOptions ignoredModulesOptions)
throws CheckstyleException {
return loadConfiguration(configSource, overridePropsResolver,
ignoredModulesOptions, ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
}
/**
* Returns the module configurations from a specified input source.
* Note that if the source does wrap an open byte or character
* stream, clients are required to close that stream by themselves
*
* @param configSource the input stream to the Checkstyle configuration
* @param overridePropsResolver overriding properties
* @param ignoredModulesOptions {@code OMIT} if modules with severity
* 'ignore' should be omitted, {@code EXECUTE} otherwise
* @param threadModeSettings the thread mode configuration
* @return the check configurations
* @throws CheckstyleException if an error occurs
* @noinspection WeakerAccess
* @noinspectionreason WeakerAccess - we avoid 'protected' when possible
*/
public static Configuration loadConfiguration(InputSource configSource,
PropertyResolver overridePropsResolver,
IgnoredModulesOptions ignoredModulesOptions,
ThreadModeSettings threadModeSettings)
throws CheckstyleException {
try {
final boolean omitIgnoreModules = ignoredModulesOptions == IgnoredModulesOptions.OMIT;
final ConfigurationLoader loader =
new ConfigurationLoader(overridePropsResolver,
omitIgnoreModules, threadModeSettings);
loader.parseInputSource(configSource);
return loader.configuration;
}
catch (final SAXParseException ex) {
final String message = String.format(Locale.ROOT, SAX_PARSE_EXCEPTION_FORMAT,
UNABLE_TO_PARSE_EXCEPTION_PREFIX,
ex.getMessage(), ex.getLineNumber(), ex.getColumnNumber());
throw new CheckstyleException(message, ex);
}
catch (final ParserConfigurationException | IOException | SAXException ex) {
throw new CheckstyleException(UNABLE_TO_PARSE_EXCEPTION_PREFIX, ex);
}
}
/**
* Replaces {@code ${xxx}} style constructions in the given value
* with the string value of the corresponding data types. This method must remain
* outside inner class for easier testing since inner class requires an instance.
*
* <p>Code copied from
* <a href="https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/ProjectHelper.java">
* ant
* </a>
*
* @param value The string to be scanned for property references. Must
* not be {@code null}.
* @param props Mapping (String to String) of property names to their
* values. Must not be {@code null}.
* @param defaultValue default to use if one of the properties in value
* cannot be resolved from props.
*
* @return the original string with the properties replaced.
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
*/
private static String replaceProperties(
String value, PropertyResolver props, String defaultValue)
throws CheckstyleException {
final List<String> fragments = new ArrayList<>();
final List<String> propertyRefs = new ArrayList<>();
parsePropertyString(value, fragments, propertyRefs);
final StringBuilder sb = new StringBuilder(256);
final Iterator<String> fragmentsIterator = fragments.iterator();
final Iterator<String> propertyRefsIterator = propertyRefs.iterator();
while (fragmentsIterator.hasNext()) {
String fragment = fragmentsIterator.next();
if (fragment == null) {
final String propertyName = propertyRefsIterator.next();
fragment = props.resolve(propertyName);
if (fragment == null) {
if (defaultValue != null) {
sb.replace(0, sb.length(), defaultValue);
break;
}
throw new CheckstyleException(
"Property ${" + propertyName + "} has not been set");
}
}
sb.append(fragment);
}
return sb.toString();
}
/**
* Parses a string containing {@code ${xxx}} style property
* references into two collections. The first one is a collection
* of text fragments, while the other is a set of string property names.
* {@code null} entries in the first collection indicate a property
* reference from the second collection.
*
* <p>Code copied from
* <a href="https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/ProjectHelper.java">
* ant
* </a>
*
* @param value Text to parse. Must not be {@code null}.
* @param fragments Collection to add text fragments to.
* Must not be {@code null}.
* @param propertyRefs Collection to add property names to.
* Must not be {@code null}.
*
* @throws CheckstyleException if the string contains an opening
* {@code ${} without a closing
* {@code }}
*/
private static void parsePropertyString(String value,
Collection<String> fragments,
Collection<String> propertyRefs)
throws CheckstyleException {
int prev = 0;
// search for the next instance of $ from the 'prev' position
int pos = value.indexOf(DOLLAR_SIGN, prev);
while (pos >= 0) {
// if there was any text before this, add it as a fragment
if (pos > 0) {
fragments.add(value.substring(prev, pos));
}
// if we are at the end of the string, we tack on a $
// then move past it
if (pos == value.length() - 1) {
fragments.add(DOLLAR_SIGN_STRING);
prev = pos + 1;
}
else if (value.charAt(pos + 1) == '{') {
// property found, extract its name or bail on a typo
final int endName = value.indexOf('}', pos);
if (endName == -1) {
throw new CheckstyleException("Syntax error in property: "
+ value);
}
final String propertyName = value.substring(pos + 2, endName);
fragments.add(null);
propertyRefs.add(propertyName);
prev = endName + 1;
}
else {
if (value.charAt(pos + 1) == DOLLAR_SIGN) {
// backwards compatibility two $ map to one mode
fragments.add(DOLLAR_SIGN_STRING);
}
else {
// new behaviour: $X maps to $X for all values of X!='$'
fragments.add(value.substring(pos, pos + 2));
}
prev = pos + 2;
}
// search for the next instance of $ from the 'prev' position
pos = value.indexOf(DOLLAR_SIGN, prev);
}
// no more $ signs found
// if there is any tail to the file, append it
if (prev < value.length()) {
fragments.add(value.substring(prev));
}
}
/**
* Implements the SAX document handler interfaces, so they do not
* appear in the public API of the ConfigurationLoader.
*/
private final class InternalLoader
extends XmlLoader {
/** Module elements. */
private static final String MODULE = "module";
/** Name attribute. */
private static final String NAME = "name";
/** Property element. */
private static final String PROPERTY = "property";
/** Value attribute. */
private static final String VALUE = "value";
/** Default attribute. */
private static final String DEFAULT = "default";
/** Name of the severity property. */
private static final String SEVERITY = "severity";
/** Name of the message element. */
private static final String MESSAGE = "message";
/** Name of the message element. */
private static final String METADATA = "metadata";
/** Name of the key attribute. */
private static final String KEY = "key";
/**
* Creates a new InternalLoader.
*
* @throws SAXException if an error occurs
* @throws ParserConfigurationException if an error occurs
*/
/* package */ InternalLoader()
throws SAXException, ParserConfigurationException {
super(createIdToResourceNameMap());
}
@Override
public void startElement(String uri,
String localName,
String qName,
Attributes attributes)
throws SAXException {
if (MODULE.equals(qName)) {
// create configuration
final String originalName = attributes.getValue(NAME);
final String name = threadModeSettings.resolveName(originalName);
final DefaultConfiguration conf =
new DefaultConfiguration(name, threadModeSettings);
if (configuration == null) {
configuration = conf;
}
// add configuration to it's parent
if (!configStack.isEmpty()) {
final DefaultConfiguration top =
configStack.peek();
top.addChild(conf);
}
configStack.push(conf);
}
else if (PROPERTY.equals(qName)) {
// extract value and name
final String attributesValue = attributes.getValue(VALUE);
final String value;
try {
value = replaceProperties(attributesValue,
overridePropsResolver, attributes.getValue(DEFAULT));
}
catch (final CheckstyleException ex) {
// -@cs[IllegalInstantiation] SAXException is in the overridden
// method signature
throw new SAXException(ex);
}
final String name = attributes.getValue(NAME);
// add to attributes of configuration
final DefaultConfiguration top =
configStack.peek();
top.addProperty(name, value);
}
else if (MESSAGE.equals(qName)) {
// extract key and value
final String key = attributes.getValue(KEY);
final String value = attributes.getValue(VALUE);
// add to messages of configuration
final DefaultConfiguration top = configStack.peek();
top.addMessage(key, value);
}
else {
if (!METADATA.equals(qName)) {
throw new IllegalStateException("Unknown name:" + qName + ".");
}
}
}
@Override
public void endElement(String uri,
String localName,
String qName) throws SAXException {
if (MODULE.equals(qName)) {
final Configuration recentModule =
configStack.pop();
// get severity attribute if it exists
SeverityLevel level = null;
if (containsAttribute(recentModule, SEVERITY)) {
try {
final String severity = recentModule.getProperty(SEVERITY);
level = SeverityLevel.getInstance(severity);
}
catch (final CheckstyleException ex) {
// -@cs[IllegalInstantiation] SAXException is in the overridden
// method signature
throw new SAXException(
"Problem during accessing '" + SEVERITY + "' attribute for "
+ recentModule.getName(), ex);
}
}
// omit this module if these should be omitted and the module
// has the severity 'ignore'
final boolean omitModule = omitIgnoredModules
&& level == SeverityLevel.IGNORE;
if (omitModule && !configStack.isEmpty()) {
final DefaultConfiguration parentModule =
configStack.peek();
parentModule.removeChild(recentModule);
}
}
}
/**
* Util method to recheck attribute in module.
*
* @param module module to check
* @param attributeName name of attribute in module to find
* @return true if attribute is present in module
*/
private boolean containsAttribute(Configuration module, String attributeName) {
final String[] names = module.getPropertyNames();
final Optional<String> result = Arrays.stream(names)
.filter(name -> name.equals(attributeName)).findFirst();
return result.isPresent();
}
}
}