-
Notifications
You must be signed in to change notification settings - Fork 0
/
msbuild_xml.go
58 lines (51 loc) · 1.52 KB
/
msbuild_xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"encoding/xml"
)
// MSBuildXMLAttribute represents a xml attribute for Visual Studio projects.
type MSBuildXMLAttribute struct {
Key string
Value string
}
func xmlAttr(key, value string) MSBuildXMLAttribute {
return MSBuildXMLAttribute{Key: key, Value: value}
}
// MSBuildXMLElement represents a xml element for Visual Studio projects.
type MSBuildXMLElement struct {
Name string
Text string
Attributes []MSBuildXMLAttribute
Elements []*MSBuildXMLElement
}
// SubElement adds a new element to the sub elements.
func (elem *MSBuildXMLElement) SubElement(name string, attributes ...MSBuildXMLAttribute) *MSBuildXMLElement {
sub := &MSBuildXMLElement{
Name: name,
Attributes: attributes,
}
elem.Elements = append(elem.Elements, sub)
return sub
}
// SetText sets the text of a element.
func (elem *MSBuildXMLElement) SetText(text string) *MSBuildXMLElement {
elem.Text = text
return elem
}
// MarshalXML encodes the receiver as a XML element.
func (elem *MSBuildXMLElement) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = elem.Name
start.Attr = []xml.Attr{}
for _, attr := range elem.Attributes {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: attr.Key}, Value: attr.Value})
}
e.EncodeToken(start)
if len(elem.Text) > 0 {
e.EncodeToken(xml.CharData(elem.Text))
} else {
for _, sub := range elem.Elements {
e.EncodeElement(sub, xml.StartElement{Name: xml.Name{Local: sub.Name}})
}
}
e.EncodeToken(start.End())
return nil
}