Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terminate XML parsing as soon as a complete feed is received #53

Merged
merged 1 commit into from
Jun 7, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Sources/FeedKit/Parser/XMLFeedParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class XMLFeedParser: NSObject, XMLParserDelegate, FeedParserProtocol {

/// A parsing error, if any.
var parsingError: NSError?
var parseComplete = false

/// Starts parsing the feed.
func parse() -> Result {
Expand Down Expand Up @@ -167,6 +168,10 @@ extension XMLFeedParser {
{
// Update the current path along the XML's DOM elements by deleting last component.
self.currentXMLDOMPath = self.currentXMLDOMPath.deletingLastPathComponent()
if currentXMLDOMPath.absoluteString == "/" {
parseComplete = true
xmlParser.abortParsing()
}
}

func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data)
Expand All @@ -184,7 +189,12 @@ extension XMLFeedParser {
}

func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
self.parsingError = NSError(domain: parseError.localizedDescription, code: -1)
// Ignore errors that occur after a feed is successfully parsed. Some
// real-world feeds contain junk such as "[]" after the XML segment;
// just ignore this stuff.
guard !parseComplete, parsingError == nil else { return }
self.parsingError = NSError(domain: parseError.localizedDescription, code: -1,
userInfo: ["CurrentPath": currentXMLDOMPath.absoluteString])
}

}