1
%%--------------------------------------------------------------------
4
%% Copyright Ericsson AB 2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
18
%%----------------------------------------------------------------------
19
%% File : ct_config_xml.erl
20
%% Description : CT callback module for reading configs from XML files
22
%% Created : 16 February 2010
23
%%----------------------------------------------------------------------
24
-module(ct_config_xml).
25
-export([read_config/1, check_parameter/1]).
28
read_config(ConfigFile) ->
29
case catch do_read_xml_config(ConfigFile) of
32
{error, Error, ErroneousString}->
33
{error, Error, ErroneousString}
37
check_parameter(File)->
38
case filelib:is_file(File) of
42
{error, {nofile, File}}
45
% actual reading of the config
46
do_read_xml_config(ConfigFile)->
47
case catch xmerl_sax_parser:file(ConfigFile,
48
[{event_fun, fun event/3},
49
{event_state, []}]) of
51
{ok, lists:reverse(transform_entity_list(EntityList))};
53
{error, parsing_failed, Oops}
56
% event callback for xmerl_sax_parser
57
event(Event, _LineNo, State) ->
61
tag(startDocument, State) ->
65
tag({startElement, _Uri, "config", _QName, _Attributes}, []) ->
69
tag({startElement, _Uri, Name, _QName, _Attributes}, Tags) ->
73
tag({characters, String}, [{Tag, _Value}|Tags]) ->
77
tag({endElement, _Uri, _Name, _QName},
78
[Entity, {PrevEntityTag, PrevEntityValue}|Tags]) ->
79
NewHead = {PrevEntityTag, [Entity|PrevEntityValue]},
83
tag({endElement, _Uri, "config", _QName}, [{"config", Config}]) ->
86
% end of document, return result
87
tag(endDocument, {_Tags, Result}) ->
94
% transform of the ugly deeply nested entity list to the key-value "tree"
95
transform_entity_list(EntityList)->
96
lists:map(fun transform_entity/1, EntityList).
98
% transform entity from {list(), list()} to {atom(), term()}
99
transform_entity({Tag, [Value|Rest]}) when
101
{list_to_atom(Tag), transform_entity_list(lists:reverse([Value|Rest]))};
102
transform_entity({Tag, String})->
103
case list_to_term(String) of
105
{list_to_atom(Tag), Value};
110
% transform a string with Erlang terms
111
list_to_term(String) ->
112
{ok, T, _} = erl_scan:string(String++"."),
113
case catch erl_parse:parse_term(T) of
117
{error, Error, String}