This repository has been archived by the owner on Aug 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
149 lines (143 loc) · 3.04 KB
/
cleaner.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
package opentrans
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"sort"
"strings"
"unicode/utf8"
)
func CleanXMLNamespaces(x []byte) ([]byte, error) {
replacer := map[string]string{
"http://www.opentrans.org/XMLSchema/2.1": "",
"http://www.bmecat.org/bmecat/2005": "bmecat",
}
d := xml.NewDecoder(bytes.NewReader(x))
var fx []byte
depth := 0
for {
t, err := d.Token()
if err == io.EOF {
return fx, nil
}
if err != nil {
return nil, err
}
switch tt := t.(type) {
case xml.StartElement:
//fmt.Println(">", tt.Name.Local, tt.Name.Space)
fx = append(fx, '<')
if v := replacer[tt.Name.Space]; v != "" {
fx = append(fx, []byte(v+":")...)
}
fx = append(fx, []byte(tt.Name.Local)...)
if depth == 0 {
attrs := make([]string, len(replacer))
keys := make([]string, len(replacer))
i := 0
for k := range replacer {
keys[i] = k
i++
}
i = 0
sort.Strings(keys)
for _, k := range keys {
attrs[i] = "xmlns"
if replacer[k] != "" {
attrs[i] += ":" + replacer[k]
}
attrs[i] += `="` + k + `"`
i++
}
fx = append(fx, []byte(" "+strings.Join(attrs, " "))...)
}
for _, v := range tt.Attr {
if v.Name.Local == "xmlns" {
continue
}
fx = append(fx, []byte(" "+fmt.Sprintf(`%s="%s"`, v.Name.Local, v.Value))...)
}
fx = append(fx, '>')
depth++
case xml.EndElement:
//fmt.Println("<", tt.Name.Local, tt.Name.Space)
fx = append(fx, '<', '/')
if v := replacer[tt.Name.Space]; v != "" {
fx = append(fx, []byte(v+":")...)
}
fx = append(fx, []byte(tt.Name.Local+">")...)
depth--
case xml.CharData:
var buff bytes.Buffer
err = escapeText(&buff, tt.Copy(), false)
if err != nil {
return nil, err
}
fx = append(fx, buff.Bytes()...)
}
}
}
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
)
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
}
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
}