]> matita.cs.unibo.it Git - helm.git/blob - components/hgdome/xml2Gdome.ml
tagged 0.5.0-rc1
[helm.git] / components / hgdome / 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 (* $Id$ *)
27
28 let document_of_xml (domImplementation : Gdome.domImplementation) strm =
29  let module G = Gdome in
30  let module X = Xml in
31   let rec update_namespaces ((defaultns,bindings) as namespaces) =
32    function
33       [] -> 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) =
40    function
41       None -> None
42     | Some "xmlns" -> Some (Gdome.domString "xml-ns")
43     | Some p' ->
44        try
45         Some (List.assoc p' bindings)
46        with
47         Not_found ->
48          raise
49           (Failure ("The prefix " ^ p' ^ " is not bound to any namespace")) in
50   let get_qualified_name p n =
51    match p with
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
60     | _ -> assert false
61   in
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      aux namespaces (root : Gdome.element :> Gdome.node) root_content ;
134      document
135 ;;