KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > exporters > RSS_1_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 // $Id: RSS_1_0_Exporter.java,v 1.7 2004/04/20 22:26:42 niko_schmuck Exp $
28

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

55 public class RSS_1_0_Exporter implements ChannelExporterIF {
56
57   private static final String JavaDoc NS_DEFAULT =
58     "http://purl.org/rss/1.0/";
59   private static final String JavaDoc NS_RDF =
60     "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
61   /** RSS 1.0 Dublin Core namespace */
62   private static final String JavaDoc NS_DC =
63     "http://purl.org/dc/elements/1.1/";
64   /** RSS 1.0 Syndication Module namespace */
65   private static final String JavaDoc NS_SY =
66     "http://purl.org/rss/1.0/modules/syndication/";
67
68   private static SimpleDateFormat JavaDoc df =
69     new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ssZ");
70
71   
72   private Writer JavaDoc writer;
73   private String JavaDoc encoding;
74   
75   /**
76    * Creates a channel exporter bound to the file given in the
77    * argument. The channel will be written out in the UTF-8 encoding.
78    *
79    * @param filename - The name of the file to which the channel object
80    * is to be written.
81    */

82   public RSS_1_0_Exporter(String JavaDoc filename) throws IOException JavaDoc {
83     this(new File JavaDoc(filename), "utf-8");
84   }
85
86   /**
87    * Creates a channel exporter bound to the file given in the
88    * argument. The channel will be written out in the UTF-8 encoding.
89    *
90    * @param file - The file object to which the channel object is
91    * to be written.
92    */

93   public RSS_1_0_Exporter(File JavaDoc file) throws IOException JavaDoc {
94     this(file, "utf-8");
95   }
96
97   /**
98    * Creates a channel exporter bound to the file given in the
99    * arguments.
100    *
101    * @param file - The file object to which the channel object is
102    * to be written.
103    * @param encoding - The character encoding to write the channel
104    * object in.
105    */

106   public RSS_1_0_Exporter(File JavaDoc file, String JavaDoc encoding) throws IOException JavaDoc {
107     this.writer = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), encoding);
108     this.encoding = encoding;
109   }
110   
111   /**
112    * Creates a channel exporter bound to the Writer given in the
113    * arguments.
114    *
115    * @param writer - The Writer to which the channel object is to be
116    * written.
117    * @param encoding - The character encoding the Writer writes in.
118    */

119   public RSS_1_0_Exporter(Writer JavaDoc writer, String JavaDoc encoding) {
120     this.writer = writer;
121     this.encoding = encoding;
122   }
123
124   // ------------------------------------------------------------
125
// implementation of ChannelExporterIF interface
126
// ------------------------------------------------------------
127

128   public void write(ChannelIF channel) throws IOException JavaDoc {
129     if (writer == null) {
130       throw new RuntimeException JavaDoc("No writer has been initialized.");
131     }
132
133     // create XML outputter with indent: 2 spaces, print new lines.
134
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
135     Namespace defNs = Namespace.getNamespace(NS_DEFAULT);
136     Namespace rdfNs = Namespace.getNamespace("rdf", NS_RDF);
137     Namespace dcNs = Namespace.getNamespace("dc", NS_DC);
138     Namespace syNs = Namespace.getNamespace("sy", NS_SY);
139     
140     // ----
141
Element rootElem = new Element("RDF", rdfNs);
142     rootElem.addNamespaceDeclaration(defNs);
143     rootElem.addNamespaceDeclaration(dcNs);
144     rootElem.addNamespaceDeclaration(syNs);
145     // rootElem.setAttribute("version");
146
Element channelElem = new Element("channel", defNs);
147     if (channel.getLocation() != null) {
148       channelElem.setAttribute("about",
149                                channel.getLocation().toString(), rdfNs);
150     }
151     channelElem.addContent(new Element("title", defNs)
152                            .setText(channel.getTitle()));
153     if (channel.getSite() != null) {
154       channelElem.addContent(new Element("link", defNs)
155                              .setText(channel.getSite().toString()));
156       channelElem.addContent(new Element("source", dcNs)
157                              .setAttribute("resource",
158                                            channel.getSite().toString()));
159     }
160
161     channelElem.addContent(new Element("description", defNs)
162                            .setText(channel.getDescription()));
163     if (channel.getLanguage() != null) {
164       channelElem.addContent(new Element("language", dcNs)
165                              .setText(channel.getLanguage()));
166     }
167     if (channel.getCopyright() != null) {
168       channelElem.addContent(new Element("copyright", dcNs)
169                              .setText(channel.getCopyright()));
170     }
171     if (channel.getUpdateBase() != null) {
172       channelElem.addContent(new Element("updateBase", syNs)
173                              .setText(df.format(channel.getUpdateBase())));
174     }
175     if (channel.getUpdatePeriod() != null) {
176       // don't put out frequency without specifying period
177
channelElem.addContent(new Element("updateFrequency", syNs)
178                              .setText((new Integer JavaDoc(channel.getUpdateFrequency())).toString()));
179       channelElem.addContent(new Element("updatePeriod", syNs)
180                              .setText(channel.getUpdatePeriod()));
181     }
182
183     Element itemsElem = new Element("items", defNs);
184     Element seqElem = new Element("Seq", rdfNs);
185     Collection JavaDoc items = channel.getItems();
186     Iterator JavaDoc it = items.iterator();
187     while (it.hasNext()) {
188       ItemIF item = (ItemIF) it.next();
189       Element itemElem = new Element("li", rdfNs);
190       if (item.getLink() != null) {
191         itemElem.setAttribute("resource", item.getLink().toString());
192       }
193       seqElem.addContent(itemElem);
194     }
195     itemsElem.addContent(seqElem);
196     channelElem.addContent(itemsElem);
197     rootElem.addContent(channelElem);
198
199     // item-by-item en detail
200
items = channel.getItems();
201     it = items.iterator();
202     while (it.hasNext()) {
203       ItemIF item = (ItemIF) it.next();
204       Element itemElem = new Element("item", defNs);
205       if (item.getLink() != null) {
206         itemElem.setAttribute("about",
207                               item.getLink().toString(), rdfNs);
208       }
209       itemElem.addContent(new Element("title", defNs).setText(item.getTitle()));
210       if (item.getLink() != null) {
211         itemElem.addContent(new Element("link", defNs)
212                             .setText(item.getLink().toString()));
213       }
214       if (item.getDescription() != null) {
215         itemElem.addContent(new Element("description", dcNs)
216                             .setText(item.getDescription()));
217       }
218       if (item.getDate() != null) {
219         itemElem.addContent(new Element("date", dcNs)
220                             .setText(ParserUtils.formatDate(item.getDate())));
221       }
222                           
223       rootElem.addContent(itemElem);
224     }
225
226     // ---
227
Document doc = new Document(rootElem);
228     outputter.output(doc, writer);
229     // ---
230
writer.close();
231   }
232
233 }
234
Popular Tags