Replace regex match with NewlineCheck for more tolerative builds
- Fixes #2
This commit is contained in:
parent
78e2b52174
commit
f23a34754a
95
src/main/java/io/netty/build/checkstyle/NewlineCheck.java
Normal file
95
src/main/java/io/netty/build/checkstyle/NewlineCheck.java
Normal file
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.netty.build.checkstyle;
|
||||
|
||||
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
|
||||
import com.puppycrawl.tools.checkstyle.api.FileText;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class NewlineCheck extends AbstractFileSetCheck {
|
||||
|
||||
private enum NewlineType {
|
||||
UNKNOWN,
|
||||
CR,
|
||||
LF,
|
||||
CRLF,
|
||||
LFCR,
|
||||
RS
|
||||
}
|
||||
|
||||
private static final Pattern NON_CRLF = Pattern.compile("(?:[^\r]\n|\r[^\n])");
|
||||
private static final Pattern NON_LFCR = Pattern.compile("(?:[^\n]\r|\n[^\r])");
|
||||
|
||||
private static final NewlineType NEWLINE_TYPE;
|
||||
|
||||
static {
|
||||
final String newline = System.lineSeparator();
|
||||
if ("\n".equals(newline)) {
|
||||
NEWLINE_TYPE = NewlineType.LF;
|
||||
} else if ("\r".equals(newline)) {
|
||||
NEWLINE_TYPE = NewlineType.CR;
|
||||
} else if ("\r\n".equals(newline)) {
|
||||
NEWLINE_TYPE = NewlineType.CRLF;
|
||||
} else if ("\n\r".equals(newline)) {
|
||||
NEWLINE_TYPE = NewlineType.LFCR;
|
||||
} else if ("\u001E".equals(newline)) {
|
||||
NEWLINE_TYPE = NewlineType.RS;
|
||||
} else {
|
||||
NEWLINE_TYPE = NewlineType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void processFiltered(File aFile, List<String> aLines) {
|
||||
String text = FileText.fromLines(aFile, aLines).getFullText().toString();
|
||||
|
||||
switch (NEWLINE_TYPE) {
|
||||
case LF:
|
||||
if (text.indexOf('\r') >= 0) {
|
||||
reportNewlineViolation();
|
||||
}
|
||||
break;
|
||||
case CR:
|
||||
if (text.indexOf('\n') >= 0) {
|
||||
reportNewlineViolation();
|
||||
}
|
||||
break;
|
||||
case CRLF:
|
||||
if (NON_CRLF.matcher(text).find()) {
|
||||
reportNewlineViolation();
|
||||
}
|
||||
break;
|
||||
case LFCR:
|
||||
if (NON_LFCR.matcher(text).find()) {
|
||||
reportNewlineViolation();
|
||||
}
|
||||
break;
|
||||
case RS:
|
||||
if (text.indexOf('\u001E') >= 0) {
|
||||
reportNewlineViolation();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void reportNewlineViolation() {
|
||||
log(0, "invalid newline character (expected: " + NEWLINE_TYPE + ')');
|
||||
}
|
||||
}
|
@ -23,9 +23,7 @@
|
||||
</module>
|
||||
<module name="FileTabCharacter"/>
|
||||
<module name="JavadocPackage"/>
|
||||
<module name="NewlineAtEndOfFile">
|
||||
<property name="lineSeparator" value="lf" />
|
||||
</module>
|
||||
<module name="NewlineAtEndOfFile" />
|
||||
<!-- Copyright headers -->
|
||||
<module name="RegexpSingleline">
|
||||
<property name="format" value="^(\s|\*)*Copyright\s+[0-9]+\s+The Netty Project\s*$"/>
|
||||
@ -45,10 +43,7 @@
|
||||
<property name="message" value="IDE-generated comment"/>
|
||||
</module>
|
||||
<!-- Force UNIX line separator -->
|
||||
<module name="RegexpMultiline">
|
||||
<property name="format" value="\r"/>
|
||||
<property name="message" value="non-Unix line separator"/>
|
||||
</module>
|
||||
<module name="io.netty.build.checkstyle.NewlineCheck"/>
|
||||
<!-- Trailing whitespace -->
|
||||
<module name="RegexpSingleline">
|
||||
<property name="format" value="\s+$"/>
|
||||
|
Loading…
Reference in New Issue
Block a user