Skip to content

Commit

Permalink
expose WriteTo for tokens, define the needed writer interface
Browse files Browse the repository at this point in the history
This makes it possible to write XML fragments without having to copy
elements to a separate document.

Also satisfies the InnerXML use-case from
#71

```
var innerXML bytes.Buffer
var writeSettings etree.WriteSetting

for _, child := range element.Child {
	child.WriteTo(&innerXML, &writeSettings)
}
```

In my actual use-case I just passed in `&doc.WriteSettings`, but there
might also be a legitimate need to write out fragments with other
settings. So I find a parameter preferrable to using the settings of a
hidden associated document.
  • Loading branch information
hugowetterberg authored and beevik committed May 11, 2023
1 parent 672dfa1 commit a291eec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
40 changes: 24 additions & 16 deletions etree.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ type WriteSettings struct {
UseCRLF bool
}

// XMLWriter is a Writer that also has convenience methods for writing
// strings an single bytes.
type XMLWriter interface {
io.StringWriter
io.ByteWriter
io.Writer
}

// newWriteSettings creates a default WriteSettings record.
func newWriteSettings() WriteSettings {
return WriteSettings{
Expand Down Expand Up @@ -166,10 +174,10 @@ func NewIndentSettings() IndentSettings {
type Token interface {
Parent() *Element
Index() int
WriteTo(w XMLWriter, s *WriteSettings)
dup(parent *Element) Token
setParent(parent *Element)
setIndex(index int)
writeTo(w *bufio.Writer, s *WriteSettings)
}

// A Document is a container holding a complete XML tree.
Expand Down Expand Up @@ -347,7 +355,7 @@ func (d *Document) WriteTo(w io.Writer) (n int64, err error) {
xw := newXmlWriter(w)
b := bufio.NewWriter(xw)
for _, c := range d.Child {
c.writeTo(b, &d.WriteSettings)
c.WriteTo(b, &d.WriteSettings)
}
err, n = b.Flush(), xw.bytes
return
Expand Down Expand Up @@ -1155,18 +1163,18 @@ func (e *Element) setIndex(index int) {
e.index = index
}

// writeTo serializes the element to the writer w.
func (e *Element) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serializes the element to the writer w.
func (e *Element) WriteTo(w XMLWriter, s *WriteSettings) {
w.WriteByte('<')
w.WriteString(e.FullTag())
for _, a := range e.Attr {
w.WriteByte(' ')
a.writeTo(w, s)
a.WriteTo(w, s)
}
if len(e.Child) > 0 {
w.WriteByte('>')
for _, c := range e.Child {
c.writeTo(w, s)
c.WriteTo(w, s)
}
w.Write([]byte{'<', '/'})
w.WriteString(e.FullTag())
Expand Down Expand Up @@ -1283,8 +1291,8 @@ func (a *Attr) NamespaceURI() string {
return a.element.findLocalNamespaceURI(a.Space)
}

// writeTo serializes the attribute to the writer.
func (a *Attr) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serializes the attribute to the writer.
func (a *Attr) WriteTo(w XMLWriter, s *WriteSettings) {
w.WriteString(a.FullKey())
if s.AttrSingleQuote {
w.WriteString(`='`)
Expand Down Expand Up @@ -1420,8 +1428,8 @@ func (c *CharData) setIndex(index int) {
c.index = index
}

// writeTo serializes character data to the writer.
func (c *CharData) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serializes character data to the writer.
func (c *CharData) WriteTo(w XMLWriter, s *WriteSettings) {
if c.IsCData() {
w.WriteString(`<![CDATA[`)
w.WriteString(c.Data)
Expand Down Expand Up @@ -1493,8 +1501,8 @@ func (c *Comment) setIndex(index int) {
c.index = index
}

// writeTo serialies the comment to the writer.
func (c *Comment) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serialies the comment to the writer.
func (c *Comment) WriteTo(w XMLWriter, s *WriteSettings) {
w.WriteString("<!--")
w.WriteString(c.Data)
w.WriteString("-->")
Expand Down Expand Up @@ -1558,8 +1566,8 @@ func (d *Directive) setIndex(index int) {
d.index = index
}

// writeTo serializes the XML directive to the writer.
func (d *Directive) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serializes the XML directive to the writer.
func (d *Directive) WriteTo(w XMLWriter, s *WriteSettings) {
w.WriteString("<!")
w.WriteString(d.Data)
w.WriteString(">")
Expand Down Expand Up @@ -1626,8 +1634,8 @@ func (p *ProcInst) setIndex(index int) {
p.index = index
}

// writeTo serializes the processing instruction to the writer.
func (p *ProcInst) writeTo(w *bufio.Writer, s *WriteSettings) {
// WriteTo serializes the processing instruction to the writer.
func (p *ProcInst) WriteTo(w XMLWriter, s *WriteSettings) {
w.WriteString("<?")
w.WriteString(p.Target)
if p.Inst != "" {
Expand Down
3 changes: 1 addition & 2 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package etree

import (
"bufio"
"io"
"strings"
"unicode/utf8"
Expand Down Expand Up @@ -330,7 +329,7 @@ const (
)

// escapeString writes an escaped version of a string to the writer.
func escapeString(w *bufio.Writer, s string, m escapeMode) {
func escapeString(w XMLWriter, s string, m escapeMode) {
var esc []byte
last := 0
for i := 0; i < len(s); {
Expand Down

0 comments on commit a291eec

Please sign in to comment.