]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/xml2Gdomexmath.ml
Control Factory modified.
[helm.git] / helm / ocaml / cic_transformations / xml2Gdomexmath.ml
1 (* Copyright (C) 2000-2002, 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 (* cut and paste from xml2Gdome.ml: there was the namespace problem.
27    This is a fst patch: we generate a fixed namespace for math *)
28 let document_of_xml (domImplementation : Gdome.domImplementation) strm =
29  let module G = Gdome in
30  let module X = Xml in
31  let namespace = "http://www.w3.org/1998/Math/MathML" in
32   let root_name,root_attributes,root_content =
33    (* 
34    ignore (Stream.next strm) ; (* to skip the <?xml ...?> declaration *)
35    ignore (Stream.next strm) ; (* to skip the DOCTYPE declaration *)
36    *)
37    match Stream.next strm with
38       X.Empty(n,l) -> n,l,[<>]
39     | X.NEmpty(n,l,c) -> n,l,c
40     | _ -> assert false
41   in
42    let document =
43     domImplementation#createDocument ~namespaceURI:(Some (Gdome.domString namespace))
44      ~qualifiedName:(Gdome.domString ("m:" ^ root_name)) ~doctype:None
45    in
46    document#get_documentElement#setAttribute (Gdome.domString "xmlns:m") (Gdome.domString namespace);
47    let rec aux (node : Gdome.node) =
48     parser
49       [< 'X.Str a ; s >] ->
50         let textnode = document#createTextNode ~data:(Gdome.domString a) in
51          ignore (node#appendChild ~newChild:(textnode :> Gdome.node)) ;
52          aux node s
53     | [< 'X.Empty(n,l) ; s >] ->
54         let element = document#createElementNS
55          ~namespaceURI:(Some (Gdome.domString namespace))
56          ~qualifiedName:(Gdome.domString ("m:" ^ n)) in
57          List.iter 
58            (function (n,v) -> 
59             let i = 
60               (try String.index n ':' 
61                with Not_found -> 0) in
62             if i = 0 then
63               element#setAttribute
64               ~name:(Gdome.domString n) ~value:(Gdome.domString v) 
65             else
66               let ns_label = String.sub n 0 i in
67               let ns = 
68                 if ns_label = "helm" then "http://www.cs.unibo.it/helm"
69                 else if ns_label = "xlink" then "http://www.w3.org/1999/xlink"
70                 else assert false in
71               element#setAttributeNS 
72                ~namespaceURI:(Some (Gdome.domString ns))
73                ~qualifiedName:(Gdome.domString n) 
74                ~value:(Gdome.domString v)) l ;
75          ignore
76           (node#appendChild ~newChild:(element : Gdome.element :> Gdome.node)) ;
77          aux node s
78     | [< 'X.NEmpty(n,l,c) ; s >] ->
79         let element = document#createElementNS 
80          ~namespaceURI:(Some (Gdome.domString namespace))
81          ~qualifiedName:(Gdome.domString ("m:" ^ n)) in
82          List.iter
83           (function (n,v) -> 
84             let i = 
85               (try String.index n ':' 
86                with Not_found -> 0) in
87             if i = 0 then
88               element#setAttribute
89               ~name:(Gdome.domString n) ~value:(Gdome.domString v) 
90             else
91               let ns_label = String.sub n 0 i in
92               let ns = 
93                 if ns_label = "helm" then "http://www.cs.unibo.it/helm"
94                 else if ns_label = "xlink" then "http://www.w3.org/1999/xlink"
95                 else assert false in
96               element#setAttributeNS 
97                ~namespaceURI:(Some (Gdome.domString ns))
98                ~qualifiedName:(Gdome.domString n) 
99                ~value:(Gdome.domString v)) l ;
100          ignore (node#appendChild ~newChild:(element :> Gdome.node)) ;
101          aux (element :> Gdome.node) c ;
102          aux node s
103     | [< >] -> ()
104    in
105     let root = document#get_documentElement in
106      List.iter (function (n,v) -> root#setAttribute
107       ~name:(Gdome.domString n) ~value:(Gdome.domString v)) root_attributes ;
108      aux (root : Gdome.element :> Gdome.node) root_content ;
109      document
110 ;;
111