| 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.File; |
| 9 |
import java.io.IOException; |
| 10 |
import java.net.HttpURLConnection; |
| 11 |
import java.net.URL; |
| 12 |
import java.nio.charset.StandardCharsets; |
| 13 |
import java.nio.file.FileVisitResult; |
| 14 |
import java.nio.file.Files; |
| 15 |
import java.nio.file.Path; |
| 16 |
import java.nio.file.Paths; |
| 17 |
import java.nio.file.SimpleFileVisitor; |
| 18 |
import java.nio.file.attribute.BasicFileAttributes; |
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.HashMap; |
| 22 |
|
| 23 |
import org.h2.tools.Server; |
| 24 |
import org.h2.util.StringUtils; |
| 25 |
|
| 26 |
/** |
| 27 |
* The link checker makes sure that each link in the documentation |
| 28 |
* points to an existing target. |
| 29 |
*/ |
| 30 |
public class LinkChecker { |
| 31 |
|
| 32 |
private static final boolean TEST_EXTERNAL_LINKS = false; |
| 33 |
private static final boolean OPEN_EXTERNAL_LINKS = false; |
| 34 |
private static final String[] IGNORE_MISSING_LINKS_TO = { |
| 35 |
"SysProperties", "ErrorCode", |
| 36 |
// TODO check these replacement link too |
| 37 |
"#build_index", |
| 38 |
"#datatypes_index", |
| 39 |
"#faq_index", |
| 40 |
"#commands_index", |
| 41 |
"#grammar_index", |
| 42 |
"#functions_index", |
| 43 |
"#functions_aggregate_index", |
| 44 |
"#functions_window_index", |
| 45 |
"#tutorial_index", |
| 46 |
"docs/javadoc/" |
| 47 |
}; |
| 48 |
|
| 49 |
private static enum TargetKind { |
| 50 |
FILE, ID |
| 51 |
} |
| 52 |
private final HashMap<String, TargetKind> targets = new HashMap<>(); |
| 53 |
/** |
| 54 |
* Map of source link (i.e. <a> tag) in the document, to the document path |
| 55 |
*/ |
| 56 |
private final HashMap<String, String> links = new HashMap<>(); |
| 57 |
|
| 58 |
/** |
| 59 |
* This method is called when executing this application from the command |
| 60 |
* line. |
| 61 |
* |
| 62 |
* @param args the command line parameters |
| 63 |
*/ |
| 64 |
public static void main(String... args) throws Exception { |
| 65 |
new LinkChecker().run(args); |
| 66 |
} |
| 67 |
|
| 68 |
private void run(String... args) throws Exception { |
| 69 |
Path dir = Paths.get("docs"); |
| 70 |
for (int i = 0; i < args.length; i++) { |
| 71 |
if ("-dir".equals(args[i])) { |
| 72 |
dir = Paths.get(args[++i]); |
| 73 |
} |
| 74 |
} |
| 75 |
process(dir); |
| 76 |
listExternalLinks(); |
| 77 |
listBadLinks(); |
| 78 |
} |
| 79 |
|
| 80 |
private void listExternalLinks() { |
| 81 |
for (String link : links.keySet()) { |
| 82 |
if (link.startsWith("http")) { |
| 83 |
if (link.indexOf("//localhost") > 0) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
if (TEST_EXTERNAL_LINKS) { |
| 87 |
try { |
| 88 |
URL url = new URL(link); |
| 89 |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 90 |
conn.setConnectTimeout(2000); |
| 91 |
conn.setRequestMethod("GET"); |
| 92 |
HttpURLConnection.setFollowRedirects(false); |
| 93 |
conn.connect(); |
| 94 |
int code = conn.getResponseCode(); |
| 95 |
String msg; |
| 96 |
switch (code) { |
| 97 |
case 200: |
| 98 |
msg = "OK"; |
| 99 |
break; |
| 100 |
case 301: |
| 101 |
msg = "Moved Permanently"; |
| 102 |
break; |
| 103 |
case 302: |
| 104 |
msg = "Found"; |
| 105 |
break; |
| 106 |
case 403: |
| 107 |
msg = "Forbidden"; |
| 108 |
break; |
| 109 |
case 404: |
| 110 |
msg = "Not Found"; |
| 111 |
break; |
| 112 |
case 500: |
| 113 |
msg = "Internal Server Error"; |
| 114 |
break; |
| 115 |
default: |
| 116 |
msg = "?"; |
| 117 |
} |
| 118 |
System.out.println(code + " " + msg + " " + link); |
| 119 |
conn.getInputStream().close(); |
| 120 |
} catch (IOException e) { |
| 121 |
System.out.println("link checker error " + e.toString() + " " + link); |
| 122 |
// ignore |
| 123 |
} |
| 124 |
} |
| 125 |
if (OPEN_EXTERNAL_LINKS) { |
| 126 |
System.out.println(link); |
| 127 |
try { |
| 128 |
Server.openBrowser(link); |
| 129 |
Thread.sleep(100); |
| 130 |
} catch (Exception e) { |
| 131 |
// ignore |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
private void listBadLinks() throws Exception { |
| 139 |
ArrayList<String> errors = new ArrayList<>(); |
| 140 |
for (String link : links.keySet()) { |
| 141 |
if (!link.startsWith("http") && !link.endsWith("h2.pdf") |
| 142 |
&& /* For Javadoc 8 */ !link.startsWith("docs/javadoc")) { |
| 143 |
if (targets.get(link) == null) { |
| 144 |
errors.add(links.get(link) + ": Link missing " + link); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
for (String link : links.keySet()) { |
| 149 |
if (!link.startsWith("http")) { |
| 150 |
targets.remove(link); |
| 151 |
} |
| 152 |
} |
| 153 |
for (String name : targets.keySet()) { |
| 154 |
if (targets.get(name) == TargetKind.ID) { |
| 155 |
boolean ignore = false; |
| 156 |
for (String to : IGNORE_MISSING_LINKS_TO) { |
| 157 |
if (name.contains(to)) { |
| 158 |
ignore = true; |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
if (!ignore) { |
| 163 |
errors.add("No link to " + name); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
Collections.sort(errors); |
| 168 |
for (String error : errors) { |
| 169 |
System.out.println(error); |
| 170 |
} |
| 171 |
if (!errors.isEmpty()) { |
| 172 |
throw new Exception("Problems where found by the Link Checker"); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
private void process(Path path) throws Exception { |
| 177 |
Files.walkFileTree(path, new SimpleFileVisitor<Path>() { |
| 178 |
@Override |
| 179 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 180 |
processFile(file); |
| 181 |
return FileVisitResult.CONTINUE; |
| 182 |
} |
| 183 |
}); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Process a file. |
| 188 |
* |
| 189 |
* @param file the file |
| 190 |
*/ |
| 191 |
void processFile(Path file) throws IOException { |
| 192 |
String path = file.toString(); |
| 193 |
targets.put(path, TargetKind.FILE); |
| 194 |
String fileName = file.getFileName().toString(); |
| 195 |
String lower = StringUtils.toLowerEnglish(fileName); |
| 196 |
if (!lower.endsWith(".html") && !lower.endsWith(".htm")) { |
| 197 |
return; |
| 198 |
} |
| 199 |
Path parent = file.getParent(); |
| 200 |
final String html = new String(Files.readAllBytes(file), StandardCharsets.UTF_8); |
| 201 |
// find all the target fragments in the document (those elements marked with id attribute) |
| 202 |
int idx = -1; |
| 203 |
while (true) { |
| 204 |
idx = html.indexOf(" id=\"", idx + 1); |
| 205 |
if (idx < 0) { |
| 206 |
break; |
| 207 |
} |
| 208 |
int start = idx + " id=\"".length(); |
| 209 |
int end = html.indexOf('"', start); |
| 210 |
if (end < 0) { |
| 211 |
error(fileName, "Expected \" after id= " + html.substring(idx, idx + 100)); |
| 212 |
} |
| 213 |
String ref = html.substring(start, end); |
| 214 |
if (!ref.startsWith("_")) { |
| 215 |
targets.put(path + "#" + ref.replaceAll("%3C|<", "<").replaceAll("%3E|>", ">"), // |
| 216 |
TargetKind.ID); |
| 217 |
} |
| 218 |
} |
| 219 |
// find all the href links in the document |
| 220 |
idx = -1; |
| 221 |
while (true) { |
| 222 |
idx = html.indexOf(" href=\"", idx + 1); |
| 223 |
if (idx < 0) { |
| 224 |
break; |
| 225 |
} |
| 226 |
int start = html.indexOf('"', idx); |
| 227 |
if (start < 0) { |
| 228 |
error(fileName, "Expected \" after href= at " + html.substring(idx, idx + 100)); |
| 229 |
} |
| 230 |
int end = html.indexOf('"', start + 1); |
| 231 |
if (end < 0) { |
| 232 |
error(fileName, "Expected \" after href= at " + html.substring(idx, idx + 100)); |
| 233 |
} |
| 234 |
String ref = html.substring(start + 1, end); |
| 235 |
if (ref.startsWith("http:") || ref.startsWith("https:")) { |
| 236 |
// ok |
| 237 |
} else if (ref.startsWith("javascript:")) { |
| 238 |
ref = null; |
| 239 |
// ok |
| 240 |
} else if (ref.length() == 0) { |
| 241 |
ref = null; |
| 242 |
// ok |
| 243 |
} else if (ref.startsWith("#")) { |
| 244 |
ref = path + ref; |
| 245 |
} else { |
| 246 |
Path p = parent; |
| 247 |
while (ref.startsWith(".")) { |
| 248 |
if (ref.startsWith("./")) { |
| 249 |
ref = ref.substring(2); |
| 250 |
} else if (ref.startsWith("../")) { |
| 251 |
ref = ref.substring(3); |
| 252 |
p = p.getParent(); |
| 253 |
} |
| 254 |
} |
| 255 |
ref = p + File.separator + ref; |
| 256 |
} |
| 257 |
if (ref != null) { |
| 258 |
links.put(ref.replace('/', File.separatorChar) // |
| 259 |
.replaceAll("%5B", "[").replaceAll("%5D", "]") // |
| 260 |
.replaceAll("%3C", "<").replaceAll("%3E", ">"), // |
| 261 |
path); |
| 262 |
} |
| 263 |
} |
| 264 |
idx = -1; |
| 265 |
while (true) { |
| 266 |
idx = html.indexOf("<a ", idx + 1); |
| 267 |
if (idx < 0) { |
| 268 |
break; |
| 269 |
} |
| 270 |
int equals = html.indexOf('=', idx); |
| 271 |
if (equals < 0) { |
| 272 |
error(fileName, "Expected = after <a at " + html.substring(idx, idx + 100)); |
| 273 |
} |
| 274 |
String type = html.substring(idx + 2, equals).trim(); |
| 275 |
int start = html.indexOf('"', idx); |
| 276 |
if (start < 0) { |
| 277 |
error(fileName, "Expected \" after <a at " + html.substring(idx, idx + 100)); |
| 278 |
} |
| 279 |
int end = html.indexOf('"', start + 1); |
| 280 |
if (end < 0) { |
| 281 |
error(fileName, "Expected \" after <a at " + html.substring(idx, idx + 100)); |
| 282 |
} |
| 283 |
String ref = html.substring(start + 1, end); |
| 284 |
if (type.equals("href")) { |
| 285 |
// already checked |
| 286 |
} else if (type.equals("id")) { |
| 287 |
// For Javadoc 8 |
| 288 |
targets.put(path + "#" + ref, TargetKind.ID); |
| 289 |
} else if (!type.equals("name")) { |
| 290 |
error(fileName, "Unsupported <a ?: " + html.substring(idx, idx + 100)); |
| 291 |
} |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
private static void error(String fileName, String string) { |
| 296 |
System.out.println("ERROR with " + fileName + ": " + string); |
| 297 |
} |
| 298 |
|
| 299 |
} |