RSSParser.java (3149B)
1 /* 2 * Copyright (C) 2010 A. Horn 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.mcsoxford.rss; 18 19 import java.io.IOException; 20 import java.io.InputStream; 21 22 import javax.xml.parsers.ParserConfigurationException; 23 import javax.xml.parsers.SAXParser; 24 import javax.xml.parsers.SAXParserFactory; 25 26 import org.xml.sax.InputSource; 27 import org.xml.sax.SAXException; 28 import org.xml.sax.XMLReader; 29 30 /** 31 * Internal thread-safe RSS parser SPI implementation. 32 * 33 * @author Mr Horn 34 */ 35 class RSSParser implements RSSParserSPI { 36 37 private final RSSConfig config; 38 39 /* Internal constructor for RSSReader */ 40 RSSParser(RSSConfig config) { 41 this.config = config; 42 } 43 44 /** 45 * Parses input stream as RSS feed. It is the responsibility of the caller to 46 * close the RSS feed input stream. 47 * 48 * @param feed RSS 2.0 feed input stream 49 * @return in-memory representation of RSS feed 50 * @throws RSSFault if an unrecoverable parse error occurs 51 */ 52 @Override 53 public RSSFeed parse(InputStream feed) { 54 try { 55 // Since SAXParserFactory implementations are not guaranteed to be 56 // thread-safe, a new local object is instantiated. 57 final SAXParserFactory factory = SAXParserFactory.newInstance(); 58 59 // Support Android 1.6 (see Issue 1) 60 factory.setFeature("http://xml.org/sax/features/namespaces", false); 61 factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 62 63 final SAXParser parser = factory.newSAXParser(); 64 65 return parse(parser, feed); 66 } catch (ParserConfigurationException e) { 67 throw new RSSFault(e); 68 } catch (SAXException e) { 69 throw new RSSFault(e); 70 } catch (IOException e) { 71 throw new RSSFault(e); 72 } 73 } 74 75 /** 76 * Parses input stream as an RSS 2.0 feed. 77 * 78 * @return in-memory representation of an RSS feed 79 * @throws IllegalArgumentException if either argument is {@code null} 80 */ 81 private RSSFeed parse(SAXParser parser, InputStream feed) 82 throws SAXException, IOException { 83 if (parser == null) { 84 throw new IllegalArgumentException("RSS parser must not be null."); 85 } else if (feed == null) { 86 throw new IllegalArgumentException("RSS feed must not be null."); 87 } 88 89 // SAX automatically detects the correct character encoding from the stream 90 // See also http://www.w3.org/TR/REC-xml/#sec-guessing 91 final InputSource source = new InputSource(feed); 92 final XMLReader xmlreader = parser.getXMLReader(); 93 final RSSHandler handler = new RSSHandler(config); 94 95 xmlreader.setContentHandler(handler); 96 xmlreader.parse(source); 97 98 return handler.feed(); 99 } 100 101 } 102