Debian Codegraph @

src/tools/org/h2/build/doc/SpellChecker.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.doc;
7
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.nio.file.Paths;
12 import java.util.Arrays;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.StringTokenizer;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.h2.build.BuildBase;
20 import org.h2.util.StringUtils;
21 import org.h2.util.Utils;
22
23 /**
24 * The spell checker makes sure that each word used in the source code
25 * is spelled correctly, by comparing the words with a word list.
26 * Camel case and uppercase words are checked as well.
27 * HTTP links are not checked; however they may not end with a dot.
28 */
29 public class SpellChecker {
30
31 private static final String[] SUFFIX = { "html", "java", "sql", "txt",
32 "xml", "jsp", "css", "bat", "csv", "xml", "js", "Driver", "Processor",
33 "properties", "task", "MF", "mf", "sh", "" };
34 private static final String[] IGNORE = { "dev", "nsi", "gif", "png", "odg",
35 "ico", "sxd", "zip", "bz2", "rc", "layout", "res", "dll", "jar",
36 "svg", "prefs", "prop", "iml", "class", "json" };
37 private static final String DELIMITERS =
38 " \n.();-\"=,*/{}_<>+\r:'@[]&\\!#|?$^%~`\t";
39 private static final String PREFIX_IGNORE = "abc";
40 private static final String[] IGNORE_FILES = { "mainWeb.html" };
41
42 // These are public so we can set them during development testing
43
44 /**
45 * Whether debugging is enabled.
46 */
47 public boolean debug;
48
49 /**
50 * Whether to print the dictionary.
51 */
52 public boolean printDictionary;
53
54 private final HashSet<String> dictionary =
55 new HashSet<>();
56 private final HashSet<String> used =
57 new HashSet<>();
58 private final HashMap<String, Integer> unknown =
59 new HashMap<>();
60 private boolean addToDictionary;
61 private int errorCount;
62 private int contextCount;
63
64 /**
65 * This method is called when executing this application from the command
66 * line.
67 *
68 * @param args the command line parameters
69 */
70 public static void main(String... args) throws IOException {
71 String dir = Utils.getProperty("spellcheckDir", "src");
72 new SpellChecker().run("src/tools/org/h2/build/doc/dictionary.txt", dir);
73 }
74
75 private void run(String dictionaryFileName, String dir) throws IOException {
76 process(Paths.get(dictionaryFileName));
77 process(Paths.get(dir));
78 HashSet<String> unused = new HashSet<>();
79 unused.addAll(dictionary);
80 unused.removeAll(used);
81 // System.out.println("UNUSED WORDS");
82 // System.out.println(unused);
83 if (printDictionary) {
84 System.out.println("USED WORDS");
85 String[] list = used.toArray(new String[used.size()]);
86 Arrays.sort(list);
87 StringBuilder buff = new StringBuilder();
88 for (String s : list) {
89 if (buff.length() > 0) {
90 if (buff.length() + s.length() > 80) {
91 System.out.println(buff.toString());
92 buff.setLength(0);
93 } else {
94 buff.append(' ');
95 }
96 }
97 buff.append(s);
98 }
99 System.out.println(buff.toString());
100 }
101 if (unknown.size() > 0) {
102 System.out.println();
103 System.out.println("UNKNOWN WORDS");
104 for (String s : unknown.keySet()) {
105 // int count = unknown.get(s);
106 System.out.print(s + " ");
107 errorCount++;
108 }
109 System.out.println();
110 System.out.println();
111 }
112 if (errorCount > 0) {
113 throw new IOException(errorCount + " error found");
114 }
115 }
116
117 private void process(Path file) throws IOException {
118 String name = file.getFileName().toString();
119 if (name.endsWith(".svn") || name.endsWith(".DS_Store")) {
120 return;
121 }
122 if (name.startsWith("_") && name.indexOf("_en") < 0) {
123 return;
124 }
125 if (Files.isDirectory(file)) {
126 for (Path f : Files.newDirectoryStream(file)) {
127 process(f);
128 }
129 } else {
130 String fileName = file.toAbsolutePath().toString();
131 int idx = fileName.lastIndexOf('.');
132 String suffix;
133 if (idx < 0) {
134 suffix = "";
135 } else {
136 suffix = fileName.substring(idx + 1);
137 }
138 for (String s : IGNORE) {
139 if (s.equals(suffix)) {
140 return;
141 }
142 }
143 for (String ignoreFile : IGNORE_FILES) {
144 if (fileName.endsWith(ignoreFile)) {
145 return;
146 }
147 }
148 boolean ok = false;
149 for (String s : SUFFIX) {
150 if (s.equals(suffix)) {
151 ok = true;
152 break;
153 }
154 }
155 if (!ok) {
156 throw new IOException("Unsupported suffix: " +
157 suffix + " for file: " + fileName);
158 }
159 String text = new String(BuildBase.readFile(file));
160 if (fileName.endsWith("dictionary.txt")) {
161 addToDictionary = true;
162 } else {
163 addToDictionary = false;
164 }
165 scan(fileName, text);
166 }
167 }
168
169 private void scan(String fileName, String text) {
170 HashSet<String> notFound = new HashSet<>();
171 text = removeLinks(fileName, text);
172 StringTokenizer tokenizer = new StringTokenizer(text, DELIMITERS);
173 while (tokenizer.hasMoreTokens()) {
174 String token = tokenizer.nextToken();
175 char first = token.charAt(0);
176 if (Character.isDigit(first)) {
177 continue;
178 }
179 if (!addToDictionary && debug) {
180 System.out.print(token + " ");
181 }
182 scanCombinedToken(notFound, token);
183 if (!addToDictionary && debug) {
184 System.out.println();
185 }
186 }
187 if (!notFound.isEmpty()) {
188 System.out.println("file: " + fileName);
189 for (String s : notFound) {
190 System.out.print(s + " ");
191 }
192 System.out.println();
193 }
194 }
195
196 private String removeLinks(String fileName, String text) {
197 Pattern linkPattern = Pattern.compile("http[s]?://");
198 StringBuilder buff = new StringBuilder(text.length());
199 int pos = 0, last = 0;
200 if (fileName.endsWith(".properties")) {
201 text = StringUtils.replaceAll(text, "\\:", ":");
202 }
203 while (true) {
204 Matcher m = linkPattern.matcher(text.substring(pos));
205 if (!m.find()) {
206 break;
207 }
208 int start = m.start() + pos;
209 pos = m.end() + pos;
210 buff.append(text.substring(last, start));
211 while (true) {
212 char c = text.charAt(pos);
213 if (!Character.isJavaIdentifierPart(c) &&
214 ".#/?&=%+_-:".indexOf(c) < 0) {
215 break;
216 }
217 pos++;
218 }
219 String link = text.substring(start, pos);
220 if (link.endsWith(".")) {
221 System.out.println("Link ending with dot in " + fileName + ": " + link);
222 errorCount++;
223 }
224 last = pos;
225 }
226 buff.append(text.substring(last));
227 String changed = buff.toString();
228 return changed;
229 }
230
231 private void scanCombinedToken(HashSet<String> notFound, String token) {
232 for (int i = 1; i < token.length(); i++) {
233 char charLeft = token.charAt(i - 1);
234 char charRight = token.charAt(i);
235 if (Character.isLowerCase(charLeft)
236 && Character.isUpperCase(charRight)) {
237 scanToken(notFound, token.substring(0, i));
238 token = token.substring(i);
239 i = 1;
240 } else if (Character.isUpperCase(charLeft)
241 && Character.isLowerCase(charRight)) {
242 scanToken(notFound, token.substring(0, i - 1));
243 token = token.substring(i - 1);
244 i = 1;
245 }
246 }
247 scanToken(notFound, token);
248 }
249
250 private void scanToken(HashSet<String> notFound, String token) {
251 if (token.length() < 3) {
252 return;
253 }
254 if (contextCount > 0) {
255 // System.out.println(token);
256 contextCount--;
257 }
258 while (true) {
259 char last = token.charAt(token.length() - 1);
260 if (!Character.isDigit(last)) {
261 break;
262 }
263 token = token.substring(0, token.length() - 1);
264 }
265 if (token.length() < 3) {
266 return;
267 }
268 for (int i = 0; i < token.length(); i++) {
269 if (Character.isDigit(token.charAt(i))) {
270 return;
271 }
272 }
273 token = token.toLowerCase();
274 if (!addToDictionary && debug) {
275 System.out.print(token + " ");
276 }
277 if (token.startsWith(PREFIX_IGNORE)) {
278 return;
279 }
280 // ignore repeated characters
281 if (StringUtils.replaceAll(token, token.substring(0, 1), "").length() == 0) {
282 return;
283 }
284 if (addToDictionary) {
285 dictionary.add(token);
286 } else {
287 if (!dictionary.contains(token)) {
288 notFound.add(token);
289 increment(unknown, token);
290 } else {
291 used.add(token);
292 }
293 }
294 }
295
296 private void increment(HashMap<String, Integer> map, String key) {
297 Integer value = map.get(key);
298 value = Integer.valueOf(value == null ? 0 : value + 1);
299 map.put(key, value);
300 contextCount = 10;
301 }
302
303 }