-
Notifications
You must be signed in to change notification settings - Fork 405
/
mochiweb_xpath_utils.erl
72 lines (59 loc) · 1.64 KB
/
mochiweb_xpath_utils.erl
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
%% xpath_utils.erl
%% @author Pablo Polvorin
%% @doc Utility functions, mainly for type conversion
%% Conversion rules taken from http://www.w3.org/TR/1999/REC-xpath-19991116
%% created on 2008-05-07
-module(mochiweb_xpath_utils).
-export([string_value/1,
number_value/1,
node_set_value/1,
boolean_value/1,
convert/2]).
string_value(N) when is_list(N)->
case N of
[X|_] -> string_value(X);
[] -> <<>>
end;
string_value({_,_,Contents}) ->
L = lists:filter(fun
({_,_,_}) ->false;
(B) when is_binary(B) -> true
end,Contents),
list_to_binary(L);
string_value(N) when is_integer(N) ->
list_to_binary(integer_to_list(N));
string_value(B) when is_binary(B) ->
B;
string_value(B) when is_atom(B) ->
list_to_binary(atom_to_list(B)).
node_set_value(N) when is_list(N) ->
N;
node_set_value(N) ->
throw({node_set_expected,N}).
number_value(N) when is_integer(N) or is_float(N) ->
N;
number_value(N) when is_binary(N)->
String = binary_to_list(N),
case erl_scan:string(String) of
{ok, [{integer,1,I}],1} -> I;
{ok, [{float,1,F}],1} -> F
end;
number_value(N) ->
number_value(string_value(N)).
boolean_value([]) ->
false;
boolean_value([_|_]) ->
true;
boolean_value(N) when is_number(N) ->
N /= 0;
boolean_value(B) when is_binary(B) ->
size(B) /= 0;
boolean_value(B) when B == true;
B == false ->
B.
convert(Value,number) ->
number_value(Value);
convert(Value,string) ->
string_value(Value);
convert(Value,node_set) ->
node_set_value(Value).