yq/pkg/xml/xml.go

73 lines
2.0 KiB
Go
Raw Normal View History

2024-03-12 04:45:08 +00:00
package xml
import "github.com/mikefarah/yq/v4/pkg/yqlib"
2022-10-25 03:27:16 +00:00
type XmlPreferences struct {
2024-02-24 04:03:30 +00:00
Indent int
2022-10-25 03:27:16 +00:00
AttributePrefix string
ContentName string
StrictMode bool
KeepNamespace bool
UseRawToken bool
ProcInstPrefix string
DirectiveName string
SkipProcInst bool
SkipDirectives bool
}
func NewDefaultXmlPreferences() XmlPreferences {
return XmlPreferences{
2024-02-24 04:03:30 +00:00
Indent: 2,
2022-11-10 11:22:55 +00:00
AttributePrefix: "+@",
2022-10-25 03:27:16 +00:00
ContentName: "+content",
StrictMode: false,
KeepNamespace: true,
UseRawToken: true,
2022-10-25 03:27:16 +00:00
ProcInstPrefix: "+p_",
DirectiveName: "+directive",
SkipProcInst: false,
SkipDirectives: false,
}
}
2024-02-24 04:03:30 +00:00
func (p *XmlPreferences) Copy() XmlPreferences {
return XmlPreferences{
Indent: p.Indent,
AttributePrefix: p.AttributePrefix,
ContentName: p.ContentName,
StrictMode: p.StrictMode,
KeepNamespace: p.KeepNamespace,
UseRawToken: p.UseRawToken,
ProcInstPrefix: p.ProcInstPrefix,
DirectiveName: p.DirectiveName,
SkipProcInst: p.SkipProcInst,
SkipDirectives: p.SkipDirectives,
}
}
2022-10-25 03:27:16 +00:00
var ConfiguredXMLPreferences = NewDefaultXmlPreferences()
2024-03-12 04:45:08 +00:00
2024-03-12 05:43:53 +00:00
var XMLFormat = &yqlib.Format{"xml", []string{"x"}, "xml",
2024-03-12 04:45:08 +00:00
func() yqlib.Encoder { return NewXMLEncoder(ConfiguredXMLPreferences) },
func() yqlib.Decoder { return NewXMLDecoder(ConfiguredXMLPreferences) },
2024-03-12 05:43:53 +00:00
func(indent int) yqlib.Encoder {
prefs := ConfiguredXMLPreferences.Copy()
prefs.Indent = indent
return NewXMLEncoder(prefs)
},
2024-03-12 04:45:08 +00:00
}
var xmlYqRules = []*yqlib.ParticipleYqRule{
2024-03-12 05:43:53 +00:00
{"XMLEncodeWithIndent", `to_?xml\([0-9]+\)`, yqlib.CreateEncodeYqActionParsingIndent(XMLFormat), 0},
{"XmlDecode", `from_?xml|@xmld`, yqlib.CreateDecodeOpYqAction(XMLFormat), 0},
{"XMLEncode", `to_?xml`, yqlib.CreateEncodeOpYqAction(XMLFormat, 2), 0},
{"XMLEncodeNoIndent", `@xml`, yqlib.CreateEncodeOpYqAction(XMLFormat, 0), 0},
{"LoadXML", `load_?xml|xml_?load`, yqlib.CreateLoadOpYqAction(NewXMLDecoder(ConfiguredXMLPreferences)), 0},
2024-03-12 04:45:08 +00:00
}
func RegisterXmlFormat() {
yqlib.RegisterFormat(XMLFormat)
yqlib.RegisterRules(xmlYqRules)
}