yq/pkg/yqlib/encoder_xml.go

147 lines
3.5 KiB
Go
Raw Normal View History

2021-12-21 04:56:08 +00:00
package yqlib
import (
"encoding/xml"
"fmt"
"io"
2021-12-21 05:08:37 +00:00
"strings"
2021-12-21 04:56:08 +00:00
yaml "gopkg.in/yaml.v3"
)
type xmlEncoder struct {
2021-12-21 05:08:37 +00:00
xmlEncoder *xml.Encoder
attributePrefix string
2021-12-21 05:19:27 +00:00
contentName string
2021-12-21 04:56:08 +00:00
}
2021-12-21 05:19:27 +00:00
func NewXmlEncoder(writer io.Writer, indent int, attributePrefix string, contentName string) Encoder {
2021-12-21 04:56:08 +00:00
encoder := xml.NewEncoder(writer)
var indentString = ""
for index := 0; index < indent; index++ {
indentString = indentString + " "
}
encoder.Indent("", indentString)
2021-12-21 05:19:27 +00:00
return &xmlEncoder{encoder, attributePrefix, contentName}
2021-12-21 04:56:08 +00:00
}
func (e *xmlEncoder) Encode(node *yaml.Node) error {
switch node.Kind {
case yaml.MappingNode:
2021-12-21 05:52:54 +00:00
err := e.encodeTopLevelMap(node)
if err != nil {
return err
}
var charData xml.CharData = []byte("\n")
err = e.xmlEncoder.EncodeToken(charData)
if err != nil {
return err
}
return e.xmlEncoder.Flush()
2021-12-21 04:56:08 +00:00
case yaml.DocumentNode:
return e.Encode(unwrapDoc(node))
case yaml.ScalarNode:
var charData xml.CharData = []byte(node.Value)
2021-12-21 05:52:54 +00:00
err := e.xmlEncoder.EncodeToken(charData)
if err != nil {
return err
}
return e.xmlEncoder.Flush()
2021-12-21 04:56:08 +00:00
}
return fmt.Errorf("unsupported type %v", node.Tag)
}
2021-12-21 05:08:37 +00:00
func (e *xmlEncoder) encodeTopLevelMap(node *yaml.Node) error {
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i+1]
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
err := e.doEncode(value, start)
2021-12-21 04:56:08 +00:00
if err != nil {
return err
}
2021-12-21 05:08:37 +00:00
}
return nil
}
func (e *xmlEncoder) doEncode(node *yaml.Node, start xml.StartElement) error {
switch node.Kind {
case yaml.MappingNode:
return e.encodeMap(node, start)
2021-12-21 04:56:08 +00:00
case yaml.SequenceNode:
return e.encodeArray(node, start)
case yaml.ScalarNode:
err := e.xmlEncoder.EncodeToken(start)
if err != nil {
return err
}
var charData xml.CharData = []byte(node.Value)
err = e.xmlEncoder.EncodeToken(charData)
if err != nil {
return err
}
return e.xmlEncoder.EncodeToken(start.End())
}
return fmt.Errorf("unsupported type %v", node.Tag)
}
func (e *xmlEncoder) encodeArray(node *yaml.Node, start xml.StartElement) error {
for i := 0; i < len(node.Content); i++ {
value := node.Content[i]
err := e.doEncode(value, start.Copy())
if err != nil {
return err
}
}
return nil
}
2021-12-21 05:08:37 +00:00
func (e *xmlEncoder) encodeMap(node *yaml.Node, start xml.StartElement) error {
//first find all the attributes and put them on the start token
2021-12-21 04:56:08 +00:00
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i+1]
2021-12-21 05:19:27 +00:00
if strings.HasPrefix(key.Value, e.attributePrefix) && key.Value != e.contentName {
2021-12-21 05:08:37 +00:00
if value.Kind == yaml.ScalarNode {
attributeName := strings.Replace(key.Value, e.attributePrefix, "", 1)
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: attributeName}, Value: value.Value})
} else {
return fmt.Errorf("cannot use %v as attribute, only scalars are supported", value.Tag)
}
2021-12-21 04:56:08 +00:00
}
}
2021-12-21 05:08:37 +00:00
err := e.xmlEncoder.EncodeToken(start)
if err != nil {
return err
}
//now we encode non attribute tokens
for i := 0; i < len(node.Content); i += 2 {
key := node.Content[i]
value := node.Content[i+1]
2021-12-21 05:19:27 +00:00
if !strings.HasPrefix(key.Value, e.attributePrefix) && key.Value != e.contentName {
2021-12-21 05:08:37 +00:00
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
err := e.doEncode(value, start)
if err != nil {
return err
}
2021-12-21 05:19:27 +00:00
} else if key.Value == e.contentName {
// directly encode the contents
var charData xml.CharData = []byte(value.Value)
err = e.xmlEncoder.EncodeToken(charData)
if err != nil {
return err
}
2021-12-21 05:08:37 +00:00
}
}
return e.xmlEncoder.EncodeToken(start.End())
2021-12-21 04:56:08 +00:00
}