summaryrefslogtreecommitdiffstats
path: root/org/madore/damlengine/WeblogRSS.java
blob: 85d9fc0911ab4dd6a33874d79f28ccd96d6734ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package org.madore.damlengine;

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSParser;
import org.w3c.dom.ls.LSSerializer;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSOutput;

public final class WeblogRSS {

    private WeblogRSS() { // Forbid instantiation
	throw new AssertionError("WeblogRSS cannot be instantiated");
    }

    public static void fullProcess(OutputStream out)
	throws Exception {

	final String weblogBaseURI = "http://www.madore.org/~david/weblog/";

	final Connection conn = WeblogDatabaseConnection.getConnection();

	final PreparedStatement selSt
	    = conn.prepareStatement("SELECT id , edate , to_char(cdate,'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') , lang , do_single_page , title , content FROM entries ORDER BY id DESC LIMIT 25");

	final ResultSet selRes = selSt.executeQuery();

	final DOMImplementation domi
	    = (DamlEngine.IncantDOM.getDOMI());
	final DOMImplementationLS domils = (DOMImplementationLS)(domi);
	LSSerializer ser = domils.createLSSerializer();
	ser.getDomConfig().setParameter("xml-declaration", true);
	LSParser par = domils.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);

	final LSOutput lsout = domils.createLSOutput();
	lsout.setByteStream(out);
	lsout.setEncoding("UTF-8");

	Document rssDoc = domi.createDocument(DamlEngine.RDF_NS, "rdf:RDF", null);
	Element rssRoot = rssDoc.getDocumentElement();
	// rssDoc.appendChild(rssDoc.createTextNode("\n"));

	rssRoot.setAttributeNS(DamlEngine.XMLNS_NS, "xmlns:rdf", DamlEngine.RDF_NS);
	rssRoot.setAttributeNS(DamlEngine.XMLNS_NS, "xmlns", DamlEngine.RSS10_NS);
	rssRoot.setAttributeNS(DamlEngine.XMLNS_NS, "xmlns:dc", DamlEngine.DUBLINCORE_NS);
	rssRoot.setAttributeNS(DamlEngine.XMLNS_NS, "xmlns:sy", DamlEngine.SYNDICATION_NS);
	rssRoot.setAttributeNS(DamlEngine.XML_NS, "xml:lang", "en");
	rssRoot.appendChild(rssDoc.createTextNode("\n"));

	Element channel = rssDoc.createElementNS(DamlEngine.RSS10_NS, "channel");
	rssRoot.appendChild(channel);
	rssRoot.appendChild(rssDoc.createTextNode("\n"));
	channel.setAttributeNS(DamlEngine.RDF_NS, "rdf:about", weblogBaseURI);
	channel.appendChild(rssDoc.createTextNode("\n"));

	Element chanTitle = rssDoc.createElementNS(DamlEngine.RSS10_NS, "title");
	channel.appendChild(chanTitle);
	channel.appendChild(rssDoc.createTextNode("\n"));
	chanTitle.appendChild(rssDoc.createTextNode("David Madore's WebLog"));
	Element chanLink = rssDoc.createElementNS(DamlEngine.RSS10_NS, "link");
	channel.appendChild(chanLink);
	channel.appendChild(rssDoc.createTextNode("\n"));
	chanLink.appendChild(rssDoc.createTextNode(weblogBaseURI));
	Element chanDescription = rssDoc.createElementNS(DamlEngine.RSS10_NS, "description");
	channel.appendChild(chanDescription);
	channel.appendChild(rssDoc.createTextNode("\n"));
	chanDescription.appendChild(rssDoc.createTextNode("David Madore's WebLog"));
	Element syUpdatePeriod = rssDoc.createElementNS(DamlEngine.SYNDICATION_NS, "sy:updatePeriod");
	channel.appendChild(syUpdatePeriod);
	channel.appendChild(rssDoc.createTextNode("\n"));
	syUpdatePeriod.appendChild(rssDoc.createTextNode("hourly"));
	Element syUpdateFrequency = rssDoc.createElementNS(DamlEngine.SYNDICATION_NS, "sy:updateFrequency");
	channel.appendChild(syUpdateFrequency);
	channel.appendChild(rssDoc.createTextNode("\n"));
	syUpdateFrequency.appendChild(rssDoc.createTextNode("3"));
	Element syUpdateBase = rssDoc.createElementNS(DamlEngine.SYNDICATION_NS, "sy:updateBase");
	channel.appendChild(syUpdateBase);
	channel.appendChild(rssDoc.createTextNode("\n"));
	syUpdateBase.appendChild(rssDoc.createTextNode("2003-05-01T10:00:00Z"));
	Element chanItems = rssDoc.createElementNS(DamlEngine.RSS10_NS, "items");
	channel.appendChild(chanItems);
	channel.appendChild(rssDoc.createTextNode("\n"));
	chanItems.appendChild(rssDoc.createTextNode("\n"));
	Element itemsSeq = rssDoc.createElementNS(DamlEngine.RDF_NS, "rdf:Seq");
	chanItems.appendChild(itemsSeq);
	chanItems.appendChild(rssDoc.createTextNode("\n"));
	itemsSeq.appendChild(rssDoc.createTextNode("\n"));

