Replace xmerl with erlsom

Also includes small syntax fixes to sys.config, as well as one
compilation warning removal.
This commit is contained in:
Maciej Szlosarczyk 2020-02-27 09:50:24 +02:00
parent 8d46c736d7
commit 83aae9baee
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
8 changed files with 43 additions and 37 deletions

View file

@ -2,27 +2,25 @@
-export([find_cltrid/1, get_command/1, parse/1]).
-include_lib("xmerl/include/xmerl.hrl").
%% We are only interested in start element of a kind. The list produced
%% by this will need reversing after it is complete.
%% This parsing is naive, expects command/hello element to come right
%% after epp, but this should be everything we need for the purpose.
-define(PARSER_FUN,
fun (Event, Acc) ->
case Event of
{startElement, _, Name, _, _} -> [Name | Acc];
_ -> Acc
end
end).
%% Get command from an xmlElement. Otherwise return undefined.
get_command(Record)
when is_record(Record, xmlElement) ->
case xmerl_xpath:string("name(/epp/*[1])", Record) of
{xmlObj, string, "hello"} -> "hello";
{xmlObj, string, "command"} -> get_command1(Record);
{xmlObj, string, []} -> undefined
end;
%% Get command a list of elements found by erlsom.
%% Otherwise return undefined.
get_command(["epp", "command", Command | _Rest]) ->
Command;
get_command(["epp", "hello" | _Rest]) -> "hello";
get_command(_) -> undefined.
get_command1(Record)
when is_record(Record, xmlElement) ->
case xmerl_xpath:string("name(/epp/command/*[1])",
Record)
of
{xmlObj, string, []} -> undefined;
{xmlObj, string, Command} -> Command
end.
%% xml_erl expects a list of characters, so let's give it to it.
%% Otherwise return an error tuple.
parse(Text) when is_list(Text) -> parse_list(Text);
@ -30,13 +28,14 @@ parse(Text) when is_binary(Text) ->
List = binary_to_list(Text), parse_list(List);
parse(_) -> {error, {fatal, {expected_binary_or_list}}}.
%% Parse a record that came from the wire and return a xmlElement record.
parse_list(List) when is_list(List) ->
try xmerl_scan:string(List, [{quiet, true}]) of
{Record, []} when is_record(Record, xmlElement) ->
{ok, Record}
try erlsom:parse_sax(List, [], ?PARSER_FUN) of
{ok, Result, _} ->
ProperResult = lists:reverse(Result), {ok, ProperResult}
catch
exit:X -> {error, X}
{error, Error} -> {error, Error};
error:Error -> {error, Error};
Error -> {error, Error}
end.
%% The idea is that even when XML command is invalid,

View file

@ -74,6 +74,5 @@ readable_ip_test_case(_Config) ->
path_for_file_test_case(_Config) ->
AbsoluteFilename = "/usr/bin",
AbsoluteFilename = epp_util:path_for_file(AbsoluteFilename),
RelativeFilename = "usr/bin",
true = (AbsoluteFilename =:= epp_util:path_for_file(AbsoluteFilename)),
ok.

View file

@ -1,9 +1,7 @@
-module(epp_xml_SUITE).
-include_lib("common_test/include/ct.hrl").
%% This is required for parse tests.
-include_lib("xmerl/include/xmerl.hrl").
-include_lib("stdlib/include/assert.hrl").
-define(sampleCommandList,
"<epp>
@ -59,24 +57,28 @@ parse_not_a_list_or_binary_test_case(_Config) ->
parse_sample_valid_xml_list_test_case(_Config) ->
Input = ?sampleCommandList,
{ok, Record} = epp_xml:parse(Input),
true = is_record(Record, xmlElement),
{ok, Result} = epp_xml:parse(Input),
?assertEqual(["epp", "command", "login", "clID", "pw", "clTRID"],
Result),
ok.
parse_sample_valid_xml_binary_test_case(_Config) ->
Input = list_to_binary(?sampleCommandList),
{ok, Record} = epp_xml:parse(Input),
true = is_record(Record, xmlElement),
{ok, Result} = epp_xml:parse(Input),
?assertEqual(["epp", "command", "login", "clID", "pw", "clTRID"],
Result),
ok.
parse_sample_invalid_xml_list_test_case(_Config) ->
Input = "Some text",
{error, {fatal, _Details}} = epp_xml:parse(Input),
ExpectedResult = {error, "Malformed: Illegal character in prolog"},
?assertEqual(ExpectedResult, epp_xml:parse(Input)),
ok.
parse_sample_invalid_xml_binary_test_case(_Config) ->
Input = list_to_binary("Some text"),
{error, {fatal, _Details}} = epp_xml:parse(Input),
Input = <<"</epp>\n">>,
ExpectedResult = {error, {badmatch, []}},
?assertEqual(ExpectedResult, epp_xml:parse(Input)),
ok.
%% find_cltrid