2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : xml_stream.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
2004-12-03 23:54:02 +01:00
|
|
|
%%% Purpose : Parse XML streams
|
2002-11-18 21:39:47 +01:00
|
|
|
%%% Created : 17 Nov 2002 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(xml_stream).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2004-12-03 23:54:02 +01:00
|
|
|
-export([start/1, start/2,
|
|
|
|
init/1, init/2,
|
|
|
|
send_text/2,
|
|
|
|
new/1,
|
|
|
|
parse/2,
|
2004-12-05 21:54:55 +01:00
|
|
|
close/1,
|
|
|
|
parse_element/1]).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2003-10-20 20:23:30 +02:00
|
|
|
-define(XML_START, 0).
|
|
|
|
-define(XML_END, 1).
|
|
|
|
-define(XML_CDATA, 2).
|
|
|
|
-define(XML_ERROR, 3).
|
|
|
|
|
2004-12-01 23:48:53 +01:00
|
|
|
-define(PARSE_COMMAND, 0).
|
2004-12-05 21:54:55 +01:00
|
|
|
-define(PARSE_FINAL_COMMAND, 1).
|
2004-12-01 23:48:53 +01:00
|
|
|
|
2004-12-03 23:54:02 +01:00
|
|
|
-record(xml_stream_state, {callback_pid, port, stack}).
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
start(CallbackPid) ->
|
|
|
|
spawn(?MODULE, init, [CallbackPid]).
|
|
|
|
|
2004-07-28 22:08:53 +02:00
|
|
|
start(Receiver, CallbackPid) ->
|
|
|
|
spawn(?MODULE, init, [Receiver, CallbackPid]).
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
init(CallbackPid) ->
|
|
|
|
Port = open_port({spawn, expat_erl}, [binary]),
|
|
|
|
loop(CallbackPid, Port, []).
|
|
|
|
|
2004-07-28 22:08:53 +02:00
|
|
|
init(Receiver, CallbackPid) ->
|
|
|
|
erlang:monitor(process, Receiver),
|
|
|
|
Port = open_port({spawn, expat_erl}, [binary]),
|
|
|
|
loop(CallbackPid, Port, []).
|
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
loop(CallbackPid, Port, Stack) ->
|
|
|
|
receive
|
|
|
|
{Port, {data, Bin}} ->
|
|
|
|
Data = binary_to_term(Bin),
|
|
|
|
loop(CallbackPid, Port, process_data(CallbackPid, Stack, Data));
|
2003-10-20 20:23:30 +02:00
|
|
|
{_From, {send, Str}} ->
|
2004-12-01 23:48:53 +01:00
|
|
|
Res = port_control(Port, ?PARSE_COMMAND, Str),
|
|
|
|
NewStack = lists:foldl(
|
|
|
|
fun(Data, St) ->
|
|
|
|
process_data(CallbackPid, St, Data)
|
|
|
|
end, Stack, binary_to_term(Res)),
|
|
|
|
loop(CallbackPid, Port, NewStack);
|
2004-07-28 22:08:53 +02:00
|
|
|
{'DOWN', _Ref, _Type, _Object, _Info} ->
|
|
|
|
ok
|
2002-11-18 21:39:47 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
process_data(CallbackPid, Stack, Data) ->
|
|
|
|
case Data of
|
2003-10-20 20:23:30 +02:00
|
|
|
{?XML_START, {Name, Attrs}} ->
|
2004-08-26 23:47:33 +02:00
|
|
|
if
|
|
|
|
Stack == [] ->
|
2002-11-18 21:39:47 +01:00
|
|
|
gen_fsm:send_event(CallbackPid,
|
|
|
|
{xmlstreamstart, Name, Attrs});
|
2004-08-26 23:47:33 +02:00
|
|
|
true ->
|
|
|
|
ok
|
2002-11-18 21:39:47 +01:00
|
|
|
end,
|
|
|
|
[{xmlelement, Name, Attrs, []} | Stack];
|
2003-10-20 20:23:30 +02:00
|
|
|
{?XML_END, EndName} ->
|
2002-11-18 21:39:47 +01:00
|
|
|
case Stack of
|
|
|
|
[{xmlelement, Name, Attrs, Els} | Tail] ->
|
|
|
|
NewEl = {xmlelement, Name, Attrs, lists:reverse(Els)},
|
2004-08-26 23:47:33 +02:00
|
|
|
case Tail of
|
|
|
|
[] ->
|
2002-11-18 21:39:47 +01:00
|
|
|
gen_fsm:send_event(CallbackPid,
|
2004-08-26 23:47:33 +02:00
|
|
|
{xmlstreamend, EndName}),
|
2002-11-18 21:39:47 +01:00
|
|
|
Tail;
|
2004-08-26 23:47:33 +02:00
|
|
|
[_] ->
|
2002-11-18 21:39:47 +01:00
|
|
|
gen_fsm:send_event(CallbackPid,
|
2004-08-26 23:47:33 +02:00
|
|
|
{xmlstreamelement, NewEl}),
|
|
|
|
Tail;
|
|
|
|
[{xmlelement, Name1, Attrs1, Els1} | Tail1] ->
|
|
|
|
[{xmlelement, Name1, Attrs1, [NewEl | Els1]} |
|
|
|
|
Tail1]
|
2002-11-18 21:39:47 +01:00
|
|
|
end
|
|
|
|
end;
|
2003-10-20 20:23:30 +02:00
|
|
|
{?XML_CDATA, CData} ->
|
2004-08-26 23:47:33 +02:00
|
|
|
case Stack of
|
|
|
|
[El] ->
|
|
|
|
[El];
|
|
|
|
[{xmlelement, Name, Attrs, Els} | Tail] ->
|
|
|
|
[{xmlelement, Name, Attrs, [{xmlcdata, CData} | Els]} |
|
|
|
|
Tail];
|
|
|
|
[] -> []
|
|
|
|
end;
|
|
|
|
{?XML_ERROR, Err} ->
|
|
|
|
gen_fsm:send_event(CallbackPid, {xmlstreamerror, Err})
|
2002-11-18 21:39:47 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
send_text(Pid, Text) ->
|
|
|
|
Pid ! {self(), {send, Text}}.
|
|
|
|
|
2004-12-03 23:54:02 +01:00
|
|
|
|
|
|
|
new(CallbackPid) ->
|
|
|
|
Port = open_port({spawn, expat_erl}, [binary]),
|
|
|
|
#xml_stream_state{callback_pid = CallbackPid,
|
|
|
|
port = Port,
|
|
|
|
stack = []}.
|
|
|
|
|
|
|
|
|
|
|
|
parse(#xml_stream_state{callback_pid = CallbackPid,
|
|
|
|
port = Port,
|
|
|
|
stack = Stack} = State, Str) ->
|
|
|
|
Res = port_control(Port, ?PARSE_COMMAND, Str),
|
|
|
|
NewStack = lists:foldl(
|
|
|
|
fun(Data, St) ->
|
|
|
|
process_data(CallbackPid, St, Data)
|
|
|
|
end, Stack, binary_to_term(Res)),
|
|
|
|
State#xml_stream_state{stack = NewStack}.
|
|
|
|
|
|
|
|
close(#xml_stream_state{port = Port}) ->
|
|
|
|
port_close(Port).
|
2004-12-05 21:54:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
parse_element(Str) ->
|
|
|
|
Port = open_port({spawn, expat_erl}, [binary]),
|
|
|
|
Res = port_control(Port, ?PARSE_FINAL_COMMAND, Str),
|
|
|
|
port_close(Port),
|
|
|
|
process_element_events(binary_to_term(Res)).
|
|
|
|
|
|
|
|
process_element_events(Events) ->
|
|
|
|
process_element_events(Events, []).
|
|
|
|
|
|
|
|
process_element_events([], _Stack) ->
|
|
|
|
{error, parse_error};
|
|
|
|
process_element_events([Event | Events], Stack) ->
|
|
|
|
case Event of
|
|
|
|
{?XML_START, {Name, Attrs}} ->
|
|
|
|
process_element_events(
|
|
|
|
Events, [{xmlelement, Name, Attrs, []} | Stack]);
|
|
|
|
{?XML_END, _EndName} ->
|
|
|
|
case Stack of
|
|
|
|
[{xmlelement, Name, Attrs, Els} | Tail] ->
|
|
|
|
NewEl = {xmlelement, Name, Attrs, lists:reverse(Els)},
|
|
|
|
case Tail of
|
|
|
|
[] ->
|
|
|
|
if
|
|
|
|
Events == [] ->
|
|
|
|
NewEl;
|
|
|
|
true ->
|
|
|
|
{error, parse_error}
|
|
|
|
end;
|
|
|
|
[{xmlelement, Name1, Attrs1, Els1} | Tail1] ->
|
|
|
|
process_element_events(
|
|
|
|
Events,
|
|
|
|
[{xmlelement, Name1, Attrs1, [NewEl | Els1]} |
|
|
|
|
Tail1])
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
{?XML_CDATA, CData} ->
|
|
|
|
case Stack of
|
|
|
|
[{xmlelement, Name, Attrs, Els} | Tail] ->
|
|
|
|
process_element_events(
|
|
|
|
Events,
|
|
|
|
[{xmlelement, Name, Attrs, [{xmlcdata, CData} | Els]} |
|
|
|
|
Tail]);
|
|
|
|
[] ->
|
|
|
|
process_element_events(Events, [])
|
|
|
|
end;
|
|
|
|
{?XML_ERROR, Err} ->
|
|
|
|
{error, Err}
|
|
|
|
end.
|
|
|
|
|