-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
210 lines (189 loc) · 4.64 KB
/
node.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
package cborquery
import (
"bytes"
"fmt"
"io"
"net/http"
"sort"
"strings"
"github.com/fxamacker/cbor/v2"
)
type keyValue struct {
key string
value interface{}
}
// A NodeType is the type of a Node.
type NodeType uint
const (
// DocumentNode is a document object that, as the root of the document tree,
// provides access to the entire XML document.
DocumentNode NodeType = iota
// ElementNode is an element.
ElementNode
// TextNode is the text content of a node.
TextNode
)
// A Node consists of a NodeType and some Name (tag name for
// element nodes, content for text) and are part of a tree of Nodes.
type Node struct {
Parent, PrevSibling, NextSibling, FirstChild, LastChild *Node
Type NodeType
Name string
value interface{}
level int
}
// Gets the object value.
func (n *Node) Value() interface{} {
return n.value
}
// ChildNodes gets all child nodes of the node.
func (n *Node) ChildNodes() []*Node {
var a []*Node
for nn := n.FirstChild; nn != nil; nn = nn.NextSibling {
a = append(a, nn)
}
return a
}
// InnerText will gets the value of the node and all its child nodes.
//
// Deprecated: Use Value() to get object value.
func (n *Node) InnerText() string {
var output func(*strings.Builder, *Node)
output = func(b *strings.Builder, n *Node) {
if n.Type == TextNode {
b.WriteString(fmt.Sprintf("%v", n.value))
return
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
output(b, child)
}
}
var b strings.Builder
output(&b, n)
return b.String()
}
func outputXML(buf *bytes.Buffer, n *Node) {
if n.Type == TextNode {
buf.WriteString(fmt.Sprintf("%v", n.value))
return
}
buf.WriteString("<" + n.Name + ">")
for child := n.FirstChild; child != nil; child = child.NextSibling {
outputXML(buf, child)
}
buf.WriteString("</" + n.Name + ">")
}
// OutputXML prints the XML string.
func (n *Node) OutputXML() string {
var buf bytes.Buffer
buf.WriteString(`<?xml version="1.0"?>`)
buf.WriteString("<root>")
for n := n.FirstChild; n != nil; n = n.NextSibling {
outputXML(&buf, n)
}
buf.WriteString("</root>")
return buf.String()
}
// SelectElement finds the first of child elements with the specified name.
func (n *Node) SelectElement(name string) *Node {
for nn := n.FirstChild; nn != nil; nn = nn.NextSibling {
if nn.Name == name {
return nn
}
}
return nil
}
// LoadURL loads the document from the specified URL.
func LoadURL(url string) (*Node, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return Parse(resp.Body)
}
// Parse CBOR document.
func Parse(r io.Reader) (*Node, error) {
buf, err := io.ReadAll(r)
if err != nil {
return nil, err
}
var msg interface{}
if err := cbor.Unmarshal(buf, &msg); err != nil {
return nil, err
}
doc := &Node{Type: DocumentNode}
err = parseValue(doc, msg, "", 1)
return doc, err
}
func parseValue(parent *Node, msg interface{}, parentName string, level int) error {
if parentName == "" {
parentName = "element"
}
switch v := msg.(type) {
case []interface{}:
// Array
for _, vv := range v {
n := &Node{Name: parentName, Type: ElementNode, level: level, value: vv}
addNode(parent, n)
if err := parseValue(n, vv, "", level+1); err != nil {
return err
}
}
case map[interface{}]interface{}:
// Object
// Sort the elements by name as the map access is randomized and this
// will break at least tests
elements := make([]keyValue, 0, len(v))
for k, val := range v {
key, err := toNodeName(k)
if err != nil {
return fmt.Errorf("object key %v: %w", k, err)
}
elements = append(elements, keyValue{key, val})
}
sort.Slice(elements, func(i, j int) bool {
return elements[i].key < elements[j].key
})
for _, el := range elements {
key, val := el.key, el.value
if _, isArray := val.([]interface{}); isArray {
if err := parseValue(parent, val, key, level+1); err != nil {
return err
}
} else {
n := &Node{Name: key, Type: ElementNode, level: level, value: val}
if err := parseValue(n, val, parent.Name, level+1); err != nil {
return err
}
addNode(parent, n)
}
}
default:
// Elementary types
n := &Node{Name: fmt.Sprintf("%v", v), Type: TextNode, level: level, value: v}
addNode(parent, n)
}
return nil
}
func addNode(top, n *Node) {
if n.level == top.level {
top.NextSibling = n
n.PrevSibling = top
n.Parent = top.Parent
if top.Parent != nil {
top.Parent.LastChild = n
}
} else if n.level > top.level {
n.Parent = top
if top.FirstChild == nil {
top.FirstChild = n
top.LastChild = n
} else {
t := top.LastChild
t.NextSibling = n
n.PrevSibling = t
top.LastChild = n
}
}
}