Use XmlStreamReader to support different encodings

This commit is contained in:
daniel oeh 2012-09-24 14:08:26 +02:00
parent 7b4aa15f85
commit 08e4408717
4 changed files with 23 additions and 4 deletions

View File

@ -58,6 +58,11 @@
<artifactId>annotations</artifactId> <artifactId>annotations</artifactId>
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies> </dependencies>

View File

@ -1,12 +1,17 @@
package de.danoeh.antennapod.syndication.handler; package de.danoeh.antennapod.syndication.handler;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.input.XmlStreamReader;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import de.danoeh.antennapod.feed.Feed; import de.danoeh.antennapod.feed.Feed;
@ -22,8 +27,13 @@ public class FeedHandler {
SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true); factory.setNamespaceAware(true);
SAXParser saxParser = factory.newSAXParser(); SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new File(feed.getFile_url()), handler); File file = new File(feed.getFile_url());
InputStream inputStream = new FileInputStream(file);
Reader inputStreamReader = new XmlStreamReader(file);
InputSource inputSource = new InputSource(inputStreamReader);
saxParser.parse(inputSource, handler);
inputStream.close();
return handler.state.feed; return handler.state.feed;
} }
} }

View File

@ -47,8 +47,7 @@ public class SyndHandler extends DefaultHandler {
if (!state.tagstack.empty()) { if (!state.tagstack.empty()) {
if (state.getTagstack().size() >= 2) { if (state.getTagstack().size() >= 2) {
if (state.contentBuf != null) { if (state.contentBuf != null) {
String content = new String(ch, start, length); state.contentBuf.append(ch, start, length);
state.contentBuf.append(content);
} }
} }
} }

View File

@ -24,6 +24,12 @@ public class FeedHandlerTest extends AndroidTestCase {
private static final String FEEDS_DIR = "testfeeds"; private static final String FEEDS_DIR = "testfeeds";
private static final String[] urls = { private static final String[] urls = {
"http://www.dradio.de/rss/podcast/sendungen/wissenschaftundbildung/",
"http://www.dradio.de/rss/podcast/sendungen/wirtschaftundverbraucher/",
"http://www.dradio.de/rss/podcast/sendungen/literatur/",
"http://www.dradio.de/rss/podcast/sendungen/sport/",
"http://www.dradio.de/rss/podcast/sendungen/wirtschaftundgesellschaft/",
"http://www.dradio.de/rss/podcast/sendungen/filmederwoche/",
"http://feeds.feedburner.com/buildanalyze", "http://feeds.feedburner.com/buildanalyze",
"http://www.blacksweetstories.com/feed/podcast/", "http://www.blacksweetstories.com/feed/podcast/",
"http://bitlove.org/ranzzeit/ranz/feed", "http://bitlove.org/ranzzeit/ranz/feed",
@ -169,7 +175,6 @@ public class FeedHandlerTest extends AndroidTestCase {
"http://bitlove.org/hoaxmaster/hoaxilla/feed", "http://bitlove.org/hoaxmaster/hoaxilla/feed",
"http://bitlove.org/hoaxmaster/psychotalk/feed", "http://bitlove.org/hoaxmaster/psychotalk/feed",
"http://bitlove.org/hoaxmaster/skeptoskop/feed", "http://bitlove.org/hoaxmaster/skeptoskop/feed",
"http://bitlove.org/hobbykoch-podcast/souffle/feed",
"http://bitlove.org/hoersuppe/vorcast/feed", "http://bitlove.org/hoersuppe/vorcast/feed",
"http://bitlove.org/holgi/wrint/feed", "http://bitlove.org/holgi/wrint/feed",
"http://bitlove.org/ich-bin-radio/fir/feed", "http://bitlove.org/ich-bin-radio/fir/feed",