| 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.FileVisitResult; |
| 10 |
import java.nio.file.Files; |
| 11 |
import java.nio.file.Path; |
| 12 |
import java.nio.file.Paths; |
| 13 |
import java.nio.file.SimpleFileVisitor; |
| 14 |
import java.nio.file.attribute.BasicFileAttributes; |
| 15 |
import java.sql.Connection; |
| 16 |
import java.sql.DriverManager; |
| 17 |
import java.sql.PreparedStatement; |
| 18 |
import java.sql.ResultSet; |
| 19 |
import java.sql.ResultSetMetaData; |
| 20 |
import java.sql.Statement; |
| 21 |
import java.util.ArrayList; |
| 22 |
import java.util.Collections; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.List; |
| 25 |
|
| 26 |
import org.h2.bnf.Bnf; |
| 27 |
import org.h2.engine.Constants; |
| 28 |
import org.h2.server.web.PageParser; |
| 29 |
import org.h2.tools.Csv; |
| 30 |
import org.h2.util.StringUtils; |
| 31 |
|
| 32 |
/** |
| 33 |
* This application generates sections of the documentation |
| 34 |
* by converting the built-in help section |
| 35 |
* to cross linked html. |
| 36 |
*/ |
| 37 |
public class GenerateDoc { |
| 38 |
|
| 39 |
private static final String IN_HELP = "src/main/org/h2/res/help.csv"; |
| 40 |
private Path inDir = Paths.get("src/docsrc/html"); |
| 41 |
private Path outDir = Paths.get("docs/html"); |
| 42 |
private Connection conn; |
| 43 |
private final HashMap<String, Object> session = |
| 44 |
new HashMap<>(); |
| 45 |
private Bnf bnf; |
| 46 |
|
| 47 |
/** |
| 48 |
* This method is called when executing this application from the command |
| 49 |
* line. |
| 50 |
* |
| 51 |
* @param args the command line parameters |
| 52 |
*/ |
| 53 |
public static void main(String... args) throws Exception { |
| 54 |
new GenerateDoc().run(args); |
| 55 |
} |
| 56 |
|
| 57 |
private void run(String... args) throws Exception { |
| 58 |
for (int i = 0; i < args.length; i++) { |
| 59 |
if (args[i].equals("-in")) { |
| 60 |
inDir = Paths.get(args[++i]); |
| 61 |
} else if (args[i].equals("-out")) { |
| 62 |
outDir = Paths.get(args[++i]); |
| 63 |
} |
| 64 |
} |
| 65 |
Class.forName("org.h2.Driver"); |
| 66 |
conn = DriverManager.getConnection("jdbc:h2:mem:"); |
| 67 |
Files.createDirectories(outDir); |
| 68 |
new RailroadImages().run(outDir.resolve("images")); |
| 69 |
bnf = Bnf.getInstance(null); |
| 70 |
bnf.linkStatements(); |
| 71 |
session.put("version", Constants.VERSION); |
| 72 |
session.put("versionDate", Constants.BUILD_DATE); |
| 73 |
session.put("downloadRoot", |
| 74 |
"https://github.com/h2database/h2database/releases/download/version-" + Constants.VERSION); |
| 75 |
String help = "SELECT ROWNUM ID, * FROM CSVREAD('" + |
| 76 |
IN_HELP + "', NULL, 'lineComment=#') WHERE SECTION "; |
| 77 |
map("commandsDML", |
| 78 |
help + "= 'Commands (DML)' ORDER BY ID", true, false); |
| 79 |
map("commandsDDL", |
| 80 |
help + "= 'Commands (DDL)' ORDER BY ID", true, false); |
| 81 |
map("commandsOther", |
| 82 |
help + "= 'Commands (Other)' ORDER BY ID", true, false); |
| 83 |
map("literals", |
| 84 |
help + "= 'Literals' ORDER BY ID", true, false); |
| 85 |
map("datetimeFields", |
| 86 |
help + "= 'Datetime fields' ORDER BY ID", true, false); |
| 87 |
map("otherGrammar", |
| 88 |
help + "= 'Other Grammar' ORDER BY ID", true, false); |
| 89 |
|
| 90 |
map("functionsNumeric", |
| 91 |
help + "= 'Functions (Numeric)' ORDER BY ID", true, false); |
| 92 |
map("functionsString", |
| 93 |
help + "= 'Functions (String)' ORDER BY ID", true, false); |
| 94 |
map("functionsTimeDate", |
| 95 |
help + "= 'Functions (Time and Date)' ORDER BY ID", true, false); |
| 96 |
map("functionsSystem", |
| 97 |
help + "= 'Functions (System)' ORDER BY ID", true, false); |
| 98 |
map("functionsJson", |
| 99 |
help + "= 'Functions (JSON)' ORDER BY ID", true, false); |
| 100 |
map("functionsTable", |
| 101 |
help + "= 'Functions (Table)' ORDER BY ID", true, false); |
| 102 |
|
| 103 |
map("aggregateFunctionsGeneral", |
| 104 |
help + "= 'Aggregate Functions (General)' ORDER BY ID", true, false); |
| 105 |
map("aggregateFunctionsBinarySet", |
| 106 |
help + "= 'Aggregate Functions (Binary Set)' ORDER BY ID", true, false); |
| 107 |
map("aggregateFunctionsOrdered", |
| 108 |
help + "= 'Aggregate Functions (Ordered)' ORDER BY ID", true, false); |
| 109 |
map("aggregateFunctionsHypothetical", |
| 110 |
help + "= 'Aggregate Functions (Hypothetical Set)' ORDER BY ID", true, false); |
| 111 |
map("aggregateFunctionsInverse", |
| 112 |
help + "= 'Aggregate Functions (Inverse Distribution)' ORDER BY ID", true, false); |
| 113 |
map("aggregateFunctionsJSON", |
| 114 |
help + "= 'Aggregate Functions (JSON)' ORDER BY ID", true, false); |
| 115 |
|
| 116 |
map("windowFunctionsRowNumber", |
| 117 |
help + "= 'Window Functions (Row Number)' ORDER BY ID", true, false); |
| 118 |
map("windowFunctionsRank", |
| 119 |
help + "= 'Window Functions (Rank)' ORDER BY ID", true, false); |
| 120 |
map("windowFunctionsLeadLag", |
| 121 |
help + "= 'Window Functions (Lead or Lag)' ORDER BY ID", true, false); |
| 122 |
map("windowFunctionsNth", |
| 123 |
help + "= 'Window Functions (Nth Value)' ORDER BY ID", true, false); |
| 124 |
map("windowFunctionsOther", |
| 125 |
help + "= 'Window Functions (Other)' ORDER BY ID", true, false); |
| 126 |
|
| 127 |
map("dataTypes", |
| 128 |
help + "LIKE 'Data Types%' ORDER BY SECTION, ID", true, true); |
| 129 |
map("intervalDataTypes", |
| 130 |
help + "LIKE 'Interval Data Types%' ORDER BY SECTION, ID", true, true); |
| 131 |
HashMap<String, String> informationSchemaTables = new HashMap<>(); |
| 132 |
HashMap<String, String> informationSchemaColumns = new HashMap<>(512); |
| 133 |
Csv csv = new Csv(); |
| 134 |
csv.setLineCommentCharacter('#'); |
| 135 |
try (ResultSet rs = csv.read("src/docsrc/help/information_schema.csv", null, null)) { |
| 136 |
while (rs.next()) { |
| 137 |
String tableName = rs.getString(1); |
| 138 |
String columnName = rs.getString(2); |
| 139 |
String description = rs.getString(3); |
| 140 |
if (columnName != null) { |
| 141 |
informationSchemaColumns.put(tableName == null ? columnName : tableName + '.' + columnName, |
| 142 |
description); |
| 143 |
} else { |
| 144 |
informationSchemaTables.put(tableName, description); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
int errorCount = 0; |
| 149 |
try (Statement stat = conn.createStatement(); |
| 150 |
PreparedStatement prep = conn.prepareStatement("SELECT COLUMN_NAME, " |
| 151 |
+ "DATA_TYPE_SQL('INFORMATION_SCHEMA', TABLE_NAME, 'TABLE', DTD_IDENTIFIER) DT " |
| 152 |
+ "FROM INFORMATION_SCHEMA.COLUMNS " |
| 153 |
+ "WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA' AND TABLE_NAME = ? ORDER BY ORDINAL_POSITION")) { |
| 154 |
ResultSet rs = stat.executeQuery("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES " |
| 155 |
+ "WHERE TABLE_SCHEMA = 'INFORMATION_SCHEMA' ORDER BY TABLE_NAME"); |
| 156 |
|
| 157 |
ArrayList<HashMap<String, String>> list = new ArrayList<>(); |
| 158 |
StringBuilder builder = new StringBuilder(); |
| 159 |
while (rs.next()) { |
| 160 |
HashMap<String, String> map = new HashMap<>(8); |
| 161 |
String table = rs.getString(1); |
| 162 |
map.put("table", table); |
| 163 |
map.put("link", "information_schema_" + StringUtils.urlEncode(table.toLowerCase())); |
| 164 |
String description = informationSchemaTables.get(table); |
| 165 |
if (description == null) { |
| 166 |
System.out.println("No documentation for INFORMATION_SCHEMA." + table); |
| 167 |
errorCount++; |
| 168 |
description = ""; |
| 169 |
} |
| 170 |
map.put("description", StringUtils.xmlText(description)); |
| 171 |
prep.setString(1, table); |
| 172 |
ResultSet rs2 = prep.executeQuery(); |
| 173 |
builder.setLength(0); |
| 174 |
while (rs2.next()) { |
| 175 |
if (rs2.getRow() > 1) { |
| 176 |
builder.append('\n'); |
| 177 |
} |
| 178 |
String column = rs2.getString(1); |
| 179 |
description = informationSchemaColumns.get(table + '.' + column); |
| 180 |
if (description == null) { |
| 181 |
description = informationSchemaColumns.get(column); |
| 182 |
if (description == null) { |
| 183 |
System.out.println("No documentation for INFORMATION_SCHEMA." + table + '.' + column); |
| 184 |
errorCount++; |
| 185 |
description = ""; |
| 186 |
} |
| 187 |
} |
| 188 |
builder.append("<tr><td>").append(column).append("</td><td>").append(rs2.getString(2)) |
| 189 |
.append("</td></tr><tr><td colspan=\"2\">") |
| 190 |
.append(StringUtils.xmlText(description)).append("</td></tr>"); |
| 191 |
} |
| 192 |
map.put("columns", builder.toString()); |
| 193 |
list.add(map); |
| 194 |
} |
| 195 |
putToMap("informationSchema", list); |
| 196 |
} |
| 197 |
Files.walkFileTree(inDir, new SimpleFileVisitor<Path>() { |
| 198 |
@Override |
| 199 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 200 |
process(file); |
| 201 |
return FileVisitResult.CONTINUE; |
| 202 |
} |
| 203 |
}); |
| 204 |
conn.close(); |
| 205 |
if (errorCount > 0) { |
| 206 |
throw new IOException(errorCount + (errorCount == 1 ? " error" : " errors") + " found"); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Process a file. |
| 212 |
* |
| 213 |
* @param inFile the file |
| 214 |
*/ |
| 215 |
void process(Path inFile) throws IOException { |
| 216 |
Path outFile = outDir.resolve(inDir.relativize(inFile)); |
| 217 |
Files.createDirectories(outFile.getParent()); |
| 218 |
byte[] bytes = Files.readAllBytes(inFile); |
| 219 |
if (inFile.getFileName().toString().endsWith(".html")) { |
| 220 |
String page = new String(bytes); |
| 221 |
page = PageParser.parse(page, session); |
| 222 |
bytes = page.getBytes(); |
| 223 |
} |
| 224 |
Files.write(outFile, bytes); |
| 225 |
} |
| 226 |
|
| 227 |
private void map(String key, String sql, boolean railroads, boolean forDataTypes) |
| 228 |
throws Exception { |
| 229 |
try (Statement stat = conn.createStatement(); |
| 230 |
ResultSet rs = stat.executeQuery(sql)) { |
| 231 |
ArrayList<HashMap<String, String>> list = |
| 232 |
new ArrayList<>(); |
| 233 |
while (rs.next()) { |
| 234 |
HashMap<String, String> map = new HashMap<>(); |
| 235 |
ResultSetMetaData meta = rs.getMetaData(); |
| 236 |
for (int i = 1; i <= meta.getColumnCount(); i++) { |
| 237 |
String k = StringUtils.toLowerEnglish(meta.getColumnLabel(i)); |
| 238 |
String value = rs.getString(i); |
| 239 |
value = value.trim(); |
| 240 |
map.put(k, PageParser.escapeHtml(value)); |
| 241 |
} |
| 242 |
String topic = rs.getString("TOPIC"); |
| 243 |
// Convert "INT Type" to "INT" etc. |
| 244 |
if (forDataTypes && topic.endsWith(" Type")) { |
| 245 |
map.put("topic", topic.substring(0, topic.length() - 5)); |
| 246 |
} |
| 247 |
String syntax = rs.getString("SYNTAX").trim(); |
| 248 |
if (railroads) { |
| 249 |
BnfRailroad r = new BnfRailroad(); |
| 250 |
String railroad = r.getHtml(bnf, syntax); |
| 251 |
map.put("railroad", railroad); |
| 252 |
} |
| 253 |
BnfSyntax visitor = new BnfSyntax(); |
| 254 |
String syntaxHtml = visitor.getHtml(bnf, syntax); |
| 255 |
map.put("syntax", syntaxHtml); |
| 256 |
|
| 257 |
// remove newlines in the regular text |
| 258 |
String text = map.get("text"); |
| 259 |
if (text != null) { |
| 260 |
// text is enclosed in <p> .. </p> so this works. |
| 261 |
text = StringUtils.replaceAll(text, |
| 262 |
"<br /><br />", "</p><p>"); |
| 263 |
text = StringUtils.replaceAll(text, |
| 264 |
"<br />", " "); |
| 265 |
text = addCode(text); |
| 266 |
text = addLinks(text); |
| 267 |
map.put("text", text); |
| 268 |
} |
| 269 |
|
| 270 |
String link = topic.toLowerCase(); |
| 271 |
link = link.replace(' ', '_'); |
| 272 |
// link = StringUtils.replaceAll(link, "_", ""); |
| 273 |
link = link.replace('@', '_'); |
| 274 |
map.put("link", StringUtils.urlEncode(link)); |
| 275 |
|
| 276 |
list.add(map); |
| 277 |
} |
| 278 |
putToMap(key, list); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
private void putToMap(String key, ArrayList<HashMap<String, String>> list) { |
| 283 |
session.put(key, list); |
| 284 |
int div = 3; |
| 285 |
int part = (list.size() + div - 1) / div; |
| 286 |
for (int i = 0, start = 0; i < div; i++, start += part) { |
| 287 |
int end = Math.min(start + part, list.size()); |
| 288 |
List<HashMap<String, String>> listThird = start <= end ? list.subList(start, end) |
| 289 |
: Collections.emptyList(); |
| 290 |
session.put(key + '-' + i, listThird); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
private static String addCode(String text) { |
| 295 |
text = StringUtils.replaceAll(text, """, "\""); |
| 296 |
StringBuilder buff = new StringBuilder(text.length()); |
| 297 |
int len = text.length(); |
| 298 |
boolean code = false, codeQuoted = false; |
| 299 |
for (int i = 0; i < len; i++) { |
| 300 |
char c = text.charAt(i); |
| 301 |
if (i < len - 1) { |
| 302 |
char next = text.charAt(i+1); |
| 303 |
if (!code && !codeQuoted) { |
| 304 |
if (Character.isUpperCase(c) && Character.isUpperCase(next)) { |
| 305 |
buff.append("<code>"); |
| 306 |
code = true; |
| 307 |
} else if (c == '\"' && (i == 0 || text.charAt(i - 1) != '\\')) { |
| 308 |
buff.append("<code>"); |
| 309 |
codeQuoted = true; |
| 310 |
continue; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
if (code) { |
| 315 |
if (!Character.isLetterOrDigit(c) && "_.".indexOf(c) < 0) { |
| 316 |
buff.append("</code>"); |
| 317 |
code = false; |
| 318 |
} |
| 319 |
} else if (codeQuoted && c == '\"' && (i == 0 || text.charAt(i - 1) != '\\')) { |
| 320 |
buff.append("</code>"); |
| 321 |
codeQuoted = false; |
| 322 |
continue; |
| 323 |
} |
| 324 |
buff.append(c); |
| 325 |
} |
| 326 |
if (code) { |
| 327 |
buff.append("</code>"); |
| 328 |
} |
| 329 |
String s = buff.toString(); |
| 330 |
s = StringUtils.replaceAll(s, "</code>, <code>", ", "); |
| 331 |
s = StringUtils.replaceAll(s, ".</code>", "</code>."); |
| 332 |
s = StringUtils.replaceAll(s, ",</code>", "</code>."); |
| 333 |
s = StringUtils.replaceAll(s, " @<code>", " <code>@"); |
| 334 |
s = StringUtils.replaceAll(s, "</code> <code>", " "); |
| 335 |
s = StringUtils.replaceAll(s, "<code>SQL</code>", "SQL"); |
| 336 |
s = StringUtils.replaceAll(s, "<code>XML</code>", "XML"); |
| 337 |
s = StringUtils.replaceAll(s, "<code>URL</code>", "URL"); |
| 338 |
s = StringUtils.replaceAll(s, "<code>URLs</code>", "URLs"); |
| 339 |
s = StringUtils.replaceAll(s, "<code>HTML</code>", "HTML"); |
| 340 |
s = StringUtils.replaceAll(s, "<code>KB</code>", "KB"); |
| 341 |
s = StringUtils.replaceAll(s, "<code>MB</code>", "MB"); |
| 342 |
s = StringUtils.replaceAll(s, "<code>GB</code>", "GB"); |
| 343 |
return s; |
| 344 |
} |
| 345 |
|
| 346 |
private static String addLinks(String text) { |
| 347 |
int start = nextLink(text, 0); |
| 348 |
if (start < 0) { |
| 349 |
return text; |
| 350 |
} |
| 351 |
StringBuilder buff = new StringBuilder(text.length()); |
| 352 |
int len = text.length(); |
| 353 |
int offset = 0; |
| 354 |
do { |
| 355 |
if (start > 2 && text.regionMatches(start - 2, "](https://h2database.com/html/", 0, 30)) { |
| 356 |
int descEnd = start - 2; |
| 357 |
int descStart = text.lastIndexOf('[', descEnd - 1) + 1; |
| 358 |
int linkStart = start + 28; |
| 359 |
int linkEnd = text.indexOf(')', start + 29); |
| 360 |
buff.append(text, offset, descStart - 1) // |
| 361 |
.append("<a href=\"").append(text, linkStart, linkEnd).append("\">") // |
| 362 |
.append(text, descStart, descEnd) // |
| 363 |
.append("</a>"); |
| 364 |
offset = linkEnd + 1; |
| 365 |
} else { |
| 366 |
int end = start + 7; |
| 367 |
for (; end < len && !Character.isWhitespace(text.charAt(end)); end++) { |
| 368 |
// Nothing to do |
| 369 |
} |
| 370 |
buff.append(text, offset, start) // |
| 371 |
.append("<a href=\"").append(text, start, end).append("\">") // |
| 372 |
.append(text, start, end) // |
| 373 |
.append("</a>"); |
| 374 |
offset = end; |
| 375 |
} |
| 376 |
} while ((start = nextLink(text, offset)) >= 0); |
| 377 |
return buff.append(text, offset, len).toString(); |
| 378 |
} |
| 379 |
|
| 380 |
private static int nextLink(String text, int i) { |
| 381 |
int found = -1; |
| 382 |
found = findLink(text, i, "http://", found); |
| 383 |
found = findLink(text, i, "https://", found); |
| 384 |
return found; |
| 385 |
} |
| 386 |
|
| 387 |
private static int findLink(String text, int offset, String prefix, int found) { |
| 388 |
int idx = text.indexOf(prefix, offset); |
| 389 |
if (idx >= 0 && (found < 0 || idx < found)) { |
| 390 |
found = idx; |
| 391 |
} |
| 392 |
return found; |
| 393 |
} |
| 394 |
|
| 395 |
} |