]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/xml2Gdome.ml
* the transformations have been ported so to generate BoxML + MathML
[helm.git] / helm / ocaml / cic_transformations / xml2Gdome.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 let document_of_xml (domImplementation : Gdome.domImplementation) strm =
27 prerr_endline "LUCA: entro nella document_of_xml" ;
28  let module G = Gdome in
29  let module X = Xml in
30   let rec update_namespaces ((defaultns,bindings) as namespaces) =
31    function
32       [] -> namespaces
33     | (None,"xmlns",value)::tl ->
34        update_namespaces (Some (Gdome.domString value),bindings) tl
35     | (prefix,name,value)::tl when prefix = Some "xmlns"  ->
36         update_namespaces (defaultns,(name,Gdome.domString value)::bindings) tl
37     | _::tl -> update_namespaces namespaces tl in
38   let rec namespace_of_prefix (defaultns,bindings) =
39    function
40       None -> None
41     | Some "xmlns" -> Some (Gdome.domString "xml-ns")
42     | Some p' ->
43        try
44         Some (List.assoc p' bindings)
45        with
46         Not_found ->
47          raise
48           (Failure ("The prefix " ^ p' ^ " is not bound to any namespace")) in
49   let get_qualified_name p n =
50    match p with
51       None -> Gdome.domString n
52     | Some p' -> Gdome.domString (p' ^ ":" ^ n) in
53   let root_prefix,root_name,root_attributes,root_content =
54    ignore (Stream.next strm) ; (* to skip the <?xml ...?> declaration *)
55    ignore (Stream.next strm) ; (* to skip the DOCTYPE declaration *)
56    match Stream.next strm with
57       X.Empty(p,n,l) -> p,n,l,[<>]
58     | X.NEmpty(p,n,l,c) -> p,n,l,c
59     | _ -> assert false
60   in
61 prerr_endline "LUCA: entro nella document_of_xml interna" ;
62    let namespaces = update_namespaces (None,[]) root_attributes in
63    let namespaceURI = namespace_of_prefix namespaces root_prefix in
64    let document =
65     domImplementation#createDocument ~namespaceURI
66      ~qualifiedName:(get_qualified_name root_prefix root_name)
67      ~doctype:None
68    in
69    let rec aux namespaces (node : Gdome.node) =
70     parser
71       [< 'X.Str a ; s >] ->
72         let textnode = document#createTextNode ~data:(Gdome.domString a) in
73          ignore (node#appendChild ~newChild:(textnode :> Gdome.node)) ;
74          aux namespaces node s
75     | [< 'X.Empty(p,n,l) ; s >] ->
76         let namespaces' = update_namespaces namespaces l in
77          let namespaceURI = namespace_of_prefix namespaces' p in
78           let element =
79            document#createElementNS ~namespaceURI
80             ~qualifiedName:(get_qualified_name p n)
81           in
82            List.iter
83             (function (p,n,v) ->
84               if p = None then
85                element#setAttribute ~name:(Gdome.domString n)
86                 ~value:(Gdome.domString v)
87               else
88                let namespaceURI = namespace_of_prefix namespaces' p in
89                 element#setAttributeNS
90                  ~namespaceURI
91                  ~qualifiedName:(get_qualified_name p n)
92                  ~value:(Gdome.domString v)
93             ) l ;
94           ignore
95            (node#appendChild
96              ~newChild:(element : Gdome.element :> Gdome.node)) ;
97           aux namespaces node s
98     | [< 'X.NEmpty(p,n,l,c) ; s >] ->
99         let namespaces' = update_namespaces namespaces l in
100          let namespaceURI = namespace_of_prefix namespaces' p in
101           let element =
102            document#createElementNS ~namespaceURI
103             ~qualifiedName:(get_qualified_name p n)
104           in
105            List.iter
106             (function (p,n,v) ->
107               if p = None then
108                element#setAttribute ~name:(Gdome.domString n)
109                 ~value:(Gdome.domString v)
110               else
111                let namespaceURI = namespace_of_prefix namespaces' p in
112                 element#setAttributeNS ~namespaceURI
113                  ~qualifiedName:(get_qualified_name p n)
114                  ~value:(Gdome.domString v)
115             ) l ;
116            ignore (node#appendChild ~newChild:(element :> Gdome.node)) ;
117            aux namespaces' (element :> Gdome.node) c ;
118            aux namespaces node s
119     | [< >] -> ()
120    in
121     let root = document#get_documentElement in
122      List.iter
123       (function (p,n,v) ->
124         if p = None then
125          root#setAttribute ~name:(Gdome.domString n)
126           ~value:(Gdome.domString v)
127         else
128          let namespaceURI = namespace_of_prefix namespaces p in
129           root#setAttributeNS ~namespaceURI
130            ~qualifiedName:(get_qualified_name p n)
131            ~value:(Gdome.domString v)
132       ) root_attributes ;
133 prerr_endline "LUCA: prima di aux" ;
134      aux namespaces (root : Gdome.element :> Gdome.node) root_content ;
135 prerr_endline "LUCA: dopo di aux" ;
136      document
137 ;;