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"
|
|
|
|
)
|
|
|
|
|
2022-03-28 03:05:10 +00:00
|
|
|
var XMLPreferences = xmlPreferences{AttributePrefix: "+", ContentName: "+content", StrictMode: false}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2021-12-21 04:56:08 +00:00
|
|
|
type xmlEncoder struct {
|
2021-12-21 05:08:37 +00:00
|
|
|
attributePrefix string
|
2021-12-21 05:19:27 +00:00
|
|
|
contentName string
|
2022-01-15 00:57:59 +00:00
|
|
|
indentString string
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func NewXMLEncoder(indent int, attributePrefix string, contentName string) Encoder {
|
2021-12-21 04:56:08 +00:00
|
|
|
var indentString = ""
|
|
|
|
|
|
|
|
for index := 0; index < indent; index++ {
|
|
|
|
indentString = indentString + " "
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
return &xmlEncoder{attributePrefix, contentName, indentString}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) CanHandleAliases() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) PrintDocumentSeparator(writer io.Writer) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) PrintLeadingContent(writer io.Writer, content string) error {
|
|
|
|
return nil
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
func (e *xmlEncoder) Encode(writer io.Writer, node *yaml.Node) error {
|
|
|
|
encoder := xml.NewEncoder(writer)
|
|
|
|
encoder.Indent("", e.indentString)
|
|
|
|
|
2021-12-21 04:56:08 +00:00
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
err := e.encodeTopLevelMap(encoder, node)
|
2021-12-21 05:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
case yaml.DocumentNode:
|
|
|
|
err := e.encodeComment(encoder, headAndLineComment(node))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = e.encodeTopLevelMap(encoder, unwrapDoc(node))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = e.encodeComment(encoder, footComment(node))
|
2021-12-21 05:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 04:56:08 +00:00
|
|
|
case yaml.ScalarNode:
|
|
|
|
var charData xml.CharData = []byte(node.Value)
|
2022-01-15 00:57:59 +00:00
|
|
|
err := encoder.EncodeToken(charData)
|
2021-12-21 05:52:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
return encoder.Flush()
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported type %v", node.Tag)
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
var charData xml.CharData = []byte("\n")
|
|
|
|
return encoder.EncodeToken(charData)
|
|
|
|
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yaml.Node) error {
|
|
|
|
err := e.encodeComment(encoder, headAndLineComment(node))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 05:08:37 +00:00
|
|
|
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}}
|
2022-01-15 00:57:59 +00:00
|
|
|
log.Debugf("comments of key %v", key.Value)
|
|
|
|
err := e.encodeComment(encoder, headAndLineComment(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("recursing")
|
|
|
|
|
|
|
|
err = e.doEncode(encoder, value, start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = e.encodeComment(encoder, footComment(key))
|
2021-12-21 04:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 05:08:37 +00:00
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeComment(encoder, footComment(node))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
|
|
|
err := encoder.EncodeToken(start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.encodeComment(encoder, headComment(node))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) encodeEnd(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
|
|
|
err := encoder.EncodeToken(start.End())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.encodeComment(encoder, footComment(node))
|
2021-12-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (e *xmlEncoder) doEncode(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
2021-12-21 05:08:37 +00:00
|
|
|
switch node.Kind {
|
|
|
|
case yaml.MappingNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeMap(encoder, node, start)
|
2021-12-21 04:56:08 +00:00
|
|
|
case yaml.SequenceNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeArray(encoder, node, start)
|
2021-12-21 04:56:08 +00:00
|
|
|
case yaml.ScalarNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
err := e.encodeStart(encoder, node, start)
|
2021-12-21 04:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var charData xml.CharData = []byte(node.Value)
|
2022-01-15 00:57:59 +00:00
|
|
|
err = encoder.EncodeToken(charData)
|
2021-12-21 04:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
if err = e.encodeComment(encoder, lineComment(node)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.encodeEnd(encoder, node, start)
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("unsupported type %v", node.Tag)
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error {
|
|
|
|
if commentStr != "" {
|
|
|
|
log.Debugf("encoding comment %v", commentStr)
|
|
|
|
if !strings.HasSuffix(commentStr, " ") {
|
|
|
|
commentStr = commentStr + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
var comment xml.Comment = []byte(commentStr)
|
|
|
|
err := encoder.EncodeToken(comment)
|
2021-12-21 04:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (e *xmlEncoder) encodeArray(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
|
|
|
|
|
|
|
if err := e.encodeComment(encoder, headAndLineComment(node)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(node.Content); i++ {
|
|
|
|
value := node.Content[i]
|
|
|
|
if err := e.doEncode(encoder, value, start.Copy()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e.encodeComment(encoder, footComment(node))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) encodeMap(encoder *xml.Encoder, node *yaml.Node, start xml.StartElement) error {
|
|
|
|
log.Debug("its a map")
|
2021-12-21 05:08:37 +00:00
|
|
|
|
|
|
|
//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
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
err := e.encodeStart(encoder, node, start)
|
2021-12-21 05:08:37 +00:00
|
|
|
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]
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
err := e.encodeComment(encoder, headAndLineComment(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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}}
|
2022-01-15 00:57:59 +00:00
|
|
|
err := e.doEncode(encoder, value, start)
|
2021-12-21 05:08:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 05:19:27 +00:00
|
|
|
} else if key.Value == e.contentName {
|
|
|
|
// directly encode the contents
|
2022-01-15 00:57:59 +00:00
|
|
|
err = e.encodeComment(encoder, headAndLineComment(value))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-21 05:19:27 +00:00
|
|
|
var charData xml.CharData = []byte(value.Value)
|
2022-01-15 00:57:59 +00:00
|
|
|
err = encoder.EncodeToken(charData)
|
2021-12-21 05:19:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
err = e.encodeComment(encoder, footComment(value))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = e.encodeComment(encoder, footComment(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeEnd(encoder, node, start)
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|