KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > exporters > RSS_2_0_Exporter


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27
28 // $Id: RSS_2_0_Exporter.java,v 1.3 2004/08/26 06:43:52 niko_schmuck Exp $
29

30 package de.nava.informa.exporters;
31
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStreamWriter JavaDoc;
36 import java.io.Writer JavaDoc;
37 import java.text.SimpleDateFormat JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 import org.jdom.Document;
42 import org.jdom.Element;
43 import org.jdom.Namespace;
44 import org.jdom.output.Format;
45 import org.jdom.output.XMLOutputter;
46
47 import de.nava.informa.core.ChannelExporterIF;
48 import de.nava.informa.core.ChannelIF;
49 import de.nava.informa.core.ItemIF;
50 import de.nava.informa.core.CategoryIF;
51
52 import de.nava.informa.utils.ParserUtils;
53
54 /**
55  * A channel exporter that can write channel objects out into the interchange
56  * syntax defined by RSS 2.0.
57  * </p>
58  *
59  * @author Sami Alireza
60  */

61
62 public class RSS_2_0_Exporter implements ChannelExporterIF {
63
64     private static final String JavaDoc RSS_VERSION = "2.0";
65
66     /**
67      * RSS 1.0 Dublin Core namespace
68      */

69     private static final String JavaDoc NS_DC = "http://purl.org/dc/elements/1.1/";
70
71     /**
72      * RSS 1.0 Syndication Module namespace
73      */

74     private static final String JavaDoc NS_SY = "http://purl.org/rss/1.0/modules/syndication/";
75
76     private static final String JavaDoc NS_ADMIN = "http://webns.net/mvcb/";
77
78     private static final String JavaDoc NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
79
80     private static SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ssZ");
81
82     private Writer JavaDoc writer;
83
84     private String JavaDoc encoding;
85
86     /**
87      * Creates a channel exporter bound to the file given in the argument. The
88      * channel will be written out in the UTF-8 encoding.
89      *
90      * @param filename - The name of the file to which the channel object is to be
91      * written.
92      */

93     public RSS_2_0_Exporter(String JavaDoc filename) throws IOException JavaDoc {
94         this(new File JavaDoc(filename), "utf-8");
95     }
96
97
98     /**
99      * Creates a channel exporter bound to the file given in the argument. The
100      * <p/>
101      * channel will be written out in the UTF-8 encoding.
102      *
103      * @param file - The file object to which the channel object is to be
104      * <p/>
105      * written.
106      */

107     public RSS_2_0_Exporter(File JavaDoc file) throws IOException JavaDoc {
108         this(file, "utf-8");
109     }
110
111     /**
112      * Creates a channel exporter bound to the file given in the arguments.
113      *
114      * @param file - The file object to which the channel object is to be
115      * <p/>
116      * written.
117      * @param encoding -
118      * <p/>
119      * The character encoding to write the channel object in.
120      */

121
122     public RSS_2_0_Exporter(File JavaDoc file, String JavaDoc encoding) throws IOException JavaDoc {
123         this.writer = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), encoding);
124         this.encoding = encoding;
125     }
126
127     /**
128      * Creates a channel exporter bound to the Writer given in the arguments.
129      *
130      * @param writer -
131      * <p/>
132      * The Writer to which the channel object is to be written.
133      * @param encoding -
134      * <p/>
135      * The character encoding the Writer writes in.
136      */

137     public RSS_2_0_Exporter(Writer JavaDoc writer, String JavaDoc encoding) {
138         this.writer = writer;
139         this.encoding = encoding;
140     }
141
142     // ------------------------------------------------------------
143
// Build a hierarchical category String from CategoryIF
144
// ------------------------------------------------------------
145

146     private Element getCategoryElements(Element elem, CategoryIF category, StringBuffer JavaDoc catString) {
147         StringBuffer JavaDoc l_catString;
148         if (catString == null || catString.length() < 1)
149             l_catString = new StringBuffer JavaDoc(category.getTitle());
150         else
151             l_catString = catString.append("/").append(category.getTitle());
152
153         Collection JavaDoc categories = category.getChildren();
154         if (categories.size() == 0) {
155             elem.addContent(new Element("category").setText(l_catString.toString()));
156         } else {
157             Iterator JavaDoc catIt = categories.iterator();
158             while (catIt.hasNext()) {
159                 CategoryIF childCat = (CategoryIF) catIt.next();
160                 elem = getCategoryElements(elem, childCat, l_catString);
161             }
162         }
163         return elem;
164     }
165
166     // ------------------------------------------------------------
167
// implementation of ChannelExporterIF interface
168
// ------------------------------------------------------------
169

