-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxron.go
95 lines (88 loc) · 2.04 KB
/
xron.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
package xron
import (
"encoding/xml"
"fmt"
"io"
"strings"
)
func ConvertXmlToXpath(r io.Reader) (rows []string) {
ConvertXmlToXpathFunc(r, func(row string) {
rows = append(rows, row)
})
return
}
func ConvertXmlToXpathFunc(r io.Reader, emitRow func(string)) {
d := xml.NewDecoder(r)
// stack of names of surrounding XML elements
stack := []string{}
// inText is non-empty when merging consecutive text/CDATA
inText := []string{}
// FIXME: should only happen if really has any root, probably?
emitRow("/")
for {
t, err := d.Token()
// Note: err handling is further down
if len(inText) != 0 {
shouldEmit := true
switch t.(type) {
case xml.ProcInst, xml.Directive, xml.Comment,
xml.CharData:
shouldEmit = false
}
if shouldEmit {
text := strings.Join(inText, "")
// TODO: modify tests to not mandate single quotes
text = fmt.Sprintf("/text() = '%s'", text)
prefix := ""
if len(stack) > 0 {
prefix = "/" + strings.Join(stack, "/")
}
emitRow(prefix + text)
inText = nil
}
}
if err == io.EOF {
return
} else if err != nil {
panic(err) // TODO[LATER]: allow returning errors
}
switch t := t.(type) {
case xml.StartElement:
attrs := []string{}
for _, a := range t.Attr {
attrs = append(attrs, fmt.Sprintf(`[@%s=%q]`, a.Name.Local, a.Value))
}
stack = append(stack, t.Name.Local+strings.Join(attrs, ""))
emitRow("/" + strings.Join(stack, "/"))
case xml.EndElement:
stack = stack[:len(stack)-1]
case xml.CharData:
text := trimSpace(t)
if len(text) == 0 {
// FIXME[LATER]: handle properly if inText
continue
}
text = fmt.Sprintf("%q", text)
text = text[1 : len(text)-1]
inText = append(inText, text)
case xml.ProcInst, xml.Directive, xml.Comment:
// ignore
}
}
}
func trimSpace(c xml.CharData) string {
const cutset = " \n"
s := strings.TrimLeft(string(c), cutset)
if len(s) != len(c) {
s = " " + s
}
n := len(s)
s = strings.TrimRight(s, cutset)
if len(s) != n {
s = s + " "
}
if s == " " {
return ""
}
return s
}