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
103 let text_string = QName.to_string QName.text
105 let rec start_element_handler parser_ ctx tag attr_list =
107 let parent = top ctx in
108 let n = { tag = QName.of_string tag;
112 next_sibling = dummy;
116 if parent.first_child == dummy then parent.first_child <- n
117 else parent.next_sibling <- n;
120 and consume_closing ctx n =
121 if n.next_sibling != dummy then
122 let _ = pop ctx in consume_closing ctx (top ctx)
124 and end_element_handler parser_ ctx tag =
126 let node = top ctx in
127 if nodef.first_child == dummy then node.first_child <- nil
129 node.next_sibling <- nil;
130 consume_closing ctx node
133 and do_text parser_ ctx =
134 if Buffer.length ctx.text_buffer != 0 then
135 let s = Buffer.contents ctx.text_buffer in
136 Buffer.clear ctx.text_buffer;
137 start_element_handler parser_ ctx text_string [];
138 let node = top ctx in
140 end_element_handler parser_ ctx text_string;
141 Format.eprintf "DEBUG: %a\n\n" debug_ctx ctx
144 let character_data_handler parser_ ctx text =
145 Buffer.add_string ctx.text_buffer text
147 let create_parser () =
148 let ctx = { text_buffer = Buffer.create 512;
149 current_preorder = 0;
151 let parser_ = Expat.parser_create ~encoding:None in
152 Expat.set_start_element_handler parser_ (start_element_handler parser_ ctx);
153 Expat.set_end_element_handler parser_ (end_element_handler parser_ ctx);
154 Expat.set_character_data_handler parser_ (character_data_handler parser_ ctx);
155 push { tag = QName.document;
159 next_sibling = dummy;
164 let node = top ctx in
165 node.next_sibling <- nil;
166 consume_closing ctx node;
169 root.next_sibling <- nil;
171 | _ -> raise (Expat.Expat_error Expat.UNCLOSED_TOKEN)
176 let parser_, finalize = create_parser () in
177 Expat.parse parser_ s;
181 let buffer = String.create 4096 in
182 let parser_, finalize = create_parser () in
184 let read = input fd buffer 0 4096 in
186 let () = Expat.parse_sub parser_ buffer 0 read in
188 in loop (); finalize ()
193 let load_xml_file = Parser.parse_file
194 let load_xml_string = Parser.parse_string
196 let rec print_xml out tree_ node =
199 if node.tag == QName.text then
200 output_string out node.data
202 let tag = QName.to_string node.tag in
204 output_string out tag;
205 (* print attributes *)
206 if node.first_child == nil then output_string out "/>"
209 print_xml out tree_ node.first_child;
210 output_string out "</";
211 output_string out tag;
215 print_xml out tree_ node.next_sibling
219 let first_child _ n = n.first_child
220 let next_sibling _ n = n.next_sibling
221 let parent _ n = n.parent
223 let data _ n = n.data
224 let preorder _ n = n.preorder