1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-12-26 23:02:29 -05:00

Ignore whitespace character data xml Tokens

This commit is contained in:
Aaron Lindsay 2017-03-13 21:10:19 -04:00
parent 257495a343
commit b90c3e0dcd
3 changed files with 28 additions and 11 deletions

29
ofx.go
View File

@ -239,8 +239,25 @@ func (or *Response) readSGMLHeaders(r *bufio.Reader) error {
return nil
}
func nextNonWhitespaceToken(decoder *xml.Decoder) (xml.Token, error) {
for {
tok, err := decoder.Token()
if err != nil {
return nil, err
} else if chars, ok := tok.(xml.CharData); ok {
strippedBytes := bytes.TrimSpace(chars)
if len(strippedBytes) != 0 {
return tok, nil
}
} else {
return tok, nil
}
}
}
func (or *Response) readXMLHeaders(decoder *xml.Decoder) error {
tok, err := decoder.Token()
var tok xml.Token
tok, err := nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if xmlElem, ok := tok.(xml.ProcInst); !ok || xmlElem.Target != "xml" {
@ -248,7 +265,7 @@ func (or *Response) readXMLHeaders(decoder *xml.Decoder) error {
}
// parse the OFX header
tok, err = decoder.Token()
tok, err = nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if ofxElem, ok := tok.(xml.ProcInst); ok && ofxElem.Target == "OFX" {
@ -330,7 +347,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error {
}
}
tok, err := decoder.Token()
tok, err := nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if ofxStart, ok := tok.(xml.StartElement); !ok || ofxStart.Name.Local != "OFX" {
@ -338,7 +355,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error {
}
// Unmarshal the signon message
tok, err = decoder.Token()
tok, err = nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if signonStart, ok := tok.(xml.StartElement); ok && signonStart.Name.Local == "SIGNONMSGSRSV1" {
@ -349,7 +366,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error {
return errors.New("Missing opening SIGNONMSGSRSV1 xml element")
}
tok, err = decoder.Token()
tok, err = nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if signonEnd, ok := tok.(xml.EndElement); !ok || signonEnd.Name.Local != "SIGNONMSGSRSV1" {
@ -360,7 +377,7 @@ func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error {
}
for {
tok, err = decoder.Token()
tok, err = nextNonWhitespaceToken(decoder)
if err != nil {
return err
} else if ofxEnd, ok := tok.(xml.EndElement); ok && ofxEnd.Name.Local == "OFX" {

View File

@ -63,7 +63,7 @@ type MessageSetList []MessageSet
func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for {
var msgset MessageSet
tok, err := d.Token()
tok, err := nextNonWhitespaceToken(d)
if err != nil {
return err
} else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {
@ -71,7 +71,7 @@ func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement)
return nil
} else if _, ok := tok.(xml.StartElement); ok {
// Found starting tag for <xxxMSGSET>. Get the next one (xxxMSGSETVn) and decode that struct
tok, err := d.Token()
tok, err := nextNonWhitespaceToken(d)
if err != nil {
return err
} else if versionStart, ok := tok.(xml.StartElement); ok {
@ -83,7 +83,7 @@ func (msl *MessageSetList) UnmarshalXML(d *xml.Decoder, start xml.StartElement)
}
// Eat ending tags for <xxxMSGSET>
tok, err = d.Token()
tok, err = nextNonWhitespaceToken(d)
if err != nil {
return err
} else if _, ok := tok.(xml.EndElement); !ok {
@ -129,7 +129,7 @@ func (pr ProfileResponse) Valid() (bool, error) {
func DecodeProfileMessageSet(d *xml.Decoder, start xml.StartElement) ([]Message, error) {
var msgs []Message
for {
tok, err := d.Token()
tok, err := nextNonWhitespaceToken(d)
if err != nil {
return nil, err
} else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {

View File

@ -146,7 +146,7 @@ func (air AcctInfoResponse) Valid() (bool, error) {
func DecodeSignupMessageSet(d *xml.Decoder, start xml.StartElement) ([]Message, error) {
var msgs []Message
for {
tok, err := d.Token()
tok, err := nextNonWhitespaceToken(d)
if err != nil {
return nil, err
} else if end, ok := tok.(xml.EndElement); ok && end.Name.Local == start.Name.Local {