]> matita.cs.unibo.it Git - helm.git/blob - components/xml/xml.ml
tagged 0.5.0-rc1
[helm.git] / components / 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 (* $Id$ *)
40
41
42 (* the type token for XML cdata, empty elements and not-empty elements *)
43 (* Usage:                                                             *)
44 (*  Str cdata                                                         *)
45 (*  Empty (prefix, element_name,                                      *)
46 (*   [prefix1, attrname1, value1 ; ... ; prefixn, attrnamen, valuen]  *)
47 (*  NEmpty (prefix, element_name,                                     *)
48 (*   [prefix1, attrname1, value1 ; ... ; prefixn, attrnamen, valuen], *)
49 (*    content                                                         *)
50 type token =
51    Str of string
52  | Empty of string option * string * (string option * string * string) list
53  | NEmpty of string option * string * (string option * string * string) list *
54     token Stream.t
55 ;;
56
57 (* currified versions of the constructors make the code more readable *)
58 let xml_empty ?prefix name attrs =
59  [< 'Empty(prefix,name,attrs) >]
60 let xml_nempty ?prefix name attrs content =
61  [< 'NEmpty(prefix,name,attrs,content) >]
62 let xml_cdata str =
63  [< 'Str str >]
64
65 (** low level for other PPs: pretty print each token of strm applying 'f' to a
66 canonical string representation of each token *)
67 let pp_gen f strm =
68  let pprefix =
69   function
70      None -> ""
71    | Some p -> p ^ ":" in
72  let rec pp_r m =
73   parser
74   | [< 'Str a ; s >] ->
75       print_spaces m ;
76       f (a ^ "\n") ;
77       pp_r m s
78   | [< 'Empty(p,n,l) ; s >] ->
79       print_spaces m ;
80       f ("<" ^ (pprefix p) ^ n) ;
81       List.iter (fun (p,n,v) -> f (" " ^ (pprefix p) ^ n ^ "=\"" ^ v ^ "\"")) l;
82       f "/>\n" ;
83       pp_r m s
84   | [< 'NEmpty(p,n,l,c) ; s >] ->
85       print_spaces m ;
86       f ("<" ^ (pprefix p) ^ n) ;
87       List.iter (fun (p,n,v) -> f (" " ^ (pprefix p) ^ n ^ "=\"" ^ v ^ "\"")) l;
88       f ">\n" ;
89       pp_r (m+1) c ;
90       print_spaces m ;
91       f ("</" ^ (pprefix p) ^ n ^ ">\n") ;
92       pp_r m s
93   | [< >] -> ()
94  and print_spaces m =
95   for i = 1 to m do f "  " done
96  in
97  pp_r 0 strm
98 ;;
99
100 (** pretty printer on output channels *)
101 let pp_to_outchan strm oc =
102   pp_gen (fun s -> output_string oc s) strm;
103   flush oc
104 ;;
105
106 let pp_to_gzipchan strm oc =
107   pp_gen (fun s -> Gzip.output oc s 0 (String.length s)) strm
108 ;;
109
110 (** pretty printer to string *)
111 let pp_to_string strm =
112   let buf = Buffer.create 10240 in
113   pp_gen (Buffer.add_string buf) strm;
114   Buffer.contents buf
115 ;;
116
117 (** pretty printer to file *)
118 (* Usage:                                                                   *)
119 (*  pp tokens None     pretty prints the output on stdout                   *)
120 (*  pp tokens (Some filename) pretty prints the output on the file filename *)
121 let pp ?(gzip=false) strm fn =
122   if gzip then
123     match fn with
124     | Some filename ->
125         let outchan = Gzip.open_out filename in
126         begin try
127           pp_to_gzipchan strm outchan;
128         with e ->
129           Gzip.close_out outchan;
130           raise e
131         end;
132         Gzip.close_out outchan;
133         HExtlib.chmod 0o664 filename;
134     | None -> failwith "Can't sent gzipped output to stdout"
135   else
136     match fn with
137     | Some filename ->
138         let outchan = open_out filename in
139         begin try
140           pp_to_outchan strm outchan;
141         with e ->
142           close_out outchan;
143           raise e
144         end;
145         close_out outchan;
146         HExtlib.chmod 0o664 filename
147     | None -> pp_to_outchan strm stdout
148 ;;
149
150 let pp =
151  let profiler = HExtlib.profile "Xml.pp" in
152   fun ?gzip strm fn ->
153    profiler.HExtlib.profile (pp ?gzip strm) fn
154 ;;
155
156 let add_xml_declaration stream =
157   let box_prefix = "b" in
158   [<
159     xml_cdata "<?xml version=\"1.0\" ?>\n" ;
160     xml_cdata "\n";
161     xml_nempty ~prefix:box_prefix "box"
162       [ Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
163         Some "xmlns","b","http://helm.cs.unibo.it/2003/BoxML" ;
164         Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
165         Some "xmlns","xlink","http://www.w3.org/1999/xlink"
166       ] stream
167   >]
168
169   (* TODO BRRRRR .... *)
170   (** strip first 4 line of a string, used to strip xml declaration and doctype
171   declaration from XML strings generated by Xml.pp_to_string *)
172 let strip_xml_headings s =
173   let rec aux n pos =
174     if n = 0
175     then String.sub s pos (String.length s - pos)
176     else aux (n - 1) (String.index_from s pos '\n' + 1)
177   in
178   try
179     aux 4 0
180   with Not_found -> s
181