1 17 package com.sun.syndication.io; 18 19 import com.sun.syndication.feed.WireFeed; 20 import com.sun.syndication.io.impl.FeedGenerators; 21 import org.jdom.Document; 22 import org.jdom.JDOMException; 23 import org.jdom.output.DOMOutputter; 24 import org.jdom.output.Format; 25 import org.jdom.output.XMLOutputter; 26 27 import java.io.IOException ; 28 import java.io.Writer ; 29 import java.io.File ; 30 import java.io.FileWriter ; 31 import java.util.List ; 32 33 43 public class WireFeedOutput { 44 private final static FeedGenerators GENERATORS = new FeedGenerators(); 45 46 54 public static List getSupportedFeedTypes() { 55 return GENERATORS.getSupportedFeedTypes(); 56 } 57 58 63 public WireFeedOutput() { 64 } 65 66 82 public String outputString(WireFeed feed) throws IllegalArgumentException ,FeedException { 83 Document doc = outputJDom(feed); 84 String encoding = feed.getEncoding(); 85 Format format = Format.getPrettyFormat(); 86 if (encoding!=null) { 87 format.setEncoding(encoding); 88 } 89 XMLOutputter outputter = new XMLOutputter(format); 90 return outputter.outputString(doc); 91 } 92 93 110 public void output(WireFeed feed,File file) throws IllegalArgumentException ,IOException ,FeedException { 111 Writer writer = new FileWriter (file); 112 output(feed,writer); 113 writer.close(); 114 } 115 116 132 public void output(WireFeed feed,Writer writer) throws IllegalArgumentException ,IOException , FeedException { 133 Document doc = outputJDom(feed); 134 String encoding = feed.getEncoding(); 135 Format format = Format.getPrettyFormat(); 136 if (encoding!=null) { 137 format.setEncoding(encoding); 138 } 139 XMLOutputter outputter = new XMLOutputter(format); 140 outputter.output(doc,writer); 141 } 142 143 157 public org.w3c.dom.Document outputW3CDom(WireFeed feed) throws IllegalArgumentException ,FeedException { 158 Document doc = outputJDom(feed); 159 DOMOutputter outputter = new DOMOutputter(); 160 try { 161 return outputter.output(doc); 162 } 163 catch (JDOMException jdomEx) { 164 throw new FeedException("Could not create DOM",jdomEx); 165 } 166 } 167 168 182 public Document outputJDom(WireFeed feed) throws IllegalArgumentException ,FeedException { 183 String type = feed.getFeedType(); 184 WireFeedGenerator generator = GENERATORS.getGenerator(type); 185 if (generator==null) { 186 throw new IllegalArgumentException ("Invalid feed type ["+type+"]"); 187 } 188 189 if (!generator.getType().equals(type)) { 190 throw new IllegalArgumentException ("WireFeedOutput type["+type+"] and WireFeed type ["+ 191 type+"] don't match"); 192 } 193 return generator.generate(feed); 194 } 195 196 } 197 | Popular Tags |