X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fcic_transformations%2Fxml2Gdome.ml;h=3d07bf21cc37aaa97b9e86b1fe44d313a6ffe36c;hb=6f65a2e518d723ea722b23bfd9fa0162ff8be457;hp=c4e9445ebbe74a583225e7ea25ac3ef8de5b9558;hpb=b266dce15b2f669a70daaee3bd0887f8d9c345b2;p=helm.git diff --git a/helm/ocaml/cic_transformations/xml2Gdome.ml b/helm/ocaml/cic_transformations/xml2Gdome.ml index c4e9445eb..3d07bf21c 100644 --- a/helm/ocaml/cic_transformations/xml2Gdome.ml +++ b/helm/ocaml/cic_transformations/xml2Gdome.ml @@ -26,46 +26,108 @@ let document_of_xml (domImplementation : Gdome.domImplementation) strm = let module G = Gdome in let module X = Xml in - let root_name,root_attributes,root_content = + let rec update_namespaces ((defaultns,bindings) as namespaces) = + function + [] -> namespaces + | (None,"xmlns",value)::tl -> + update_namespaces (Some (Gdome.domString value),bindings) tl + | (prefix,name,value)::tl when prefix = Some "xmlns" -> + update_namespaces (defaultns,(name,Gdome.domString value)::bindings) tl + | _::tl -> update_namespaces namespaces tl in + let rec namespace_of_prefix (defaultns,bindings) = + function + None -> None + | Some "xmlns" -> Some (Gdome.domString "xml-ns") + | Some p' -> + try + Some (List.assoc p' bindings) + with + Not_found -> + raise + (Failure ("The prefix " ^ p' ^ " is not bound to any namespace")) in + let get_qualified_name p n = + match p with + None -> Gdome.domString n + | Some p' -> Gdome.domString (p' ^ ":" ^ n) in + let root_prefix,root_name,root_attributes,root_content = ignore (Stream.next strm) ; (* to skip the declaration *) ignore (Stream.next strm) ; (* to skip the DOCTYPE declaration *) match Stream.next strm with - X.Empty(n,l) -> n,l,[<>] - | X.NEmpty(n,l,c) -> n,l,c + X.Empty(p,n,l) -> p,n,l,[<>] + | X.NEmpty(p,n,l,c) -> p,n,l,c | _ -> assert false in + let namespaces = update_namespaces (None,[]) root_attributes in + let namespaceURI = namespace_of_prefix namespaces root_prefix in let document = - domImplementation#createDocument ~namespaceURI:None - ~qualifiedName:(Gdome.domString root_name) ~doctype:None + domImplementation#createDocument ~namespaceURI + ~qualifiedName:(get_qualified_name root_prefix root_name) + ~doctype:None in - let rec aux (node : Gdome.node) = + let rec aux namespaces (node : Gdome.node) = parser [< 'X.Str a ; s >] -> let textnode = document#createTextNode ~data:(Gdome.domString a) in ignore (node#appendChild ~newChild:(textnode :> Gdome.node)) ; - aux node s - | [< 'X.Empty(n,l) ; s >] -> - let element = document#createElement ~tagName:(Gdome.domString n) in - List.iter (function (n,v) -> element#setAttribute - ~name:(Gdome.domString n) ~value:(Gdome.domString v)) l ; - ignore - (node#appendChild ~newChild:(element : Gdome.element :> Gdome.node)) ; - aux node s - | [< 'X.NEmpty(n,l,c) ; s >] -> - let element = document#createElement ~tagName:(Gdome.domString n) in - List.iter - (function (n,v) -> - element#setAttribute ~name:(Gdome.domString n) - ~value:(Gdome.domString v) - ) l ; - ignore (node#appendChild ~newChild:(element :> Gdome.node)) ; - aux (element :> Gdome.node) c ; - aux node s + aux namespaces node s + | [< 'X.Empty(p,n,l) ; s >] -> + let namespaces' = update_namespaces namespaces l in + let namespaceURI = namespace_of_prefix namespaces' p in + let element = + document#createElementNS ~namespaceURI + ~qualifiedName:(get_qualified_name p n) + in + List.iter + (function (p,n,v) -> + if p = None then + element#setAttribute ~name:(Gdome.domString n) + ~value:(Gdome.domString v) + else + let namespaceURI = namespace_of_prefix namespaces' p in + element#setAttributeNS + ~namespaceURI + ~qualifiedName:(get_qualified_name p n) + ~value:(Gdome.domString v) + ) l ; + ignore + (node#appendChild + ~newChild:(element : Gdome.element :> Gdome.node)) ; + aux namespaces node s + | [< 'X.NEmpty(p,n,l,c) ; s >] -> + let namespaces' = update_namespaces namespaces l in + let namespaceURI = namespace_of_prefix namespaces' p in + let element = + document#createElementNS ~namespaceURI + ~qualifiedName:(get_qualified_name p n) + in + List.iter + (function (p,n,v) -> + if p = None then + element#setAttribute ~name:(Gdome.domString n) + ~value:(Gdome.domString v) + else + let namespaceURI = namespace_of_prefix namespaces' p in + element#setAttributeNS ~namespaceURI + ~qualifiedName:(get_qualified_name p n) + ~value:(Gdome.domString v) + ) l ; + ignore (node#appendChild ~newChild:(element :> Gdome.node)) ; + aux namespaces' (element :> Gdome.node) c ; + aux namespaces node s | [< >] -> () in let root = document#get_documentElement in - List.iter (function (n,v) -> root#setAttribute - ~name:(Gdome.domString n) ~value:(Gdome.domString v)) root_attributes ; - aux (root : Gdome.element :> Gdome.node) root_content ; + List.iter + (function (p,n,v) -> + if p = None then + root#setAttribute ~name:(Gdome.domString n) + ~value:(Gdome.domString v) + else + let namespaceURI = namespace_of_prefix namespaces p in + root#setAttributeNS ~namespaceURI + ~qualifiedName:(get_qualified_name p n) + ~value:(Gdome.domString v) + ) root_attributes ; + aux namespaces (root : Gdome.element :> Gdome.node) root_content ; document ;;