~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to lib/common_test/src/ct_config_xml.erl

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%--------------------------------------------------------------------
 
2
%% %CopyrightBegin%
 
3
%%
 
4
%% Copyright Ericsson AB 2010. All Rights Reserved.
 
5
%%
 
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/.
 
11
%%
 
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
 
15
%% under the License.
 
16
%%
 
17
%% %CopyrightEnd%
 
18
%%----------------------------------------------------------------------
 
19
%% File    : ct_config_xml.erl
 
20
%% Description : CT callback module for reading configs from XML files
 
21
%%
 
22
%% Created : 16 February 2010
 
23
%%----------------------------------------------------------------------
 
24
-module(ct_config_xml).
 
25
-export([read_config/1, check_parameter/1]).
 
26
 
 
27
% read config file
 
28
read_config(ConfigFile) ->
 
29
    case catch do_read_xml_config(ConfigFile) of
 
30
        {ok, Config}->
 
31
            {ok, Config};
 
32
        {error, Error, ErroneousString}->
 
33
            {error, Error, ErroneousString}
 
34
    end.
 
35
 
 
36
% check file exists
 
37
check_parameter(File)->
 
38
    case filelib:is_file(File) of
 
39
        true->
 
40
            {ok, {file, File}};
 
41
        false->
 
42
            {error, {nofile, File}}
 
43
    end.
 
44
 
 
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
 
50
        {ok, EntityList, _}->
 
51
            {ok, lists:reverse(transform_entity_list(EntityList))};
 
52
        Oops->
 
53
            {error, parsing_failed, Oops}
 
54
    end.
 
55
 
 
56
% event callback for xmerl_sax_parser
 
57
event(Event, _LineNo, State) ->
 
58
    tag(Event, State).
 
59
 
 
60
% document start
 
61
tag(startDocument, State) ->
 
62
    State;
 
63
 
 
64
% start of the config
 
65
tag({startElement, _Uri, "config", _QName, _Attributes}, []) ->
 
66
        [{"config", []}];
 
67
 
 
68
% start tag
 
69
tag({startElement, _Uri, Name, _QName, _Attributes}, Tags) ->
 
70
        [{Name, []}|Tags];
 
71
 
 
72
% value
 
73
tag({characters, String}, [{Tag, _Value}|Tags]) ->
 
74
        [{Tag, String}|Tags];
 
75
 
 
76
% end tag
 
77
tag({endElement, _Uri, _Name, _QName},
 
78
        [Entity, {PrevEntityTag, PrevEntityValue}|Tags]) ->
 
79
        NewHead = {PrevEntityTag, [Entity|PrevEntityValue]},
 
80
        [NewHead|Tags];
 
81
 
 
82
% end of the config
 
83
tag({endElement, _Uri, "config", _QName}, [{"config", Config}]) ->
 
84
        Config;
 
85
 
 
86
% end of document, return result
 
87
tag(endDocument,  {_Tags, Result}) ->
 
88
    Result;
 
89
 
 
90
% default
 
91
tag(_El, State) ->
 
92
        State.
 
93
 
 
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).
 
97
 
 
98
% transform entity from {list(), list()} to {atom(), term()}
 
99
transform_entity({Tag, [Value|Rest]}) when
 
100
    is_tuple(Value)->
 
101
        {list_to_atom(Tag), transform_entity_list(lists:reverse([Value|Rest]))};
 
102
transform_entity({Tag, String})->
 
103
    case list_to_term(String) of
 
104
        {ok, Value}->
 
105
            {list_to_atom(Tag), Value};
 
106
        Error->
 
107
             throw(Error)
 
108
    end.
 
109
 
 
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
 
114
        {ok, Term} ->
 
115
            {ok, Term};
 
116
        Error ->
 
117
            {error, Error, String}
 
118
    end.