01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.tools.doc;
07:
08: import java.io.File;
09: import java.io.FileReader;
10: import java.io.FileWriter;
11: import java.io.PrintWriter;
12:
13: import org.h2.util.StringUtils;
14:
15: /**
16: * This application merges the html documentation to one file
17: * (onePage.html), so that the PDF document can be created.
18: */
19: public class MergeDocs {
20:
21: String baseDir = "docs/html";
22:
23: public static void main(String[] args) throws Exception {
24: new MergeDocs().run(args);
25: }
26:
27: private void run(String[] args) throws Exception {
28: // the order of pages is important here
29: String[] pages = { "quickstartText.html", "installation.html",
30: "tutorial.html", "features.html", "performance.html",
31: "advanced.html", "grammar.html", "functions.html",
32: "datatypes.html", "build.html", "history.html",
33: "faq.html", "license.html" };
34: StringBuffer buff = new StringBuffer();
35: for (int i = 0; i < pages.length; i++) {
36: String fileName = pages[i];
37: String text = getContent(fileName);
38: for (int j = 0; j < pages.length; j++) {
39: text = StringUtils
40: .replaceAll(text, pages[j] + "#", "#");
41: }
42: text = removeHeaderFooter(fileName, text);
43: buff.append(text);
44: }
45: String finalText = buff.toString();
46: File output = new File(baseDir, "onePage.html");
47: PrintWriter writer = new PrintWriter(new FileWriter(output));
48: writer
49: .println("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" /><title>");
50: writer.println("H2 Documentation");
51: writer
52: .println("</title><link rel=\"stylesheet\" type=\"text/css\" href=\"stylesheetPdf.css\" /></head><body>");
53: writer.println(finalText);
54: writer.println("</body></html>");
55: writer.close();
56: }
57:
58: private String removeHeaderFooter(String fileName, String text) {
59: // String start = "<body";
60: // String end = "</body>";
61:
62: String start = "<div class=\"contentDiv\"";
63: String end = "</div></td></tr></table><!-- analytics --></body></html>";
64:
65: int idx = text.indexOf(end);
66: if (idx < 0) {
67: throw new Error("Footer not found in file " + fileName);
68: }
69: text = text.substring(0, idx);
70: idx = text.indexOf(start);
71: idx = text.indexOf('>', idx);
72: text = text.substring(idx + 1);
73: return text;
74: }
75:
76: String getContent(String fileName) throws Exception {
77: File file = new File(baseDir, fileName);
78: int length = (int) file.length();
79: char[] data = new char[length];
80: FileReader reader = new FileReader(file);
81: int off = 0;
82: while (length > 0) {
83: int len = reader.read(data, off, length);
84: off += len;
85: length -= len;
86: }
87: reader.close();
88: String s = new String(data);
89: return s;
90: }
91: }
|