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 start_element_handler parser_ ctx att [];
136 n.kind <- Tree.NodeKind.Attribute;
137 end_element_handler parser_ ctx att
139 and consume_closing ctx n =
140 if n.next_sibling != dummy then
141 let _ = pop ctx in consume_closing ctx (top ctx)
143 and end_element_handler parser_ ctx _ =
145 let node = top ctx in
146 if node.first_child == dummy then node.first_child <- nil
148 node.next_sibling <- nil;
149 consume_closing ctx node
152 and do_text parser_ ctx =
153 if Buffer.length ctx.text_buffer != 0 then
154 let s = Buffer.contents ctx.text_buffer in
155 Buffer.clear ctx.text_buffer;
156 start_element_handler parser_ ctx text_string [];
157 let node = top ctx in
159 node.kind <- Tree.NodeKind.Text;
160 end_element_handler parser_ ctx text_string
162 and comment_handler parser_ ctx s =
164 start_element_handler parser_ ctx comment_string [];
165 let node = top ctx in
167 node.kind <- Tree.NodeKind.Comment;
168 end_element_handler parser_ ctx comment_string
170 and processing_instruction_handler parser_ ctx tag data =
172 start_element_handler parser_ ctx tag [];
173 let node = top ctx in
175 node.kind <- Tree.NodeKind.ProcessingInstruction;
176 end_element_handler parser_ ctx tag
179 let character_data_handler _parser ctx text =
180 Buffer.add_string ctx.text_buffer text
182 let create_parser () =
183 let ctx = { text_buffer = Buffer.create 512;
184 current_preorder = 0;
186 let psr = Expat.parser_create ~encoding:None in
187 Expat.set_start_element_handler psr (start_element_handler psr ctx);
188 Expat.set_end_element_handler psr (end_element_handler psr ctx);
189 Expat.set_character_data_handler
190 psr (character_data_handler psr ctx);
191 Expat.set_comment_handler psr (comment_handler psr ctx);
192 Expat.set_processing_instruction_handler psr
193 (processing_instruction_handler psr ctx);
194 push { tag = QName.document;
196 kind = Tree.NodeKind.Document;
199 next_sibling = dummy;
204 let node = top ctx in
205 node.next_sibling <- nil;
206 consume_closing ctx node;
208 let root = List.hd ctx.stack in
209 root.next_sibling <- nil;
210 let a = Array.make ctx.current_preorder nil in
221 size = ctx.current_preorder;
226 let error e parser_ =
227 let msg = Printf.sprintf "%i.%i %s"
228 (Expat.get_current_line_number parser_)
229 (Expat.get_current_column_number parser_)
230 (Expat.xml_error_to_string e)
232 raise (Tree.Parse_error msg)
235 let parser_, finalize = create_parser () in
237 Expat.parse parser_ s;
240 Expat.Expat_error e -> error e parser_
243 let buffer = String.make 4096 '\000' in
244 let parser_, finalize = create_parser () in
246 let read = input fd buffer 0 4096 in
248 let () = Expat.parse_sub parser_ buffer 0 read in
253 Expat.Expat_error e -> error e parser_
258 let load_xml_file = Parser.parse_file
259 let load_xml_string = Parser.parse_string
261 let output_escape_string out s =
262 for i = 0 to String.length s - 1 do
264 | '<' -> output_string out "<"
265 | '>' -> output_string out ">"
266 | '&' -> output_string out "&"
267 | '"' -> output_string out """
268 | '\'' -> output_string out "'"
269 | c -> output_char out c
273 let rec print_attributes ?(sep=true) out tree_ node =
274 if (node.kind == Tree.NodeKind.Attribute) then
275 let tag = QName.to_string node.tag in
276 if sep then output_char out ' ';
277 output_string out tag;
278 output_string out "=\"";
279 output_escape_string out node.data;
280 output_char out '\"';
281 print_attributes out tree_ node.next_sibling
285 let rec print_xml out tree_ node =
288 let open Tree.NodeKind in
291 | Text -> output_escape_string out node.data
292 | Element | Document ->
293 let tag = QName.to_string node.tag in
295 output_string out tag;
296 let fchild = print_attributes out tree_ node.first_child in
297 if fchild == nil then output_string out "/>"
300 print_xml out tree_ fchild;
301 output_string out "</";
302 output_string out tag;
305 | Attribute -> ignore (print_attributes ~sep:false out tree_ node)
307 output_string out "<!--";
308 output_string out node.data;
309 output_string out "-->"
310 | ProcessingInstruction ->
311 output_string out "<?";
312 output_string out (QName.to_string node.tag);
314 output_string out node.data;
315 output_string out "?>"
317 print_xml out tree_ node.next_sibling
319 let print_xml out tree_ node =
320 let nnode = { node with next_sibling = nil } in print_xml out tree_ nnode
324 let first_child _ n = n.first_child
325 let next_sibling _ n = n.next_sibling
326 let parent _ n = n.parent
328 let data _ n = n.data
329 let kind _ n = n.kind
330 let preorder _ n = n.preorder
331 let by_preorder t i =
332 if i >= 0 && i < t.size then Array.unsafe_get t.by_preorder i
333 else let e = Invalid_argument "by_preorder" in raise e
334 let print_node fmt n = Parser.debug_node fmt n