| 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.jcr; |
| 7 |
|
| 8 |
import java.io.InputStreamReader; |
| 9 |
import java.io.Reader; |
| 10 |
import java.nio.file.Files; |
| 11 |
import java.nio.file.Path; |
| 12 |
import java.nio.file.Paths; |
| 13 |
import java.sql.ResultSet; |
| 14 |
import java.sql.ResultSetMetaData; |
| 15 |
import java.util.ArrayList; |
| 16 |
import java.util.HashMap; |
| 17 |
import java.util.List; |
| 18 |
import org.h2.bnf.Bnf; |
| 19 |
import org.h2.build.BuildBase; |
| 20 |
import org.h2.build.doc.BnfRailroad; |
| 21 |
import org.h2.build.doc.BnfSyntax; |
| 22 |
import org.h2.build.doc.RailroadImages; |
| 23 |
import org.h2.server.web.PageParser; |
| 24 |
import org.h2.tools.Csv; |
| 25 |
import org.h2.util.StringUtils; |
| 26 |
|
| 27 |
/** |
| 28 |
* JCR 2.0 / SQL-2 railroad generator. |
| 29 |
*/ |
| 30 |
public class Railroads { |
| 31 |
|
| 32 |
private Bnf bnf; |
| 33 |
private final HashMap<String, Object> session = new HashMap<>(); |
| 34 |
|
| 35 |
/** |
| 36 |
* This method is called when executing this application from the command |
| 37 |
* line. |
| 38 |
* |
| 39 |
* @param args the command line parameters |
| 40 |
* @throws Exception on failure |
| 41 |
*/ |
| 42 |
public static void main(String... args) throws Exception { |
| 43 |
new Railroads().process(); |
| 44 |
} |
| 45 |
|
| 46 |
private void process() throws Exception { |
| 47 |
RailroadImages.main(); |
| 48 |
bnf = Bnf.getInstance(getReader()); |
| 49 |
Csv csv = new Csv(); |
| 50 |
csv.setLineCommentCharacter('#'); |
| 51 |
ResultSet rs = csv.read(getReader(), null); |
| 52 |
map("grammar", rs, true); |
| 53 |
processHtml("jcr-sql2.html"); |
| 54 |
} |
| 55 |
|
| 56 |
private void processHtml(String fileName) throws Exception { |
| 57 |
String source = "src/tools/org/h2/jcr/"; |
| 58 |
String target = "docs/html/"; |
| 59 |
byte[] s = BuildBase.readFile(Paths.get(source + "stylesheet.css")); |
| 60 |
BuildBase.writeFile(Paths.get(target + "stylesheet.css"), s); |
| 61 |
Path inFile = Paths.get(source + fileName); |
| 62 |
Path outFile = Paths.get(target + fileName); |
| 63 |
Files.createDirectories(outFile.getParent()); |
| 64 |
byte[] bytes = Files.readAllBytes(inFile) ; |
| 65 |
if (fileName.endsWith(".html")) { |
| 66 |
String page = new String(bytes); |
| 67 |
page = PageParser.parse(page, session); |
| 68 |
bytes = page.getBytes(); |
| 69 |
} |
| 70 |
Files.write(outFile, bytes); |
| 71 |
} |
| 72 |
|
| 73 |
private static Reader getReader() { |
| 74 |
return new InputStreamReader(Railroads.class.getResourceAsStream("help.csv")); |
| 75 |
} |
| 76 |
|
| 77 |
private void map(String key, ResultSet rs, boolean railroads) throws Exception { |
| 78 |
ArrayList<HashMap<String, String>> list; |
| 79 |
list = new ArrayList<>(); |
| 80 |
while (rs.next()) { |
| 81 |
HashMap<String, String> map = new HashMap<>(); |
| 82 |
ResultSetMetaData meta = rs.getMetaData(); |
| 83 |
for (int i = 0; i < meta.getColumnCount(); i++) { |
| 84 |
String k = StringUtils.toLowerEnglish(meta.getColumnLabel(i + 1)); |
| 85 |
String value = rs.getString(i + 1); |
| 86 |
value = value.trim(); |
| 87 |
map.put(k, PageParser.escapeHtml(value)); |
| 88 |
} |
| 89 |
String topic = rs.getString("TOPIC"); |
| 90 |
String syntax = rs.getString("SYNTAX").trim(); |
| 91 |
if (railroads) { |
| 92 |
BnfRailroad r = new BnfRailroad(); |
| 93 |
String railroad = r.getHtml(bnf, syntax); |
| 94 |
map.put("railroad", railroad); |
| 95 |
} |
| 96 |
BnfSyntax visitor = new BnfSyntax(); |
| 97 |
String syntaxHtml = visitor.getHtml(bnf, syntax); |
| 98 |
map.put("syntax", syntaxHtml); |
| 99 |
// remove newlines in the regular text |
| 100 |
String text = map.get("text"); |
| 101 |
if (text != null) { |
| 102 |
// text is enclosed in <p> .. </p> so this works. |
| 103 |
text = StringUtils.replaceAll(text, "<br /><br />", "</p><p>"); |
| 104 |
text = StringUtils.replaceAll(text, "<br />", " "); |
| 105 |
map.put("text", text); |
| 106 |
} |
| 107 |
|
| 108 |
String link = topic.toLowerCase(); |
| 109 |
link = link.replace(' ', '_'); |
| 110 |
// link = StringUtils.replaceAll(link, "_", ""); |
| 111 |
link = link.replace('@', '_'); |
| 112 |
map.put("link", StringUtils.urlEncode(link)); |
| 113 |
list.add(map); |
| 114 |
} |
| 115 |
session.put(key, list); |
| 116 |
int div = 3; |
| 117 |
int part = (list.size() + div - 1) / div; |
| 118 |
for (int i = 0, start = 0; i < div; i++, start += part) { |
| 119 |
List<HashMap<String, String>> listThird = list.subList(start, |
| 120 |
Math.min(start + part, list.size())); |
| 121 |
session.put(key + "-" + i, listThird); |
| 122 |
} |
| 123 |
rs.close(); |
| 124 |
} |
| 125 |
|
| 126 |
} |