170     public void write(ChannelIF channel) throws IOException JavaDoc {
171         if (writer == null) {
172             throw new RuntimeException JavaDoc("No writer has been initialized.");
173         }
174
175         // create XML outputter with indent: 2 spaces, print new lines.
176
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
177         Namespace dcNs = Namespace.getNamespace("dc", NS_DC);
178         Namespace syNs = Namespace.getNamespace("sy", NS_SY);
179         Namespace adminNs = Namespace.getNamespace("admin", NS_ADMIN);
180         Namespace rdfNs = Namespace.getNamespace("rdf", NS_RDF);
181
182         Element rootElem = new Element("rss");
183         rootElem.addNamespaceDeclaration(dcNs);
184         rootElem.addNamespaceDeclaration(syNs);
185         rootElem.addNamespaceDeclaration(adminNs);
186         rootElem.setAttribute("version", RSS_VERSION);
187
188         Element channelElem = new Element("channel");
189         // rootElem.setAttribute("version");
190
channelElem.addContent(new Element("title").setText(channel.getTitle()));
191         if (channel.getSite() != null) {
192             channelElem.addContent(new Element("link").setText(channel.getSite()
193                     .toString()));
194         }
195
196         channelElem.addContent(new Element("description").setText(channel
197                 .getDescription()));
198         if (channel.getLanguage() != null) {
199             channelElem.addContent(new Element("language", dcNs).setText(channel
200                     .getLanguage()));
201         }
202         if (channel.getCopyright() != null) {
203             channelElem.addContent(new Element("copyright", dcNs).setText(channel
204                     .getCopyright()));
205         }
206         if (channel.getPubDate() != null) {
207             channelElem.addContent(new Element("pubDate").setText(ParserUtils
208                     .formatDate(channel.getPubDate())));
209         }
210         if (channel.getCategories() != null) {
211             Collection JavaDoc categories = channel.getCategories();
212             Iterator JavaDoc catIt = categories.iterator();
213             while (catIt.hasNext()) {
214                 CategoryIF cat = (CategoryIF) catIt.next();
215                 channelElem = getCategoryElements(channelElem, cat, null);
216             }
217         }
218
219         if (channel.getUpdateBase() != null) {
220             channelElem.addContent(new Element("updateBase", syNs).setText(df
221                     .format(channel.getUpdateBase())));
222         }
223         if (channel.getUpdatePeriod() != null) {
224             // don't put out frequency without specifying period
225
channelElem.addContent(new Element("updateFrequency", syNs)
226                     .setText((new Integer JavaDoc(channel.getUpdateFrequency())).toString()));
227             channelElem.addContent(new Element("updatePeriod", syNs).setText(channel
228                     .getUpdatePeriod()));
229         }
230
231         Collection JavaDoc items = channel.getItems();
232         Iterator JavaDoc it = items.iterator();
233         while (it.hasNext()) {
234             ItemIF item = (ItemIF) it.next();
235             Element itemElem = new Element("item");
236             itemElem.addContent(new Element("title").setText(item.getTitle()));
237             if (item.getLink() != null) {
238                 itemElem.addContent(new Element("link").setText(item.getLink()
239                         .toString()));
240             }
241             if (item.getDescription() != null) {
242                 itemElem.addContent(new Element("description").setText(item
243                         .getDescription()));
244             }
245             if (item.getCategories() != null) {
246                 Collection JavaDoc categories = item.getCategories();
247                 Iterator JavaDoc catIt = categories.iterator();
248                 while (catIt.hasNext()) {
249                     CategoryIF cat = (CategoryIF) catIt.next();
250                     itemElem = getCategoryElements(itemElem, cat, null);
251                 }
252             }
253             if (item.getDate() != null) {
254                 itemElem.addContent(new Element("pubDate").setText(ParserUtils
255                         .formatDate(item.getDate())));
256             }
257             if (item.getGuid() != null) {
258                 Element guid = new Element("guid").setText(item.getGuid().getLocation());
259                 guid.setAttribute("isPermaLink", Boolean.toString(item.getGuid().isPermaLink()));
260                 itemElem.addContent(guid);
261             }
262             if (item.getComments() != null) {
263                 itemElem.addContent(new Element("comments").setText(item.getComments()
264                         .toString()));
265             }
266             channelElem.addContent(itemElem);
267         }
268
269         rootElem.addContent(channelElem);
270
271         // ---
272
Document doc = new Document(rootElem);
273         outputter.output(doc, writer);
274         // ---
275
writer.close();
276     }
277 }
278
279
Popular Tags