| 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.charset.StandardCharsets; |
| 10 |
import java.nio.file.FileVisitResult; |
| 11 |
import java.nio.file.Files; |
| 12 |
import java.nio.file.Path; |
| 13 |
import java.nio.file.Paths; |
| 14 |
import java.nio.file.SimpleFileVisitor; |
| 15 |
import java.nio.file.attribute.BasicFileAttributes; |
| 16 |
import java.util.Stack; |
| 17 |
|
| 18 |
/** |
| 19 |
* This class checks that the HTML and XML part of the source code |
| 20 |
* is well-formed XML. |
| 21 |
*/ |
| 22 |
public class XMLChecker { |
| 23 |
|
| 24 |
/** |
| 25 |
* This method is called when executing this application from the command |
| 26 |
* line. |
| 27 |
* |
| 28 |
* @param args the command line parameters |
| 29 |
*/ |
| 30 |
public static void main(String... args) throws Exception { |
| 31 |
XMLChecker.run(args); |
| 32 |
} |
| 33 |
|
| 34 |
private static void run(String... args) throws Exception { |
| 35 |
Path dir = Paths.get("."); |
| 36 |
for (int i = 0; i < args.length; i++) { |
| 37 |
if ("-dir".equals(args[i])) { |
| 38 |
dir = Paths.get(args[++i]); |
| 39 |
} |
| 40 |
} |
| 41 |
process(dir.resolve("src")); |
| 42 |
process(dir.resolve("docs")); |
| 43 |
} |
| 44 |
|
| 45 |
private static void process(Path path) throws Exception { |
| 46 |
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { |
| 47 |
@Override |
| 48 |
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 49 |
// For Javadoc 8 |
| 50 |
if (dir.getFileName().toString().equals("javadoc")) { |
| 51 |
return FileVisitResult.SKIP_SUBTREE; |
| 52 |
} |
| 53 |
return FileVisitResult.CONTINUE; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 58 |
processFile(file); |
| 59 |
return FileVisitResult.CONTINUE; |
| 60 |
} |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Process a file. |
| 66 |
* |
| 67 |
* @param file the file |
| 68 |
*/ |
| 69 |
static void processFile(Path file) throws IOException { |
| 70 |
String fileName = file.getFileName().toString(); |
| 71 |
int idx = fileName.lastIndexOf('.'); |
| 72 |
if (idx < 0) { |
| 73 |
return; |
| 74 |
} |
| 75 |
String suffix = fileName.substring(idx + 1); |
| 76 |
if (!suffix.equals("html") && !suffix.equals("xml") && !suffix.equals("jsp")) { |
| 77 |
return; |
| 78 |
} |
| 79 |
// System.out.println("Checking file:" + fileName); |
| 80 |
String s = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); |
| 81 |
Exception last = null; |
| 82 |
try { |
| 83 |
checkXML(s, !suffix.equals("xml")); |
| 84 |
} catch (Exception e) { |
| 85 |
last = e; |
| 86 |
System.out.println("ERROR in file " + fileName + " " + e.toString()); |
| 87 |
} |
| 88 |
if (last != null) { |
| 89 |
last.printStackTrace(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
private static void checkXML(String xml, boolean html) throws Exception { |
| 94 |
// String lastElement = null; |
| 95 |
// <li>: replace <li>([^\r]*[^<]*) with <li>$1</li> |
| 96 |
// use this for html file, for example if <li> is not closed |
| 97 |
String[] noClose = {"br", "hr", "input", "link", "meta", "wbr"}; |
| 98 |
XMLParser parser = new XMLParser(xml); |
| 99 |
Stack<Object[]> stack = new Stack<>(); |
| 100 |
boolean rootElement = false; |
| 101 |
loop: for (;;) { |
| 102 |
int event = parser.next(); |
| 103 |
if (event == XMLParser.END_DOCUMENT) { |
| 104 |
break; |
| 105 |
} else if (event == XMLParser.START_ELEMENT) { |
| 106 |
if (stack.isEmpty()) { |
| 107 |
if (rootElement) { |
| 108 |
throw new Exception("Second root element at " + parser.getRemaining()); |
| 109 |
} |
| 110 |
rootElement = true; |
| 111 |
} |
| 112 |
String name = parser.getName(); |
| 113 |
if (html) { |
| 114 |
for (String n : noClose) { |
| 115 |
if (name.equals(n)) { |
| 116 |
name = null; |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
if (name != null) { |
| 122 |
stack.add(new Object[] { name, parser.getPos() }); |
| 123 |
} |
| 124 |
} else if (event == XMLParser.END_ELEMENT) { |
| 125 |
String name = parser.getName(); |
| 126 |
if (html) { |
| 127 |
for (String n : noClose) { |
| 128 |
if (name.equals(n)) { |
| 129 |
continue loop; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
while (true) { |
| 134 |
Object[] pop = stack.pop(); |
| 135 |
String p = (String) pop[0]; |
| 136 |
if (p.equals(name)) { |
| 137 |
break; |
| 138 |
} |
| 139 |
String remaining = xml.substring((Integer) pop[1]); |
| 140 |
if (remaining.length() > 100) { |
| 141 |
remaining = remaining.substring(0, 100); |
| 142 |
} |
| 143 |
throw new Exception("Unclosed element " + p + " at " + remaining); |
| 144 |
} |
| 145 |
} else if (event == XMLParser.CHARACTERS) { |
| 146 |
// lastElement = parser.getText(); |
| 147 |
} else if (event == XMLParser.DTD) { |
| 148 |
// ignore |
| 149 |
} else if (event == XMLParser.COMMENT) { |
| 150 |
// ignore |
| 151 |
} else { |
| 152 |
int eventType = parser.getEventType(); |
| 153 |
throw new Exception("Unexpected event " + eventType + " at " |
| 154 |
+ parser.getRemaining()); |
| 155 |
} |
| 156 |
} |
| 157 |
if (!stack.isEmpty()) { |
| 158 |
throw new Exception("Unclosed root element"); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
} |