1 17 package com.sun.syndication.io; 18 19 import com.sun.syndication.feed.WireFeed; 20 import com.sun.syndication.io.impl.FeedParsers; 21 import com.sun.syndication.io.impl.XmlFixerReader; 22 import org.jdom.Document; 23 import org.jdom.input.DOMBuilder; 24 import org.jdom.input.JDOMParseException; 25 import org.jdom.input.SAXBuilder; 26 import org.xml.sax.EntityResolver ; 27 import org.xml.sax.InputSource ; 28 29 import java.io.*; 30 import java.util.List ; 31 32 44 public class WireFeedInput { 45 private static FeedParsers FEED_PARSERS = new FeedParsers(); 46 private static final InputSource EMPTY_INPUTSOURCE = new InputSource (new ByteArrayInputStream(new byte[0])); 47 private static final EntityResolver RESOLVER = new EmptyEntityResolver(); 48 49 private static class EmptyEntityResolver implements EntityResolver { 50 public InputSource resolveEntity(String publicId, String systemId) { 51 if(systemId != null && systemId.endsWith(".dtd")) return EMPTY_INPUTSOURCE; 52 return null; 53 } 54 } 55 56 private boolean _validate; 57 58 private boolean _xmlHealerOn; 59 60 68 public static List getSupportedFeedTypes() { 69 return FEED_PARSERS.getSupportedFeedTypes(); 70 } 71 72 77 public WireFeedInput() { 78 this (false); 79 } 80 81 87 public WireFeedInput(boolean validate) { 88 _validate = false; _xmlHealerOn = true; 90 } 91 92 106 public void setXmlHealerOn(boolean heals) { 107 _xmlHealerOn = heals; 108 } 109 110 124 public boolean getXmlHealerOn() { 125 return _xmlHealerOn; 126 } 127 128 141 public WireFeed build(File file) throws FileNotFoundException,IOException,IllegalArgumentException ,FeedException { 142 WireFeed feed; 143 Reader reader = new FileReader(file); 144 if (_xmlHealerOn) { 145 reader = new XmlFixerReader(reader); 146 } 147 feed = build(reader); 148 reader.close(); 149 return feed; 150 } 151 152 163 public WireFeed build(Reader reader) throws IllegalArgumentException ,FeedException { 164 SAXBuilder saxBuilder = new SAXBuilder(_validate); 165 saxBuilder.setEntityResolver(RESOLVER); 166 try { 167 if (_xmlHealerOn) { 168 reader = new XmlFixerReader(reader); 169 } 170 Document document = saxBuilder.build(reader); 171 return build(document); 172 } 173 catch (JDOMParseException ex) { 174 throw new ParsingFeedException("Invalid XML: " + ex.getMessage(), ex); 175 } 176 catch (Exception ex) { 177 throw new ParsingFeedException("Invalid XML",ex); 178 } 179 } 180 181 192 public WireFeed build(InputSource is) throws IllegalArgumentException ,FeedException { 193 SAXBuilder saxBuilder = new SAXBuilder(_validate); 194 saxBuilder.setEntityResolver(RESOLVER); 195 try { 196 Document document = saxBuilder.build(is); 197 return build(document); 198 } 199 catch (JDOMParseException ex) { 200 throw new ParsingFeedException("Invalid XML: " + ex.getMessage(), ex); 201 } 202 catch (Exception ex) { 203 throw new ParsingFeedException("Invalid XML",ex); 204 } 205 } 206 207 218 public WireFeed build(org.w3c.dom.Document document) throws IllegalArgumentException ,FeedException { 219 DOMBuilder domBuilder = new DOMBuilder(); 220 try { 221 Document jdomDoc = domBuilder.build(document); 222 return build(jdomDoc); 223 } 224 catch (Exception ex) { 225 throw new ParsingFeedException("Invalid XML",ex); 226 } 227 } 228 229 240 public WireFeed build(Document document) throws IllegalArgumentException ,FeedException { 241 WireFeedParser parser = FEED_PARSERS.getParserFor(document); 242 if (parser==null) { 243 throw new IllegalArgumentException ("Invalid document"); 244 } 245 return parser.parse(document, _validate); 246 } 247 248 } 249 | Popular Tags |