forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InlineConfigParser.java
129 lines (116 loc) · 5.32 KB
/
InlineConfigParser.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
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2021 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.bdd;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
public final class InlineConfigParser {
/** A pattern matching the symbol: "\" or "/". */
private static final Pattern SLASH_PATTERN = Pattern.compile("[\\\\/]");
/** A pattern to find the string: "// violation". */
private static final Pattern VIOLATION_PATTERN = Pattern
.compile(".*//\\s*violation(?:\\W+\'(.*)\')?$");
/** Stop instances being created. **/
private InlineConfigParser() {
}
/**
* Parses the input file provided.
*
* @param inputFilePath the input file path.
* @throws Exception if unable to read file or file not formatted properly.
*/
public static TestInputConfiguration parse(String inputFilePath) throws Exception {
final Path filePath = Paths.get(inputFilePath);
final List<String> lines = readFile(filePath);
final TestInputConfiguration.Builder inputConfigBuilder =
new TestInputConfiguration.Builder();
setCheckName(inputConfigBuilder, inputFilePath, lines);
setCheckProperties(inputConfigBuilder, lines);
setViolations(inputConfigBuilder, lines);
return inputConfigBuilder.build();
}
private static String getPackageFromFilePath(String filePath) {
final String path = SLASH_PATTERN.matcher(filePath).replaceAll("\\.");
final int beginIndex = path.indexOf("com.puppycrawl");
final int endIndex = path.lastIndexOf('.',
path.lastIndexOf('.', path.lastIndexOf('.') - 1) - 1);
return path.substring(beginIndex, endIndex);
}
private static List<String> readFile(Path filePath) throws CheckstyleException {
try {
return Files.readAllLines(filePath);
}
catch (IOException ex) {
throw new CheckstyleException("Failed to read " + filePath, ex);
}
}
private static void setCheckName(TestInputConfiguration.Builder inputConfigBuilder,
String filePath, List<String> lines)
throws CheckstyleException {
if (lines.size() < 2) {
throw new CheckstyleException("Config not specified in " + filePath);
}
final String checkName = lines.get(1);
final String checkPath = getPackageFromFilePath(filePath) + "." + checkName + "Check";
inputConfigBuilder.setCheckName(checkPath);
}
private static void setCheckProperties(TestInputConfiguration.Builder inputConfigBuilder,
List<String> lines)
throws Exception {
final StringBuilder stringBuilder = new StringBuilder(128);
int lineNo = 2;
for (String line = lines.get(lineNo); !line.contains("*/");
++lineNo, line = lines.get(lineNo)) {
stringBuilder.append(line).append('\n');
}
final Properties properties = new Properties();
try (Reader reader = new StringReader(stringBuilder.toString())) {
properties.load(reader);
}
for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
final String key = entry.getKey().toString();
final String value = entry.getValue().toString();
if (value.startsWith("(default)")) {
inputConfigBuilder.addDefaultProperty(key,
value.substring(value.indexOf(')') + 1));
}
else {
inputConfigBuilder.addNonDefaultProperty(key, value);
}
}
}
private static void setViolations(TestInputConfiguration.Builder inputConfigBuilder,
List<String> lines) {
for (int lineNo = 2; lineNo < lines.size(); lineNo++) {
final Matcher violationMatcher = VIOLATION_PATTERN.matcher(lines.get(lineNo));
if (violationMatcher.matches()) {
inputConfigBuilder.addViolation(lineNo + 1, violationMatcher.group(1));
}
}
}
}