Debian Codegraph @

src/tools/org/h2/build/code/CheckTextFiles.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.build.code;
7
8 import java.io.ByteArrayOutputStream;
9 import java.io.File;
10 import java.io.IOException;
11 import java.nio.file.FileVisitResult;
12 import java.nio.file.Files;
13 import java.nio.file.Path;
14 import java.nio.file.Paths;
15 import java.nio.file.SimpleFileVisitor;
16 import java.nio.file.attribute.BasicFileAttributes;
17 import java.util.Arrays;
18
19 /**
20 * This tool checks that source code files only contain the allowed set of
21 * characters, and that the copyright license is included in each file. It also
22 * removes trailing spaces.
23 */
24 public class CheckTextFiles {
25
26 private static final int MAX_SOURCE_LINE_SIZE = 120;
27
28 // must contain "+" otherwise this here counts as well
29 private static final String COPYRIGHT1 = "Copyright 2004-2023";
30 private static final String COPYRIGHT2 = "H2 Group.";
31 private static final String LICENSE = "Multiple-Licensed " +
32 "under the MPL 2.0";
33
34 private static final String[] SUFFIX_CHECK = { "html", "jsp", "js", "css",
35 "bat", "nsi", "java", "txt", "properties", "sql", "xml", "csv",
36 "Driver", "Processor", "prefs" };
37 private static final String[] SUFFIX_IGNORE = { "gif", "png", "odg", "ico",
38 "sxd", "layout", "res", "win", "jar", "task", "svg", "MF", "mf",
39 "sh", "DS_Store", "prop", "class", "json" };
40 private static final String[] SUFFIX_CRLF = { "bat" };
41
42 private static final boolean ALLOW_TAB = false;
43 private static final boolean ALLOW_CR = true;
44 private static final boolean ALLOW_TRAILING_SPACES = false;
45 private static final int SPACES_PER_TAB = 4;
46 private static final boolean AUTO_FIX = true;
47
48 private boolean failOnError;
49 private boolean useCRLF;
50 private final String[] suffixIgnoreLicense = {
51 "bat", "nsi", "txt", "properties", "xml",
52 "java.sql.Driver", "task", "sh", "prefs" };
53 private boolean hasError;
54
55 /**
56 * This method is called when executing this application from the command
57 * line.
58 *
59 * @param args the command line parameters
60 */
61 public static void main(String... args) throws Exception {
62 new CheckTextFiles().run();
63 }
64
65 private void run() throws Exception {
66 Files.walkFileTree(Paths.get("src"), new SimpleFileVisitor<Path>() {
67 @Override
68 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
69 check(file);
70 return FileVisitResult.CONTINUE;
71 }
72 });
73 if (hasError) {
74 throw new Exception("Errors found");
75 }
76 }
77
78 void check(Path file) throws IOException {
79 String name = file.getFileName().toString();
80 String suffix = "";
81 int lastDot = name.lastIndexOf('.');
82 if (lastDot >= 0) {
83 suffix = name.substring(lastDot + 1);
84 }
85 boolean check = false, ignore = false;
86 for (String s : SUFFIX_CHECK) {
87 if (suffix.equals(s)) {
88 check = true;
89 }
90 }
91 for (String s : SUFFIX_IGNORE) {
92 if (suffix.equals(s)) {
93 ignore = true;
94 }
95 }
96 boolean checkLicense = true;
97 for (String ig : suffixIgnoreLicense) {
98 if (suffix.equals(ig) || name.endsWith(ig)) {
99 checkLicense = false;
100 break;
101 }
102 }
103 if (ignore == check) {
104 throw new RuntimeException("Unknown suffix: " + suffix
105 + " for file: " + file.toAbsolutePath());
106 }
107 useCRLF = false;
108 for (String s : SUFFIX_CRLF) {
109 if (suffix.equals(s)) {
110 useCRLF = true;
111 break;
112 }
113 }
114 if (check) {
115 checkOrFixFile(file, AUTO_FIX, checkLicense);
116 }
117 }
118
119 /**
120 * Check a source code file. The following properties are checked:
121 * copyright, license, incorrect source switches, trailing white space,
122 * newline characters, tab characters, and characters codes (only characters
123 * below 128 are allowed).
124 *
125 * @param file the file to check
126 * @param fix automatically fix newline characters and trailing spaces
127 * @param checkLicense check the license and copyright
128 */
129 public void checkOrFixFile(Path file, boolean fix, boolean checkLicense) throws IOException {
130 byte[] data = Files.readAllBytes(file);
131 ByteArrayOutputStream out = fix ? new ByteArrayOutputStream() : null;
132 if (checkLicense) {
133 if (data.length > COPYRIGHT1.length() + LICENSE.length()) {
134 // don't check tiny files
135 String text = new String(data);
136 if (text.indexOf(COPYRIGHT1) < 0) {
137 fail(file, "copyright is missing", 0);
138 }
139 if (text.indexOf(COPYRIGHT2) < 0) {
140 fail(file, "copyright is missing", 0);
141 }
142 if (text.indexOf(LICENSE) < 0) {
143 fail(file, "license is missing", 0);
144 }
145 if (text.indexOf("// " + "##") > 0) {
146 fail(file, "unexpected space between // and ##", 0);
147 }
148 if (text.indexOf("/* " + "##") > 0) {
149 fail(file, "unexpected space between /* and ##", 0);
150 }
151 if (text.indexOf("##" + " */") > 0) {
152 fail(file, "unexpected space between ## and */", 0);
153 }
154 }
155 }
156 int line = 1;
157 int startLinePos = 0;
158 boolean lastWasWhitespace = false;
159 for (int i = 0; i < data.length; i++) {
160 char ch = (char) (data[i] & 0xff);
161 boolean isWhitespace = Character.isWhitespace(ch);
162 if (ch > 127) {
163 fail(file, "contains character " + (int) ch + " at "
164 + new String(data, i - 10, 20), line);
165 return;
166 } else if (ch < 32) {
167 if (ch == '\n') {
168 if (lastWasWhitespace && !ALLOW_TRAILING_SPACES) {
169 fail(file, "contains trailing white space", line);
170 return;
171 }
172 if (fix) {
173 if (useCRLF) {
174 out.write('\r');
175 }
176 out.write(ch);
177 }
178 lastWasWhitespace = false;
179 line++;
180 int lineLength = i - startLinePos;
181 if (file.getFileName().toString().endsWith(".java")) {
182 if (i > 0 && data[i - 1] == '\r') {
183 lineLength--;
184 }
185 if (lineLength > MAX_SOURCE_LINE_SIZE) {
186 String s = new String(data, startLinePos, lineLength).trim();
187 if (!s.startsWith("// http://") && !s.startsWith("// https://")) {
188 fail(file, "line too long: " + lineLength, line);
189 }
190 }
191 }
192 startLinePos = i;
193 } else if (ch == '\r') {
194 if (!ALLOW_CR) {
195 fail(file, "contains CR", line);
196 return;
197 }
198 if (lastWasWhitespace && !ALLOW_TRAILING_SPACES) {
199 fail(file, "contains trailing white space", line);
200 return;
201 }
202 lastWasWhitespace = false;
203 // ok
204 } else if (ch == '\t') {
205 if (fix) {
206 for (int j = 0; j < SPACES_PER_TAB; j++) {
207 out.write(' ');
208 }
209 } else {
210 if (!ALLOW_TAB) {
211 fail(file, "contains TAB", line);
212 return;
213 }
214 }
215 lastWasWhitespace = true;
216 // ok
217 } else {
218 fail(file, "contains character " + (int) ch, line);
219 return;
220 }
221 } else if (isWhitespace) {
222 lastWasWhitespace = true;
223 if (fix) {
224 boolean write = true;
225 for (int j = i + 1; j < data.length; j++) {
226 char ch2 = (char) (data[j] & 0xff);
227 if (ch2 == '\n' || ch2 == '\r') {
228 write = false;
229 lastWasWhitespace = false;
230 ch = ch2;
231 i = j - 1;
232 break;
233 } else if (!Character.isWhitespace(ch2)) {
234 break;
235 }
236 }
237 if (write) {
238 out.write(ch);
239 }
240 }
241 } else {
242 if (fix) {
243 out.write(ch);
244 }
245 lastWasWhitespace = false;
246 }
247 }
248 if (lastWasWhitespace && !ALLOW_TRAILING_SPACES) {
249 fail(file, "contains trailing white space at the very end", line);
250 return;
251 }
252 if (fix) {
253 byte[] changed = out.toByteArray();
254 if (!Arrays.equals(data, changed)) {
255 Files.write(file, changed);
256 System.out.println("CHANGED: " + file.getFileName());
257 }
258 }
259 line = 1;
260 for (int i = 0; i < data.length; i++) {
261 if (data[i] < 32) {
262 line++;
263 for (int j = i + 1; j < data.length; j++) {
264 if (data[j] != 32) {
265 int mod = (j - i - 1) & 3;
266 if (mod != 0 && (mod != 1 || data[j] != '*')) {
267 fail(file, "contains wrong number " +
268 "of heading spaces: " + (j - i - 1), line);
269 }
270 break;
271 }
272 }
273 }
274 }
275 }
276
277 private void fail(Path file, String error, int line) {
278 file = file.toAbsolutePath();
279 if (line <= 0) {
280 line = 1;
281 }
282 String name = file.toString();
283 int idx = name.lastIndexOf(File.separatorChar);
284 if (idx >= 0) {
285 name = name.replace(File.separatorChar, '.');
286 name = name + "(" + name.substring(idx + 1) + ":" + line + ")";
287 idx = name.indexOf("org.");
288 if (idx > 0) {
289 name = name.substring(idx);
290 }
291 }
292 System.out.println("FAIL at " + name + " " + error + " " + file.toAbsolutePath());
293 hasError = true;
294 if (failOnError) {
295 throw new RuntimeException("FAIL");
296 }
297 }
298
299 }