Skip to content

Commit f570ce4

Browse files
committed
Allow SpringHeaderCheck to load custom files
Update `SpringHeaderCheck` to allow customer header files to be used. Closes gh-70
1 parent 2655bda commit f570ce4

File tree

8 files changed

+91
-13
lines changed

8 files changed

+91
-13
lines changed

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/SpringChecks.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class SpringChecks extends AbstractFileSetCheck implements ExternalResour
5959

6060
private String headerCopyrightPattern = SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN;
6161

62+
private String headerFile;
63+
6264
/**
6365
* Sets tab width.
6466
* @param tabWidth the distance between tab stops
@@ -91,12 +93,19 @@ public void finishLocalSetup() {
9193
context.add("tabWidth", String.valueOf(this.tabWidth));
9294
context.add("moduleFactory", this.moduleFactory);
9395
Properties properties = new Properties();
94-
properties.put("headerType", this.headerType);
95-
properties.put("headerCopyrightPattern", this.headerCopyrightPattern);
96+
put(properties, "headerType", this.headerType);
97+
put(properties, "headerCopyrightPattern", this.headerCopyrightPattern);
98+
put(properties, "headerFile", this.headerFile);
9699
this.checks = new SpringConfigurationLoader(context, this.moduleFactory)
97100
.load(new PropertiesExpander(properties));
98101
}
99102

103+
private void put(Properties properties, String name, Object value) {
104+
if (value != null) {
105+
properties.put(name, value);
106+
}
107+
}
108+
100109
@Override
101110
public Set<String> getExternalResourceLocations() {
102111
Set<String> locations = new LinkedHashSet<>();
@@ -147,4 +156,8 @@ public void setHeaderCopyrightPattern(String headerCopyrightPattern) {
147156
this.headerCopyrightPattern = headerCopyrightPattern;
148157
}
149158

159+
public void setHeaderFile(String headerFile) {
160+
this.headerFile = headerFile;
161+
}
162+
150163
}

spring-javaformat/spring-javaformat-checkstyle/src/main/java/io/spring/javaformat/checkstyle/check/SpringHeaderCheck.java

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
import java.io.InputStreamReader;
2424
import java.io.LineNumberReader;
2525
import java.io.Reader;
26+
import java.io.UnsupportedEncodingException;
27+
import java.net.URI;
28+
import java.nio.charset.Charset;
29+
import java.nio.charset.StandardCharsets;
2630
import java.util.ArrayList;
2731
import java.util.List;
2832
import java.util.regex.Pattern;
@@ -59,36 +63,50 @@ public class SpringHeaderCheck extends AbstractFileSetCheck {
5963
*/
6064
public static final String DEFAULT_HEADER_COPYRIGHT_PATTERN = "20\\d\\d(-20\\d\\d)?";
6165

66+
private static final String DEFAULT_CHARSET = System.getProperty("file.encoding",
67+
StandardCharsets.UTF_8.name());
68+
69+
private String charset = DEFAULT_CHARSET;
70+
6271
private String headerType = DEFAULT_HEADER_TYPE;
6372

73+
private URI headerFile;
74+
6475
private String headerCopyrightPattern = DEFAULT_HEADER_COPYRIGHT_PATTERN;
6576

6677
private String packageInfoHeaderType;
6778

79+
private URI packageInfoHeaderFile;
80+
6881
private HeaderCheck check;
6982

7083
private HeaderCheck packageInfoCheck;
7184

7285
@Override
7386
protected void finishLocalSetup() throws CheckstyleException {
7487
try {
75-
this.check = createCheck(this.headerType);
76-
this.packageInfoCheck = createCheck(this.packageInfoHeaderType != null
77-
? this.packageInfoHeaderType : this.headerType);
88+
this.check = createCheck(this.headerType, this.headerFile);
89+
String packageInfoHeaderType = this.packageInfoHeaderType != null
90+
? this.packageInfoHeaderType : this.headerType;
91+
URI packageInfoHeaderFile = this.packageInfoHeaderFile != null
92+
? this.packageInfoHeaderFile : this.headerFile;
93+
this.packageInfoCheck = createCheck(packageInfoHeaderType,
94+
packageInfoHeaderFile);
7895
}
7996
catch (IOException ex) {
8097
throw new IllegalStateException(ex);
8198
}
8299
}
83100

