2023-03-01 02:19:06 +00:00
|
|
|
//go:build !yq_noxml
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
package xml
|
2021-12-21 04:56:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-28 03:16:46 +00:00
|
|
|
"regexp"
|
2021-12-21 05:08:37 +00:00
|
|
|
"strings"
|
2024-03-12 04:45:08 +00:00
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/pkg/yqlib"
|
2021-12-21 04:56:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type xmlEncoder struct {
|
2022-10-28 03:16:46 +00:00
|
|
|
indentString string
|
|
|
|
writer io.Writer
|
|
|
|
prefs XmlPreferences
|
|
|
|
leadingContent string
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func NewXMLEncoder(prefs XmlPreferences) yqlib.Encoder {
|
2021-12-21 04:56:08 +00:00
|
|
|
var indentString = ""
|
|
|
|
|
2024-02-24 04:03:30 +00:00
|
|
|
for index := 0; index < prefs.Indent; index++ {
|
2021-12-21 04:56:08 +00:00
|
|
|
indentString = indentString + " "
|
|
|
|
}
|
2022-10-28 03:16:46 +00:00
|
|
|
return &xmlEncoder{indentString, nil, prefs, ""}
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *xmlEncoder) CanHandleAliases() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (e *xmlEncoder) PrintDocumentSeparator(_ io.Writer) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (e *xmlEncoder) PrintLeadingContent(_ io.Writer, content string) error {
|
2023-03-01 23:57:54 +00:00
|
|
|
e.leadingContent = content
|
2022-01-15 00:57:59 +00:00
|
|
|
return nil
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) Encode(writer io.Writer, node *yqlib.CandidateNode) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder := xml.NewEncoder(writer)
|
2022-10-23 23:09:42 +00:00
|
|
|
// hack so we can manually add newlines to procInst and directives
|
|
|
|
e.writer = writer
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder.Indent("", e.indentString)
|
2023-03-01 23:57:54 +00:00
|
|
|
var newLine xml.CharData = []byte("\n")
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if node.Tag == "!!map" {
|
2023-03-01 23:57:54 +00:00
|
|
|
// make sure <?xml .. ?> processing instructions are encoded first
|
2023-10-18 01:11:53 +00:00
|
|
|
for i := 0; i < len(node.Content); i += 2 {
|
|
|
|
key := node.Content[i]
|
|
|
|
value := node.Content[i+1]
|
2023-03-01 23:57:54 +00:00
|
|
|
|
|
|
|
if key.Value == (e.prefs.ProcInstPrefix + "xml") {
|
|
|
|
name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1)
|
|
|
|
procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)}
|
|
|
|
if err := encoder.EncodeToken(procInst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := e.writer.Write([]byte("\n")); err != nil {
|
|
|
|
log.Warning("Unable to write newline, skipping: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
if e.leadingContent != "" {
|
2023-03-01 23:57:54 +00:00
|
|
|
|
|
|
|
// remove first and last newlines if present
|
2022-10-28 03:16:46 +00:00
|
|
|
err := e.encodeComment(encoder, e.leadingContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-01 23:57:54 +00:00
|
|
|
err = encoder.EncodeToken(newLine)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-28 03:16:46 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 04:56:08 +00:00
|
|
|
switch node.Kind {
|
2024-03-12 04:45:08 +00:00
|
|
|
case yqlib.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
|
|
|
|
}
|
2024-03-12 04:45:08 +00:00
|
|
|
case yqlib.ScalarNode:
|
2021-12-21 04:56:08 +00:00
|
|
|
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:
|
2023-10-18 01:11:53 +00:00
|
|
|
return fmt.Errorf("cannot encode %v to XML - only maps can be encoded", node.Tag)
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
2023-03-01 23:57:54 +00:00
|
|
|
|
|
|
|
return encoder.EncodeToken(newLine)
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2021-12-21 04:56:08 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) encodeTopLevelMap(encoder *xml.Encoder, node *yqlib.CandidateNode) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
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
|
|
|
|
}
|
2023-03-01 23:57:54 +00:00
|
|
|
if headAndLineComment(key) != "" {
|
|
|
|
var newLine xml.CharData = []byte("\n")
|
|
|
|
err = encoder.EncodeToken(newLine)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
if key.Value == (e.prefs.ProcInstPrefix + "xml") { //nolint
|
2022-10-23 23:09:42 +00:00
|
|
|
// dont double process these.
|
|
|
|
} else if strings.HasPrefix(key.Value, e.prefs.ProcInstPrefix) {
|
|
|
|
name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1)
|
|
|
|
procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)}
|
|
|
|
if err := encoder.EncodeToken(procInst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := e.writer.Write([]byte("\n")); err != nil {
|
|
|
|
log.Warning("Unable to write newline, skipping: %w", err)
|
|
|
|
}
|
|
|
|
} else if key.Value == e.prefs.DirectiveName {
|
|
|
|
var directive xml.Directive = []byte(value.Value)
|
|
|
|
if err := encoder.EncodeToken(directive); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := e.writer.Write([]byte("\n")); err != nil {
|
|
|
|
log.Warning("Unable to write newline, skipping: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
2022-01-15 00:57:59 +00:00
|
|
|
|
2022-10-23 23:09:42 +00:00
|
|
|
log.Debugf("recursing")
|
|
|
|
|
|
|
|
err = e.doEncode(encoder, value, start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) encodeStart(encoder *xml.Encoder, node *yqlib.CandidateNode, start xml.StartElement) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
err := encoder.EncodeToken(start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.encodeComment(encoder, headComment(node))
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) encodeEnd(encoder *xml.Encoder, node *yqlib.CandidateNode, start xml.StartElement) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
err := encoder.EncodeToken(start.End())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return e.encodeComment(encoder, footComment(node))
|
2021-12-21 05:08:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) doEncode(encoder *xml.Encoder, node *yqlib.CandidateNode, start xml.StartElement) error {
|
2021-12-21 05:08:37 +00:00
|
|
|
switch node.Kind {
|
2023-10-18 01:11:53 +00:00
|
|
|
case MappingNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeMap(encoder, node, start)
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
2022-01-15 00:57:59 +00:00
|
|
|
return e.encodeArray(encoder, node, start)
|
2023-10-18 01:11:53 +00:00
|
|
|
case 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)
|
|
|
|
}
|
|
|
|
|
2023-03-01 23:57:54 +00:00
|
|
|
var xmlEncodeMultilineCommentRegex = regexp.MustCompile(`(^|\n) *# ?(.*)`)
|
|
|
|
var xmlEncodeSingleLineCommentRegex = regexp.MustCompile(`^\s*#(.*)\n?`)
|
|
|
|
var chompRegexp = regexp.MustCompile(`\n$`)
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func (e *xmlEncoder) encodeComment(encoder *xml.Encoder, commentStr string) error {
|
|
|
|
if commentStr != "" {
|
2023-03-01 23:57:54 +00:00
|
|
|
log.Debugf("got comment [%v]", commentStr)
|
|
|
|
// multi line string
|
|
|
|
if len(commentStr) > 2 && strings.Contains(commentStr[1:len(commentStr)-1], "\n") {
|
|
|
|
commentStr = chompRegexp.ReplaceAllString(commentStr, "")
|
|
|
|
log.Debugf("chompRegexp [%v]", commentStr)
|
|
|
|
commentStr = xmlEncodeMultilineCommentRegex.ReplaceAllString(commentStr, "$1$2")
|
2023-09-26 04:43:08 +00:00
|
|
|
log.Debugf("processed multiline [%v]", commentStr)
|
2023-03-01 23:57:54 +00:00
|
|
|
// if the first line is non blank, add a space
|
|
|
|
if commentStr[0] != '\n' && commentStr[0] != ' ' {
|
|
|
|
commentStr = " " + commentStr
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
commentStr = xmlEncodeSingleLineCommentRegex.ReplaceAllString(commentStr, "$1")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(commentStr, " ") && !strings.HasSuffix(commentStr, "\n") {
|
2022-01-15 00:57:59 +00:00
|
|
|
commentStr = commentStr + " "
|
2023-03-01 23:57:54 +00:00
|
|
|
log.Debugf("added suffix [%v]", commentStr)
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
2023-03-01 23:57:54 +00:00
|
|
|
log.Debugf("encoding comment [%v]", commentStr)
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) encodeArray(encoder *xml.Encoder, node *yqlib.CandidateNode, start xml.StartElement) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-10-23 23:09:42 +00:00
|
|
|
func (e *xmlEncoder) isAttribute(name string) bool {
|
|
|
|
return strings.HasPrefix(name, e.prefs.AttributePrefix) &&
|
|
|
|
name != e.prefs.ContentName &&
|
|
|
|
name != e.prefs.DirectiveName &&
|
|
|
|
!strings.HasPrefix(name, e.prefs.ProcInstPrefix)
|
|
|
|
}
|
|
|
|
|
2024-03-12 04:45:08 +00:00
|
|
|
func (e *xmlEncoder) encodeMap(encoder *xml.Encoder, node *yqlib.CandidateNode, start xml.StartElement) error {
|
2022-01-15 00:57:59 +00:00
|
|
|
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]
|
|
|
|
|
2022-10-23 23:09:42 +00:00
|
|
|
if e.isAttribute(key.Value) {
|
2023-10-18 01:11:53 +00:00
|
|
|
if value.Kind == ScalarNode {
|
2022-10-23 23:09:42 +00:00
|
|
|
attributeName := strings.Replace(key.Value, e.prefs.AttributePrefix, "", 1)
|
2021-12-21 05:08:37 +00:00
|
|
|
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
|
|
|
|
}
|
2022-10-23 23:09:42 +00:00
|
|
|
if strings.HasPrefix(key.Value, e.prefs.ProcInstPrefix) {
|
|
|
|
name := strings.Replace(key.Value, e.prefs.ProcInstPrefix, "", 1)
|
|
|
|
procInst := xml.ProcInst{Target: name, Inst: []byte(value.Value)}
|
|
|
|
if err := encoder.EncodeToken(procInst); err != nil {
|
2021-12-21 05:08:37 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-23 23:09:42 +00:00
|
|
|
} else if key.Value == e.prefs.DirectiveName {
|
|
|
|
var directive xml.Directive = []byte(value.Value)
|
|
|
|
if err := encoder.EncodeToken(directive); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if key.Value == e.prefs.ContentName {
|
2021-12-21 05:19:27 +00:00
|
|
|
// 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
|
|
|
|
}
|
2022-10-23 23:09:42 +00:00
|
|
|
} else if !e.isAttribute(key.Value) {
|
|
|
|
start := xml.StartElement{Name: xml.Name{Local: key.Value}}
|
|
|
|
err := e.doEncode(encoder, value, start)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|