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
|
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 javax.xml.parsers.DocumentBuilder;
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 Connection conn = WeblogDatabaseConnection.getConnection();
final PreparedStatement selSt
= conn.prepareStatement("SELECT id , edate , to_char(cdate,'YYYY-MM-DD\"T\"HH24:MI:SS\"Z\"') , lang , title , content FROM entries ORDER BY id DESC LIMIT 15");
final ResultSet selRes = selSt.executeQuery();
final DocumentBuilder db = DamlEngine.GetDocumentBuilder.db;
final DOMImplementationLS domi
= (DOMImplementationLS)(db.getDOMImplementation());
LSSerializer ser = domi.createLSSerializer();
ser.getDomConfig().setParameter("xml-declaration", true);
LSParser par = domi.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
final LSOutput lsout = domi.createLSOutput();
lsout.setByteStream(out);
lsout.setEncoding("UTF-8");
Document rssDoc = db.newDocument();
Element rssRoot = rssDoc.createElementNS(DamlEngine.RDF_NS, "rdf:RDF");
rssDoc.appendChild(rssRoot);
// 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", "http://www.madore.org/~david/weblog/");
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 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() ) {
int id = selRes.getInt(1);
String date = selRes.getString(2);
String cdate = selRes.getString(3);
String lang = selRes.getString(4);
String title = selRes.getString(5);
String contentXml = selRes.getString(6);
Matcher matcher = Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$").matcher(date);
if ( ! matcher.matches() )
throw new IllegalArgumentException("entry "+id+" has badly formed date");
String yearStr = matcher.group(1);
String monthStr = matcher.group(2);
// String dayStr = matcher.group(3);
String numberStr = String.format("%04d",id);
String aboutUri = "http://www.madore.org/~david/weblog/"
+ "#d." + date + "." + numberStr;
String linkUri = "http://www.madore.org/~david/weblog/"
+ yearStr + "-" + monthStr + ".html"
+ "#d." + date + "." + numberStr;
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 = domi.createLSInput();
input.setStringData(contentXml);
Document temp = par.parse(input);
String contentStr = temp.getDocumentElement().getTextContent();
int bound;
try {
bound = Character.offsetByCodePoints(contentStr, 0, 160);
} 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"));
}
}
|