-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathxml.ex
183 lines (146 loc) · 4.6 KB
/
xml.ex
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
defmodule AWS.XML do
@moduledoc """
Specifies the behaviour of a XML parser.
You can switch the default XML parser which uses AWS.Util underneath
by defining a different implementation by setting the `:xml_module`
configuration in `AWS.Client`:
client = %AWS.Client{xml_module: MyCustomXMLParser}
AWS.SNS.publish(client, %{})
"""
@doc """
Encodes a map into XML iodata. Raises in case of errors.
"""
@callback encode_to_iodata!(input :: map(), options :: keyword()) :: iodata()
@doc """
Decodes a XML into a map. Raises in case of errors.
"""
@callback decode!(input :: iodata(), options :: keyword()) :: map()
import Record
@text "__text"
defrecord(:xmlElement, extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl"))
defrecord(:xmlText, extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl"))
def encode_to_iodata!(map, _opts \\ []) do
map
|> Map.to_list()
|> Enum.map(&encode_xml_key_value/1)
|> :erlang.iolist_to_binary()
end
def decode!(xml, _opts \\ []) do
# See: https://elixirforum.com/t/utf-8-issue-with-erlang-xmerl-scan-function/1668/9
xml_str = :erlang.binary_to_list(xml)
opts = [{:hook_fun, &hook_fun/2}]
{element, []} = :xmerl_scan.string(xml_str, opts)
element
end
defp encode_xml_key_value({k, v}) when is_binary(k) and is_binary(v) do
["<", k, ">", v, "</", k, ">"]
end
defp encode_xml_key_value({k, values}) when is_binary(k) and is_list(values) do
for v <- values do
encode_xml_key_value({k, v})
end
end
defp encode_xml_key_value({k, v}) when is_binary(k) and is_integer(v) do
["<", k, ">", Integer.to_charlist(v), "</", k, ">"]
end
defp encode_xml_key_value({k, v}) when is_binary(k) and is_float(v) do
["<", k, ">", Float.to_charlist(v), "</", k, ">"]
end
defp encode_xml_key_value({k, v}) when is_binary(k) and is_boolean(v) do
["<", k, ">", to_string(v), "</", k, ">"]
end
defp encode_xml_key_value({k, v}) when is_binary(k) and is_map(v) do
[
"<",
k,
">",
v
|> Map.to_list()
|> Enum.map(&encode_xml_key_value/1),
"</",
k,
">"
]
end
# allow passing a map to use as xml attributes
defp encode_xml_key_value({{k, attrmap}, v})
when is_binary(k) and is_map(attrmap) and is_map(v) do
# note: assume that special chars in av are quoted by caller
attrlist =
for {a, av} <- attrmap do
[" ", to_string(a), "=", "\"#{av}\""]
end
|> List.flatten()
[
"<",
k,
attrlist,
">",
v
|> Map.to_list()
|> Enum.map(&encode_xml_key_value/1),
"</",
k,
">"
]
end
_ = """
Callback hook_fun for xmerl parser
"""
defp hook_fun(element, global_state) when Record.is_record(element, :xmlElement) do
tag = xmlElement(element, :name)
content = xmlElement(element, :content)
value =
case List.foldr(content, :none, &content_to_map/2) do
v = %{@text => text} ->
case String.trim(text) do
"" -> Map.delete(v, @text)
trimmed -> Map.put(v, @text, trimmed)
end
v ->
v
end
{%{Atom.to_string(tag) => value}, global_state}
end
defp hook_fun(text, global_state) when Record.is_record(text, :xmlText) do
text = xmlText(text, :value)
{:unicode.characters_to_binary(text), global_state}
end
_ = """
Convert the content of an Xml node into a map.
When there is more than one element with the same tag name, their
values get merged into a list.
If the content is only text then that is what gets returned.
If the content is a mix between text and child elements, then the
elements are processed as described above and all the text parts
are merged under the `__text' key.
"""
defp content_to_map(x, :none) do
x
end
defp content_to_map(x, acc) when is_map(x) and is_map(acc) do
[{tag, value}] = Map.to_list(x)
case Map.has_key?(acc, tag) do
true ->
update_fun = fn
l when is_list(l) -> [value | l]
v -> [value, v]
end
Map.update!(acc, tag, update_fun)
false ->
Map.put(acc, tag, value)
end
end
defp content_to_map(x, %{@text => text} = acc) when is_binary(x) and is_map(acc) do
%{acc | @text => <<x::binary, text::binary>>}
end
defp content_to_map(x, acc) when is_binary(x) and is_map(acc) do
Map.put(acc, @text, x)
end
defp content_to_map(x, acc) when is_binary(x) and is_binary(acc) do
<<x::binary, acc::binary>>
end
defp content_to_map(x, acc) when is_map(x) and is_binary(acc) do
Map.put(x, @text, acc)
end
end