	while ( selRes.next() ) {
	    final int id = selRes.getInt(1);
	    final String date = selRes.getString(2);
	    final String cdate = selRes.getString(3);
	    final String lang = selRes.getString(4);
	    final String doSinglePage = selRes.getString(5);
	    final String title = selRes.getString(6);
	    final String contentXml = selRes.getString(7);
	    final Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(date);
	    if ( ! matcher.matches() )
		throw new IllegalArgumentException("entry "+id+" has badly formed date");
	    final String yearStr = matcher.group(1);
	    final String monthStr = matcher.group(2);
	    final String dayStr = matcher.group(3);
	    final String numberStr = String.format("%04d",id);
	    final WeblogLink lk = new WeblogLink(yearStr, monthStr, dayStr,
						 numberStr, "",
						 doSinglePage);
	    lk.setTypeStandard();
	    final String aboutUri = weblogBaseURI + "#" + lk.getFragment();
	    final String linkUri = lk.getTarget(weblogBaseURI);
	    Element li = rssDoc.createElementNS(DamlEngine.RDF_NS, "rdf:li");
	    itemsSeq.appendChild(li);
	    itemsSeq.appendChild(rssDoc.createTextNode("\n"));
	    li.setAttributeNS(DamlEngine.RDF_NS, "rdf:resource",
			      aboutUri);
	    Element item = rssDoc.createElementNS(DamlEngine.RSS10_NS, "item");
	    rssRoot.appendChild(item);
	    rssRoot.appendChild(rssDoc.createTextNode("\n"));
	    item.setAttributeNS(DamlEngine.RDF_NS, "rdf:about",
				aboutUri);
	    item.setAttributeNS(DamlEngine.XML_NS, "xml:lang", lang);
	    item.appendChild(rssDoc.createTextNode("\n"));
	    Element itemTitle = rssDoc.createElementNS(DamlEngine.RSS10_NS, "title");
	    item.appendChild(itemTitle);
	    item.appendChild(rssDoc.createTextNode("\n"));
	    itemTitle.appendChild(rssDoc.createTextNode(title));
	    Element itemLink = rssDoc.createElementNS(DamlEngine.RSS10_NS, "link");
	    item.appendChild(itemLink);
	    item.appendChild(rssDoc.createTextNode("\n"));
	    itemLink.appendChild(rssDoc.createTextNode(linkUri));
	    Element itemDate = rssDoc.createElementNS(DamlEngine.DUBLINCORE_NS, "dc:date");
	    item.appendChild(itemDate);
	    item.appendChild(rssDoc.createTextNode("\n"));
	    itemDate.appendChild(rssDoc.createTextNode(cdate));
	    LSInput input = domils.createLSInput();
	    input.setStringData(contentXml);
	    Document temp = par.parse(input);
	    String contentStr = temp.getDocumentElement().getTextContent();
	    int bound;
	    try {
		bound = Character.offsetByCodePoints(contentStr, 0, 500);
	    } catch (IndexOutOfBoundsException e) {
		bound = contentStr.length();
	    }
	    String descrStr = contentStr.substring(0, bound).replace("\n"," ");
	    Element itemDescr = rssDoc.createElementNS(DamlEngine.RSS10_NS, "description");
	    item.appendChild(itemDescr);
	    item.appendChild(rssDoc.createTextNode("\n"));
	    itemDescr.appendChild(rssDoc.createTextNode(descrStr));
	}

	ser.write(rssDoc, lsout);
	out.write("\n".getBytes("UTF-8"));

    }

}