-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.go
246 lines (228 loc) · 4.92 KB
/
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2018, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package svg
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"io"
"unicode/utf8"
)
// XMLEncoder is a minimal XML encoder that formats output with Attr
// each on a new line, using same API as xml.Encoder
type XMLEncoder struct {
Writer io.Writer
DoIndent bool
IndBytes []byte
PreBytes []byte
CurIndent int
CurStart string
NoEndIndent bool
}
func NewXMLEncoder(wr io.Writer) *XMLEncoder {
return &XMLEncoder{Writer: wr}
}
func (xe *XMLEncoder) Indent(prefix, indent string) {
if len(indent) > 0 {
xe.DoIndent = true
}
xe.IndBytes = []byte(indent)
xe.PreBytes = []byte(prefix)
}
func (xe *XMLEncoder) EncodeToken(t xml.Token) error {
switch t := t.(type) {
case xml.StartElement:
if err := xe.WriteStart(&t); err != nil {
return err
}
case xml.EndElement:
if err := xe.WriteEnd(t.Name.Local); err != nil {
return err
}
case xml.CharData:
if xe.CurStart != "" {
xe.WriteString(">")
xe.CurStart = ""
xe.NoEndIndent = true // don't indent the end now
}
EscapeText(xe.Writer, t, false)
}
return nil
}
func (xe *XMLEncoder) WriteString(str string) {
xe.Writer.Write([]byte(str))
}
func (xe *XMLEncoder) WriteIndent() {
xe.Writer.Write(xe.PreBytes)
xe.Writer.Write(bytes.Repeat(xe.IndBytes, xe.CurIndent))
}
func (xe *XMLEncoder) WriteEOL() {
xe.Writer.Write([]byte("\n"))
}
// Decide whether the given rune is in the XML Character Range, per
// the Char production of https://www.xml.com/axml/testaxml.htm,
// Section 2.2 Characters.
func isInCharacterRange(r rune) (inrange bool) {
return r == 0x09 ||
r == 0x0A ||
r == 0x0D ||
r >= 0x20 && r <= 0xD7FF ||
r >= 0xE000 && r <= 0xFFFD ||
r >= 0x10000 && r <= 0x10FFFF
}
var (
escQuot = []byte(""") // shorter than """
escApos = []byte("'") // shorter than "'"
escAmp = []byte("&")
escLT = []byte("<")
escGT = []byte(">")
escTab = []byte("	")
escNL = []byte("
")
escCR = []byte("
")
escFFFD = []byte("\uFFFD") // Unicode replacement character
)
// XMLEscapeText writes to w the properly escaped XML equivalent
// of the plain text data s. If escapeNewline is true, newline
// XMLcharacters will be escaped.
func EscapeText(w io.Writer, s []byte, escapeNewline bool) error {
var esc []byte
last := 0
for i := 0; i < len(s); {
r, width := utf8.DecodeRune(s[i:])
i += width
switch r {
case '"':
esc = escQuot
case '\'':
esc = escApos
case '&':
esc = escAmp
case '<':
esc = escLT
case '>':
esc = escGT
case '\t':
esc = escTab
case '\n':
if !escapeNewline {
continue
}
esc = escNL
case '\r':
esc = escCR
default:
if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
esc = escFFFD
break
}
continue
}
if _, err := w.Write(s[last : i-width]); err != nil {
return err
}
if _, err := w.Write(esc); err != nil {
return err
}
last = i
}
_, err := w.Write(s[last:])
return err
}
// EscapeString writes to p the properly escaped XML equivalent
// of the plain text data s.
func (xe *XMLEncoder) EscapeString(s string, escapeNewline bool) {
var esc []byte
last := 0
for i := 0; i < len(s); {
r, width := utf8.DecodeRuneInString(s[i:])
i += width
switch r {
case '"':
esc = escQuot
case '\'':
esc = escApos
case '&':
esc = escAmp
case '<':
esc = escLT
case '>':
esc = escGT
case '\t':
esc = escTab
case '\n':
if !escapeNewline {
continue
}
esc = escNL
case '\r':
esc = escCR
default:
if !isInCharacterRange(r) || (r == 0xFFFD && width == 1) {
esc = escFFFD
break
}
continue
}
xe.WriteString(s[last : i-width])
xe.Writer.Write(esc)
last = i
}
xe.WriteString(s[last:])
}
func (xe *XMLEncoder) WriteStart(start *xml.StartElement) error {
if start.Name.Local == "" {
return fmt.Errorf("xml: start tag with no name")
}
if xe.CurStart != "" {
xe.WriteString(">")
xe.WriteEOL()
}
xe.WriteIndent()
xe.WriteString("<")
xe.WriteString(start.Name.Local)
xe.CurIndent++
xe.CurStart = start.Name.Local
// Attributes
for _, attr := range start.Attr {
name := attr.Name
if name.Local == "" {
continue
}
xe.WriteEOL()
xe.WriteIndent()
xe.WriteString(name.Local)
xe.WriteString(`="`)
xe.EscapeString(attr.Value, false)
xe.WriteString(`"`)
}
return nil
}
func (xe *XMLEncoder) WriteEnd(name string) error {
xe.CurIndent--
if name == "" {
return fmt.Errorf("xml: end tag with no name")
}
if xe.CurStart == name {
xe.WriteString(" />")
xe.WriteEOL()
} else {
if !xe.NoEndIndent {
xe.WriteIndent()
}
xe.NoEndIndent = false
xe.WriteString("</")
xe.WriteString(name)
xe.WriteString(">")
xe.WriteEOL()
}
xe.CurStart = ""
xe.Flush()
return nil
}
func (xe *XMLEncoder) Flush() {
if bw, isb := xe.Writer.(*bufio.Writer); isb {
bw.Flush()
}
}