|
2 | 2 |
|
3 | 3 | -export([find_cltrid/1, get_command/1, parse/1]). |
4 | 4 |
|
5 | | --include_lib("xmerl/include/xmerl.hrl"). |
6 | | - |
7 | | -%% Get command from an xmlElement. Otherwise return undefined. |
8 | | -get_command(Record) |
9 | | - when is_record(Record, xmlElement) -> |
10 | | - case xmerl_xpath:string("name(/epp/*[1])", Record) of |
11 | | - {xmlObj, string, "hello"} -> "hello"; |
12 | | - {xmlObj, string, "command"} -> get_command1(Record); |
13 | | - {xmlObj, string, []} -> undefined |
14 | | - end; |
| 5 | +%% We are only interested in start element of a kind. The list produced |
| 6 | +%% by this will need reversing after it is complete. |
| 7 | +%% This parsing is naive, expects command/hello element to come right |
| 8 | +%% after epp, but this should be everything we need for the purpose. |
| 9 | +-define(PARSER_FUN, |
| 10 | + fun (Event, Acc) -> |
| 11 | + case Event of |
| 12 | + {startElement, _, Name, _, _} -> [Name | Acc]; |
| 13 | + _ -> Acc |
| 14 | + end |
| 15 | + end). |
| 16 | + |
| 17 | +%% Get command a list of elements found by erlsom. |
| 18 | +%% Otherwise return undefined. |
| 19 | +get_command(["epp", "command", Command | _Rest]) -> |
| 20 | + Command; |
| 21 | +get_command(["epp", "hello" | _Rest]) -> "hello"; |
15 | 22 | get_command(_) -> undefined. |
16 | 23 |
|
17 | | -get_command1(Record) |
18 | | - when is_record(Record, xmlElement) -> |
19 | | - case xmerl_xpath:string("name(/epp/command/*[1])", |
20 | | - Record) |
21 | | - of |
22 | | - {xmlObj, string, []} -> undefined; |
23 | | - {xmlObj, string, Command} -> Command |
24 | | - end. |
25 | | - |
26 | 24 | %% xml_erl expects a list of characters, so let's give it to it. |
27 | 25 | %% Otherwise return an error tuple. |
28 | 26 | parse(Text) when is_list(Text) -> parse_list(Text); |
29 | 27 | parse(Text) when is_binary(Text) -> |
30 | 28 | List = binary_to_list(Text), parse_list(List); |
31 | 29 | parse(_) -> {error, {fatal, {expected_binary_or_list}}}. |
32 | 30 |
|
33 | | -%% Parse a record that came from the wire and return a xmlElement record. |
34 | 31 | parse_list(List) when is_list(List) -> |
35 | | - try xmerl_scan:string(List, [{quiet, true}]) of |
36 | | - {Record, []} when is_record(Record, xmlElement) -> |
37 | | - {ok, Record} |
| 32 | + try erlsom:parse_sax(List, [], ?PARSER_FUN) of |
| 33 | + {ok, Result, _} -> |
| 34 | + ProperResult = lists:reverse(Result), {ok, ProperResult} |
38 | 35 | catch |
39 | | - exit:X -> {error, X} |
| 36 | + {error, Error} -> {error, Error}; |
| 37 | + error:Error -> {error, Error}; |
| 38 | + Error -> {error, Error} |
40 | 39 | end. |
41 | 40 |
|
42 | 41 | %% The idea is that even when XML command is invalid, |
|
0 commit comments