Skip to content

Commit

Permalink
Speed up XmlCheckIsValid processing.
Browse files Browse the repository at this point in the history
  • Loading branch information
clbanning committed Feb 2, 2021
1 parent e3ab8f8 commit aab98a1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
37 changes: 37 additions & 0 deletions isvalid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package mxj

import (
"fmt"
"testing"
)

func TestXmlCheckIsValid(t *testing.T) {
fmt.Println("================== TestXmlCheckIsValid")

data := []byte(`{"":"empty", "$invalid":"hex$", "entities":"<>&", "nil": null}`)
m, err := NewMapJson(data)
if err != nil {
t.Fatal("NewMapJson err;", err)
}
fmt.Printf("%v\n", m)

XmlCheckIsValid()
defer XmlCheckIsValid()
if _, err = m.Xml(); err == nil {
t.Fatal("Xml err: nil")
}

if _, err = m.XmlIndent("", " "); err == nil {
t.Fatal("XmlIndent err: nil")
}

ms, err := NewMapXmlSeq([]byte(`<doc><elem>element</elem></doc>`))
fmt.Printf("%v\n", ms)
if _, err = ms.Xml(); err != nil {
t.Fatal("Xml err:", err)
}

if _, err = ms.XmlIndent("", " "); err != nil {
t.Fatal("XmlIndent err:", err)
}
}
22 changes: 18 additions & 4 deletions xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,15 @@ func (mv Map) Xml(rootTag ...string) ([]byte, error) {
}
done:
if xmlCheckIsValid {
if _, err = NewMapXml(b.Bytes()); err != nil {
return nil, err
d := xml.NewDecoder(bytes.NewReader(b.Bytes()))
for _, err = d.Token(); err != io.EOF; {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return b.Bytes(), err
Expand Down Expand Up @@ -937,8 +944,15 @@ func (mv Map) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, error
err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p)
}
if xmlCheckIsValid {
if _, err = NewMapXml(b.Bytes()); err != nil {
return nil, err
d := xml.NewDecoder(bytes.NewReader(b.Bytes()))
for _, err = d.Token(); err != io.EOF; {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return b.Bytes(), err
Expand Down
21 changes: 19 additions & 2 deletions xmlseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,15 @@ func (mv MapSeq) Xml(rootTag ...string) ([]byte, error) {
}
done:
if xmlCheckIsValid {
if _, err = NewMapXml([]byte(*s)); err != nil {
return nil, err
d := xml.NewDecoder(bytes.NewReader([]byte(*s)))
for _, err = d.Token(); err != io.EOF; {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return []byte(*s), err
Expand Down Expand Up @@ -544,6 +551,16 @@ func (mv MapSeq) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, er
if _, err = NewMapXml([]byte(*s)); err != nil {
return nil, err
}
d := xml.NewDecoder(bytes.NewReader([]byte(*s)))
for _, err = d.Token(); err != io.EOF; {
_, err = d.Token()
if err == io.EOF {
err = nil
break
} else if err != nil {
return nil, err
}
}
}
return []byte(*s), err
}
Expand Down

0 comments on commit aab98a1

Please sign in to comment.