84-
private HeaderCheck createCheck(String headerType) throws IOException {
101+
private HeaderCheck createCheck(String headerType, URI headerFile)
102+
throws IOException {
85103
if (UNCHECKED.equals(headerType)) {
86104
return HeaderCheck.NONE;
87105
}
88106
if (NONE.equals(headerType)) {
89107
return new NoHeaderCheck();
90108
}
91-
return new RegexHeaderCheck(headerType, this.headerCopyrightPattern);
109+
return new RegexHeaderCheck(headerType, headerFile);
92110
}
93111

94112
@Override
@@ -104,10 +122,22 @@ private HeaderCheck getCheck(File file) {
104122
return this.check;
105123
}
106124

125+
public void setCharset(String charset) throws UnsupportedEncodingException {
126+
if (!Charset.isSupported(charset)) {
127+
throw new UnsupportedEncodingException(
128+
"unsupported charset: '" + charset + "'");
129+
}
130+
this.charset = charset;
131+
}
132+
107133
public void setHeaderType(String headerType) {
108134
this.headerType = headerType;
109135
}
110136

137+
public void setHeaderFile(URI headerFile) throws CheckstyleException {
138+
this.headerFile = headerFile;
139+
}
140+
111141
public void setHeaderCopyrightPattern(String headerCopyrightPattern) {
112142
this.headerCopyrightPattern = headerCopyrightPattern;
113143
}
@@ -116,6 +146,10 @@ public void setPackageInfoHeaderType(String packageInfoHeaderType) {
116146
this.packageInfoHeaderType = packageInfoHeaderType;
117147
}
118148

149+
public void setPackageInfoHeaderFile(URI packageInfoHeaderFile) {
150+
this.packageInfoHeaderFile = packageInfoHeaderFile;
151+
}
152+
119153
/**
120154
* Interface used to check for a header.
121155
*/
@@ -142,27 +176,35 @@ private class RegexHeaderCheck implements HeaderCheck {
142176

143177
private final List<Pattern> lines;
144178

145-
RegexHeaderCheck(String type, String copyrightPattern) throws IOException {
179+
RegexHeaderCheck(String type, URI file) throws IOException {
180+
this.lines = loadLines(openInputStream(type, file));
181+
}
182+
183+
private InputStream openInputStream(String type, URI file) throws IOException {
184+
if (file != null) {
185+
return file.toURL().openStream();
186+
}
146187
String name = "header-" + type + ".txt";
147188
InputStream inputStream = SpringHeaderCheck.class.getResourceAsStream(name);
148189
if (inputStream == null) {
149190
throw new IllegalStateException("Unknown header type " + type);
150191
}
151-
this.lines = loadLines(inputStream, copyrightPattern);
192+
return inputStream;
152193
}
153194

154-
private List<Pattern> loadLines(InputStream inputStream, String copyrightPattern)
155-
throws IOException {
195+
private List<Pattern> loadLines(InputStream inputStream) throws IOException {
156196
inputStream = new BufferedInputStream(inputStream);
157-
try (Reader reader = new InputStreamReader(inputStream, "UTF-8")) {
197+
try (Reader reader = new InputStreamReader(inputStream,
198+
SpringHeaderCheck.this.charset)) {
158199
LineNumberReader lineReader = new LineNumberReader(reader);
159200
List<Pattern> lines = new ArrayList<>();
160201
while (true) {
161202
String line = lineReader.readLine();
162203
if (line == null) {
163204
return lines;
164205
}
165-
lines.add(loadLine(line, copyrightPattern));
206+
lines.add(loadLine(line,
207+
SpringHeaderCheck.this.headerCopyrightPattern));
166208
}
167209
}
168210
}

spring-javaformat/spring-javaformat-checkstyle/src/main/resources/io/spring/javaformat/checkstyle/spring-checkstyle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<module name="io.spring.javaformat.checkstyle.check.SpringHeaderCheck">
99
<property name="fileExtensions" value="java" />
1010
<property name="headerType" value="${headerType}"/>
11+
<property name="headerFile" value="${headerFile}" default=""/>
1112
<property name="headerCopyrightPattern" value="${headerCopyrightPattern}"/>
1213
</module>
1314
<module name="com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck">

spring-javaformat/spring-javaformat-checkstyle/src/test/java/io/spring/javaformat/checkstyle/SpringConfigurationLoaderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void loadShouldLoadChecks() {
5252
private PropertyResolver getPropertyResolver() {
5353
Properties properties = new Properties();
5454
properties.put("headerType", SpringHeaderCheck.DEFAULT_HEADER_TYPE);
55+
properties.put("headerFile", "");
5556
properties.put("headerCopyrightPattern",
5657
SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN);
5758
return new PropertiesExpander(properties);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+0 errors
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="com.puppycrawl.tools.checkstyle.Checker">
6+
<module name="io.spring.javaformat.checkstyle.SpringChecks">
7+
<property name="headerFile" value="src/test/resources/customHeaderFile.txt"/>
8+
</module>
9+
</module>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// (c) Somebody ${copyright-pattern}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// (c) Somebody 2017-2019
2+
3+
/**
4+
* A custom header file.
5+
*
6+
* @author Phillip Webb
7+
*/
8+
public class HeaderFile {
9+
10+
}

0 commit comments

Comments
 (0)