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 (***********************************************************************)
17 Time-stamp: <Last modified on 2013-02-14 16:14:00 CET by Kim Nguyen>
24 mutable data : string;
25 mutable first_child : node;
26 mutable next_sibling : node;
41 let dummy_tag = QName.of_string "#dummy"
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 =
75 Format.fprintf fmt "{ tag=%s; preorder=%i; data=%s; first_child=%a; next_sibling=%a; parent=%a }"
76 (QName.to_string node.tag)
79 print_node_ptr node.first_child
80 print_node_ptr node.next_sibling
81 print_node_ptr node.parent
84 let debug_ctx fmt ctx =
85 Format.fprintf fmt "Current context: { preorder = %i\n; stack = \n%a\n }\n-------------\n"
87 (Pretty.print_list ~sep:";\n" debug_node) ctx.stack
90 let push n ctx = ctx.stack <- n :: ctx.stack
93 [] -> failwith "XML parse error"
94 | e :: l -> ctx.stack <- l; e
96 let top ctx = match ctx.stack with
97 | [] -> failwith "XML parse error"
101 let i = ctx.current_preorder in
102 ctx.current_preorder <- 1 + i;
105 let is_left n = n.next_sibling == dummy
108 let text_string = QName.to_string QName.text
109 let attr_map_string = QName.to_string QName.attribute_map
111 let att_pref = QName.node QName.attribute_prefix
112 let rec start_element_handler parser_ ctx tag attr_list =
114 let parent = top ctx in
115 let n = { tag = QName.of_string tag;
119 next_sibling = dummy;
123 if parent.first_child == dummy then parent.first_child <- n
124 else parent.next_sibling <- n;
129 start_element_handler parser_ ctx attr_map_string [];
130 List.iter (do_attribute parser_ ctx) attr_list;
131 end_element_handler parser_ ctx attr_map_string
133 and do_attribute parser_ ctx (att, value) =
134 let att_tag = att_pref ^ att in
135 start_element_handler parser_ ctx att_tag [];
136 start_element_handler parser_ ctx text_string [];
137 let n = top ctx in n.data <- value;
138 end_element_handler parser_ ctx text_string;
139 end_element_handler parser_ ctx att_tag
141 and consume_closing ctx n =
142 if n.next_sibling != dummy then
143 let _ = pop ctx in consume_closing ctx (top ctx)
145 and end_element_handler parser_ ctx tag =
147 let node = top ctx in
148 if node.first_child == dummy then node.first_child <- nil
150 node.next_sibling <- nil;
151 consume_closing ctx node
154 and do_text parser_ ctx =
155 if Buffer.length ctx.text_buffer != 0 then
156 let s = Buffer.contents ctx.text_buffer in
157 Buffer.clear ctx.text_buffer;
158 start_element_handler parser_ ctx text_string [];
159 let node = top ctx in
161 end_element_handler parser_ ctx text_string
165 let character_data_handler parser_ ctx text =
166 Buffer.add_string ctx.text_buffer text
168 let create_parser () =
169 let ctx = { text_buffer = Buffer.create 512;
170 current_preorder = 0;
172 let parser_ = Expat.parser_create ~encoding:None in
173 Expat.set_start_element_handler parser_ (start_element_handler parser_ ctx);
174 Expat.set_end_element_handler parser_ (end_element_handler parser_ ctx);
175 Expat.set_character_data_handler parser_ (character_data_handler parser_ ctx);
176 push { tag = QName.document;
180 next_sibling = dummy;
185 let node = top ctx in
186 node.next_sibling <- nil;
187 consume_closing ctx node;
190 root.next_sibling <- nil;
192 | _ -> raise (Expat.Expat_error Expat.UNCLOSED_TOKEN)
197 let parser_, finalize = create_parser () in
198 Expat.parse parser_ s;
202 let buffer = String.create 4096 in
203 let parser_, finalize = create_parser () in
205 let read = input fd buffer 0 4096 in
207 let () = Expat.parse_sub parser_ buffer 0 read in
209 in loop (); finalize ()
214 let load_xml_file = Parser.parse_file
215 let load_xml_string = Parser.parse_string
218 let output_escape_string out s =
219 for i = 0 to String.length s - 1 do
221 | '<' -> output_string out "<"
222 | '>' -> output_string out ">"
223 | '&' -> output_string out "&"
224 | '"' -> output_string out """
225 | '\'' -> output_string out "'"
226 | c -> output_char out c
229 let rec print_attributes out tree_ node =
230 if node != nil then begin
231 output_string out (QName.to_string node.tag);
232 output_string out "=\"";
233 output_escape_string out node.first_child.data;
235 print_attributes out tree_ node.next_sibling
238 let rec print_xml out tree_ node =
241 if node.tag == QName.text then
242 output_escape_string out node.data
244 let tag = QName.to_string node.tag in
246 output_string out tag;
248 if node.first_child.tag == QName.attribute_map then
250 print_attributes out tree_ node.first_child.first_child
252 node.first_child.next_sibling
256 if fchild == nil then output_string out "/>"
259 print_xml out tree_ fchild;
260 output_string out "</";
261 output_string out tag;
265 print_xml out tree_ node.next_sibling
269 let first_child _ n = n.first_child
270 let next_sibling _ n = n.next_sibling
271 let parent _ n = n.parent
273 let data _ n = n.data
274 let preorder _ n = n.preorder