let buflen = 1024 let buf = String.create buflen let load_from_file p s = let ic = try open_in s with exn -> let msg = Printf.sprintf "load_xml, file \"%s\": %s" s (Printexc.to_string exn) in raise (Location.Generic msg) in let rec loop () = let n = input ic buf 0 buflen in if (n > 0) then (Expat.parse_sub p buf 0 n; loop ()) in try loop(); Expat.final p; close_in ic with exn -> close_in ic; raise exn let load_expat s = let p = Expat.parser_create "" in Expat.set_start_element_handler p Load_xml.start_element_handler; Expat.set_end_element_handler p Load_xml.end_element_handler; Expat.set_character_data_handler p Load_xml.text_handler; let u = Url.process s in try match u with | Url.Url s -> Expat.parse p s | Url.Filename s -> load_from_file p s with Expat.Expat_error e -> let line = Expat.get_current_line_number p and col = Expat.get_current_column_number p in let src = match u with | Url.Url s -> "" | Url.Filename s -> Printf.sprintf " file \"%s\"," s in let msg = Printf.sprintf "load_xml,%s at line %i, column %i: %s" src (Expat.get_current_line_number p) (Expat.get_current_column_number p) (Expat.xml_error_to_string e) in raise (Location.Generic msg) let use () = Load_xml.xml_parser := load_expat let () = Config.register "expat" "Expat XML parser" use