]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/xml/xmlPushParser.ml
moved here xmlPushParser.ml and corresponding test
[helm.git] / helm / ocaml / xml / xmlPushParser.ml
1 (* Copyright (C) 2004-2005, 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://helm.cs.unibo.it/
24  *)
25
26 let gzip_bufsize = 10240
27
28 type callbacks = {
29   start_element: (string -> (string * string) list -> unit) option;
30   end_element: (string -> unit) option;
31   character_data: (string -> unit) option;
32   processing_instruction: (string -> string -> unit) option;
33   comment: (string -> unit) option;
34 }
35
36 let default_callbacks = {
37   start_element = None;
38   end_element = None;
39   character_data = None;
40   processing_instruction = None;
41   comment = None;
42 }
43
44 type xml_source =
45   [ `Channel of in_channel
46   | `File of string
47   | `Gzip_channel of Gzip.in_channel
48   | `Gzip_file of string
49   | `String of string
50   ]
51
52 type position = int * int
53
54 type xml_parser = Expat.expat_parser
55
56 let create_parser callbacks =
57   let expat_parser = Expat.parser_create ~encoding:None in
58   (match callbacks.start_element with
59   | Some f -> Expat.set_start_element_handler expat_parser f
60   | _ -> ());
61   (match callbacks.end_element with
62   | Some f -> Expat.set_end_element_handler expat_parser f
63   | _ -> ());
64   (match callbacks.character_data with
65   | Some f -> Expat.set_character_data_handler expat_parser f
66   | _ -> ());
67   (match callbacks.processing_instruction with
68   | Some f -> Expat.set_processing_instruction_handler expat_parser f
69   | _ -> ());
70   (match callbacks.comment with
71   | Some f -> Expat.set_comment_handler expat_parser f
72   | _ -> ());
73   expat_parser
74
75 let final = Expat.final
76
77 let get_position expat_parser =
78   (Expat.get_current_line_number expat_parser,
79    Expat.get_current_column_number expat_parser)
80
81 let parse expat_parser =
82   let parse_fun = Expat.parse expat_parser in
83   let rec aux = function
84     | `Channel ic ->
85         (try
86           while true do parse_fun (input_line ic ^ "\n") done
87         with End_of_file -> final expat_parser)
88     | `File fname ->
89         let ic = open_in fname in
90         aux (`Channel ic);
91         close_in ic
92     | `Gzip_channel ic ->
93         let buf = String.create gzip_bufsize in
94         (try
95           while true do
96             let bytes = Gzip.input ic buf 0 gzip_bufsize in
97             if bytes = 0 then raise End_of_file;
98             parse_fun (String.sub buf 0 bytes)
99           done
100         with End_of_file -> final expat_parser)
101     | `Gzip_file fname ->
102         let ic = Gzip.open_in fname in
103         aux (`Gzip_channel ic);
104         Gzip.close_in ic
105     | `String s -> parse_fun s
106   in
107   aux
108