作者:Brian McBride
發表時間:2000年9月2日
原文鏈接:
http://www.hpl.hp.co.uk/people/bwm/rdf/jena/rssinjena.htm譯者:
dlee翻譯時間:2001年5月26日
? ?
RSS 1.0 是最近宣布的一個格式,順從于 W3C 的 RDF (資源定義框架),用來分發(distributing) 站點摘要 (site summary) 和企業聯合 (syndication) 元數據。一個站點摘要文檔的例子可以在規范中找到。Jena 是一套實驗性的用來處理 RDF 的 Java API。這篇筆記描述了一個應用程序使用 Jena 來將一個站點摘要文檔翻譯成 HTML。整個程序的源代碼作為 RenderToHTML 可以在 Jena 發布的例子包里得到。
? ? 這篇文章和例子代碼基于 RSS 規范候選發布版本1 (Release Candidate 1 version)。
? ? 這個應用程序以創建一個 RDF 模型開始,實際上在內存中是一個 RDF statement 的集合。然后解析站點摘要文檔,使用一個 RDF 解析器,并加載 statement 到新創建的模型中。
? ? ? Model model = new ModelMem();
? ? ? model.read("
http://www.xml.com/xml/news.rss");
? ? 在寫出一個樣板 HTML 頭后,程序列出和處理在輸入流中的每個 channel。在 RDF 術語中,channel 是具有一個 rdf:type 屬性的 rss:channel 的資源。我們使用 Jena API 來列出具有一個有這個值的 rdf:type 屬性的所有的資源。在下面的代碼中,假設輸出是一個接收 HTML 輸出的 PrintWriter。
? ? ? ResIterator channels =? model.listSubjectsWithProperty(RDF.type, RSS.channel);
? ? ? while (channels.hasNext()) {
? ? ? ? ? renderChannel(channels.next(), out);
? ? ? }
? ? 為了呈現 (render) 一個 channel,程序首先寫出它的 title,description 和相關的 image 和 textinput 字段 (如果有的話)。getProperty 方法用來得到 channel 的 title,link 和 description 屬性,隨后這些可以被呈現為 HTML。
? ? ? void renderChannel(Resource channel, PrintStream out)
? ? ? ? ? ? ?throws RDFException {
? ? ? ? ? String url = null;
? ? ? ? ? String title = null;
? ? ? ? ? String desc = null;
? ? ? ? ? url = channel.getProperty(RSS.link).getString();
? ? ? ? ? title = channel.getProperty(RSS.title).getString();
? ? ? ? ? desc = channel.getProperty(RSS.description).getString();
? ? 一個 channel 可以有一個相關聯的 image 和 textinput,測試是否存在這些屬性和是否需要調用合適的方法來呈現它們是很簡單的。
? ? ? if (channel.hasProperty(RSS.image)) {
????? ? ? renderImage(channel.getProperty(RSS.image) .getResource(), out);
????? }
????? if (channel.hasProperty(RSS.textinput)) {
????? ? ? renderImage(channel.getProperty(RSS.textinput) .getResource(), out);
????? }
? ? 為了處理一個 image,同樣的調用被用來確定 image 的屬性。
????? String url = null;
????? String title = null;
????? // does the image have a link?
????? if (image.hasProperty(RSS.link)) {
????? ??? url = image.getProperty(RSS.link).getString();
????? }
????? // does the image have a title?
????? if (image.hasProperty(RSS.title)) {
????????? title = image.getProperty(RSS.title).getString();
????? } image.getProperty(SSF.title).getString();
? ? 然后這個 image 可以被呈現成為 output stream。textinput 以 相似的方法處理。
? ? channel 有一個 items 屬性,它的值為一個 RDF sequence。因此使用 getProperty 方法得到這個 sequence 就很簡單的了。然后迭代每一個元素以呈現它。
????? if (channel.hasProperty(RSS.items)) {
????? ??? Seq items = channel.getProperty(RSS.items).getSeq();
????? ??? for (int i=1; i<= items.size(); i++) {
????? ??? ? ? renderItem(items.getResource(i), out);
????? ??? }
????? }
? ? 以相同的模式呈現每一個 items。首先得到需要呈現的 items 的屬性,然后寫出 HTML。
????? void renderItem(Resource item, PrintWriter out) {
????? ??? String url = item.getProperty(RSS.link).getString();
????? ??? String title = item.getProperty(RSS.title).getString();
????? ??? String desc = item.getProperty(RSS.description) .getString();
????? ??? ...
????? }
? ? 使用 Jena 來處理 RSS 1.0 流是簡單和直接的。
? ? Brian McBride
? ? HP實驗室,2000年9月2日
? ? 修改于2000年12月2日