1 (* Copyright (C) 2000-2002, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 let document_of_xml (domImplementation : Gdome.domImplementation) strm =
27 prerr_endline "LUCA: entro nella document_of_xml" ;
28 let module G = Gdome in
30 let rec update_namespaces ((defaultns,bindings) as 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) =
41 | Some "xmlns" -> Some (Gdome.domString "xml-ns")
44 Some (List.assoc p' bindings)
48 (Failure ("The prefix " ^ p' ^ " is not bound to any namespace")) in
49 let get_qualified_name p n =
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
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
65 domImplementation#createDocument ~namespaceURI
66 ~qualifiedName:(get_qualified_name root_prefix root_name)
69 let rec aux namespaces (node : Gdome.node) =
72 let textnode = document#createTextNode ~data:(Gdome.domString a) in
73 ignore (node#appendChild ~newChild:(textnode :> Gdome.node)) ;
75 | [< 'X.Empty(p,n,l) ; s >] ->
76 let namespaces' = update_namespaces namespaces l in
77 let namespaceURI = namespace_of_prefix namespaces' p in
79 document#createElementNS ~namespaceURI
80 ~qualifiedName:(get_qualified_name p n)
85 element#setAttribute ~name:(Gdome.domString n)
86 ~value:(Gdome.domString v)
88 let namespaceURI = namespace_of_prefix namespaces' p in
89 element#setAttributeNS
91 ~qualifiedName:(get_qualified_name p n)
92 ~value:(Gdome.domString v)
96 ~newChild:(element : Gdome.element :> Gdome.node)) ;
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
102 document#createElementNS ~namespaceURI
103 ~qualifiedName:(get_qualified_name p n)
108 element#setAttribute ~name:(Gdome.domString n)
109 ~value:(Gdome.domString v)
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)
116 ignore (node#appendChild ~newChild:(element :> Gdome.node)) ;
117 aux namespaces' (element :> Gdome.node) c ;
118 aux namespaces node s
121 let root = document#get_documentElement in
125 root#setAttribute ~name:(Gdome.domString n)
126 ~value:(Gdome.domString v)
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)
133 prerr_endline "LUCA: prima di aux" ;
134 aux namespaces (root : Gdome.element :> Gdome.node) root_content ;
135 prerr_endline "LUCA: dopo di aux" ;