KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > exporters > RSS_0_91_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_0_91_Exporter.java,v 1.8 2004/04/20 22:26:41 niko_schmuck Exp $
28

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

54 public class RSS_0_91_Exporter implements ChannelExporterIF {
55
56   public static final String JavaDoc PUBLIC_ID =
57     "-//Netscape Communications//DTD RSS 0.91//EN";
58   public static final String JavaDoc SYSTEM_ID =
59     "http://my.netscape.com/publish/formats/rss-0.91.dtd";
60   public static final String JavaDoc RSS_VERSION = "0.91";
61   
62   private Writer JavaDoc writer;
63   private String JavaDoc encoding;
64   
65   /**
66    * Creates a channel exporter bound to the file given in the
67    * argument. The channel will be written out in the UTF-8 encoding.
68    *
69    * @param filename - The name of the file to which the channel object
70    * is to be written.
71    */

72   public RSS_0_91_Exporter(String JavaDoc filename) throws IOException JavaDoc {
73     this(new File JavaDoc(filename), "utf-8");
74   }
75
76   /**
77    * Creates a channel exporter bound to the file given in the
78    * argument. The channel will be written out in the UTF-8 encoding.
79    *
80    * @param file - The file object to which the channel object is
81    * to be written.
82    */

83   public RSS_0_91_Exporter(File JavaDoc file) throws IOException JavaDoc {
84     this(file, "utf-8");
85   }
86
87   /**
88    * Creates a channel exporter bound to the file given in the
89    * arguments.
90    *
91    * @param file - The file object to which the channel object is
92    * to be written.
93    * @param encoding - The character encoding to write the channel
94    * object in.
95    */

96   public RSS_0_91_Exporter(File JavaDoc file, String JavaDoc encoding) throws IOException JavaDoc {
97     this.writer = new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), encoding);
98     this.encoding = encoding;
99   }
100   
101   /**
102    * Creates a channel exporter bound to the Writer given in
103    * the arguments.
104    *
105    * @param writer - The Writer to which the channel object is to be
106    * written.
107    * @param encoding - The character encoding the Writer writes in.
108    */

109   public RSS_0_91_Exporter(Writer JavaDoc writer, String JavaDoc encoding) {
110     this.writer = writer;
111     this.encoding = encoding;
112   }
113
114   // ------------------------------------------------------------
115
// implementation of ChannelExporterIF interface
116
// ------------------------------------------------------------
117

118   public void write(ChannelIF channel) throws IOException JavaDoc {
119     if (writer == null) {
120       throw new RuntimeException JavaDoc("No writer has been initialized.");
121     }
122
123     // create XML outputter with indent: 2 spaces, print new lines.
124
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
125     
126     // ----
127
Element rootElem = new Element("rss");
128     rootElem.setAttribute("version", RSS_VERSION);
129     Element channelElem = new Element("channel");
130             
131     channelElem.addContent(new Element("title").setText(channel.getTitle()));
132     
133     channelElem.addContent(new Element("description")
134                            .setText(channel.getDescription()));
135     if (channel.getSite() != null) {
136       channelElem.addContent(new Element("link")
137                              .setText(channel.getSite().toString()));
138     }
139     if (channel.getLanguage() != null) {
140       channelElem.addContent(new Element("language")
141                              .setText(channel.getLanguage()));
142     }
143
144     Collection JavaDoc items = channel.getItems();
145     Iterator JavaDoc it = items.iterator();
146     while (it.hasNext()) {
147       ItemIF item = (ItemIF) it.next();
148       Element itemElem = new Element("item");
149       itemElem.addContent(new Element("title").setText(item.getTitle()));
150       if (item.getLink() != null) {
151         itemElem.addContent(new Element("link")
152                             .setText(item.getLink().toString()));
153       }
154       if (item.getDescription() != null) {
155         itemElem.addContent(new Element("description")
156                             .setText(item.getDescription()));
157       }
158       if (item.getDate() != null) {
159         itemElem.addContent(new Element("pubDate")
160                             .setText(ParserUtils.formatDate(item.getDate())));
161       }
162       channelElem.addContent(itemElem);
163     }
164
165     // TODO: add exporting image
166
// if (channel.getImage() != null) {
167
// channelElem.addContent(channel.getImage().getElement());
168
// }
169
// if (channel.getTextInput() != null) {
170
// channelElem.addContent(channel.getTextInput().getElement());
171
// }
172

173     if (channel.getCopyright() != null) {
174       channelElem.addContent(new Element("copyright")
175                              .setText(channel.getCopyright()));
176     }
177
178     // we have all together for the channel definition
179
rootElem.addContent(channelElem);
180     // ---
181
DocType docType = new DocType("rss", PUBLIC_ID, SYSTEM_ID);
182     Document doc = new Document(rootElem, docType);
183     outputter.output(doc, writer);
184   }
185   
186 }
187
Popular Tags