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 kind : Tree.NodeKind.t;
20 mutable data : string;
21 mutable first_child : node;
22 mutable next_sibling : node;
30 kind = Tree.NodeKind.Element;
38 let dummy_tag = QName.of_string "#dummy"
41 kind = Tree.NodeKind.Element;
53 by_preorder : node array;
54 (* TODO add other intersting stuff *)
63 mutable stack : node list;
64 text_buffer : Buffer.t;
65 mutable current_preorder : int;
68 let print_node_ptr fmt n =
69 Format.fprintf fmt "<%s>"
70 (if n == nil then "NIL" else
71 if n == dummy then "DUMMY" else
72 "NODE " ^ string_of_int n.preorder)
74 let debug_node fmt node =
76 "{ tag=%s; preorder=%i; data=%S;\
77 first_child=%a; next_sibling=%a; parent=%a }"
78 (QName.to_string node.tag)
81 print_node_ptr node.first_child
82 print_node_ptr node.next_sibling
83 print_node_ptr node.parent
86 let debug_ctx fmt ctx =
87 Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\
90 (Pretty.print_list ~sep:";\n" debug_node) ctx.stack
93 let push n ctx = ctx.stack <- n :: ctx.stack
96 [] -> failwith "XML parse error"
97 | e :: l -> ctx.stack <- l; e
99 let top ctx = match ctx.stack with
100 | [] -> failwith "XML parse error"
104 let i = ctx.current_preorder in
105 ctx.current_preorder <- 1 + i;
108 let is_left n = n.next_sibling == dummy
111 let text_string = QName.to_string QName.text
112 let comment_string = QName.to_string QName.comment
115 let rec start_element_handler parser_ ctx tag attr_list =
117 let parent = top ctx in
118 let n = { tag = QName.of_string tag;
119 kind = Tree.NodeKind.Element;
123 next_sibling = dummy;
127 if parent.first_child == dummy then parent.first_child <- n
128 else parent.next_sibling <- n;
130 List.iter (do_attribute parser_ ctx) attr_list
132 and do_attribute parser_ ctx (att, value) =
133 let att_tag = QName.to_string (QName.attribute (QName.of_string att)) in
134 start_element_handler parser_ ctx att_tag [];
137 n.kind <- Tree.NodeKind.Attribute;
138 end_element_handler parser_ ctx att_tag
140 and consume_closing ctx n =
141 if n.next_sibling != dummy then
142 let _ = pop ctx in consume_closing ctx (top ctx)
144 and end_element_handler parser_ ctx _ =
146 let node = top ctx in
147 if node.first_child == dummy then node.first_child <- nil
149 node.next_sibling <- nil;
150 consume_closing ctx node
153 and do_text parser_ ctx =
154 if Buffer.length ctx.text_buffer != 0 then
155 let s = Buffer.contents ctx.text_buffer in
156 Buffer.clear ctx.text_buffer;
157 start_element_handler parser_ ctx text_string [];
158 let node = top ctx in
160 node.kind <- Tree.NodeKind.Text;
161 end_element_handler parser_ ctx text_string
163 and comment_handler parser_ ctx s =
165 start_element_handler parser_ ctx comment_string [];
166 let node = top ctx in
168 node.kind <- Tree.NodeKind.Comment;
169 end_element_handler parser_ ctx comment_string
171 and processing_instruction_handler parser_ ctx tag data =
173 let pi = QName.to_string
174 (QName.processing_instruction (QName.of_string tag))
176 start_element_handler parser_ ctx pi [];
177 let node = top ctx in
179 node.kind <- Tree.NodeKind.ProcessingInstruction;
180 end_element_handler parser_ ctx pi
183 let character_data_handler _parser ctx text =
184 Buffer.add_string ctx.text_buffer text
186 let create_parser () =
187 let ctx = { text_buffer = Buffer.create 512;
188 current_preorder = 0;
190 let psr = Expat.parser_create ~encoding:None in
191 Expat.set_start_element_handler psr (start_element_handler psr ctx);
192 Expat.set_end_element_handler psr (end_element_handler psr ctx);
193 Expat.set_character_data_handler
194 psr (character_data_handler psr ctx);
195 Expat.set_comment_handler psr (comment_handler psr ctx);
196 Expat.set_processing_instruction_handler psr
197 (processing_instruction_handler psr ctx);
198 push { tag = QName.document;
200 kind = Tree.NodeKind.Document;
203 next_sibling = dummy;
208 let node = top ctx in
209 node.next_sibling <- nil;
210 consume_closing ctx node;
212 let root = List.hd ctx.stack in
213 root.next_sibling <- nil;
214 let a = Array.create ctx.current_preorder nil in
225 size = ctx.current_preorder;
230 let error e parser_ =
231 let msg = Printf.sprintf "%i.%i %s"
232 (Expat.get_current_line_number parser_)
233 (Expat.get_current_column_number parser_)
234 (Expat.xml_error_to_string e)
236 raise (Tree.Parse_error msg)
239 let parser_, finalize = create_parser () in
241 Expat.parse parser_ s;
244 Expat.Expat_error e -> error e parser_
247 let buffer = String.create 4096 in
248 let parser_, finalize = create_parser () in
250 let read = input fd buffer 0 4096 in
252 let () = Expat.parse_sub parser_ buffer 0 read in
257 Expat.Expat_error e -> error e parser_
262 let load_xml_file = Parser.parse_file
263 let load_xml_string = Parser.parse_string
265 let output_escape_string out s =
266 for i = 0 to String.length s - 1 do
268 | '<' -> output_string out "<"
269 | '>' -> output_string out ">"
270 | '&' -> output_string out "&"
271 | '"' -> output_string out """
272 | '\'' -> output_string out "'"
273 | c -> output_char out c
277 let rec print_attributes ?(sep=true) out tree_ node =
278 if (node.kind == Tree.NodeKind.Attribute) then
279 let tag = QName.to_string (QName.remove_prefix node.tag) in
280 if sep then output_char out ' ';
281 output_string out tag;
282 output_string out "=\"";
283 output_escape_string out node.data;
284 output_char out '\"';
285 print_attributes out tree_ node.next_sibling
289 let rec print_xml out tree_ node =
292 let open Tree.NodeKind in
295 | Text -> output_escape_string out node.data
296 | Element | Document ->
297 let tag = QName.to_string node.tag in
299 output_string out tag;
300 let fchild = print_attributes out tree_ node.first_child in
301 if fchild == nil then output_string out "/>"
304 print_xml out tree_ fchild;
305 output_string out "</";
306 output_string out tag;
309 | Attribute -> ignore (print_attributes ~sep:false out tree_ node)
311 output_string out "<!--";
312 output_string out node.data;
313 output_string out "-->"
314 | ProcessingInstruction ->
315 output_string out "<?";
316 output_string out (QName.to_string (QName.remove_prefix node.tag));
318 output_string out node.data;
319 output_string out "?>"
321 print_xml out tree_ node.next_sibling
323 let print_xml out tree_ node =
324 let nnode = { node with next_sibling = nil } in print_xml out tree_ nnode
328 let first_child _ n = n.first_child
329 let next_sibling _ n = n.next_sibling
330 let parent _ n = n.parent
332 let data _ n = n.data
333 let kind _ n = n.kind
334 let preorder _ n = n.preorder
335 let by_preorder t i =
336 if i >= 0 && i < t.size then Array.unsafe_get t.by_preorder i
337 else let e = Invalid_argument "by_preorder" in raise e
338 let print_node fmt n = Parser.debug_node fmt n