Skip to content

Commit 6ad00b2

Browse files
committed
Update and move extractor utilities
1 parent be72260 commit 6ad00b2

File tree

1 file changed

+71
-13
lines changed

1 file changed

+71
-13
lines changed

src/xmlutils/extractor_utils.jl renamed to src/xmlutils/findnode.jl

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
1-
export findalllocal, findfirstlocal, findtext, findtextloacl, findvecttextlocal
21
################################################################
3-
# Searchers Utils
2+
# Utilities
3+
################################################################
4+
function Base.get(node::Node, name::String, defval)
5+
if haskey(node, name)
6+
return node[name]
7+
else
8+
return defval
9+
end
10+
end
11+
################################################################
12+
13+
"""
14+
findfirstatt(name, node)
15+
16+
Finds the first attribute with `name`
17+
18+
findfirst with ignoring namespaces. It considers att.name for returning the elements
19+
20+
Much faster than EzXML.findfirst
21+
"""
22+
function findfirstatt(name::String, node::Node)
23+
out = nothing # return nothing if nothing is found
24+
for att in eachattribute(node)
25+
if att.name == name
26+
out = att
27+
break
28+
end
29+
end
30+
return out
31+
end
32+
33+
"""
34+
findallatt(string, node)
35+
36+
Finds all the attributes with `name`
37+
38+
findallatt with ignoring namespaces. It considers att.name for returning the elements
39+
40+
Much faster than EzXML.findall
41+
"""
42+
function findallatt(name::String, node::Node)
43+
out = Node[]
44+
for att in eachattribute(node)
45+
if att.name == name
46+
push!(out, att)
47+
end
48+
end
49+
if !isempty(out)
50+
return out
51+
else # return nothing if nothing is found
52+
return nothing
53+
end
54+
end
55+
56+
457
################################################################
558
# Local searchers (no namespace)
659
"""
7-
findfirstlocal(string, node)
60+
findfirstelm(string, node)
861
962
findfirst with ignoring namespaces. It considers element.name for returning the elements
1063
1164
Much faster than EzXML.findfirst
1265
"""
13-
function findfirstlocal(name::String, node::Node)
66+
function findfirstelm(name::String, node::Node)
1467
out = nothing # return nothing if nothing is found
1568
for child in eachelement(node)
1669
if child.name == name
@@ -22,13 +75,13 @@ function findfirstlocal(name::String, node::Node)
2275
end
2376

2477
"""
25-
findalllocal(string, node)
78+
findallelm(string, node)
2679
27-
findalllocal with ignoring namespaces. It considers element.name for returning the elements
80+
findallelm with ignoring namespaces. It considers element.name for returning the elements
2881
2982
Much faster than EzXML.findall
3083
"""
31-
function findalllocal(name::String, node::Node)
84+
function findallelm(name::String, node::Node)
3285
out = Node[]
3386
for child in eachelement(node)
3487
if child.name == name
@@ -42,6 +95,9 @@ function findalllocal(name::String, node::Node)
4295
end
4396
end
4497

98+
################################################################
99+
# Text node utils
100+
45101
function findtext(indexstr::String, node::Node)
46102
if indexstr == ""
47103
index = 1
@@ -83,13 +139,13 @@ function parse_textindex(indexstr::String)
83139
end
84140

85141
"""
86-
findtextlocal(index::Integer, node)
142+
findfirsttext(index::Integer, node)
87143
88144
finds the text node at position given by index.
89145
90146
faster than `findtext()`
91147
"""
92-
function findtextlocal(index::Integer, node::Node)
148+
function findfirsttext(index::Integer, node::Node)
93149
iText = 0
94150
out = nothing # return nothing if nothing is found
95151
for child in eachnode(node)
@@ -103,7 +159,7 @@ function findtextlocal(index::Integer, node::Node)
103159
end
104160
return out
105161
end
106-
function findtextlocal(index::Float64, node::Node)
162+
function findfirsttext(index::Float64, node::Node)
107163
if index != Inf
108164
error("index should be \"end\"")
109165
end
@@ -133,13 +189,13 @@ function parse_textindices(indicesstr::String)
133189
end
134190

135191
"""
136-
findvecttextlocal(indices, node)
192+
findalltext(indices, node)
137193
138194
finds the text node at positions given by indices.
139195
140196
faster than `findvecttext()`
141197
"""
142-
function findvecttextlocal(indices::Colon, node::Node)
198+
function findalltext(indices::Colon, node::Node)
143199
out = Node[]
144200
for child in eachnode(node)
145201
if istext(child)
@@ -153,7 +209,7 @@ function findvecttextlocal(indices::Colon, node::Node)
153209
end
154210
end
155211

156-
function findvecttextlocal(indices::AbstractVector, node::Node)
212+
function findalltext(indices::AbstractVector, node::Node)
157213
out = Node[]
158214
iText = 0
159215
for child in eachnode(node)
@@ -170,3 +226,5 @@ function findvecttextlocal(indices::AbstractVector, node::Node)
170226
return nothing
171227
end
172228
end
229+
230+
################################################################

0 commit comments

Comments
 (0)