-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcxml.ml
278 lines (233 loc) · 7.21 KB
/
cxml.ml
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
(*
* $Id$
* Copyright (c) 2003, Hugues Cassé <[email protected]>
*
* Pretty printer of XML document.
*)
(** Provide types and pretty printing for XML.
*)
(** Attribute representation.
*)
type attr = string * string
(** Representation of nodes.
*)
type node =
| TEXT of string (** Simple text *)
| COM of string (** Commentary *)
| PI of string * string (** Processing instruction *)
| ELT of string * attr list * node list (** Element *)
(** Representation of an XML document.
*)
type document = {
version: string; (** Version, usually 1.0 *)
encoding: string; (** Encoding (only current encoding supported now) *)
standalone: bool; (** Standalone attribute *)
element: node; (** Document root element *)
}
(** [validate_identifiers elt] validates that identifiers in the
document are unique and non-empty.
@since 4.1.0 *)
let validate_identifiers toplevel =
let open Set.Make(String) in
let rec node ids x ~accept ~reject = match x with
| TEXT _ | COM _ | PI _ -> accept ids
| ELT (_,attrs,xs) as elt ->
match List.assoc_opt "id" attrs with
| None -> nodes ids xs ~accept ~reject
| Some "" -> reject elt
| Some id -> if mem id ids
then reject elt
else nodes (add id ids) xs ~reject ~accept
and nodes ids xs ~accept ~reject = match xs with
| [] -> accept ids
| x :: xs -> node ids x ~reject ~accept:(fun ids ->
nodes ids xs ~accept ~reject) in
node empty toplevel
~accept:(fun _ -> Ok toplevel)
~reject:(fun elt -> Error elt)
(** [deduplicate elt] traverses [elt] and removes all
sub-elements with duplicating identifiers.
When several elements have the same identifier the first (in the
DSF traversal order) element is preserved and all subsequent are
removed.
If all children of a parent are duplicates of some other elements,
then the father is still preserved with an empty list of children
(unless it is itself a duplicate of some other element).
@since 4.1.0 *)
let deduplicate top =
let module Ids = Set.Make(String) in
let check_id ids attrs = match List.assoc_opt "id" attrs with
| None -> Ok ids
| Some id ->
if Ids.mem id ids then Error ()
else Ok (Ids.add id ids) in
let rec node ids elt ~accept ~reject = match elt with
| TEXT _ | COM _ | PI _ as elt -> accept ids elt
| ELT (name,attrs,xs) -> match check_id ids attrs with
| Error () -> reject ()
| Ok ids -> nodes ids xs @@ fun ids xs ->
accept ids (ELT (name,attrs,xs))
and nodes ids xs accept = match xs with
| [] -> accept ids []
| x :: xs ->
node ids x
~reject:(fun () -> nodes ids xs accept)
~accept:(fun ids x -> nodes ids xs @@ fun ids xs ->
accept ids (x::xs)) in
node Ids.empty top ~accept:(fun _ x -> x) ~reject:(fun () -> top)
(** Build a simple document with default initialization.
@param elt Main element of the document.
*)
let new_simple_doc elt: document =
{
version = "1.0";
encoding = "iso-8859-1";
standalone = true;
element = deduplicate elt
}
(** Build a full document.
@param vers XML version.
@param enc Document encoding.
@param sa Stand-alone attribute.
@param elt Document element.
*)
let new_doc vers enc sa elt: document =
{
version = vers;
encoding = enc;
standalone = sa;
element = deduplicate elt
}
(** Build an attribute.
@param name Name of the attribute.
@param cont Content of the attribute.
*)
let new_attr name cont: attr = (name, cont)
(** Build a new element.
@param name Name of the element.
@param attrs Attributes.
@param children Children nodes.
*)
let new_elt name attrs children = ELT(name, attrs, children)
(** Build a new text node.
@param text Content of the node.
*)
let new_text text = TEXT text
(** Add children to an element node.
@param node Element to add to.
@param children Children to add.
@return Passed element with children added to the end.
*)
let add_children node children =
match node with
| ELT (name, attrs, orig_children) ->
ELT (name, attrs, List.append orig_children children)
| _ -> raise (Invalid_argument "not an element")
(** Escape the given attribute value for output.
@param text Text of the attribute.
@param quote Quote character used for the attribute, either '"' or '\''.
*)
let escape_attr text quote =
let buf = Buffer.create 32 in
let rec perform i =
if i >= String.length text then Buffer.contents buf else
begin
(match String.get text i with
'&'
-> Buffer.add_string buf "&"
| '<'
-> Buffer.add_string buf "<"
| c when c = quote
-> Buffer.add_string buf
(if quote = '"' then ""e;" else "'")
| c
-> Buffer.add_char buf c);
perform (i + 1)
end in
perform 0
(** Output an attribute.
@param out Output channel.
@param name Name of the attribute.
@param text Value of the attribute.
*)
let output_attr out (name, text) =
output_char out ' ';
output_string out name;
output_string out "=\"";
output_string out (escape_attr text '"');
output_char out '"'
(** Output a node on the given channel.
@param out Channel to output to.
@param node Node to output.
@param indent Indentation.
*)
let rec output_node out indent node =
let output str = output_string out str in
let rec only_text children =
match children with
[] -> true
| (TEXT _)::tl -> only_text tl
| _ -> false in
match node with
TEXT text
-> output text
| COM text
-> begin
output indent;
output "<!--";
output text;
output "-->"
end
| PI (id, data)
-> begin
output indent;
output "<!";
output id;
output " ";
output data;
output ">"
end
| ELT(name, attrs, children)
-> begin
output indent;
output "<";
output name;
List.iter (output_attr out) attrs;
if children = []
then output "/>"
else begin
output ">";
List.iter (output_node out (indent ^ "\t")) children;
if not (only_text children) then output indent;
output "</";
output name;
output ">"
end;
end
(** Output an XML document to the given output channel.
@param out Output channel.
@param doc Document to output.
*)
let output_doc out doc =
output_string out "<?xml version=\"";
output_string out doc.version;
output_string out "\" encoding=\"";
output_string out doc.encoding;
output_string out "\" standalone=\"";
output_string out (if doc.standalone then "yes" else "no");
output_string out "\"?>";
output_node out "\n" doc.element;
output_char out '\n'
(** Output the given XML document on the standard output.
@param doc XML document to output.
*)
let output doc = output_doc stdout doc
(** Output the given XML document on the named file.
@param filename Path to the file to write to.
@param doc XML document to output.
@raise Sys_error In case of error during opening of the file.
*)
let output_file filename doc =
let out = open_out filename in
output_doc out doc;
close_out out