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 , special_name , 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 specialName = 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, "", specialName); 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")); } }