001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.tools.doc;
007:
008: import java.io.File;
009: import java.io.FileInputStream;
010: import java.io.FileOutputStream;
011: import java.sql.Connection;
012: import java.sql.DriverManager;
013: import java.sql.ResultSet;
014: import java.sql.ResultSetMetaData;
015: import java.sql.Statement;
016: import java.util.ArrayList;
017: import java.util.HashMap;
018:
019: import org.h2.bnf.Bnf;
020: import org.h2.server.web.PageParser;
021: import org.h2.util.IOUtils;
022: import org.h2.util.JdbcUtils;
023: import org.h2.util.StringUtils;
024:
025: /**
026: * This application generates sections of the documentation
027: * by converting the built-in help section (INFORMATION_SCHEMA.HELP)
028: * to cross linked html.
029: */
030: public class GenerateDoc {
031:
032: public static void main(String[] args) throws Exception {
033: new GenerateDoc().run(args);
034: }
035:
036: String inDir = "src/docsrc/html";
037: String outDir = "docs/html";
038: Connection conn;
039: HashMap session = new HashMap();
040: Bnf bnf;
041:
042: void run(String[] args) throws Exception {
043: System.out.println(getClass().getName());
044: for (int i = 0; i < args.length; i++) {
045: if (args[i].equals("-in")) {
046: inDir = args[++i];
047: } else if (args[i].equals("-out")) {
048: outDir = args[++i];
049: }
050: }
051: Class.forName("org.h2.Driver");
052: conn = DriverManager.getConnection("jdbc:h2:mem:");
053: new File(outDir).mkdirs();
054: bnf = Bnf.getInstance(null);
055: map(
056: "commands",
057: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION LIKE 'Commands%' ORDER BY ID");
058: map(
059: "commandsDML",
060: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION='Commands (DML)' ORDER BY ID");
061: map(
062: "commandsDDL",
063: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION='Commands (DDL)' ORDER BY ID");
064: map(
065: "commandsOther",
066: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION='Commands (Other)' ORDER BY ID");
067: map(
068: "otherGrammar",
069: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION='Other Grammar' ORDER BY ID");
070: map(
071: "functionsAggregate",
072: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION = 'Functions (Aggregate)' ORDER BY ID");
073: map(
074: "functionsNumeric",
075: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION = 'Functions (Numeric)' ORDER BY ID");
076: map(
077: "functionsString",
078: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION = 'Functions (String)' ORDER BY ID");
079: map(
080: "functionsTimeDate",
081: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION = 'Functions (Time and Date)' ORDER BY ID");
082: map(
083: "functionsSystem",
084: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION = 'Functions (System)' ORDER BY ID");
085: map(
086: "functionsAll",
087: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION LIKE 'Functions%' ORDER BY SECTION, ID");
088: map(
089: "dataTypes",
090: "SELECT * FROM INFORMATION_SCHEMA.HELP WHERE SECTION LIKE 'Data Types%' ORDER BY SECTION, ID");
091: processAll("");
092: conn.close();
093: }
094:
095: void processAll(String dir) throws Exception {
096: if (dir.endsWith(".svn")) {
097: return;
098: }
099: File[] list = new File(inDir + "/" + dir).listFiles();
100: for (int i = 0; i < list.length; i++) {
101: File file = list[i];
102: if (file.isDirectory()) {
103: processAll(dir + file.getName());
104: } else {
105: process(dir, file.getName());
106: }
107: }
108: }
109:
110: void process(String dir, String fileName) throws Exception {
111: String inFile = inDir + "/" + dir + "/" + fileName;
112: String outFile = outDir + "/" + dir + "/" + fileName;
113: new File(outFile).getParentFile().mkdirs();
114: FileOutputStream out = new FileOutputStream(outFile);
115: FileInputStream in = new FileInputStream(inFile);
116: byte[] bytes = IOUtils.readBytesAndClose(in, 0);
117: if (fileName.endsWith(".html")) {
118: String page = new String(bytes);
119: page = PageParser.parse(null, page, session);
120: bytes = page.getBytes();
121: }
122: out.write(bytes);
123: out.close();
124: }
125:
126: void map(String key, String sql) throws Exception {
127: ResultSet rs = null;
128: Statement stat = null;
129: try {
130: stat = conn.createStatement();
131: rs = stat.executeQuery(sql);
132: ArrayList list = new ArrayList();
133: while (rs.next()) {
134: HashMap map = new HashMap();
135: ResultSetMetaData meta = rs.getMetaData();
136: for (int i = 0; i < meta.getColumnCount(); i++) {
137: String k = StringUtils.toLowerEnglish(meta
138: .getColumnLabel(i + 1));
139: String value = rs.getString(i + 1);
140: map.put(k, PageParser.escapeHtml(value));
141: }
142: String topic = rs.getString("TOPIC");
143: String syntax = rs.getString("SYNTAX");
144: syntax = PageParser.escapeHtml(syntax);
145: syntax = StringUtils.replaceAll(syntax, "<br />", "");
146: syntax = bnf.getSyntaxHtml(topic, syntax);
147: map.put("syntax", syntax);
148: String link = topic.toLowerCase();
149: link = StringUtils.replaceAll(link, " ", "");
150: link = StringUtils.replaceAll(link, "_", "");
151: map.put("link", StringUtils.urlEncode(link));
152: list.add(map);
153: }
154: session.put(key, list);
155: } finally {
156: JdbcUtils.closeSilently(rs);
157: JdbcUtils.closeSilently(stat);
158: }
159: }
160: }
|