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