1 /*
2  * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 package javadoc.tester;
25 
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.nio.file.FileVisitResult;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.SimpleFileVisitor;
33 import java.nio.file.attribute.BasicFileAttributes;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.function.Function;
38 
39 public abstract class HtmlChecker extends HtmlParser {
40     static final Path currDir = Paths.get(".").toAbsolutePath().normalize();
41 
42     protected Path currFile;
43     protected int files;
44     protected int errors;
45 
HtmlChecker(PrintStream out, Function<Path,String> fileReader)46     protected HtmlChecker(PrintStream out, Function<Path,String> fileReader) {
47         super(out, fileReader);
48     }
49 
checkDirectory(Path dir)50     public void checkDirectory(Path dir) throws IOException {
51         checkFiles(List.of(dir), false, Collections.emptySet());
52     }
53 
checkFiles(List<Path> files, boolean skipSubdirs, Set<Path> excludeFiles)54     public void checkFiles(List<Path> files, boolean skipSubdirs, Set<Path> excludeFiles) throws IOException {
55         for (Path file : files) {
56             Files.walkFileTree(file, new SimpleFileVisitor<Path>() {
57                 int depth = 0;
58 
59                 @Override
60                 public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
61                     if ((skipSubdirs && depth > 0) || excludeFiles.contains(dir)) {
62                         return FileVisitResult.SKIP_SUBTREE;
63                     }
64                     depth++;
65                     return FileVisitResult.CONTINUE;
66                 }
67 
68                 @Override
69                 public FileVisitResult visitFile(Path p, BasicFileAttributes attrs) {
70                     if (excludeFiles.contains(p)) {
71                         return FileVisitResult.CONTINUE;
72                     }
73 
74                     if (Files.isRegularFile(p) && p.getFileName().toString().endsWith(".html")) {
75                         checkFile(p);
76                     }
77                     return FileVisitResult.CONTINUE;
78                 }
79 
80                 @Override
81                 public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
82                     depth--;
83                     return super.postVisitDirectory(dir, e);
84                 }
85             });
86         }
87     }
88 
checkFile(Path file)89     protected void checkFile(Path file) {
90         try {
91             currFile = file.toAbsolutePath().normalize();
92             read(file);
93             files++;
94         } catch (IOException e) {
95             error(file, 0, e);
96         }
97     }
98 
report()99     protected abstract void report();
100 
getErrorCount()101     protected int getErrorCount() {
102         return errors;
103     }
104 
105     @Override
error(Path file, int lineNumber, String message)106     protected void error(Path file, int lineNumber, String message) {
107         super.error(relativePath(file), lineNumber, message);
108         errors++;
109     }
110 
111     @Override
error(Path file, int lineNumber, Throwable t)112     protected void error(Path file, int lineNumber, Throwable t) {
113         super.error(relativePath(file), lineNumber, t);
114         errors++;
115     }
116 
relativePath(Path path)117     protected Path relativePath(Path path) {
118         return path.startsWith(currDir) ? currDir.relativize(path) : path;
119     }
120 }
121