Add CharsetReader function pointer to Document struct as a public field. This allows the caller to specify their own charset conversion handler when loading xml content.

This commit is contained in:
jim teeuwen 2012-02-29 11:08:37 +01:00
parent b14dd79d8d
commit fcfc98fd64
1 changed files with 17 additions and 15 deletions

View File

@ -37,25 +37,29 @@ import (
"strings" "strings"
) )
type CharsetFunc func(charset string, input io.Reader) (io.Reader, error)
// represents a single XML document. // represents a single XML document.
type Document struct { type Document struct {
Version string // XML version Version string // XML version
Encoding string // Encoding found in document. If absent, assumes UTF-8. Encoding string // Encoding found in document. If absent, assumes UTF-8.
StandAlone string // Value of XML doctype's 'standalone' attribute. StandAlone string // Value of XML doctype's 'standalone' attribute.
SaveDocType bool // Whether not to include the XML doctype in saves. SaveDocType bool // Whether not to include the XML doctype in saves.
Root *Node // The document's root node. Root *Node // The document's root node.
Entity map[string]string // Mapping of custom entity conversions. Entity map[string]string // Mapping of custom entity conversions.
Verbose bool // [depracated] Not actually used anymore. CharsetReader CharsetFunc // Override the xml decoder's CharsetReader. Defaults to nil.
Verbose bool // [depracated] Not actually used anymore.
} }
// Create a new, empty XML document instance. // Create a new, empty XML document instance.
func New() *Document { func New() *Document {
return &Document{ return &Document{
Version: "1.0", Version: "1.0",
Encoding: "utf-8", Encoding: "utf-8",
StandAlone: "yes", StandAlone: "yes",
SaveDocType: true, SaveDocType: true,
Entity: make(map[string]string), Entity: make(map[string]string),
CharsetReader: nil,
} }
} }
@ -82,9 +86,7 @@ func (this *Document) SelectNodes(namespace, name string) []*Node {
func (this *Document) LoadStream(r io.Reader) (err error) { func (this *Document) LoadStream(r io.Reader) (err error) {
xp := xml.NewDecoder(r) xp := xml.NewDecoder(r)
xp.Entity = this.Entity xp.Entity = this.Entity
//xp.CharsetReader = func(enc string, input io.Reader) (io.Reader, error) { xp.CharsetReader = this.CharsetReader
// return charset.NewReader(enc, input)
//}
this.Root = NewNode(NT_ROOT) this.Root = NewNode(NT_ROOT)
ct := this.Root ct := this.Root