-
Notifications
You must be signed in to change notification settings - Fork 14
/
query.go
179 lines (147 loc) · 3.47 KB
/
query.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
package xmlparser
import (
"github.com/tamerh/xpath"
)
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
func (x *XMLParser) CreateXPathNavigator(top *XMLElement) *XmlNodeNavigator {
return &XmlNodeNavigator{curr: top, root: top, attr: -1}
}
// Compile the given xpath expression
func (x *XMLParser) CompileXpath(expr string) (*xpath.Expr, error) {
exp, err := xpath.Compile(expr)
if err != nil {
return nil, err
}
return exp, nil
}
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
func createXPathNavigator(top *XMLElement) *XmlNodeNavigator {
return &XmlNodeNavigator{curr: top, root: top, attr: -1}
}
type XmlNodeNavigator struct {
root, curr *XMLElement
attr int
}
// Find searches the Node that matches by the specified XPath expr.
func find(top *XMLElement, expr string) ([]*XMLElement, error) {
exp, err := xpath.Compile(expr)
if err != nil {
return []*XMLElement{}, err
}
t := exp.Select(createXPathNavigator(top))
var elems []*XMLElement
for t.MoveNext() {
elems = append(elems, t.Current().(*XmlNodeNavigator).curr)
}
return elems, nil
}
// FindOne searches the Node that matches by the specified XPath expr,
// and returns first element of matched.
func findOne(top *XMLElement, expr string) (*XMLElement, error) {
exp, err := xpath.Compile(expr)
if err != nil {
return nil, err
}
t := exp.Select(createXPathNavigator(top))
var elem *XMLElement
if t.MoveNext() {
elem = t.Current().(*XmlNodeNavigator).curr //getCurrentNode(t)
}
return elem, nil
}
func (x *XmlNodeNavigator) Current() *XMLElement {
return x.curr
}
func (x *XmlNodeNavigator) NodeType() xpath.NodeType {
if x.curr == x.root {
return xpath.RootNode
}
if x.attr != -1 {
return xpath.AttributeNode
}
return xpath.ElementNode
}
func (x *XmlNodeNavigator) LocalName() string {
if x.attr != -1 {
return x.curr.attrs[x.attr].name
}
return x.curr.localName
}
func (x *XmlNodeNavigator) Prefix() string {
return x.curr.prefix
}
func (x *XmlNodeNavigator) Value() string {
if x.attr != -1 {
return x.curr.attrs[x.attr].value
}
return x.curr.InnerText
}
func (x *XmlNodeNavigator) Copy() xpath.NodeNavigator {
n := *x
return &n
}
func (x *XmlNodeNavigator) MoveToRoot() {
x.curr = x.root
}
func (x *XmlNodeNavigator) MoveToParent() bool {
if x.attr != -1 {
x.attr = -1
return true
} else if node := x.curr.parent; node != nil {
x.curr = node
return true
}
return false
}
func (x *XmlNodeNavigator) MoveToNextAttribute() bool {
if x.attr >= len(x.curr.attrs)-1 {
return false
}
x.attr++
return true
}
func (x *XmlNodeNavigator) MoveToChild() bool {
if node := x.curr.FirstChild(); node != nil {
x.curr = node
return true
}
return false
}
func (x *XmlNodeNavigator) MoveToFirst() bool {
if x.curr.parent != nil {
node := x.curr.parent.FirstChild()
if node != nil {
x.curr = node
return true
}
}
return false
}
func (x *XmlNodeNavigator) MoveToPrevious() bool {
node := x.curr.PrevSibling()
if node != nil {
x.curr = node
return true
}
return false
}
func (x *XmlNodeNavigator) MoveToNext() bool {
node := x.curr.NextSibling()
if node != nil {
x.curr = node
return true
}
return false
}
func (x *XmlNodeNavigator) String() string {
return x.Value()
}
func (x *XmlNodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
node, ok := other.(*XmlNodeNavigator)
if !ok || node.root != x.root {
return false
}
x.curr = node.curr
x.attr = node.attr
return true
}