(***********************************************************************) (* *) (* TAToo *) (* *) (* Kim Nguyen, LRI UMR8623 *) (* Université Paris-Sud & CNRS *) (* *) (* Copyright 2010-2012 Université Paris-Sud and Centre National de la *) (* Recherche Scientifique. All rights reserved. This file is *) (* distributed under the terms of the GNU Lesser General Public *) (* License, with the special exception on linking described in file *) (* ../LICENSE. *) (* *) (***********************************************************************) open Format let time f arg msg = let t1 = Unix.gettimeofday () in let r = f arg in let t2 = Unix.gettimeofday () in let time = (t2 -. t1) *. 1000. in Logger.msg `STATS "%s: %fms" msg time; r let main () = let () = Options.parse () in let doc = let fd = open_in !Options.input_file in let d = time Naive_tree.load_xml_file fd "parsing xml document" in close_in fd; d in let query = time Xpath.Parser.parse (Ulexing.from_latin1_string !Options.query) "parsing XPath query" in let auto = time Xpath.Compile.path query "compiling XPath query" in let output = match !Options.output_file with | None | Some "-" | Some "/dev/stdout" -> stdout | Some f -> open_out f in if !Options.stats then begin Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query; Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto; end; let module Naive = Eval.Make(Naive_tree) in let results = time (Naive.eval auto doc) (Naive_tree.root doc) "evaluating query" in time (fun () -> output_string output "\n"; if !Options.count then begin output_string output (string_of_int (List.length results)); output_char output '\n'; end else List.iter (fun n -> Naive_tree.print_xml output doc n; output_char output '\n' ) results; output_string output "\n"; flush output; if output != stdout then close_out output ) () "serializing results" let () = try main () with Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1 | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2 | Tree.Parse_error msg -> eprintf "Error: file %s, %s\n%!" !Options.input_file msg; exit 3 | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4 | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128