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.samples;
07:
08: import java.io.FileOutputStream;
09: import java.io.InputStream;
10: import java.io.InputStreamReader;
11: import java.io.OutputStreamWriter;
12: import java.io.Writer;
13: import java.sql.Connection;
14: import java.sql.DriverManager;
15: import java.sql.ResultSet;
16:
17: import org.h2.tools.RunScript;
18: import org.h2.util.StringUtils;
19:
20: /**
21: * The newsfeed application uses XML functions to create an RSS and Atom feed
22: * from a simple SQL script. A textual representation of the data is created as
23: * well.
24: */
25: public class Newsfeed {
26:
27: public static void main(String[] args) throws Exception {
28: String targetDir = args.length == 0 ? "." : args[0];
29: Class.forName("org.h2.Driver");
30: Connection conn = DriverManager.getConnection("jdbc:h2:mem:",
31: "sa", "");
32: InputStream in = Newsfeed.class
33: .getResourceAsStream("newsfeed.sql");
34: ResultSet rs = RunScript.execute(conn, new InputStreamReader(
35: in, "ISO-8859-1"));
36: while (rs.next()) {
37: String file = rs.getString("FILE");
38: String content = rs.getString("CONTENT");
39: if (file.endsWith(".txt")) {
40: content = convertHtml2Text(content);
41: }
42: FileOutputStream out = new FileOutputStream(targetDir + "/"
43: + file);
44: Writer writer = new OutputStreamWriter(out, "UTF-8");
45: writer.write(content);
46: writer.close();
47: out.close();
48: }
49: conn.close();
50: }
51:
52: /**
53: * Convert HTML text to plain text.
54: *
55: * @param html the html text
56: * @return the plain text
57: */
58: private static String convertHtml2Text(String html) {
59: String s = html;
60: s = StringUtils.replaceAll(s, "<b>", "");
61: s = StringUtils.replaceAll(s, "</b>", "");
62: s = StringUtils.replaceAll(s, "<ul>", "");
63: s = StringUtils.replaceAll(s, "</ul>", "");
64: s = StringUtils.replaceAll(s, "<li>", "- ");
65: s = StringUtils.replaceAll(s, "</li>", "");
66: s = StringUtils.replaceAll(s, "<a href=\"", "( ");
67: s = StringUtils.replaceAll(s, "\">", " ) ");
68: s = StringUtils.replaceAll(s, "</a>", "");
69: s = StringUtils.replaceAll(s, "<br />", "");
70: s = StringUtils.replaceAll(s, "<br/>", "");
71: s = StringUtils.replaceAll(s, "<br>", "");
72: if (s.indexOf('<') >= 0 || s.indexOf('>') >= 0) {
73: throw new Error("Unsupported HTML Tag: < or > in " + s);
74: }
75: return s;
76: }
77: }
|