]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/xml/xml.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / xml / xml.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (******************************************************************************)
27 (*                                                                            *)
28 (*                               PROJECT HELM                                 *)
29 (*                                                                            *)
30 (*                     A tactic to print Coq objects in XML                   *)
31 (*                                                                            *)
32 (*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
33 (*                                 18/10/2000                                 *)
34 (*                                                                            *)
35 (* This module defines a pretty-printer and the stream of commands to the pp  *)
36 (*                                                                            *)
37 (******************************************************************************)
38
39
40 (* the type token for XML cdata, empty elements and not-empty elements *)
41 (* Usage:                                                             *)
42 (*  Str cdata                                                         *)
43 (*  Empty (prefix, element_name,                                      *)
44 (*   [prefix1, attrname1, value1 ; ... ; prefixn, attrnamen, valuen]  *)
45 (*  NEmpty (prefix, element_name,                                     *)
46 (*   [prefix1, attrname1, value1 ; ... ; prefixn, attrnamen, valuen], *)
47 (*    content                                                         *)
48 type token =
49    Str of string
50  | Empty of string option * string * (string option * string * string) list
51  | NEmpty of string option * string * (string option * string * string) list *
52     token Stream.t
53 ;;
54
55 (* currified versions of the constructors make the code more readable *)
56 let xml_empty ?prefix name attrs =
57  [< 'Empty(prefix,name,attrs) >]
58 let xml_nempty ?prefix name attrs content =
59  [< 'NEmpty(prefix,name,attrs,content) >]
60 let xml_cdata str =
61  [< 'Str str >]
62
63 (** low level for other PPs: pretty print each token of strm applying 'f' to a
64 canonical string representation of each token *)
65 let pp_gen f strm =
66  let pprefix =
67   function
68      None -> ""
69    | Some p -> p ^ ":" in
70  let rec pp_r m =
71   parser
72   | [< 'Str a ; s >] ->
73       print_spaces m ;
74       f (a ^ "\n") ;
75       pp_r m s
76   | [< 'Empty(p,n,l) ; s >] ->
77       print_spaces m ;
78       f ("<" ^ (pprefix p) ^ n) ;
79       List.iter (fun (p,n,v) -> f (" " ^ (pprefix p) ^ n ^ "=\"" ^ v ^ "\"")) l;
80       f "/>\n" ;
81       pp_r m s
82   | [< 'NEmpty(p,n,l,c) ; s >] ->
83       print_spaces m ;
84       f ("<" ^ (pprefix p) ^ n) ;
85       List.iter (fun (p,n,v) -> f (" " ^ (pprefix p) ^ n ^ "=\"" ^ v ^ "\"")) l;
86       f ">\n" ;
87       pp_r (m+1) c ;
88       print_spaces m ;
89       f ("</" ^ (pprefix p) ^ n ^ ">\n") ;
90       pp_r m s
91   | [< >] -> ()
92  and print_spaces m =
93   for i = 1 to m do f "  " done
94  in
95  pp_r 0 strm
96 ;;
97
98 (** pretty printer on output channels *)
99 let pp_to_outchan strm oc =
100   pp_gen (fun s -> output_string oc s) strm;
101   flush oc
102 ;;
103
104 let pp_to_gzipchan strm oc =
105   pp_gen (fun s -> Gzip.output oc s 0 (String.length s)) strm
106 ;;
107
108 (** pretty printer to string *)
109 let pp_to_string strm =
110   let buf = Buffer.create 10240 in
111   pp_gen (Buffer.add_string buf) strm;
112   Buffer.contents buf
113 ;;
114
115 (** pretty printer to file *)
116 (* Usage:                                                                   *)
117 (*  pp tokens None     pretty prints the output on stdout                   *)
118 (*  pp tokens (Some filename) pretty prints the output on the file filename *)
119 let pp ?(gzip=false) strm fn =
120   if gzip then
121     match fn with
122     | Some filename ->
123         let outchan = Gzip.open_out filename in
124         (try
125           pp_to_gzipchan strm outchan;
126         with e ->
127           Gzip.close_out outchan;
128           raise e);
129         Gzip.close_out outchan
130     | None -> failwith "Can't sent gzipped output to stdout"
131   else
132     match fn with
133     | Some filename ->
134         let outchan = open_out filename in
135         (try
136           pp_to_outchan strm outchan;
137         with e ->
138           close_out outchan;
139           raise e);
140         close_out outchan
141     | None -> pp_to_outchan strm stdout
142 ;;
143
144 let pp =
145  let profiler = HExtlib.profile "Xml.pp" in
146   fun ?gzip strm fn ->
147    profiler.HExtlib.profile (pp ?gzip strm) fn
148 ;;
149
150 let add_xml_declaration stream =
151   let box_prefix = "b" in
152   [<
153     xml_cdata "<?xml version=\"1.0\" ?>\n" ;
154     xml_cdata "\n";
155     xml_nempty ~prefix:box_prefix "box"
156       [ Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
157         Some "xmlns","b","http://helm.cs.unibo.it/2003/BoxML" ;
158         Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
159         Some "xmlns","xlink","http://www.w3.org/1999/xlink"
160       ] stream
161   >]
162