-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmlquery_helper.go
181 lines (158 loc) · 3.56 KB
/
xmlquery_helper.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bufio"
"fmt"
"html"
"io"
"strings"
"github.com/antchfx/xmlquery"
)
type OutputConfig struct {
EmptyElement bool
Indent string
}
func OutputXML(out io.Writer, n *xmlquery.Node, config OutputConfig) {
b := bufio.NewWriter(out)
level := 0
if n.Type == xmlquery.DocumentNode {
curr := n.FirstChild
for curr != nil {
outputXML(b, curr, level, config)
curr = curr.NextSibling
}
} else {
outputXML(b, n, level, config)
}
b.Flush()
}
func AddNode(n, newnode *xmlquery.Node, sibling bool) {
if !sibling {
xmlquery.AddChild(n, newnode)
return
}
parent := n.Parent
if n == parent.FirstChild {
if nnext := n.NextSibling; nnext != nil {
newnode.NextSibling = nnext
nnext.PrevSibling = newnode
} else {
parent.LastChild = newnode
}
} else if n == parent.LastChild {
parent.LastChild = newnode
} else {
nnext := n.NextSibling
newnode.NextSibling = nnext
nnext.PrevSibling = newnode
}
newnode.PrevSibling = n
n.NextSibling = newnode
newnode.Parent = parent
}
func outputXML(b *bufio.Writer, n *xmlquery.Node, level int, config OutputConfig) {
if n.Type == xmlquery.TextNode && strings.TrimSpace(n.Data) == "" {
return
}
styling := config.Indent != ""
if styling && !isOnelineText(n) {
b.WriteString(strings.Repeat(config.Indent, level))
}
switch n.Type {
case xmlquery.TextNode:
text := strings.TrimSpace(n.Data)
if text != "" {
b.WriteString(html.EscapeString(text))
if !isOnelineText(n) {
writeStylingNewLine(b, styling)
}
}
return
case xmlquery.CharDataNode:
b.WriteString("<![CDATA[")
b.WriteString(n.Data)
b.WriteString("]]>")
writeStylingNewLine(b, styling)
return
case xmlquery.CommentNode:
b.WriteString("<!--")
b.WriteString(n.Data)
b.WriteString("-->")
writeStylingNewLine(b, styling)
return
case xmlquery.NotationNode:
fmt.Fprintf(b, "<!%s>", n.Data)
writeStylingNewLine(b, styling)
return
case xmlquery.DeclarationNode:
b.WriteString("<?" + n.Data)
default:
b.WriteByte('<')
writeName(b, n.Prefix, n.Data)
}
for _, attr := range n.Attr {
b.WriteByte(' ')
writeName(b, attr.Name.Space, attr.Name.Local)
b.WriteByte('=')
b.WriteByte('"')
b.WriteString(html.EscapeString(attr.Value))
b.WriteByte('"')
}
if n.Type == xmlquery.DeclarationNode {
b.WriteString("?>")
writeStylingNewLine(b, styling)
return
}
if n.FirstChild == nil && config.EmptyElement {
b.WriteString("/>")
writeStylingNewLine(b, styling)
return
}
b.WriteString(">")
if styling {
newline := false
curr := n.FirstChild
for curr != nil {
if !isOnelineText(curr) {
newline = true
break
}
curr = curr.NextSibling
}
if newline {
b.WriteByte('\n')
}
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
outputXML(b, child, level+1, config)
}
if n.Type != xmlquery.DeclarationNode {
if styling && !isOnelineText(n.FirstChild) {
b.WriteString(strings.Repeat(config.Indent, level))
}
b.WriteString("</")
writeName(b, n.Prefix, n.Data)
b.WriteByte('>')
}
writeStylingNewLine(b, styling)
}
func isOnelineText(n *xmlquery.Node) bool {
return n == nil ||
n.Type == xmlquery.TextNode &&
strings.IndexByte(strings.TrimSpace(n.Data), '\n') == -1 &&
n.NextSibling == nil &&
n.PrevSibling == nil
}
func writeName(b *bufio.Writer, space, name string) (int, error) {
if space == "" {
return b.WriteString(name)
}
b.WriteString(space)
b.WriteByte(':')
return b.WriteString(name)
}
func writeStylingNewLine(b *bufio.Writer, styling bool) error {
if !styling {
return nil
}
return b.WriteByte('\n')
}