001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.rss;
021:
022: import java.io.IOException;
023: import java.io.StringWriter;
024: import java.text.SimpleDateFormat;
025: import java.util.*;
026:
027: import javax.servlet.ServletContext;
028:
029: import org.jdom.Element;
030: import org.jdom.output.Format;
031: import org.jdom.output.XMLOutputter;
032:
033: import com.ecyrd.jspwiki.Release;
034: import com.ecyrd.jspwiki.WikiContext;
035: import com.ecyrd.jspwiki.WikiEngine;
036: import com.ecyrd.jspwiki.WikiPage;
037: import com.ecyrd.jspwiki.attachment.Attachment;
038: import com.ecyrd.jspwiki.providers.ProviderException;
039:
040: /**
041: * Represents an RSS 2.0 feed (with enclosures).
042: *
043: * @author jalkanen
044: *
045: * @since 2.2.27
046: */
047: public class RSS20Feed extends Feed {
048:
049: public RSS20Feed(WikiContext context) {
050: super (context);
051: }
052:
053: private List getItems() {
054: ArrayList list = new ArrayList();
055: SimpleDateFormat fmt = new SimpleDateFormat(
056: "EEE, dd MMM yyyy HH:mm:ss 'GMT'");
057:
058: WikiEngine engine = m_wikiContext.getEngine();
059: ServletContext servletContext = null;
060:
061: if (m_wikiContext.getHttpRequest() != null)
062: servletContext = m_wikiContext.getHttpRequest()
063: .getSession().getServletContext();
064:
065: for (Iterator i = m_entries.iterator(); i.hasNext();) {
066: Entry e = (Entry) i.next();
067: WikiPage p = e.getPage();
068:
069: String url = e.getURL();
070:
071: Element item = new Element("item");
072:
073: item.addContent(new Element("link").setText(url));
074:
075: item.addContent(new Element("title").setText(e.getTitle()));
076:
077: item.addContent(new Element("description").setText(e
078: .getContent()));
079:
080: //
081: // Attachments for enclosures
082: //
083:
084: if (engine.getAttachmentManager().hasAttachments(p)
085: && servletContext != null) {
086: try {
087: Collection c = engine.getAttachmentManager()
088: .listAttachments(p);
089:
090: for (Iterator a = c.iterator(); a.hasNext();) {
091: Attachment att = (Attachment) a.next();
092:
093: Element attEl = new Element("enclosure");
094: attEl.setAttribute("url", engine.getURL(
095: WikiContext.ATTACH, att.getName(),
096: null, true));
097: attEl.setAttribute("length", Long.toString(att
098: .getSize()));
099: attEl.setAttribute("type", getMimeType(
100: servletContext, att.getFileName()));
101:
102: item.addContent(attEl);
103: }
104: } catch (ProviderException ex) {
105: // FIXME: log.info("Can't get attachment data",ex);
106: }
107: }
108:
109: //
110: // Modification date.
111: //
112: Calendar cal = Calendar.getInstance();
113: cal.setTime(p.getLastModified());
114: cal.add(Calendar.MILLISECOND, -(cal
115: .get(Calendar.ZONE_OFFSET) + (cal.getTimeZone()
116: .inDaylightTime(p.getLastModified()) ? cal
117: .get(Calendar.DST_OFFSET) : 0)));
118:
119: item.addContent(new Element("pubDate").setText(fmt
120: .format(cal.getTime())));
121:
122: list.add(item);
123: }
124:
125: return list;
126: }
127:
128: public String getString() {
129: WikiEngine engine = m_wikiContext.getEngine();
130: Element root = new Element("rss");
131: root.setAttribute("version", "2.0");
132:
133: Element channel = new Element("channel");
134: root.addContent(channel);
135:
136: //
137: // Mandatory parts
138: //
139: channel.addContent(new Element("title")
140: .setText(getChannelTitle()));
141: channel.addContent(new Element("link").setText(engine
142: .getBaseURL()));
143: channel.addContent(new Element("description")
144: .setText(getChannelDescription()));
145:
146: //
147: // Optional
148: //
149: channel.addContent(new Element("language")
150: .setText(getChannelLanguage()));
151: channel.addContent(new Element("generator").setText("JSPWiki "
152: + Release.VERSTR));
153:
154: String mail = engine.getVariable(m_wikiContext,
155: RSSGenerator.PROP_RSS_AUTHOREMAIL);
156: if (mail != null) {
157: String editor = engine.getVariable(m_wikiContext,
158: RSSGenerator.PROP_RSS_AUTHOR);
159:
160: if (editor != null)
161: mail = mail + " (" + editor + ")";
162:
163: channel.addContent(new Element("managingEditor")
164: .setText(mail));
165: }
166:
167: //
168: // Items
169: //
170:
171: channel.addContent(getItems());
172:
173: //
174: // aaand output
175: //
176: XMLOutputter output = new XMLOutputter();
177:
178: output.setFormat(Format.getPrettyFormat());
179:
180: try {
181: StringWriter res = new StringWriter();
182: output.output(root, res);
183:
184: return res.toString();
185: } catch (IOException e) {
186: return null;
187: }
188: }
189:
190: }
|