Skip to content

Commit b1a7872

Browse files
committed
updater
1 parent 32bb43b commit b1a7872

File tree

2 files changed

+126
-253
lines changed

2 files changed

+126
-253
lines changed

src/xmlutils/updater.jl

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
export updatecontent!
2+
################################################################
3+
# Updaters
4+
################################################################
5+
# Update based on the elm
6+
################################################################
7+
# Nothing remove
8+
"""
9+
updatecontent!(value, elm::Node|Nothing, node, argAmlType)
10+
updatecontent!(value, elms::Vector{Node|Nothing}, node, argAmlType)
11+
12+
In the given `node`, update the content of all the given `elm` or `elms` to `value`.
13+
14+
updatecontent!(value, name::String, node, argAmlType)
15+
16+
In the given `node`, update the content of all the elements with given `name` to `value`.
17+
"""
18+
function updatecontent!(value::Nothing, elm::Node, node::Node, argAmlType::Type)
19+
unlink!(elm)
20+
end
21+
22+
# Nothing Nothing
23+
function updatecontent!(value::Nothing, elm::Nothing, node::Node, argAmlType::Type)
24+
# nothing
25+
end
26+
27+
################################################################
28+
# Number and String
29+
function updatecontent!(value::T, elm::Node, node::Node, argAmlType::Type) where {T<:Union{AbstractString, Number}}
30+
elm.content = value
31+
end
32+
33+
# Number and String Vector
34+
function updatecontent!(values::Vector{T}, elms::Vector{Node}, node::Node, argAmlType::Type) where {T<:Union{AbstractString, Number}}
35+
for (i, elm) in enumerate(elms)
36+
elm.content = values[i]
37+
end
38+
end
39+
40+
################################################################
41+
# General Type
42+
function updatecontent!(value::T, elm::Node, node::Node, argAmlType::Type) where {T}
43+
if hasmethod(string, Tuple{T})
44+
elm.content = string(value)
45+
else
46+
unlink!(elm)
47+
link!(node, value.aml)
48+
end
49+
end
50+
51+
# General Type Vector
52+
function updatecontent!(values::Vector{T}, elms::Vector{Node}, node::Node, argAmlType::Type) where {T}
53+
for (i, elm) in enumerate(elms)
54+
if isnothing(values[i])
55+
unlink!(elm)
56+
else
57+
if hasmethod(string, Tuple{T})
58+
elm.content = string(values[i])
59+
else
60+
unlink!(elm)
61+
link!(node, values[i].aml)
62+
end
63+
end
64+
end
65+
end
66+
67+
################################################################
68+
# Update based on the name
69+
################################################################
70+
# Documents
71+
function updatecontent!(value, name::String, doc::Document, argAmlType::Type{<:AbsNode})
72+
updatecontent!(value, name, root(doc), argAmlType)
73+
end
74+
################################################################
75+
# Nodes
76+
# Single Updater
77+
78+
function updatecontent!(value, name::String, node::Node, argAmlType::Type{<:Union{AbsNormal, AbsText}})
79+
elm = findfirst(name, node, argAmlType)
80+
if !isnothing(elm)
81+
updatecontent!(value, elm, node, argAmlType)
82+
else
83+
# addelm! if nothing is found
84+
addelm!(node, name, value, argAmlType)
85+
end
86+
end
87+
88+
# Fast attribute updater
89+
function updatecontent!(value, name::String, node::Node, argAmlType::Type{<:AbsAttribute})
90+
if haskey(node, name)
91+
node[name] = value
92+
else
93+
# addelm! if nothing is found
94+
addelm!(node, name, value, argAmlType)
95+
end
96+
end
97+
98+
99+
################################################################
100+
# Vector update
101+
102+
function updatecontent!(values::Vector, name::String, node::Node, argAmlType::Type{<:Union{AbsNormal, AbsText}})
103+
elms = findall(name, node, argAmlType)
104+
if !isnothing(elms)
105+
updatecontent!(values, elms, node, argAmlType)
106+
else
107+
# addelm! if nothing is found
108+
addelm!(node, name, values, argAmlType)
109+
end
110+
end
111+
112+
# Fast attribute updater
113+
function updatecontent!(values::Vector, name::String, node::Node, argAmlType::Type{<:AbsAttribute})
114+
if haskey(node, name)
115+
node[name] .= values
116+
else
117+
# addelm! if nothing is found
118+
addelm!(node, name, values, argAmlType)
119+
end
120+
end
121+
122+
################################################################
123+
# Dict Updating
124+
function updatecontent!(value::Type{AbstractDict}, name::Union{Node, String, Nothing}, node::Union{Node, Document}, argAmlType::Type{<:AbsNode})
125+
throw(MethodError("Dicts are not supported for extraction/updating"))
126+
end

src/xmlutils/updaters.jl

Lines changed: 0 additions & 253 deletions
This file was deleted.

0 commit comments

Comments
 (0)