]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mlminidom/test.ml
- the mathql interpreter is not helm-dependent any more
[helm.git] / helm / DEVEL / mlminidom / test.ml
1 (* Copyright (C) 2000, Luca Padovani <luca.padovani@cs.unibo.it>.
2  *
3  * This file is part of mlminidom, the Ocaml binding for minidom.
4  * 
5  * mlminidom is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * mlminidom is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with mlminidom; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  * 
19  * For details, send a mail to the author.
20  *)
21
22 let doc = Minidom.doc_load "test.xml"
23
24 let root = Minidom.doc_get_root_node doc
25
26 let check_attribute_ns attr =
27   Printf.printf "\n\n";
28   let ns_uri = Minidom.attr_get_ns_uri attr
29   and attr_name = Minidom.attr_get_name attr
30   and attr_value = Minidom.attr_get_value attr
31   and parent = Minidom.attr_get_parent attr
32   in
33   match parent,ns_uri,attr_name,attr_value with
34     Some parent_node,Some uri,Some attribute_name,Some attribute_value ->
35       let attr_info =
36        (Minidom.node_get_attribute_ns parent_node attribute_name uri,
37         Minidom.node_has_attribute_ns parent_node attribute_name uri)
38       in begin
39         match attr_info with
40           Some attr1, true ->
41             Printf.printf "found the attribute with ns %s (was %s)\n"
42               (Minidom.string_of_mDOMString attr1) (Minidom.string_of_mDOMString attribute_value)
43         | None, false ->
44             Printf.printf "attribute not found (uri was %s)!!!!\n" (Minidom.string_of_mDOMString uri)
45         | _,_ -> assert false
46       end
47   | _ ->
48       Printf.printf "parent_node == NULL || uri == NULL || attribute_name == NULL || attribute_value == NULL\n"
49 ;;
50     
51 let print_attribute attr =
52   check_attribute_ns attr;
53   let ns_uri = Minidom.attr_get_ns_uri attr
54   in
55   begin
56     match ns_uri with
57       Some uri -> Printf.printf " %s:" (Minidom.string_of_mDOMString uri);
58     | None -> ()
59   end;
60   match ((Minidom.attr_get_name attr), (Minidom.attr_get_value attr)) with
61     (Some attr_name, Some attr_value) ->
62       Printf.printf " %s=\"%s\"" (Minidom.string_of_mDOMString attr_name) (Minidom.string_of_mDOMString attr_value) 
63   | (Some attr_name, _) ->
64       Printf.printf " ??? attribute %s has no value !!!" (Minidom.string_of_mDOMString attr_name)
65   | (_,_) ->
66       Printf.printf " ??? very strange attribute !!!"
67 ;;
68
69 let rec print_node n node =
70   if Minidom.node_is_blank node then ()
71   else if Minidom.node_is_element node then begin
72     match Minidom.node_get_name node with
73       Some node_name -> 
74         begin
75           let children = Minidom.node_get_children node
76           and attributes = Minidom.node_get_attributes node
77           and ns_uri = Minidom.node_get_ns_uri node
78           and is_first,is_last = (Minidom.node_is_first node), (Minidom.node_is_last node)
79           in
80           for i = 1 to n do print_char ' ' done;
81           Printf.printf "<";
82           begin
83             match ns_uri with
84               Some uri -> Printf.printf "%s:" (Minidom.string_of_mDOMString uri)
85             | None     -> ()
86           end;
87           Printf.printf "%s" (Minidom.string_of_mDOMString node_name);
88           List.iter print_attribute attributes;
89           Printf.printf ">\n";
90           List.iter (print_node (n + 2)) children;
91           for i = 1 to n do print_char ' ' done;
92           Printf.printf "</%s>\n" (Minidom.string_of_mDOMString node_name)
93         end
94     | None -> Printf.printf "??? this node has no name !!!\n"
95   end else if Minidom.node_is_text node then begin
96     match Minidom.node_get_content node with
97       Some node_content ->
98         for i = 1 to n do print_char ' ' done;
99         Printf.printf "%s\n" (Minidom.string_of_mDOMString node_content)
100     | None -> Printf.printf "??? this node has no content !!!\n"
101   end else begin
102     Printf.printf "don't know how to manage a node with type %d\n" (Minidom.node_get_type node)
103   end
104 ;;
105   
106 print_node 0 root;;