1 (***********************************************************************)
5 (* Kim Nguyen, LRI UMR8623 *)
6 (* Université Paris-Sud & CNRS *)
8 (* Copyright 2010-2012 Université Paris-Sud and Centre National de la *)
9 (* Recherche Scientifique. All rights reserved. This file is *)
10 (* distributed under the terms of the GNU Lesser General Public *)
11 (* License, with the special exception on linking described in file *)
14 (***********************************************************************)
19 mutable data : string;
20 mutable first_child : node;
21 mutable next_sibling : node;
36 let dummy_tag = QName.of_string "#dummy"
49 (* TODO add other intersting stuff *)
58 mutable stack : node list;
59 text_buffer : Buffer.t;
60 mutable current_preorder : int;
63 let print_node_ptr fmt n =
64 Format.fprintf fmt "%s"
65 (if n == nil then "<NIL>" else
66 if n == dummy then "<DUMMY>" else
67 "<NODE " ^ string_of_int n.preorder ^ ">")
69 let debug_node fmt node =
70 Format.fprintf fmt "{ tag=%s; preorder=%i; data=%s; first_child=%a; next_sibling=%a; parent=%a }"
71 (QName.to_string node.tag)
74 print_node_ptr node.first_child
75 print_node_ptr node.next_sibling
76 print_node_ptr node.parent
79 let debug_ctx fmt ctx =
80 Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\n-------------\n"
82 (Pretty.print_list ~sep:";\n" debug_node) ctx.stack
85 let push n ctx = ctx.stack <- n :: ctx.stack
88 [] -> failwith "XML parse error"
89 | e :: l -> ctx.stack <- l; e
91 let top ctx = match ctx.stack with
92 | [] -> failwith "XML parse error"
96 let i = ctx.current_preorder in
97 ctx.current_preorder <- 1 + i;
100 let is_left n = n.next_sibling == dummy
102 let start_element_handler parser_ ctx tag attr_list =
103 let parent = top ctx in
104 let n = { tag = QName.of_string tag;
108 next_sibling = dummy;
112 if parent.first_child == dummy then parent.first_child <- n
113 else parent.next_sibling <- n;
116 let rec consume_closing ctx n =
117 if n.next_sibling != dummy then
118 let _ = pop ctx in consume_closing ctx (top ctx)
121 let end_element_handler parser_ ctx tag =
122 let node = top ctx in
123 if node.first_child == dummy then node.first_child <- nil
125 node.next_sibling <- nil;
126 consume_closing ctx node
129 let character_data_handler parser_ _ t text =
130 Buffer.add_string t text
132 let create_parser () =
133 let ctx = { text_buffer = Buffer.create 512;
134 current_preorder = 0;
136 let parser_ = Expat.parser_create ~encoding:None in
137 Expat.set_start_element_handler parser_ (start_element_handler parser_ ctx);
138 Expat.set_end_element_handler parser_ (end_element_handler parser_ ctx);
139 push { tag = QName.document;
143 next_sibling = dummy;
148 let node = top ctx in
149 node.next_sibling <- nil;
150 consume_closing ctx node;
153 root.next_sibling <- nil;
155 | _ -> raise (Expat.Expat_error Expat.UNCLOSED_TOKEN)
160 let parser_, finalize = create_parser () in
161 Expat.parse parser_ s;
165 let buffer = String.create 4096 in
166 let parser_, finalize = create_parser () in
168 let read = input fd buffer 0 4096 in
170 let () = Expat.parse_sub parser_ buffer 0 read in
172 in loop (); finalize ()
177 let load_xml_file = Parser.parse_file
178 let load_xml_string = Parser.parse_string
180 let rec print_xml out tree_ node =
182 let tag = QName.to_string node.tag in
184 output_string out tag;
185 (* print attributes *)
186 if node.first_child == nil then output_string out "/>"
189 print_xml out tree_ node.first_child;
190 output_string out "</";
191 output_string out tag;
194 print_xml out tree_ node.next_sibling
199 let first_child _ n = n.first_child
200 let next_sibling _ n = n.next_sibling
201 let parent _ n = n.parent
203 let data _ n = n.data
204 let preorder _ n = n.preorder