aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xmerl/src/xmerl_simple.erl
blob: 32479066cd17e74f91abfcbedac94cd4b542b09c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%


%% Description  : Simple event-based processor (front-end to xmerl_scanner)


-module(xmerl_simple).

-export([file/2,
	 string/2]).

-include("xmerl.hrl").

-record(state, {content_acc = [],
		attr_acc = [],
		content_stack = [],
		attr_stack = []}).

file(Fname, Opts) ->
    Opts1 = scanner_options(Opts),
    xmerl_scan:file(Fname, Opts1).

string(Str, Opts) ->
    Opts1 = scanner_options(Opts),
    xmerl_scan:string(Str, Opts1).

scanner_options(Opts) ->
    EventS = #state{},
    scanner_options(Opts,
		    [{event_fun, fun event/2, EventS},
		     {acc_fun, fun(_, Acc, S) -> {Acc,S} end},
		     {close_fun, fun close/1}]).

scanner_options([H|T], Opts) ->
    case catch keyreplace(H, 1, Opts) of
	false ->
	    scanner_options(T, [H|Opts]);
	NewOpts ->
	    scanner_options(T, NewOpts)
    end;
scanner_options([], Opts) ->
    Opts.

keyreplace(X, Pos, [H|T]) when element(Pos, X) == element(Pos, H) ->
    [X|T];
keyreplace(X, Pos, [H|T]) ->
    [H|keyreplace(X, Pos, T)];
keyreplace(_X, _Pos, []) ->
    throw(false).


close(S) ->
    ES = xmerl_scan:event_state(S),
    #state{attr_stack = [],
	   content_stack = [],
	   %% attr_acc may contain document attributes
	   content_acc = Content} = ES,
    lists:reverse(Content).

event(#xmerl_event{event = started, data = #xmlElement{}}, S) ->
    #state{content_acc = CAcc,
	   attr_acc = AAcc,
	   content_stack = CSt,
	   attr_stack = ASt} = ES = xmerl_scan:event_state(S),
    xmerl_scan:event_state(ES#state{content_acc = [],
				    attr_acc = [],
				    content_stack = [CAcc | CSt],
				    attr_stack = [AAcc | ASt]}, S);

event(#xmerl_event{event = ended, data = #xmlElement{name = Name}}, S) ->
    #state{content_acc = CAcc,
	   attr_acc = AAcc,
	   content_stack = [PrevCAcc | CSt],
	   attr_stack = [PrevAAcc | ASt]} = ES = xmerl_scan:event_state(S),
    Simple = {Name, lists:reverse(AAcc), lists:reverse(CAcc)},
    xmerl_scan:event_state(ES#state{content_acc = [Simple|PrevCAcc],
				    attr_acc = PrevAAcc,
				    content_stack = CSt,
				    attr_stack = ASt}, S);

event(#xmerl_event{event = ended, data = #xmlAttribute{name = Name,
						       value = Value}}, S) ->
    #state{attr_acc = AAcc} = ES = xmerl_scan:event_state(S),
    Simple = {Name, Value},
    xmerl_scan:event_state(ES#state{attr_acc = [Simple|AAcc]}, S);

event(#xmerl_event{event = ended, data = #xmlText{value = Text}}, S) ->
    #state{content_acc = CAcc} = ES = xmerl_scan:event_state(S),
    xmerl_scan:event_state(ES#state{content_acc = [Text|CAcc]}, S);

event(_E, S) ->
    S.