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.FileInputStream;
10: import java.io.FileOutputStream;
11: import java.io.IOException;
12:
13: import org.h2.samples.Newsfeed;
14: import org.h2.util.IOUtils;
15: import org.h2.util.StringUtils;
16:
17: /**
18: * Create the web site, mainly by copying the regular docs. A few items are
19: * different in the web site, for example it calls web site analytics.
20: * Also, the main entry point page is different.
21: * The newsfeeds are generated here as well.
22: */
23: public class WebSite {
24:
25: String sourceDir = "docs";
26: String targetDir = "dataWeb";
27:
28: private static final String ANALYTICS_TAG = "<!-- analytics -->";
29: private static final String ANALYTICS_SCRIPT = "<script src=\"http://www.google-analytics.com/ga.js\" type=\"text/javascript\"></script>\n"
30: + "<script type=\"text/javascript\">var pageTracker=_gat._getTracker(\"UA-2351060-1\");pageTracker._initData();pageTracker._trackPageview();</script>";
31:
32: public static void main(String[] args) throws Exception {
33: new WebSite().run();
34: }
35:
36: private void run() throws Exception {
37: deleteRecursive(new File(targetDir));
38: copy(new File(sourceDir), new File(targetDir));
39: Newsfeed.main(new String[] { "dataWeb/html" });
40: }
41:
42: private void deleteRecursive(File dir) {
43: if (dir.isDirectory()) {
44: File[] list = dir.listFiles();
45: for (int i = 0; i < list.length; i++) {
46: deleteRecursive(list[i]);
47: }
48: }
49: dir.delete();
50: }
51:
52: private void copy(File source, File target) throws IOException {
53: if (source.isDirectory()) {
54: target.mkdirs();
55: File[] list = source.listFiles();
56: for (int i = 0; i < list.length; i++) {
57: copy(list[i], new File(target, list[i].getName()));
58: }
59: } else {
60: String name = source.getName();
61: if (name.endsWith("main.html")
62: || name.endsWith("main_ja.html")
63: || name.endsWith("onePage.html")) {
64: return;
65: }
66: FileInputStream in = new FileInputStream(source);
67: byte[] bytes = IOUtils.readBytesAndClose(in, 0);
68: if (name.endsWith(".html")) {
69: String page = new String(bytes, "UTF-8");
70: page = StringUtils.replaceAll(page, ANALYTICS_TAG,
71: ANALYTICS_SCRIPT);
72: bytes = page.getBytes("UTF-8");
73: }
74: FileOutputStream out = new FileOutputStream(target);
75: out.write(bytes);
76: out.close();
77: if (name.endsWith("mainWeb.html")) {
78: target.renameTo(new File(target.getParentFile(),
79: "main.html"));
80: } else if (name.endsWith("mainWeb_ja.html")) {
81: target.renameTo(new File(target.getParentFile(),
82: "main_ja.html"));
83: }
84: }
85: }
86:
87: }
|