4d7e693c019b91dfb710bfac6cd77f4e0c40b059
[tatoo.git] / src / tatoo.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                               TAToo                                 *)
4 (*                                                                     *)
5 (*                     Kim Nguyen, LRI UMR8623                         *)
6 (*                   Université Paris-Sud & CNRS                       *)
7 (*                                                                     *)
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   *)
12 (*  ../LICENSE.                                                        *)
13 (*                                                                     *)
14 (***********************************************************************)
15
16 open Format
17
18 let time f arg msg =
19   let t1 = Unix.gettimeofday () in
20   let r = f arg in
21   let t2 = Unix.gettimeofday () in
22   let time = (t2 -. t1) *. 1000. in
23   Logger.msg `STATS "%s: %fms" msg time;
24   r
25
26
27 let main () =
28   let () = Options.parse () in
29   let doc =
30     let fd, close_fd = match !Options.input_file with
31       None -> stdin, ignore
32     | Some input ->
33         let fd = open_in input in fd, fun () -> close_in fd
34     in
35     let d = time Naive_tree.load_xml_file fd "parsing xml document" in
36     close_fd (); d
37   in
38   let query =
39     time
40       Xpath.Parser.parse
41       (Ulexing.from_latin1_string !Options.query)
42       "parsing XPath query"
43   in
44   let auto =
45     time Xpath.Compile.path query "compiling XPath query"
46   in
47   let auto = time Ata.copy auto "copying Automaton" in
48   let output =
49     match !Options.output_file with
50     | None | Some "-" | Some "/dev/stdout" -> stdout
51     | Some f -> open_out f
52   in
53   if !Options.stats then begin
54     Logger.msg `STATS "Query: %a " Xpath.Ast.print_path query;
55     Logger.msg `STATS "@[Automaton: @\n%a@]" Ata.print auto;
56   end;
57
58   let module Naive = Run.Make(Naive_tree) in
59   let results =
60     time (Naive.eval auto doc) ([Naive_tree.root doc]) "evaluating query"
61   in
62   time (fun () ->
63   output_string output "<xml_result>\n";
64   if !Options.count then begin
65     output_string output (string_of_int (List.length results));
66     output_char output '\n';
67   end else
68     List.iter (fun n ->
69       Naive_tree.print_xml output doc n;
70       output_char output '\n'
71     ) results;
72   output_string output "</xml_result>\n";
73   flush output;
74   if output != stdout then close_out output
75
76 ) () "serializing results"
77
78
79 let () =
80   try
81     main ()
82   with
83     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
84   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
85   | Tree.Parse_error msg ->
86       eprintf "Error: %s, %s\n%!"
87         (match !Options.input_file with
88           Some s -> ("file " ^ s)
89         | None -> "[stdin]") msg; exit 3
90   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
91   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128