001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.news;
020:
021: import java.io.IOException;
022: import java.io.Writer;
023: import java.text.SimpleDateFormat;
024: import java.util.Locale;
025: import java.util.Vector;
026:
027: import javax.portlet.PortletException;
028:
029: import org.jdom.DocType;
030: import org.jdom.Document;
031: import org.jdom.Element;
032: import org.jdom.Namespace;
033: import org.jdom.output.Format;
034: import org.jdom.output.XMLOutputter;
035:
036: /**
037: *
038: *
039: * @author Padmanabh Dabke
040: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class RSSExporter {
043: private static SimpleDateFormat reFormatter091 = new SimpleDateFormat(
044: "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
045:
046: private static SimpleDateFormat reFormatter10 = new SimpleDateFormat(
047: "yyyy-MM-dd'T'HH:mm Z");
048:
049: private static SimpleDateFormat reFormatter20 = new SimpleDateFormat(
050: "EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
051:
052: public static void exportRSS091(NewsChannel channel, Writer out)
053: throws PortletException {
054: try {
055:
056: XMLOutputter outputter = new XMLOutputter(Format
057: .getPrettyFormat());
058:
059: Element rssElem = new Element("rss");
060: rssElem.setAttribute("version",
061: NewsConstants.RSS_091_VERSION);
062: Element channelElem = new Element("channel");
063:
064: channelElem.addContent(new Element("title").setText(channel
065: .getTitle()));
066:
067: channelElem.addContent(new Element("description")
068: .setText(channel.getDescription()));
069: if (channel.getLink() != null) {
070: channelElem.addContent(new Element("link")
071: .setText(channel.getLink()));
072: }
073: if (channel.getLanguage() != null) {
074: channelElem.addContent(new Element("language")
075: .setText(channel.getLanguage()));
076: }
077:
078: if (channel.getLastBuildDate() != null) {
079: channelElem.addContent(new Element("lastBuildDate")
080: .setText(reFormatter091.format(channel
081: .getLastBuildDate())));
082: }
083:
084: if (channel.getLastUpdated() != null) {
085: channelElem.addContent(new Element("pubDate")
086: .setText(reFormatter091.format(channel
087: .getLastUpdated())));
088: }
089:
090: if (channel.getGenerator() != null) {
091: channelElem.addContent(new Element("generator")
092: .setText(channel.getGenerator()));
093: }
094: if (channel.getImage() != null) {
095: ChannelImage img = channel.getImage();
096: Element imgElem = new Element("image");
097: imgElem.addContent(new Element("title").setText(img
098: .getTitle()));
099: imgElem.addContent(new Element("url").setText(img
100: .getURL()));
101: imgElem.addContent(new Element("link").setText(img
102: .getLink()));
103:
104: channelElem.addContent(imgElem);
105: }
106:
107: Vector itemList = channel.getItems();
108: for (int i = 0; i < itemList.size(); i++) {
109: NewsItem item = (NewsItem) itemList.elementAt(i);
110: Element itemElem = new Element("item");
111: itemElem.addContent(new Element("title").setText(item
112: .getTitle()));
113: itemElem.addContent(new Element("link").setText(item
114: .getLink()));
115: itemElem.addContent(new Element("description")
116: .setText(item.getDescription()));
117:
118: channelElem.addContent(itemElem);
119: }
120:
121: if (channel.getCopyright() != null) {
122: channelElem.addContent(new Element("copyright")
123: .setText(channel.getCopyright()));
124: }
125:
126: if (channel.getTextInput() != null) {
127: TextInput ti = channel.getTextInput();
128: Element tiElem = new Element("textinput");
129: tiElem.setAttribute("about", ti.getLink());
130: tiElem.addContent(new Element("title").setText(ti
131: .getTitle()));
132: tiElem.addContent(new Element("description").setText(ti
133: .getDescription()));
134: tiElem.addContent(new Element("name").setText(ti
135: .getName()));
136: tiElem.addContent(new Element("link").setText(ti
137: .getLink()));
138:
139: channelElem.addContent(tiElem);
140: }
141: rssElem.addContent(channelElem);
142: DocType docType = new DocType("rss",
143: NewsConstants.RSS_091_PUBLIC_ID,
144: NewsConstants.RSS_091_SYSTEM_ID);
145: Document doc = new Document(rssElem, docType);
146: outputter.output(doc, out);
147:
148: } catch (IOException ex) {
149: throw new PortletException(
150: "Failed to write RSS channel file.", ex);
151: }
152: }
153:
154: public static void exportRSS10(NewsChannel channel, Writer out)
155: throws PortletException {
156: try {
157: XMLOutputter outputter = new XMLOutputter(Format
158: .getPrettyFormat());
159:
160: Namespace defaultNS = Namespace
161: .getNamespace(NewsConstants.DEFAULT_NS);
162: Namespace rdfNS = Namespace.getNamespace("rdf",
163: NewsConstants.RDF_NS);
164: Namespace dcNS = Namespace.getNamespace("dc",
165: NewsConstants.DC_NS);
166: Namespace syNS = Namespace.getNamespace("sy",
167: NewsConstants.SYNDICATION_NS);
168:
169: Element rdfElem = new Element("RDF", rdfNS);
170: rdfElem.addNamespaceDeclaration(defaultNS);
171: rdfElem.addNamespaceDeclaration(dcNS);
172: rdfElem.addNamespaceDeclaration(syNS);
173:
174: Element channelElem = new Element("channel", defaultNS);
175:
176: channelElem.addContent(new Element("title", defaultNS)
177: .setText(channel.getTitle()));
178:
179: if (channel.getLink() != null) {
180: channelElem.addContent(new Element("link", defaultNS)
181: .setText(channel.getLink()));
182: }
183: channelElem
184: .addContent(new Element("description", defaultNS)
185: .setText(channel.getDescription()));
186:
187: if (channel.getImage() != null) {
188: Element imgElem = new Element("image", defaultNS);
189: imgElem.setAttribute("resource", channel.getImage()
190: .getURL(), rdfNS);
191: channelElem.addContent(imgElem);
192: }
193:
194: Vector itemList = channel.getItems();
195: Element itemsElem = new Element("items", defaultNS);
196: Element seqElem = new Element("Seq", rdfNS);
197: for (int i = 0; i < itemList.size(); i++) {
198: NewsItem item = (NewsItem) itemList.elementAt(i);
199: Element itemElem = new Element("li", rdfNS);
200: itemElem.setAttribute("resource", item.getLink());
201: seqElem.addContent(itemElem);
202: }
203: itemsElem.addContent(seqElem);
204: channelElem.addContent(itemsElem);
205:
206: if (channel.getTextInput() != null) {
207: Element tiElem = new Element("textinput", defaultNS);
208: tiElem.setAttribute("resource", channel.getTextInput()
209: .getLink(), rdfNS);
210: channelElem.addContent(tiElem);
211: }
212:
213: rdfElem.addContent(channelElem);
214:
215: if (channel.getImage() != null) {
216: ChannelImage img = channel.getImage();
217: Element imgElem = new Element("image");
218: imgElem.setAttribute("about", img.getURL(), rdfNS);
219: imgElem.addContent(new Element("title").setText(img
220: .getTitle()));
221: imgElem.addContent(new Element("url").setText(img
222: .getURL()));
223: imgElem.addContent(new Element("link").setText(img
224: .getLink()));
225:
226: rdfElem.addContent(imgElem);
227: }
228:
229: for (int i = 0; i < itemList.size(); i++) {
230: NewsItem item = (NewsItem) itemList.elementAt(i);
231: Element itemElem = new Element("item", defaultNS);
232: itemElem.setAttribute("about", item.getLink(), rdfNS);
233: itemElem.addContent(new Element("title", defaultNS)
234: .setText(item.getTitle()));
235: itemElem.addContent(new Element("link", defaultNS)
236: .setText(item.getLink()));
237: itemElem.addContent(new Element("description",
238: defaultNS).setText(item.getDescription()));
239:
240: rdfElem.addContent(itemElem);
241: }
242:
243: if (channel.getTextInput() != null) {
244: TextInput ti = channel.getTextInput();
245: Element tiElem = new Element("textinput");
246: tiElem.setAttribute("about", ti.getLink(), rdfNS);
247: tiElem.addContent(new Element("title").setText(ti
248: .getTitle()));
249: tiElem.addContent(new Element("description").setText(ti
250: .getDescription()));
251: tiElem.addContent(new Element("name").setText(ti
252: .getName()));
253: tiElem.addContent(new Element("link").setText(ti
254: .getLink()));
255:
256: rdfElem.addContent(tiElem);
257: }
258:
259: if (channel.getLanguage() != null) {
260: channelElem.addContent(new Element("language", dcNS)
261: .setText(channel.getLanguage()));
262: }
263: if (channel.getCopyright() != null) {
264: channelElem.addContent(new Element("rights", dcNS)
265: .setText(channel.getCopyright()));
266: }
267: if (channel.getPublisher() != null) {
268: channelElem.addContent(new Element("publisher", dcNS)
269: .setText(channel.getPublisher()));
270: }
271: if (channel.getCreator() != null) {
272: channelElem.addContent(new Element("creator", dcNS)
273: .setText(channel.getCreator()));
274: }
275: if (channel.getLastUpdated() != null) {
276: channelElem.addContent(new Element("date", dcNS)
277: .setText(reFormatter10.format(channel
278: .getLastUpdated())));
279: }
280:
281: if (channel.getUpdatePeriod() != NewsChannel.UPDATE_PERIOD_UNSPECIFIED) {
282: channelElem
283: .addContent(new Element("updatePeriod", syNS)
284: .setText(Integer.toString(channel
285: .getUpdatePeriod())));
286: }
287: if (channel.getUpdateFrequency() > 0) {
288: channelElem.addContent(new Element("updateFrequency",
289: syNS).setText(Integer.toString(channel
290: .getUpdateFrequency())));
291: }
292: if (channel.getUpdateBase() != null) {
293: channelElem.addContent(new Element("updateBase", syNS)
294: .setText(reFormatter10.format(channel
295: .getUpdateBase())));
296: }
297:
298: Document doc = new Document(rdfElem);
299: outputter.output(doc, out);
300:
301: } catch (IOException ex) {
302: throw new PortletException(
303: "Failed to write RSS channel file.", ex);
304: }
305: }
306:
307: public static void exportRSS20(NewsChannel channel, Writer out)
308: throws PortletException {
309: try {
310:
311: XMLOutputter outputter = new XMLOutputter(Format
312: .getPrettyFormat());
313:
314: Element rssElem = new Element("rss");
315: rssElem.setAttribute("version",
316: NewsConstants.RSS_20_VERSION);
317: Element channelElem = new Element("channel");
318:
319: channelElem.addContent(new Element("title").setText(channel
320: .getTitle()));
321:
322: channelElem.addContent(new Element("description")
323: .setText(channel.getDescription()));
324: if (channel.getLink() != null) {
325: channelElem.addContent(new Element("link")
326: .setText(channel.getLink()));
327: }
328: if (channel.getLanguage() != null) {
329: channelElem.addContent(new Element("language")
330: .setText(channel.getLanguage()));
331: }
332: if (channel.getGenerator() != null) {
333: channelElem.addContent(new Element("generator")
334: .setText(channel.getGenerator()));
335: }
336:
337: if (channel.getLastBuildDate() != null) {
338: channelElem.addContent(new Element("lastBuildDate")
339: .setText(reFormatter20.format(channel
340: .getLastBuildDate())));
341: }
342:
343: if (channel.getLastUpdated() != null) {
344: channelElem.addContent(new Element("pubDate")
345: .setText(reFormatter20.format(channel
346: .getLastUpdated())));
347: }
348:
349: if (channel.getImage() != null) {
350: ChannelImage img = channel.getImage();
351: Element imgElem = new Element("image");
352: imgElem.addContent(new Element("title").setText(img
353: .getTitle()));
354: imgElem.addContent(new Element("url").setText(img
355: .getURL()));
356: imgElem.addContent(new Element("link").setText(img
357: .getLink()));
358:
359: channelElem.addContent(imgElem);
360: }
361:
362: Vector itemList = channel.getItems();
363: for (int i = 0; i < itemList.size(); i++) {
364: NewsItem item = (NewsItem) itemList.elementAt(i);
365: Element itemElem = new Element("item");
366: itemElem.addContent(new Element("title").setText(item
367: .getTitle()));
368: itemElem.addContent(new Element("link").setText(item
369: .getLink()));
370: itemElem.addContent(new Element("description")
371: .setText(item.getDescription()));
372: if (item.getPubDate() != null) {
373: itemElem.addContent(new Element("pubDate")
374: .setText(reFormatter20.format(item
375: .getPubDate())));
376: }
377: if (item.getGUID() != null) {
378: itemElem.addContent(new Element("guid", item
379: .getGUID()));
380: }
381:
382: channelElem.addContent(itemElem);
383: }
384:
385: if (channel.getCopyright() != null) {
386: channelElem.addContent(new Element("copyright")
387: .setText(channel.getCopyright()));
388: }
389:
390: if (channel.getTextInput() != null) {
391: TextInput ti = channel.getTextInput();
392: Element tiElem = new Element("textinput");
393: tiElem.setAttribute("about", ti.getLink());
394: tiElem.addContent(new Element("title").setText(ti
395: .getTitle()));
396: tiElem.addContent(new Element("description").setText(ti
397: .getDescription()));
398: tiElem.addContent(new Element("name").setText(ti
399: .getName()));
400: tiElem.addContent(new Element("link").setText(ti
401: .getLink()));
402:
403: channelElem.addContent(tiElem);
404: }
405: rssElem.addContent(channelElem);
406: Document doc = new Document(rssElem);
407: outputter.output(doc, out);
408:
409: } catch (IOException ex) {
410: throw new PortletException(
411: "Failed to write RSS channel file.", ex);
412: }
413: }
414:
415: }
|