1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : xml.erl |
3 |
|
%%% Author : Alexey Shchepin <alexey@process-one.net> |
4 |
|
%%% Purpose : XML utils |
5 |
|
%%% Created : 20 Nov 2002 by Alexey Shchepin <alexey@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2011 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%---------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(xml). |
27 |
|
-author('alexey@process-one.net'). |
28 |
|
|
29 |
|
-export([remove_cdata/1, |
30 |
|
get_cdata/1, get_tag_cdata/1, |
31 |
|
get_attr/2, get_attr_s/2, |
32 |
|
get_tag_attr/2, get_tag_attr_s/2, |
33 |
|
get_subtag/2, |
34 |
|
append_subtags/2, |
35 |
|
get_path_s/2, |
36 |
|
replace_tag_attr/3, |
37 |
|
replace_subelement/2]). |
38 |
|
|
39 |
|
-include("jlib.hrl"). |
40 |
|
|
41 |
|
-type xmlel_or_cdata() :: jlib:xmlch(). |
42 |
|
|
43 |
|
-spec remove_cdata_p(xmlel_or_cdata()) -> boolean(). |
44 |
46242 |
remove_cdata_p(#xmlel{}) -> true; |
45 |
:-( |
remove_cdata_p(_) -> false. |
46 |
|
|
47 |
|
|
48 |
|
-spec remove_cdata([xmlel_or_cdata()]) -> [xmlel_or_cdata()]. |
49 |
47383 |
remove_cdata(L) -> [E || E <- L, remove_cdata_p(E)]. |
50 |
|
|
51 |
|
|
52 |
|
-spec get_cdata([xmlel_or_cdata()]) -> binary(). |
53 |
|
get_cdata(L) -> |
54 |
2242 |
list_to_binary(get_cdata(L, "")). |
55 |
|
|
56 |
|
|
57 |
|
-spec get_cdata([xmlel_or_cdata()], [iolist()]) -> [iolist()]. |
58 |
|
get_cdata([#xmlcdata{content = CData} | L], S) -> |
59 |
2038 |
get_cdata(L, [S, CData]); |
60 |
|
get_cdata([_ | L], S) -> |
61 |
17 |
get_cdata(L, S); |
62 |
|
get_cdata([], S) -> |
63 |
2242 |
S. |
64 |
|
|
65 |
|
|
66 |
|
-spec get_tag_cdata(exml:element()) -> binary(). |
67 |
|
get_tag_cdata(#xmlel{children = Els}) -> |
68 |
2205 |
get_cdata(Els). |
69 |
|
|
70 |
|
|
71 |
|
-spec get_attr(binary() | string(), |
72 |
|
[jlib:binary_pair()]) -> 'false' | {'value', binary() | string()}. |
73 |
|
get_attr(AttrName, Attrs) -> |
74 |
2438 |
case lists:keysearch(AttrName, 1, Attrs) of |
75 |
|
{value, {_, Val}} -> |
76 |
1141 |
{value, Val}; |
77 |
|
_ -> |
78 |
1297 |
false |
79 |
|
end. |
80 |
|
|
81 |
|
|
82 |
|
-spec get_attr_s(binary(), list()) -> binary(). |
83 |
|
get_attr_s(AttrName, Attrs) -> |
84 |
201847 |
case lists:keysearch(AttrName, 1, Attrs) of |
85 |
|
{value, {_, Val}} -> |
86 |
149354 |
Val; |
87 |
|
_ -> |
88 |
52493 |
context_default(AttrName) |
89 |
|
end. |
90 |
|
|
91 |
|
-spec get_tag_attr(binary(), exml:element()) -> 'false' | {'value', binary()}. |
92 |
|
get_tag_attr(AttrName, #xmlel{attrs = Attrs}) -> |
93 |
95 |
get_attr(AttrName, Attrs). |
94 |
|
|
95 |
|
|
96 |
|
-spec get_tag_attr_s(binary(), exml:element()) -> binary(). |
97 |
|
get_tag_attr_s(AttrName, #xmlel{attrs = Attrs}) -> |
98 |
11957 |
get_attr_s(AttrName, Attrs). |
99 |
|
|
100 |
|
|
101 |
|
-spec get_subtag(exml:element(), binary()) -> 'false' | exml:element(). |
102 |
|
get_subtag(#xmlel{children = Els}, Name) -> |
103 |
8152 |
get_subtag1(Els, Name). |
104 |
|
|
105 |
|
|
106 |
|
-spec get_subtag1([xmlel_or_cdata()], binary()) -> 'false' | exml:element(). |
107 |
|
get_subtag1([El | Els], Name) -> |
108 |
7344 |
case El of |
109 |
|
#xmlel{name = Name} -> |
110 |
1801 |
El; |
111 |
|
_ -> |
112 |
5543 |
get_subtag1(Els, Name) |
113 |
|
end; |
114 |
|
get_subtag1([], _) -> |
115 |
6351 |
false. |
116 |
|
|
117 |
|
|
118 |
|
-spec append_subtags(exml:element(), [xmlel_or_cdata()]) -> exml:element(). |
119 |
|
append_subtags(XE = #xmlel{children = SubTags1}, SubTags2) -> |
120 |
5816 |
XE#xmlel{children = SubTags1 ++ SubTags2}. |
121 |
|
|
122 |
|
|
123 |
|
-spec get_path_s(exml:element(), [{elem, binary()} | {attr, binary()} | cdata]) -> |
124 |
|
iodata() | exml:element(). |
125 |
|
get_path_s(El, []) -> |
126 |
4 |
El; |
127 |
|
get_path_s(El, [{elem, Name} | Path]) -> |
128 |
3898 |
case get_subtag(El, Name) of |
129 |
|
false -> |
130 |
3576 |
context_default(Name); |
131 |
|
SubEl -> |
132 |
322 |
get_path_s(SubEl, Path) |
133 |
|
end; |
134 |
|
get_path_s(El, [{attr, Name}]) -> |
135 |
4 |
get_tag_attr_s(Name, El); |
136 |
|
get_path_s(El, [cdata]) -> |
137 |
199 |
get_tag_cdata(El). |
138 |
|
|
139 |
|
|
140 |
|
-spec replace_tag_attr(Attr :: binary(), Value :: binary(), exml:element() |
141 |
|
) -> exml:element(). |
142 |
|
replace_tag_attr(Attr, Value, XE = #xmlel{attrs = Attrs}) -> |
143 |
4429 |
Attrs1 = lists:keydelete(Attr, 1, Attrs), |
144 |
4429 |
Attrs2 = [{Attr, Value} | Attrs1], |
145 |
4429 |
XE#xmlel{attrs = Attrs2}. |
146 |
|
|
147 |
|
%% @doc Given an element and a new subelement, |
148 |
|
%% replace the instance of the subelement in element with the new subelement. |
149 |
|
-spec replace_subelement(exml:element(), exml:element()) -> exml:element(). |
150 |
|
replace_subelement(XE = #xmlel{children = SubEls}, NewSubEl) -> |
151 |
4 |
{_, NameNewSubEl, _, _} = NewSubEl, |
152 |
4 |
SubEls2 = lists:keyreplace(NameNewSubEl, 2, SubEls, NewSubEl), |
153 |
4 |
XE#xmlel{children = SubEls2}. |
154 |
|
|
155 |
|
-spec context_default(binary() | string()) -> <<>> | []. |
156 |
|
context_default(Attr) when is_list(Attr) -> |
157 |
:-( |
""; |
158 |
|
context_default(_Attr) -> |
159 |
56069 |
<<>>. |