Debian Codegraph @

ide/rocqide/protocol/xml_printer.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Xml_datatype
12
13 type xml = Xml_datatype.xml
14
15 type target = TChannel of out_channel | TBuffer of Buffer.t
16
17 type t = target
18
19 let make x = x
20
21 let buffer_pcdata tmp text =
22 let puts = Buffer.add_string tmp in
23 let putc = Buffer.add_char tmp in
24 let l = String.length text in
25 for p = 0 to l-1 do
26 match text.[p] with
27 | ' ' -> puts "&nbsp;";
28 | '>' -> puts "&gt;"
29 | '<' -> puts "&lt;"
30 | '&' ->
31 if p < l - 1 && text.[p + 1] = '#' then
32 putc '&'
33 else
34 puts "&amp;"
35 | '\'' -> puts "&apos;"
36 | '"' -> puts "&quot;"
37 | c -> putc c
38 done
39
40 let buffer_attr tmp (n,v) =
41 let puts = Buffer.add_string tmp in
42 let putc = Buffer.add_char tmp in
43 putc ' ';
44 puts n;
45 puts "=\"";
46 let l = String.length v in
47 for p = 0 to l - 1 do
48 match v.[p] with
49 | '\\' -> puts "\\\\"
50 | '"' -> puts "\\\""
51 | '<' -> puts "&lt;"
52 | '&' -> puts "&amp;"
53 | c -> putc c
54 done;
55 putc '"'
56
57 let to_buffer tmp x =
58 let pcdata = ref false in
59 let puts = Buffer.add_string tmp in
60 let putc = Buffer.add_char tmp in
61 let rec loop = function
62 | Element (tag,alist,[]) ->
63 putc '<';
64 puts tag;
65 List.iter (buffer_attr tmp) alist;
66 puts "/>";
67 pcdata := false;
68 | Element (tag,alist,l) ->
69 putc '<';
70 puts tag;
71 List.iter (buffer_attr tmp) alist;
72 putc '>';
73 pcdata := false;
74 List.iter loop l;
75 puts "</";
76 puts tag;
77 putc '>';
78 pcdata := false;
79 | PCData text ->
80 if !pcdata then putc ' ';
81 buffer_pcdata tmp text;
82 pcdata := true;
83 in
84 loop x
85
86 let pcdata_to_string s =
87 let b = Buffer.create 13 in
88 buffer_pcdata b s;
89 Buffer.contents b
90
91 let to_string x =
92 let b = Buffer.create 200 in
93 to_buffer b x;
94 Buffer.contents b
95
96 let to_string_fmt x =
97 let tmp = Buffer.create 200 in
98 let puts = Buffer.add_string tmp in
99 let putc = Buffer.add_char tmp in
100 let rec loop ?(newl=false) tab = function
101 | Element (tag, alist, []) ->
102 puts tab;
103 putc '<';
104 puts tag;
105 List.iter (buffer_attr tmp) alist;
106 puts "/>";
107 if newl then putc '\n';
108 | Element (tag, alist, [PCData text]) ->
109 puts tab;
110 putc '<';
111 puts tag;
112 List.iter (buffer_attr tmp) alist;
113 puts ">";
114 buffer_pcdata tmp text;
115 puts "</";
116 puts tag;
117 putc '>';
118 if newl then putc '\n';
119 | Element (tag, alist, l) ->
120 puts tab;
121 putc '<';
122 puts tag;
123 List.iter (buffer_attr tmp) alist;
124 puts ">\n";
125 List.iter (loop ~newl:true (tab^" ")) l;
126 puts tab;
127 puts "</";
128 puts tag;
129 putc '>';
130 if newl then putc '\n';
131 | PCData text ->
132 buffer_pcdata tmp text;
133 if newl then putc '\n';
134 in
135 loop "" x;
136 Buffer.contents tmp
137
138 let print t xml =
139 let tmp, flush = match t with
140 | TChannel oc ->
141 let b = Buffer.create 200 in
142 b, (fun () -> Buffer.output_buffer oc b; flush oc)
143 | TBuffer b ->
144 b, (fun () -> ())
145 in
146 to_buffer tmp xml;
147 flush ()