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/.
28 let document_of_xml (domImplementation : Gdome.domImplementation) strm =
29 let module G = Gdome in
31 let rec update_namespaces ((defaultns,bindings) as namespaces) =
34 | (None,"xmlns",value)::tl ->
35 update_namespaces (Some (Gdome.domString value),bindings) tl
36 | (prefix,name,value)::tl when prefix = Some "xmlns" ->
37 update_namespaces (defaultns,(name,Gdome.domString value)::bindings) tl
38 | _::tl -> update_namespaces namespaces tl in
39 let rec namespace_of_prefix (defaultns,bindings) =
42 | Some "xmlns" -> Some (Gdome.domString "xml-ns")
45 Some (List.assoc p' bindings)
49 (Failure ("The prefix " ^ p' ^ " is not bound to any namespace")) in
50 let get_qualified_name p n =
52 None -> Gdome.domString n
53 | Some p' -> Gdome.domString (p' ^ ":" ^ n) in
54 let root_prefix,root_name,root_attributes,root_content =
55 ignore (Stream.next strm) ; (* to skip the <?xml ...?> declaration *)
56 ignore (Stream.next strm) ; (* to skip the DOCTYPE declaration *)
57 match Stream.next strm with
58 X.Empty(p,n,l) -> p,n,l,[<>]
59 | X.NEmpty(p,n,l,c) -> p,n,l,c
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 aux namespaces (root : Gdome.element :> Gdome.node) root_content ;