From 817a15ca8bb8d18e1e20ec5da59da2b44ac93ab3 Mon Sep 17 00:00:00 2001 From: jim teeuwen Date: Wed, 29 Feb 2012 11:21:35 +0100 Subject: [PATCH] Remove CharsetFunc as field for Document type and instead supply it as a parameter for all Document.LoadXXX methods. There should be no need to store the function pointer in the Document struct. --- document.go | 22 ++++++++++------------ xmlx_test.go | 12 ++++++------ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/document.go b/document.go index 4ef8f4c..2bee3e1 100644 --- a/document.go +++ b/document.go @@ -46,7 +46,6 @@ type Document struct { StandAlone string // Value of XML doctype's 'standalone' attribute. Entity map[string]string // Mapping of custom entity conversions. Root *Node // The document's root node. - CharsetReader CharsetFunc // Override the xml decoder's CharsetReader. Defaults to nil. SaveDocType bool // Whether not to include the XML doctype in saves. } @@ -58,7 +57,6 @@ func New() *Document { StandAlone: "yes", SaveDocType: true, Entity: make(map[string]string), - CharsetReader: nil, } } @@ -82,10 +80,10 @@ func (this *Document) SelectNodes(namespace, name string) []*Node { } // Load the contents of this document from the supplied reader. -func (this *Document) LoadStream(r io.Reader) (err error) { +func (this *Document) LoadStream(r io.Reader, charset CharsetFunc) (err error) { xp := xml.NewDecoder(r) xp.Entity = this.Entity - xp.CharsetReader = this.CharsetReader + xp.CharsetReader = charset this.Root = NewNode(NT_ROOT) ct := this.Root @@ -151,35 +149,35 @@ func (this *Document) LoadStream(r io.Reader) (err error) { } // Load the contents of this document from the supplied byte slice. -func (this *Document) LoadBytes(d []byte) (err error) { - return this.LoadStream(bytes.NewBuffer(d)) +func (this *Document) LoadBytes(d []byte, charset CharsetFunc) (err error) { + return this.LoadStream(bytes.NewBuffer(d), charset) } // Load the contents of this document from the supplied string. -func (this *Document) LoadString(s string) (err error) { - return this.LoadStream(strings.NewReader(s)) +func (this *Document) LoadString(s string, charset CharsetFunc) (err error) { + return this.LoadStream(strings.NewReader(s), charset) } // Load the contents of this document from the supplied file. -func (this *Document) LoadFile(filename string) (err error) { +func (this *Document) LoadFile(filename string, charset CharsetFunc) (err error) { var fd *os.File if fd, err = os.Open(filename); err != nil { return } defer fd.Close() - return this.LoadStream(fd) + return this.LoadStream(fd, charset) } // Load the contents of this document from the supplied uri. -func (this *Document) LoadUri(uri string) (err error) { +func (this *Document) LoadUri(uri string, charset CharsetFunc) (err error) { var r *http.Response if r, err = http.Get(uri); err != nil { return } defer r.Body.Close() - return this.LoadStream(r.Body) + return this.LoadStream(r.Body, charset) } // Save the contents of this document to the supplied file. diff --git a/xmlx_test.go b/xmlx_test.go index aebdee0..0999c5d 100644 --- a/xmlx_test.go +++ b/xmlx_test.go @@ -9,7 +9,7 @@ import "testing" func TestLoadLocal(t *testing.T) { doc := New() - if err := doc.LoadFile("test.xml"); err != nil { + if err := doc.LoadFile("test.xml", nil); err != nil { t.Error(err.Error()) return } @@ -23,7 +23,7 @@ func TestLoadLocal(t *testing.T) { func TestWildcard(t *testing.T) { doc := New() - if err := doc.LoadFile("test2.xml"); err != nil { + if err := doc.LoadFile("test2.xml", nil); err != nil { t.Error(err.Error()) return } @@ -39,7 +39,7 @@ func TestWildcard(t *testing.T) { func _TestLoadRemote(t *testing.T) { doc := New() - if err := doc.LoadUri("http://blog.golang.org/feeds/posts/default"); err != nil { + if err := doc.LoadUri("http://blog.golang.org/feeds/posts/default", nil); err != nil { t.Error(err.Error()) return } @@ -53,7 +53,7 @@ func _TestLoadRemote(t *testing.T) { func TestSave(t *testing.T) { doc := New() - if err := doc.LoadFile("test.xml"); err != nil { + if err := doc.LoadFile("test.xml", nil); err != nil { t.Errorf("LoadFile(): %s", err) return } @@ -67,7 +67,7 @@ func TestSave(t *testing.T) { func TestNodeSearch(t *testing.T) { doc := New() - if err := doc.LoadFile("test1.xml"); err != nil { + if err := doc.LoadFile("test1.xml", nil); err != nil { t.Errorf("LoadFile(): %s", err) return } @@ -95,7 +95,7 @@ type Image struct { func TestUnmarshal(t *testing.T) { doc := New() - err := doc.LoadFile("test1.xml") + err := doc.LoadFile("test1.xml", nil) if err != nil { t.Errorf("LoadFile(): %s", err)