001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.wiki.macros;
034:
035: import com.hp.hpl.jena.mem.ModelMem;
036: import com.hp.hpl.jena.rdf.model.Model;
037: import com.hp.hpl.jena.rdf.model.ResIterator;
038: import com.hp.hpl.jena.rdf.model.Resource;
039: import com.hp.hpl.jena.rdf.model.Seq;
040: import com.hp.hpl.jena.vocabulary.RDF;
041: import com.hp.hpl.jena.vocabulary.RSS;
042:
043: import org.radeox.macro.BaseMacro;
044: import org.radeox.macro.parameter.MacroParameter;
045:
046: import java.io.IOException;
047: import java.io.Writer;
048:
049: import java.net.URL;
050:
051: import java.text.DateFormat;
052: import java.text.SimpleDateFormat;
053:
054: public class RSSMacro extends BaseMacro {
055: public String getName() {
056: return "rss";
057: }
058:
059: public void execute(Writer writer, MacroParameter params)
060: throws IllegalArgumentException, IOException {
061: try {
062: if (params.getLength() < 1) {
063: throw new IllegalArgumentException("syntax : rssUrl");
064: }
065:
066: // TODO add params
067: int max = 10;
068: boolean resume = false;
069:
070: writer.write("<div class=\"rssContent\">");
071:
072: DateFormat dateFormat = new SimpleDateFormat(
073: "EEE, d MMM yyyy HH:mm a");
074:
075: URL rssUrl = new URL(params.get(0));
076: System.out.println("RSS - url = " + rssUrl);
077:
078: // jena
079: Model model = new ModelMem();
080: model.read(params.get(0));
081:
082: ResIterator channels = model.listSubjectsWithProperty(
083: RDF.type, RSS.channel);
084:
085: while (channels.hasNext()) {
086: Resource channel = (Resource) channels.next();
087: String feedUrl = null;
088: String feedTitle = null;
089: String feedDesc = null;
090:
091: // channel infos
092: feedUrl = channel.getProperty(RSS.link).getString();
093: feedTitle = channel.getProperty(RSS.title).getString();
094: feedDesc = channel.getProperty(RSS.description)
095: .getString();
096:
097: writer.write("<h3><a href=\"" + feedUrl + "\">"
098: + feedTitle + "</a></h3>");
099: writer.write("<i>" + feedDesc + "</i><br/><br/>");
100:
101: // perform items
102: if (channel.hasProperty(RSS.items)) {
103: writer.write("<table style=\"width:98%;\">");
104:
105: Seq items = channel.getProperty(RSS.items).getSeq();
106:
107: // max
108: if (items.size() < max) {
109: max = items.size();
110: }
111:
112: for (int i = 1; i <= items.size(); i++) {
113: Resource item = (Resource) items.getResource(i);
114: String itemUrl = item.getProperty(RSS.link)
115: .getString();
116: String itemTitle = item.getProperty(RSS.title)
117: .getString();
118: String itemDesc = item.getProperty(
119: RSS.description).getString();
120: writer.write("<tr><td><a href=\"" + itemUrl
121: + "\">" + itemTitle + "</a></td>");
122:
123: if (resume) {
124: writer.write("<tr><td>"
125: + itemDesc.substring(0, 200)
126: + "...<hr/></td></tr>");
127: } else {
128: writer.write("<tr><td>" + itemDesc
129: + "<hr/></td></tr>");
130: }
131: }
132:
133: writer.write("</table>");
134: }
135: }
136:
137: writer.write("</div><br/>");
138: writer.close();
139: } catch (IOException e) {
140: throw e;
141: } catch (IllegalArgumentException e) {
142: throw e;
143: } catch (Exception e) {
144: e.printStackTrace();
145: throw new IllegalArgumentException(
146: "error in rss : invalid rss feed..."); // + e.getMessage());
147: }
148: }
149:
150: public String getDescription() {
151: return "Include an RSS feed.";
152: }
153:
154: public String[] getParamDescription() {
155: return new String[] { "1. the url of the rss feed (mandatory)" }; //, "2. the max to display (optional)", "3. the display type (resume,complete) (optional)" };
156: }
157: }
|