Commits

Stanisław Pitucha  committed fe87d0a

add basic scenario parse

  • Participants

Comments (0)

Files changed (1)

File src/scenario.erl

+-module(scenario).
+-include_lib("xmerl/include/xmerl.hrl").
+-export([read/1]).
+
+read(F) ->
+	Scen = xmerl_scan:file(F, [{space, normalize}, {validation, off}]),
+	Title = get_title(Scen),
+	Actions = get_actions(Scen),
+	{Title, Actions}.
+
+get_title_attr([]) -> not_found;
+get_title_attr([#xmlAttribute{name=name, value=Title}|_]) ->
+	Title;
+get_title_attr([_|As]) ->
+	get_title_attr(As).
+	
+get_title({#xmlElement{name = scenario, attributes = Attribs}, _}) ->
+	get_title_attr(Attribs).
+
+filter_actions(Acc, []) -> Acc;
+filter_actions(Acc, [#xmlElement{name = send} = Elem|Es]) -> filter_actions([Elem|Acc], Es);
+filter_actions(Acc, [#xmlElement{name = recv} = Elem|Es]) -> filter_actions([Elem|Acc], Es);
+filter_actions(Acc, [#xmlElement{name = pause} = Elem|Es]) -> filter_actions([Elem|Acc], Es);
+filter_actions(Acc, [#xmlElement{name = 'ResponseTimeRepartition'} = Elem|Es]) -> filter_actions([Elem|Acc], Es);
+filter_actions(Acc, [#xmlElement{name = 'CallLengthRepartition'} = Elem|Es]) -> filter_actions([Elem|Acc], Es);
+filter_actions(Acc, [#xmlText{value = Val}|Es]) ->
+	case string:strip(Val) of
+		[] -> filter_actions(Acc, Es);
+		_ -> throw({text_found_not_action, Val})
+	end;
+filter_actions(_, [#xmlElement{name = Name}|_]) -> throw({unknown_xml_element, Name});
+filter_actions(_, [E|_]) -> throw({unknown_element, E}).
+
+filter_actions(Es) when is_list(Es) -> filter_actions([], Es).
+
+get_actions({#xmlElement{name = scenario, content = Content}, _}) ->
+	filter_actions(Content).
+