1 26 27 29 package de.nava.informa.exporters; 30 31 import java.io.IOException ; 32 import java.io.File ; 33 import java.io.Writer ; 34 import java.io.OutputStreamWriter ; 35 import java.io.FileOutputStream ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import java.text.SimpleDateFormat ; 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 55 public class RSS_1_0_Exporter implements ChannelExporterIF { 56 57 private static final String NS_DEFAULT = 58 "http://purl.org/rss/1.0/"; 59 private static final String NS_RDF = 60 "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 61 62 private static final String NS_DC = 63 "http://purl.org/dc/elements/1.1/"; 64 65 private static final String NS_SY = 66 "http://purl.org/rss/1.0/modules/syndication/"; 67 68 private static SimpleDateFormat df = 69 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ"); 70 71 72 private Writer writer; 73 private String encoding; 74 75 82 public RSS_1_0_Exporter(String filename) throws IOException { 83 this(new File (filename), "utf-8"); 84 } 85 86 93 public RSS_1_0_Exporter(File file) throws IOException { 94 this(file, "utf-8"); 95 } 96 97 106 public RSS_1_0_Exporter(File file, String encoding) throws IOException { 107 this.writer = new OutputStreamWriter (new FileOutputStream (file), encoding); 108 this.encoding = encoding; 109 } 110 111 119 public RSS_1_0_Exporter(Writer writer, String encoding) { 120 this.writer = writer; 121 this.encoding = encoding; 122 } 123 124 128 public void write(ChannelIF channel) throws IOException { 129 if (writer == null) { 130 throw new RuntimeException ("No writer has been initialized."); 131 } 132 133 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 Element rootElem = new Element("RDF", rdfNs); 142 rootElem.addNamespaceDeclaration(defNs); 143 rootElem.addNamespaceDeclaration(dcNs); 144 rootElem.addNamespaceDeclaration(syNs); 145 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 channelElem.addContent(new Element("updateFrequency", syNs) 178 .setText((new Integer (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 items = channel.getItems(); 186 Iterator 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 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 Document doc = new Document(rootElem); 228 outputter.output(doc, writer); 229 writer.close(); 231 } 232 233 } 234 | Popular Tags |