From f51db12ad3791ecf917bb18502bab4a56ee91aff Mon Sep 17 00:00:00 2001 From: jim teeuwen Date: Wed, 29 Feb 2012 11:31:01 +0100 Subject: [PATCH] Add xmlx.CharsetFunc parameter to Feed.Fetch method. Can be used to supply a custom character encoding conversion routine to the xml decoder. Update test code accordingly. --- feed.go | 11 +++++++++-- feed_test.go | 7 +++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/feed.go b/feed.go index c393b03..5b90921 100644 --- a/feed.go +++ b/feed.go @@ -84,7 +84,13 @@ func New(cachetimeout int, enforcecachelimit bool, ch ChannelHandler, ih ItemHan // The value is in seconds. func (this *Feed) LastUpdate() int64 { return this.lastupdate } -func (this *Feed) Fetch(uri string) (err error) { +// Fetch retrieves the feed's latest content if necessary. +// +// The charset parameter overrides the xml decoder's CharsetReader. +// This allows us to specify a custom character encoding conversion +// routine when dealing with non-utf8 input. Supply 'nil' to use the +// default from Go's xml package. +func (this *Feed) Fetch(uri string, charset xmlx.CharsetFunc) (err error) { if !this.CanUpdate() { return } @@ -94,7 +100,8 @@ func (this *Feed) Fetch(uri string) (err error) { // Extract type and version of the feed so we can have the appropriate // function parse it (rss 0.91, rss 0.92, rss 2, atom etc). doc := xmlx.New() - if err = doc.LoadUri(uri); err != nil { + + if err = doc.LoadUri(uri, charset); err != nil { return } this.Type, this.Version = this.GetVersionInfo(doc) diff --git a/feed_test.go b/feed_test.go index 008a3e1..9bbfa19 100644 --- a/feed_test.go +++ b/feed_test.go @@ -4,9 +4,8 @@ import "testing" func TestFeed(t *testing.T) { urilist := []string{ - //"http://localhost:8081/craigslist.rss", - //"http://store.steampowered.com/feeds/news.xml", // This feed violates the rss spec. - "http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", + //"http://cyber.law.harvard.edu/rss/examples/sampleRss091.xml", // Non-utf8 encoding. + "http://store.steampowered.com/feeds/news.xml", // This feed violates the rss spec. "http://cyber.law.harvard.edu/rss/examples/sampleRss092.xml", "http://cyber.law.harvard.edu/rss/examples/rss2sample.xml", "http://blog.case.edu/news/feed.atom", @@ -18,7 +17,7 @@ func TestFeed(t *testing.T) { for _, uri := range urilist { feed = New(5, true, chanHandler, itemHandler) - if err = feed.Fetch(uri); err != nil { + if err = feed.Fetch(uri, nil); err != nil { t.Errorf("%s >>> %s", uri, err) return }