]> matita.cs.unibo.it Git - helm.git/blob - helm/hxp/hxpXML.ml
ocaml 3.09 transition
[helm.git] / helm / hxp / hxpXML.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://www.cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 module T = HxpTypes
30
31 module type Type = sig
32
33    type handle
34    
35    val start     : string -> bool -> handle
36
37    val stop     : handle -> unit 
38
39    val next     : handle -> T.xml_object   
40
41    val xnext    : handle -> handle * T.xml_xobject
42
43    val scan_for : handle -> T.xml_xobject -> handle
44
45 end 
46
47 module Make : Type = struct 
48
49    module L = HxpLexer
50    
51    module P = HxpParser
52
53    type mode_t = Text of in_channel * Lexing.lexbuf 
54                | GZip of Gzip.in_channel * Lexing.lexbuf
55
56    type handle = {mode : mode_t; level : int}
57                
58    let start s filter =
59       let gread gch s n = Gzip.input gch s 0 n in
60       if filter then    
61          let gch = Gzip.open_in (s ^ ".gz") in 
62          let glb = Lexing.from_function (gread gch) in 
63          {mode = GZip (gch, glb); level = 0}
64       else
65          let ch = open_in s in
66          let lb = Lexing.from_channel ch in 
67          {mode = Text (ch, lb); level = 0}
68
69    let stop = function
70       | {mode = GZip (gch, _)} -> Gzip.close_in gch
71       | {mode = Text (ch, _)}  -> close_in ch
72
73    let next h =
74       let lexbuf = match h.mode with
75          | GZip (_, glb) -> glb
76          | Text (_, lb)  -> lb
77       in
78       P.xml L.xml_token lexbuf
79
80    let xnext h =
81       let obj = next h in
82       match obj with
83       | T.XML_Open _  -> {h with level = succ h.level}, (obj, h.level)
84       | T.XML_Close _ -> {h with level = pred h.level}, (obj, pred h.level)
85       | _             -> h, (obj, h.level)
86
87    let rec scan_for h xobj = 
88       let h, curr_xobj = xnext h in
89       if curr_xobj = xobj then h else scan_for h xobj
90
91 end