]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic/xmlPushParser.ml
- implemented CicPushParser which parser CIC objects using Expat
[helm.git] / helm / ocaml / cic / 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 type callbacks = {
27   start_element: (string -> (string * string) list -> unit) option;
28   end_element: (string -> unit) option;
29   character_data: (string -> unit) option;
30   processing_instruction: (string -> string -> unit) option;
31   comment: (string -> unit) option;
32 }
33
34 let default_callbacks = {
35   start_element = None;
36   end_element = None;
37   character_data = None;
38   processing_instruction = None;
39   comment = None;
40 }
41
42 type xml_source =
43   [ `Channel of in_channel
44   | `File of string
45   | `String of string
46   ]
47
48 type position = int * int
49
50 type xml_parser = Expat.expat_parser
51
52 let create_parser callbacks =
53   let expat_parser = Expat.parser_create ~encoding:None in
54   (match callbacks.start_element with
55   | Some f -> Expat.set_start_element_handler expat_parser f
56   | _ -> ());
57   (match callbacks.end_element with
58   | Some f -> Expat.set_end_element_handler expat_parser f
59   | _ -> ());
60   (match callbacks.character_data with
61   | Some f -> Expat.set_character_data_handler expat_parser f
62   | _ -> ());
63   (match callbacks.processing_instruction with
64   | Some f -> Expat.set_processing_instruction_handler expat_parser f
65   | _ -> ());
66   (match callbacks.comment with
67   | Some f -> Expat.set_comment_handler expat_parser f
68   | _ -> ());
69   expat_parser
70
71 let final = Expat.final
72
73 let get_position expat_parser =
74   (Expat.get_current_line_number expat_parser,
75    Expat.get_current_column_number expat_parser)
76
77 let parse expat_parser =
78   let parse_fun = Expat.parse expat_parser in
79   let rec aux = function
80     | `Channel ic ->
81         (try
82           while true do parse_fun (input_line ic ^ "\n") done
83         with End_of_file -> final expat_parser)
84     | `File fname ->
85         let ic = open_in fname in
86         aux (`Channel ic);
87         close_in ic
88     | `String s -> parse_fun s
89   in
90   aux
91