1 26 27 28 30 package de.nava.informa.exporters; 31 32 import java.io.File ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.OutputStreamWriter ; 36 import java.io.Writer ; 37 import java.text.SimpleDateFormat ; 38 import java.util.Collection ; 39 import java.util.Iterator ; 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 61 62 public class RSS_2_0_Exporter implements ChannelExporterIF { 63 64 private static final String RSS_VERSION = "2.0"; 65 66 69 private static final String NS_DC = "http://purl.org/dc/elements/1.1/"; 70 71 74 private static final String NS_SY = "http://purl.org/rss/1.0/modules/syndication/"; 75 76 private static final String NS_ADMIN = "http://webns.net/mvcb/"; 77 78 private static final String NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; 79 80 private static SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ"); 81 82 private Writer writer; 83 84 private String encoding; 85 86 93 public RSS_2_0_Exporter(String filename) throws IOException { 94 this(new File (filename), "utf-8"); 95 } 96 97 98 107 public RSS_2_0_Exporter(File file) throws IOException { 108 this(file, "utf-8"); 109 } 110 111 121 122 public RSS_2_0_Exporter(File file, String encoding) throws IOException { 123 this.writer = new OutputStreamWriter (new FileOutputStream (file), encoding); 124 this.encoding = encoding; 125 } 126 127 137 public RSS_2_0_Exporter(Writer writer, String encoding) { 138 this.writer = writer; 139 this.encoding = encoding; 140 } 141 142 146 private Element getCategoryElements(Element elem, CategoryIF category, StringBuffer catString) { 147 StringBuffer l_catString; 148 if (catString == null || catString.length() < 1) 149 l_catString = new StringBuffer (category.getTitle()); 150 else 151 l_catString = catString.append("/").append(category.getTitle()); 152 153 Collection categories = category.getChildren(); 154 if (categories.size() == 0) { 155 elem.addContent(new Element("category").setText(l_catString.toString())); 156 } else { 157 Iterator 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 170 public void write(ChannelIF channel) throws IOException { 171 if (writer == null) { 172 throw new RuntimeException ("No writer has been initialized."); 173 } 174 175 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 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 categories = channel.getCategories(); 212 Iterator 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 channelElem.addContent(new Element("updateFrequency", syNs) 226 .setText((new Integer (channel.getUpdateFrequency())).toString())); 227 channelElem.addContent(new Element("updatePeriod", syNs).setText(channel 228 .getUpdatePeriod())); 229 } 230 231 Collection items = channel.getItems(); 232 Iterator 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 categories = item.getCategories(); 247 Iterator 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 Document doc = new Document(rootElem); 273 outputter.output(doc, writer); 274 writer.close(); 276 } 277 } 278 279 | Popular Tags |