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