forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NewlineAtEndOfFileCheck.java
142 lines (130 loc) · 5.05 KB
/
NewlineAtEndOfFileCheck.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
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2019 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.checks;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Locale;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
import com.puppycrawl.tools.checkstyle.api.FileText;
/**
* <p>
* Checks that there is a newline at the end of each file.
* </p>
* <p>
* An example of how to configure the check is:
* </p>
* <pre>
* <module name="NewlineAtEndOfFile"/></pre>
* <p>
* This will check against the platform-specific default line separator.
* </p>
* <p>
* It is also possible to enforce the use of a specific line-separator across
* platforms, with the 'lineSeparator' property:
* </p>
* <pre>
* <module name="NewlineAtEndOfFile">
* <property name="lineSeparator" value="lf"/>
* </module></pre>
* <p>
* Valid values for the 'lineSeparator' property are 'system' (system default),
* 'crlf' (windows), 'cr' (mac), 'lf' (unix) and 'lf_cr_crlf' (lf, cr or crlf).
* </p>
*
*/
@StatelessCheck
public class NewlineAtEndOfFileCheck
extends AbstractFileSetCheck {
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_UNABLE_OPEN = "unable.open";
/**
* A key is pointing to the warning message text in "messages.properties"
* file.
*/
public static final String MSG_KEY_NO_NEWLINE_EOF = "noNewlineAtEOF";
/** The line separator to check against. */
private LineSeparatorOption lineSeparator = LineSeparatorOption.LF_CR_CRLF;
@Override
protected void processFiltered(File file, FileText fileText) {
try {
readAndCheckFile(file);
}
catch (final IOException ignored) {
log(1, MSG_KEY_UNABLE_OPEN, file.getPath());
}
}
/**
* Sets the line separator to one of 'crlf', 'lf','cr', 'lf_cr_crlf' or 'system'.
*
* @param lineSeparatorParam The line separator to set
* @throws IllegalArgumentException If the specified line separator is not
* one of 'crlf', 'lf', 'cr', 'lf_cr_crlf' or 'system'
*/
public void setLineSeparator(String lineSeparatorParam) {
lineSeparator =
Enum.valueOf(LineSeparatorOption.class, lineSeparatorParam.trim()
.toUpperCase(Locale.ENGLISH));
}
/**
* Reads the file provided and checks line separators.
* @param file the file to be processed
* @throws IOException When an IO error occurred while reading from the
* file provided
*/
private void readAndCheckFile(File file) throws IOException {
// Cannot use lines as the line separators have been removed!
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
if (!endsWithNewline(randomAccessFile)) {
log(1, MSG_KEY_NO_NEWLINE_EOF, file.getPath());
}
}
}
/**
* Checks whether the content provided by the Reader ends with the platform
* specific line separator.
* @param randomAccessFile The reader for the content to check
* @return boolean Whether the content ends with a line separator
* @throws IOException When an IO error occurred while reading from the
* provided reader
*/
private boolean endsWithNewline(RandomAccessFile randomAccessFile)
throws IOException {
final boolean result;
final int len = lineSeparator.length();
if (randomAccessFile.length() < len) {
result = false;
}
else {
randomAccessFile.seek(randomAccessFile.length() - len);
final byte[] lastBytes = new byte[len];
final int readBytes = randomAccessFile.read(lastBytes);
if (readBytes != len) {
throw new IOException("Unable to read " + len + " bytes, got "
+ readBytes);
}
result = lineSeparator.matches(lastBytes);
}
return result;
}
}