1 26 27 29 package de.nava.informa.exporters; 30 31 import java.io.File ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.OutputStreamWriter ; 35 import java.io.Writer ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 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 54 public class RSS_0_91_Exporter implements ChannelExporterIF { 55 56 public static final String PUBLIC_ID = 57 "-//Netscape Communications//DTD RSS 0.91//EN"; 58 public static final String SYSTEM_ID = 59 "http://my.netscape.com/publish/formats/rss-0.91.dtd"; 60 public static final String RSS_VERSION = "0.91"; 61 62 private Writer writer; 63 private String encoding; 64 65 72 public RSS_0_91_Exporter(String filename) throws IOException { 73 this(new File (filename), "utf-8"); 74 } 75 76 83 public RSS_0_91_Exporter(File file) throws IOException { 84 this(file, "utf-8"); 85 } 86 87 96 public RSS_0_91_Exporter(File file, String encoding) throws IOException { 97 this.writer = new OutputStreamWriter (new FileOutputStream (file), encoding); 98 this.encoding = encoding; 99 } 100 101 109 public RSS_0_91_Exporter(Writer writer, String encoding) { 110 this.writer = writer; 111 this.encoding = encoding; 112 } 113 114 118 public void write(ChannelIF channel) throws IOException { 119 if (writer == null) { 120 throw new RuntimeException ("No writer has been initialized."); 121 } 122 123 XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); 125 126 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 items = channel.getItems(); 145 Iterator 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 173 if (channel.getCopyright() != null) { 174 channelElem.addContent(new Element("copyright") 175 .setText(channel.getCopyright())); 176 } 177 178 rootElem.addContent(channelElem); 180 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 |