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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See
%% the Licence for the specific language governing rights and limitations
%% under the License.
%%
%% The Initial Developer of the Original Code is Ericsson AB.
%% Portions created by Ericsson are Copyright 1999-2006, Ericsson AB.
%% All Rights Reserved.��
%%
%% $Id$
%%
-module(docb_xmerl_tree_cb).
%% This is the XMerL callback module for exporting XML to the internal
%% tree format used by DocBuilder.
%% {Doc, _Misc} = xmerl_scan:file("file.xml", [{validation,true}])
%% Tree = xmerl:export([Doc], docb_xmerl_tree_cb)
-export(['#xml-inheritance#'/0]).
-export(['#root#'/4,
'#text#'/1,
'#element#'/5]).
-include("xmerl.hrl").
%%--Functions used by xmerl---------------------------------------------
'#xml-inheritance#'() ->
[].
'#root#'(Data, _Attrs, [], _E) ->
Data.
'#text#'(Text) ->
Text2 = strip_leading_blanks(Text),
%% before
%% case Text2 of
%% [$\n|T] ->
%% case is_empty(T) of
%% true -> [];
%% false -> {pcdata, [], nl(Text2)}
%% end;
%%
%% _ ->
%% {pcdata, [], nl(Text2)}
%% end.
%% after
{pcdata, [], nl(Text2)}.
'#element#'(Tag, Data, Attrs, Parents, _E) when Tag==pre; Tag==code ->
[H|T] = reinsert_nl(Data),
NewData = [strip_nl(H)|T],
NewData2 = case Tag of
code ->
fix_single_pcdata(NewData);
pre ->
NewData
end,
{Tag, attrs(get_dtd(Parents), Tag, Attrs), NewData2};
'#element#'(Tag, Data, Attrs, Parents, _E) ->
NewData = case tag_content(Tag) of
no_pcdata -> % remove all pcdata
[Dat||
Dat <- Data,
begin
Fun = fun({pcdata,_,_}) -> false;
(_) -> true end,
Fun(Dat)
end];
single_pcdata when length(Data)>1 ->
%% merge several pcdata's into one single pcdata
fix_single_pcdata(Data);
_ ->
lists:flatten(Data)
end,
{Tag, attrs(get_dtd(Parents), Tag, Attrs), NewData}.
%%--Internal functions--------------------------------------------------
%% is_empty(Str) -> bool()
%% Returns true if the string Str only contains blanks, tabs and
%% newlines, false otherwise.
%% is_empty("\n" ++ Text) ->
%% is_empty(Text);
%% is_empty("\t" ++ Text) ->
%% is_empty(Text);
%% is_empty(" " ++ Text) ->
%% is_empty(Text);
%% is_empty("") ->
%% true;
%% is_empty(_) ->
%% false.
%% reinsert_nl(L1) -> L2
%% Workaround for <pre>: Normally empty lines are ignored. However,
%% Xmerl splits lines whenever it encounters an entity. In the case of
%% <pre>, this may lead to that we ignores what we think is an empty
%% line but is actually a line break that should be kept, for example
%% in this case:
%% <pre>
%% <input>some command</input> <-- this line break is lost!
%% <some result>
%% </pre>
%% This function reinserts line breaks where necessary.
reinsert_nl([[]|T]) ->
[{pcdata,[],"\\n"} | reinsert_nl(T)];
reinsert_nl([H|T]) ->
[H | reinsert_nl(T)];
reinsert_nl([]) ->
[].
%% sgmls treats line breaks in a way that DocBuilder relies on and
%% which must be imitated here. Replace all "\n" with "\\n" and add
%% "\n" to the end of each text element.
nl("") ->
"\n";
nl("\n"++Text) ->
"\\n"++nl(Text);
nl([Ch|Text]) ->
[Ch|nl(Text)].
%% strip_leading_blanks(Str) -> Str
%% Leading spaces and tabs before a newline are always redundant
%% and are therefore stripped of here
%% If no newline is found the original string is returned unchanged
strip_leading_blanks(Str) ->
strip_leading_blanks(Str,Str).
strip_leading_blanks([],Str) ->
Str;
strip_leading_blanks([$\s|T],Str) ->
strip_leading_blanks(T,Str);
strip_leading_blanks([$\t|T],Str) ->
strip_leading_blanks(T,Str);
strip_leading_blanks(Rest=[$\n|_],_) ->
Rest;
strip_leading_blanks(_,Str) ->
Str.
%% strip_nl(Str) -> Str
%% The XMerL scan will often result in the contents of <pre> or <code>
%% starting with a newline, as the format is normally:
%% <pre>
%% ..contents..
%% </pre>
%% However, this newline must be removed, or the resulting HTML will be
%% <pre>
%%
%% ..content..
%% </pre>
strip_nl({pcdata,[],"\\n"++Str}) -> {pcdata,[],Str};
strip_nl(E) -> E.
get_dtd([]) ->
none;
get_dtd(Parents) ->
{DTD, _} = lists:last(Parents),
DTD.
%% attrs(DTD, Tag, GivenAttrs) -> AllAttrs
%% DTD = Tag = atom() DTD and tag name
%% GivenAttrs = [#xmlAttribute{}]
%% AllAttrs = [{Name, Type, Val}]
%% Name = string() (uppercase) Example: "VALIGN"
%% Type = "CDATA" | "TOKEN"
%% Val = string() (uppercase if type is "TOKEN", as-is otherwise)
%% The XMerL scanning of <file>.xml renders only the given attributes.
%% However, DocBuilder needs also the optional attributes (which not
%% necessarily have been given), so we add them here, using the default
%% values according to the DTDs.
%% NOTE: Uses the information from the DTDs. That is, if some change is
%% done to the DTDs, also this file must be updated. Ideally, the DTDs
%% should be parsed automatically in some way.
%% It can also be noted that this check is superfluous in the case where
%% all attributes are required (except that the attributes are sorted
%% in the same order as in the DTD) and where an optional attribute has
%% type "CDATA" as no sensible default value can be specified in this
%% case.
attrs(DTD, Tag, GivenAttrs) ->
merge_attrs(Tag, default_attrs(DTD, Tag), GivenAttrs).
merge_attrs(Tag, [{NameA, Type, DefVal}|Default], GivenAttrs) ->
Val = case lists:keysearch(NameA, #xmlAttribute.name, GivenAttrs) of
{value, #xmlAttribute{value=Val0}} -> Val0;
false -> DefVal
end,
Attr = {attr_name(NameA), Type, attr_val(Type, Val)},
[Attr | merge_attrs(Tag, Default, GivenAttrs)];
merge_attrs(_Tag, [], _GivenAttrs) ->
[].
attr_name(Atom) ->
string:to_upper(atom_to_list(Atom)).
attr_val("CDATA", Val) -> Val;
attr_val("TOKEN", Val) -> string:to_upper(Val).
%% Given the DTD and element tag, return a list [{Name, Value}] where
%% Name (atom) is the name of each possible attribute and
%% Value (lowercase string) its default value.
default_attrs(_, cell) ->
[{align, "TOKEN", "left"},
{valign, "TOKEN", "middle"}];
default_attrs(_, cite) ->
[{id, "CDATA", ""}]; % required
default_attrs(_, code) ->
[{type, "TOKEN", "none"}];
default_attrs(_, codeinclude) ->
[{file, "CDATA", ""}, % required
{tag, "CDATA", ""},
{type, "TOKEN", "none"}];
default_attrs(book, contents) ->
[{level, "TOKEN", "2"}];
default_attrs(_, erleval) ->
[{expr, "CDATA", ""}]; % required
default_attrs(report, erlinclude) ->
[{file, "CDATA", ""}, % required
{tag, "CDATA", ""}]; % required
default_attrs(_, fascicule) ->
[{file, "CDATA", ""}, % required
{href, "CDATA", ""}, % required
{entry, "TOKEN", "no"}];
default_attrs(book, header) ->
[{titlestyle, "TOKEN", "normal"}];
default_attrs(_, image) ->
[{file, "CDATA", ""}]; % required
default_attrs(_, include) ->
[{file, "CDATA", ""}]; % required
default_attrs(report, index) ->
[{txt, "CDATA", ""}]; % required
default_attrs(_, list) ->
[{type, "TOKEN", "bulleted"}];
default_attrs(_, marker) ->
[{id, "CDATA", ""}]; % required
default_attrs(book, onepart) ->
[{lift, "TOKEN", "no"}];
default_attrs(book, parts) ->
[{lift, "TOKEN", "no"}];
default_attrs(_, path) ->
[{unix, "CDATA", ""},
{windows, "CDATA", ""}];
default_attrs(_, seealso) ->
[{marker, "CDATA", ""}]; % required
default_attrs(report, table) ->
[{width, "CDATA", "0"},
{colspec, "CDATA", ""}];
default_attrs(_, table) ->
[{align, "TOKEN", "center"}];
default_attrs(_, term) ->
[{id, "CDATA", ""}]; % required
default_attrs(book, theheader) ->
[{tag, "TOKEN", "none"}];
default_attrs(bookinsidecover, theheader) ->
[{tag, "TOKEN", "none"}];
default_attrs(_, url) ->
[{href, "CDATA", ""}]; % required
default_attrs(_, _) -> [].
%%--Single PCDATA broken into several fix-------------------------------
%% When text contains an entity, then XMERL splits it into two
%% PCDATA elements, the second starting with the entity.
%%
%% Example:
%% Magnus Fr�berg => [{pcdata,[],"Magnus Fr\n"},{pcdata,[],"�berg\n"}]
%%
%% This is not handled by DocBuilder which expects many tags, for
%% example title and aname, to contain a single PCDATA element. (That
%% is also what nsgmls returned.)
fix_single_pcdata([{pcdata,[],Str1}, {pcdata,[],Str2}|T]) ->
fix_single_pcdata([{pcdata,[],Str1++Str2}|T]);
fix_single_pcdata(FixedData) ->
FixedData.
tag_content(aname) -> single_pcdata;
tag_content(app) -> single_pcdata;
tag_content(approved) -> single_pcdata;
tag_content(appsummary) -> single_pcdata;
tag_content(b) -> single_pcdata;
tag_content(c) -> single_pcdata;
tag_content(cauthor) -> single_pcdata;
tag_content(cell) -> mixed_content;
tag_content(checked) -> single_pcdata;
tag_content(chowpublished) -> single_pcdata;
tag_content(code) -> single_pcdata; % mixed?
tag_content(com) -> single_pcdata;
tag_content(comsummary) -> single_pcdata;
tag_content(copyright) -> mixed_content;
tag_content(ctitle) -> single_pcdata;
tag_content(d) -> mixed_content;
tag_content(date) -> single_pcdata;
tag_content(docno) -> single_pcdata;
tag_content(em) -> mixed_content;
tag_content(email) -> single_pcdata;
tag_content(fascicule) -> single_pcdata;
tag_content(file) -> single_pcdata;
tag_content(filesummary) -> single_pcdata;
tag_content(fsummary) -> mixed_content;
tag_content(headline) -> single_pcdata;
tag_content(holder) -> single_pcdata;
tag_content(i) -> single_pcdata;
tag_content(icaption) -> single_pcdata;
tag_content(id) -> single_pcdata;
tag_content(input) -> mixed_content;
tag_content(item) -> mixed_content;
tag_content(legalnotice) -> single_pcdata;
tag_content(lib) -> single_pcdata;
tag_content(libsummary) -> single_pcdata;
tag_content(module) -> single_pcdata;
tag_content(modulesummary) -> single_pcdata;
tag_content(name) -> single_pcdata;
tag_content(nametext) -> single_pcdata;
tag_content(p) -> mixed_content;
tag_content(pagetext) -> single_pcdata;
tag_content(path) -> single_pcdata; % mixed?
tag_content(pre) -> mixed_content;
tag_content(prepared) -> single_pcdata;
tag_content(resp) -> single_pcdata;
tag_content(responsible) -> single_pcdata;
tag_content(ret) -> single_pcdata;
tag_content(rev) -> single_pcdata;
tag_content(seealso) -> single_pcdata; % mixed?
tag_content(shortdef) -> single_pcdata;
tag_content(shorttitle) -> single_pcdata;
tag_content(tag) -> mixed_content;
tag_content(tcaption) -> single_pcdata;
tag_content(termdef) -> single_pcdata;
tag_content(title) -> single_pcdata;
tag_content(url) -> single_pcdata; % mixed
tag_content(v) -> single_pcdata;
tag_content(year) -> single_pcdata;
tag_content(_) -> no_pcdata.
|