1 (******************************************************************************)
2 (* SXSI : XPath evaluator *)
3 (* Kim Nguyen (Kim.Nguyen@nicta.com.au) *)
4 (* Copyright NICTA 2008 *)
5 (* Distributed under the terms of the LGPL (see LICENCE) *)
6 (******************************************************************************)
12 type descr = Nil | Node of node_content |String of string_content
14 val parse_xml_uri : string -> t
15 val parse_xml_string : string -> t
16 val save : t -> string -> unit
17 val load : ?sample:int -> string -> t
18 val tag_pool : t -> Tag.pool
19 val string : t -> string
20 val descr : t -> descr
21 val is_node : t -> bool
24 val first_child : t -> t
25 val next_sibling : t -> t
28 val is_root : t -> bool
31 val print_xml_fast : out_channel -> t -> unit
32 val compare : t -> t -> int
33 val equal : t -> t -> bool
38 with type elt = string_content
39 val string_below : t -> string_content -> bool
40 val contains : t -> string -> DocIdSet.t
41 val contains_old : t -> string -> DocIdSet.t
42 val contains_iter : t -> string -> DocIdSet.t
43 val count_contains : t -> string -> int
44 val count : t -> string -> int
46 val get_string : t -> string_content -> string
47 val has_tagged_desc : t -> Tag.t -> bool
48 val has_tagged_foll : t -> Tag.t -> bool
49 val tagged_desc : t -> Tag.t -> t
50 val tagged_foll : t -> Tag.t -> t
51 val tagged_below : t -> Ptset.t -> Ptset.t -> t
52 val tagged_next : t -> Ptset.t -> Ptset.t -> t -> t
53 val tagged_desc_only : t -> Ptset.t -> t
54 val tagged_foll_only : t -> Ptset.t -> t -> t
55 val text_below : t -> t
56 val text_next : t -> t -> t
57 val init_tagged_next : t -> Tag.t -> unit
58 val subtree_tags : t -> Tag.t -> int
59 val is_left : t -> bool
60 val print_id : Format.formatter -> t -> unit
61 val test_xml_tree : Format.formatter -> Ptset.t -> t -> unit
62 val init_contains : t -> string -> unit
63 val init_naive_contains : t -> string -> unit
65 val test_jump : t -> Tag.t -> unit
73 type node_kind = [`Text | `Tree ]
75 let compare : 'a node -> 'a node -> int = (-)
76 let equal : 'a node -> 'a node -> bool = (==)
78 (* abstract type, values are pointers to a XMLTree C++ object *)
80 external int_of_node : 'a node -> int = "%identity"
82 external parse_xml_uri : string -> int -> bool -> bool -> t = "caml_call_shredder_uri"
83 external parse_xml_string : string -> int -> bool -> bool -> t = "caml_call_shredder_string"
85 external save_tree : t -> string -> unit = "caml_xml_tree_save"
86 external load_tree : string -> int -> t = "caml_xml_tree_load"
91 let equal : [`Text] node -> [`Text] node -> bool = equal
94 external nullt : unit -> [`Text ] node = "caml_xml_tree_nullt"
96 external get_text : t -> [`Text] node -> string = "caml_text_collection_get_text"
99 if equal nil n then ""
103 external is_empty : t -> [`Text ] node -> bool = "caml_text_collection_empty_text"
106 (equal nil n) || is_empty t n
108 external get_cached_text : t -> [`Text ] node -> string = "caml_text_collection_get_cached_text"
112 if equal nil n then ""
113 else get_cached_text t n
115 external size : t -> int = "caml_text_collection_size"
116 external is_contains : t -> string -> bool = "caml_text_collection_is_contains"
117 external count_contains : t -> string -> int = "caml_text_collection_count_contains"
118 external count : t -> string -> int = "caml_text_collection_count"
119 external contains : t -> string -> [`Text ] node array = "caml_text_collection_contains"
126 let equal : [`Tree ] node -> [`Tree] node -> bool = equal
127 external serialize : t -> string -> unit = "caml_xml_tree_serialize"
128 external unserialize : string -> t = "caml_xml_tree_unserialize"
130 external root : t -> [`Tree] node = "caml_xml_tree_root"
131 external nullt : unit -> [`Tree ] node = "caml_xml_tree_nullt"
134 let is_nil x = equal x nil
136 external parent : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_parent"
137 external parent_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_parent_doc"
138 external prev_doc : t -> [`Text ] node -> [`Tree ] node = "caml_xml_tree_prev_doc"
139 external first_child : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_first_child"
143 external next_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_next_sibling"
144 external prev_sibling : t -> [`Tree] node -> [`Tree] node = "caml_xml_tree_prev_sibling"
145 external is_leaf : t -> [`Tree] node -> bool = "caml_xml_tree_is_leaf"
147 (* external tag : t -> [`Tree ] node -> T = "caml_xml_tree_tag"*)
148 external tag_id : t -> [`Tree ] node -> Tag.t = "caml_xml_tree_tag_id"
151 let tag_hash = Array.make 6_000_000 (Tag.nullt)
154 let tag = tag_hash.(int_of_node id)
156 if tag != Tag.nullt then tag
158 let tag = tag_id t id in
159 (tag_hash.(int_of_node id) <- tag; tag)
161 let is_last t n = equal nil (next_sibling t n)
163 external prev_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_prev_text"
166 external my_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_my_text"
167 external next_text : t -> [`Tree] node -> [`Text ] node = "caml_xml_tree_next_text"
168 external doc_ids : t -> [`Tree ] node -> [`Text ] node * [`Text ] node = "caml_xml_tree_doc_ids"
169 external text_xml_id : t -> [`Text ] node -> int = "caml_xml_tree_text_xml_id"
170 external node_xml_id : t -> [`Tree ] node -> int = "caml_xml_tree_node_xml_id"
171 external is_ancestor : t -> [`Tree ] node -> [`Tree ] node -> bool = "caml_xml_tree_is_ancestor"
172 external tagged_desc : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_desc"
173 external tagged_foll : t -> [`Tree ] node -> Tag.t -> [`Tree ] node = "caml_xml_tree_tagged_foll"
174 external subtree_tags : t -> [`Tree ] node -> Tag.t -> int = "caml_xml_tree_subtree_tags"
175 external tagged_below : t -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_tagged_below"
176 external tagged_desc_only : t -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node = "caml_xml_tree_tagged_desc_only"
177 external tagged_next : t -> [`Tree ] node -> Ptset.int_vector -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_next"
178 external tagged_foll_only : t -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_only"
179 external tagged_desc_or_foll_only : t -> [`Tree ] node -> Ptset.int_vector -> [`Tree ] node -> [`Tree ] node = "caml_xml_tree_tagged_foll_only"
181 let test_jump tree tag =
182 let rec loop id ctx =
185 let first = tagged_desc tree id tag
186 and next = tagged_desc tree id tag
191 loop (root tree) (root tree)
194 let test_xml_tree ppf tags v =
195 let pr x = Format.fprintf ppf x in
201 pr "Node %i, (Tag) %i='%s' (GetTagName), NodeXMLId (Preorder)=%i\n%!"
204 (Tag.to_string (tag_id v id))
206 pr "DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s) ParentDoc(my_text)=%i PrevDoc(next_text)=%i\n%!"
207 (int_of_node (prev_text v id))
208 (Text.get_text v (prev_text v id))
209 (int_of_node (my_text v id))
210 (Text.get_text v (my_text v id))
211 (int_of_node (next_text v id))
212 (Text.get_text v (next_text v id))
213 (int_of_node(parent_doc v (my_text v id)))
214 (int_of_node(prev_doc v (next_text v id)));
215 let i1,i2 = doc_ids v id in
216 pr "Testing DocIds below (%i,%i)*\n%!"
217 (int_of_node i1) (int_of_node i2);
218 pr "Testing Tagged*\n%!";
220 let str = Tag.to_string t in
223 pr "Tag: %s : \n%!" str;
224 pr "TaggedDesc = %i%!, " (tagged_desc v id t);
225 pr "TaggedFoll = %i\n%!" (tagged_foll v id t);
226 pr "SubtreeTags = %i\n%!" (subtree_tags v id t);
228 pr "----------------------------\n";
229 aux(first_child v id);
230 aux(next_sibling v id);
243 then Printf.eprintf "#\n"
246 Printf.eprintf "Node %i has tag '%i=%s' DocOrder=%i, DocID of PrevText,MyText,NextText : (%i = %s,%i = %s,%i = %s) parent_doc(my_text)=%i\n%!"
249 (Tag.to_string (tag_id t id))
251 (int_of_node (prev_text t id))
252 (Text.get_text t (prev_text t id))
253 (int_of_node (my_text t id))
254 (Text.get_text t (my_text t id))
255 (int_of_node (next_text t id))
256 (Text.get_text t (next_text t id))
257 (int_of_node(parent_doc t (my_text t id)));
259 aux(first_child t id);
260 aux(next_sibling t id);
270 (* ignore (tag t id);
271 ignore (Text.get_text t (prev_text t id));
273 then ignore (Text.get_text t (my_text t id));
275 then ignore (Text.get_text t (next_text t id)); *)
276 aux (first_child t id);
277 aux (next_sibling t id);
287 module Binary = struct
291 | SC of [`Text ] node * [`Tree ] node
292 type string_content = [ `Text ] node
295 | Node of node_content
296 | String of string_content
300 type t = { doc : doc;
303 let dump { doc=t } = Tree.print_skel t
304 let test_xml_tree ppf tags { doc=t } = Tree.test_xml_tree ppf tags t
305 let test_jump { doc=t } tag = Tree.test_jump t tag
306 let contains_array = ref [| |]
308 let init_contains t s =
309 let a = Text.contains t.doc s
311 Array.fast_sort (compare) a;
314 let init_naive_contains t s =
315 let i,j = Tree.doc_ids t.doc (Tree.root t.doc)
317 let regexp = Str.regexp_string s in
320 let _ = Str.search_forward regexp arg 0;
324 let rec loop n acc l =
327 let s = (*Printf.eprintf "%i \n%!" n;*)Text.get_cached_text t.doc n
330 then loop (n+1) (n::acc) (l+1)
331 else loop (n+1) acc l
333 let acc,l = loop i [] 0 in
334 let a = Array.create l Text.nil in
335 let _ = List.fold_left (fun cpt e -> a.(cpt) <- e; (cpt-1)) (l-1) acc
341 module DocIdSet = struct
342 include Set.Make (struct type t = string_content
343 let compare = (-) end)
346 let is_node = function { node=Node(_) } -> true | _ -> false
348 let get_string t (i:string_content) = Text.get_text t.doc i
350 let node_of_t t = { doc= t;
351 node = Node(NC (root t)) }
354 let parse_xml_uri str = node_of_t
355 (MM((parse_xml_uri str
356 !Options.sample_factor
357 !Options.index_empty_texts
358 !Options.disable_text_collection),__LOCATION__))
360 let parse_xml_string str = node_of_t
361 (MM((parse_xml_string str
362 !Options.sample_factor
363 !Options.index_empty_texts
364 !Options.disable_text_collection),__LOCATION__))
367 let save t str = save_tree t.doc str
369 let load ?(sample=64) str = node_of_t (load_tree str sample)
372 external pool : doc -> Tag.pool = "%identity"
373 let tag_pool t = pool t.doc
375 let compare a b = match a.node,b.node with
376 | Node(NC i),Node(NC j) -> compare i j
377 | _, Node(NC( _ )) -> 1
378 | Node(SC (i,_)),Node(SC (j,_)) -> compare i j
379 | Node(NC( _ )),Node(SC (_,_)) -> -1
380 | _, Node(SC (_,_)) -> 1
381 | String i, String j -> compare i j
382 | Node _ , String _ -> -1
387 let equal a b = (compare a b) == 0
389 let string t = match t.node with
390 | String i -> Text.get_text t.doc i
393 let norm (n : [`Tree ] node ) = if is_nil n then Nil else Node (NC n)
399 | String i -> Printf.sprintf "String %i" i
400 | Node (NC t) -> Printf.sprintf "Node (NC %i)" (int_of_node t)
401 | Node (SC (t,i)) -> Printf.sprintf "Node (SC (%i,%i))" (int_of_node t) (int_of_node i)
403 let mk_nil t = { t with node = Nil }
404 let root n = { n with node = norm (Tree.root n.doc) }
405 let is_root n = match n.node with
406 | Node(NC t) -> (int_of_node t) == 0
410 if is_root n then { n with node=Nil}
415 let txt = prev_text n.doc t in
416 if Text.is_empty n.doc txt then
417 let ps = Tree.prev_sibling n.doc t in
420 Node(NC (Tree.parent n.doc t))
425 let ps = Tree.prev_sibling n.doc t in
427 then Node (NC(parent_doc n.doc i))
429 | _ -> failwith "parent"
431 { n with node = node' }
436 | Node (NC t) when is_leaf n.doc t ->
437 let txt = my_text n.doc t in
438 if Text.is_empty n.doc txt
440 else Node(SC (txt,Tree.nil))
442 let fs = first_child n.doc t in
443 let txt = prev_text n.doc fs in
444 if Text.is_empty n.doc txt
446 else Node (SC (txt, fs))
447 | Node(SC (i,_)) -> String i
448 | Nil | String _ -> failwith "first_child"
450 { n with node = node'}
455 | Node (SC (_,ns)) -> norm ns
457 let ns = next_sibling n.doc t in
458 let txt = next_text n.doc t in
459 if Text.is_empty n.doc txt
461 else Node (SC (txt, ns))
462 | Nil | String _ -> failwith "next_sibling"
464 { n with node = node'}
467 let left = first_child
468 let right = next_sibling
471 function { doc=d; node=Node(NC n)} -> node_xml_id d n
472 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id d i
474 Format.fprintf Format.err_formatter "Failure id on %s\n%!" (nts x.node);
478 function { node=Node(SC _) } -> Tag.pcdata
479 | { doc=d; node=Node(NC n)} -> tag_id d n
480 | _ -> failwith "tag"
482 let string_below t id =
483 let strid = parent_doc t.doc id in
486 (Tree.equal i strid) || (is_ancestor t.doc i strid)
487 | Node(SC(i,_)) -> Text.equal i id
491 let tagged_foll t tag =
492 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_foll"
494 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_foll d n tag) }
495 | { doc=d; node=Node(SC (_,n)) } when is_nil n -> { t with node= Nil }
496 | { doc=d; node=Node(SC (_,n)) } ->
498 if tag_id d n == tag then n
500 let n' = tagged_desc d n tag in
501 if is_nil n' then tagged_foll d n tag
503 in {t with node= norm nnode}
504 | _ -> { t with node=Nil }
507 let tagged_desc t tag =
508 if tag = Tag.attribute || tag = Tag.pcdata then failwith "tagged_desc"
510 | { doc=d; node=Node(NC n) } -> { t with node = norm (tagged_desc d n tag) }
511 | _ -> { t with node=Nil }
514 let tagged_next t tb tf s =
516 | { node = Node (NC(below)) } -> begin
518 | { doc = d; node=Node(NC n) } ->
519 { t with node= norm (tagged_next d n (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
520 | { doc = d; node=Node(SC (i,n) ) } when is_nil n ->
521 let p = parent_doc d i in
522 { t with node= norm (tagged_next d p (Ptset.to_int_vector tb) (Ptset.to_int_vector tf) below) }
523 | { doc = d; node=Node(SC (_,n) ) } ->
524 if Ptset.mem (tag_id d n) (Ptset.union tb tf)
525 then { t with node=Node(NC(n)) }
527 let vb = Ptset.to_int_vector tb in
528 let vf = Ptset.to_int_vector tf in
530 let dsc = tagged_below d n vb vf in
532 then tagged_next d n vb vf below
535 { t with node = norm node }
536 | _ -> {t with node=Nil }
539 | _ -> {t with node=Nil }
541 let tagged_foll_only t tf s =
543 | { node = Node (NC(below)) } -> begin
545 | { doc = d; node=Node(NC n) } ->
546 { t with node= norm (tagged_foll_only d n (Ptset.to_int_vector tf) below) }
547 | { doc = d; node=Node(SC (i,n) ) } when is_nil n ->
548 let p = parent_doc d i in
549 { t with node= norm (tagged_foll_only d p (Ptset.to_int_vector tf) below) }
550 | { doc = d; node=Node(SC (_,n) ) } ->
551 if Ptset.mem (tag_id d n) tf
552 then { t with node=Node(NC(n)) }
554 let vf = Ptset.to_int_vector tf in
556 let dsc = tagged_desc_only d n vf in
558 then tagged_foll_only d n vf below
561 { t with node = norm node }
562 | _ -> {t with node=Nil }
565 | _ -> {t with node=Nil }
568 let tagged_below t tc td =
570 | { doc = d; node=Node(NC n) } ->
571 let vc = Ptset.to_int_vector tc
573 let vd = Ptset.to_int_vector td
575 { t with node= norm(tagged_below d n vc vd) }
576 | _ -> { t with node=Nil }
578 let tagged_desc_only t td =
580 | { doc = d; node=Node(NC n) } ->
581 let vd = Ptset.to_int_vector td
583 { t with node= norm(tagged_desc_only d n vd) }
584 | _ -> { t with node=Nil }
588 let array_find a i j =
589 let l = Array.length a in
590 let rec loop idx x y =
591 if x > y || idx >= l then Text.nil
593 if a.(idx) >= x then if a.(idx) > y then Text.nil else (last_idx := idx;a.(idx))
594 else loop (idx+1) x y
596 if a.(0) > j || a.(l-1) < i then Text.nil
597 else loop !last_idx i j
601 let l = Array.length !contains_array in
602 if l = 0 then { t with node=Nil }
605 | { doc = d; node=Node(NC n) } ->
606 let i,j = doc_ids t.doc n in
607 let id = array_find !contains_array i j
609 if id == Text.nil then
612 {t with node = Node(SC(id, Tree.next_sibling d (Tree.prev_doc d id))) }
613 | _ -> { t with node=Nil }
615 let text_next t root =
616 let l = Array.length !contains_array in
617 if l = 0 then { t with node=Nil }
619 let inf = match t with
620 | { doc =d; node = Node(NC n) } -> snd(doc_ids d n)+1
621 | { node = Node(SC(i,_)) } -> i+1
625 | { doc = d; node= Node (NC n) } ->
626 let _,j = doc_ids t.doc n in
628 let id = array_find !contains_array inf j
630 if id == Text.nil then { doc = d; node= Nil }
632 {doc = d; node = Node(SC(id,Tree.next_sibling d (Tree.prev_doc d id))) }
633 | _ -> { t with node=Nil}
637 let subtree_tags t tag =
639 { doc = d; node = Node(NC n) } ->
643 let tagged_desc_array = ref [| |]
646 let init_tagged_next t tagid =
647 let l = subtree_tags (root t) tagid
649 tagged_desc_array := Array.create l { t with node= Nil };
652 if is_node t then begin
653 if tag t == tagid then
655 !tagged_desc_array.(!i) <- t;
658 collect (first_child t);
659 collect (next_sibling t)
666 let pr x= Format.fprintf ppf x in
668 { node=Nil } -> pr "NULLT: -1"
669 | { node=String(i) } | { node=Node(SC(i,_)) } -> pr "DocID: %i" (int_of_node i)
670 | { node=Node(NC(i)) } -> pr "Node: %i" (int_of_node i)
674 (* let tagged_next t tag =
675 if !idx >= Array.length !tagged_desc_array
676 then {t with node=Nil}
678 let r = !tagged_desc_array.(!idx)
684 let has_tagged_foll t tag = is_node (tagged_foll t tag)
685 let has_tagged_desc t tag = is_node (tagged_desc t tag)
688 Array.fold_left (fun a i -> DocIdSet.add i a) DocIdSet.empty (Text.contains t.doc s)
691 let contains_old t s =
692 let regexp = Str.regexp_string s in
695 let _ = Str.search_forward regexp arg 0;
699 let rec find t acc = match t.node with
702 if matching (string t) then DocIdSet.add i acc else acc
703 | Node(_) -> (find (left t )) ((find (right t)) acc)
705 find t DocIdSet.empty
708 let contains_iter t s =
709 let regexp = Str.regexp_string s in
712 let _ = Str.search_forward regexp arg 0;
716 let size = Text.size t.doc in
718 if n == size then acc
721 (if matching (Text.get_cached_text t.doc (Obj.magic n)) then
722 DocIdSet.add (Obj.magic n) acc
725 find DocIdSet.empty 0
730 let count_contains t s = Text.count_contains t.doc s
731 let count t s = Text.count t.doc s
734 if is_root t then false
736 if tag (parent t) == Tag.pcdata then false
738 let u = left (parent t) in
741 let print_xml_fast outc t =
742 let rec loop ?(print_right=true) t = match t.node with
744 | String (s) -> output_string outc (Text.get_text t.doc s)
745 | Node _ when Tag.equal (tag t) Tag.pcdata ->
747 if print_right then loop (right t)
750 let tg = Tag.to_string (tag t) in
754 output_char outc '<';
755 output_string outc tg;
757 Nil -> output_string outc "/>"
758 | String _ -> assert false
759 | Node(_) when Tag.equal (tag l) Tag.attribute ->
760 (loop_attributes (left l);
761 match (right l).node with
762 | Nil -> output_string outc "/>"
764 output_char outc '>';
766 output_string outc "</";
767 output_string outc tg;
768 output_char outc '>' )
770 output_char outc '>';
772 output_string outc "</";
773 output_string outc tg;
775 );if print_right then loop r
776 and loop_attributes a =
781 match (left a).node with
783 | _ -> string (left(left a))
785 output_char outc ' ';
786 output_string outc (Tag.to_string (tag a));
787 output_string outc "=\"";
788 output_string outc value;
789 output_char outc '"';
790 loop_attributes (right a)
793 loop ~print_right:false t
796 let print_xml_fast outc t =
797 if Tag.to_string (tag t) = "" then
798 print_xml_fast outc (first_child t)
799 else print_xml_fast outc t
805 let traversal t = Tree.traversal t.doc
806 let full_traversal t =
810 | String i -> () (*ignore(Text.get_text t.doc i) *)
817 let print_stats _ = ()
829 let _timings = Hashtbl.create 107
832 let time _ref f arg =
833 let t1 = Unix.gettimeofday () in
835 let t2 = Unix.gettimeofday () in
836 let t = (1000. *.(t2 -. t1)) in
838 let (time,count) = try
839 Hashtbl.find _timings _ref
844 and count = count + 1
846 Hashtbl.replace _timings _ref (time,count);r
851 let first_child_ doc node =
852 time ("XMLTree.FirstChild()") (XML.Tree.first_child doc) node
853 let next_sibling_ doc node =
854 time ("XMLTree.NextSibling()") (XML.Tree.next_sibling doc) node
856 let is_empty_ text node =
857 time ("TextCollection.IsEmpty()") (XML.Text.is_empty text) node
859 let prev_text_ doc node =
860 time ("XMLTree.PrevText()") (XML.Tree.prev_text doc) node
862 let my_text_ doc node =
863 time ("XMLTree.MyText()") (XML.Tree.my_text doc) node
865 let next_text_ doc node =
866 time ("XMLTree.NextText()") (XML.Tree.next_text doc) node
868 let is_leaf_ doc node =
869 time ("XMLTree.IsLeaf()") (XML.Tree.is_leaf doc ) node
871 let node_xml_id_ doc node =
872 time ("XMLTree.NodeXMLId()") (XML.Tree.node_xml_id doc ) node
874 let text_xml_id_ doc node =
875 time ("XMLTree.TextXMLId()") (XML.Tree.text_xml_id doc ) node
881 | Node (NC t) when is_leaf_ n.doc t ->
882 let txt = my_text_ n.doc t in
883 if is_empty_ n.doc txt
885 else Node(SC (txt,XML.Tree.nil))
887 let fs = first_child_ n.doc t in
888 let txt = prev_text_ n.doc fs in
889 if is_empty_ n.doc txt
891 else Node (SC (txt, fs))
892 | Node(SC (i,_)) -> String i
893 | Nil | String _ -> failwith "first_child"
895 { n with node = node'}
901 | Node (SC (_,ns)) -> norm ns
903 let ns = next_sibling_ n.doc t in
905 if XML.Tree.is_nil ns then
907 else prev_text_ n.doc ns
909 if is_empty_ n.doc txt
911 else Node (SC (txt, ns))
912 | Nil | String _ -> failwith "next_sibling"
914 { n with node = node'}
917 function { doc=d; node=Node(NC n)} -> node_xml_id_ d n
918 | { doc=d; node=Node(SC (i,_) )} -> text_xml_id_ d i
921 (* Wrapper around critical function *)
922 let string t = time ("TextCollection.GetText()") (string) t
923 let left = first_child
924 let right = next_sibling
925 let tag t = time ("XMLTree.GetTag()") (tag) t
927 let print_stats ppf =
928 let total_time,total_calls =
929 Hashtbl.fold (fun _ (t,c) (tacc,cacc) ->
930 tacc+. t, cacc + c) _timings (0.,0)
934 "Timing : Function Name, number of calls,%% of total calls, mean time, total time, %% of total time\n%!";
935 Hashtbl.iter (fun name (time,count) ->
936 Format.fprintf ppf "%-27s% 8d\t% 4.2f%%\t% 4.6f ms\t% 4.6f ms\t%04.2f%%\n%!"
939 (100. *. (float_of_int count)/.(float_of_int total_calls))
940 (time /. (float_of_int count))
942 (100. *. time /. total_time)) _timings;
943 Format.fprintf ppf "-------------------------------------------------------------------\n";
944 Format.fprintf ppf "%-27s% 8d\t% 4.0f%%\t########## ms\t% 4.6f ms\t% 4.0f%%\n%!"
945 "Total" total_calls 100. total_time 100.
948 let print_xml_fast outc t =
949 let rec loop ?(print_right=true) t = match t.node with
951 | String (s) -> output_string outc (string t)
952 | Node _ when Tag.equal (tag t) Tag.pcdata -> loop (left t); loop (right t)
955 let tg = Tag.to_string (tag t) in
959 output_char outc '<';
960 output_string outc tg;
962 Nil -> output_string outc "/>"
963 | String _ -> assert false
964 | Node(_) when Tag.equal (tag l) Tag.attribute ->
965 (loop_attributes (left l);
966 match (right l).node with
967 | Nil -> output_string outc "/>"
969 output_char outc '>';
971 output_string outc "</";
972 output_string outc tg;
973 output_char outc '>' )
975 output_char outc '>';
977 output_string outc "</";
978 output_string outc tg;
980 );if print_right then loop r
981 and loop_attributes a =
986 match (left a).node with
988 | _ -> string (left(left a))
990 output_char outc ' ';
991 output_string outc (Tag.to_string (tag a));
992 output_string outc "=\"";
993 output_string outc value;
994 output_char outc '"';
995 loop_attributes (right a)
998 loop ~print_right:false t
1001 let print_xml_fast outc t =
1002 if Tag.to_string (tag t) = "" then
1003 print_xml_fast outc (first_child t)
1004 else print_xml_fast outc t
1011 module Binary = DEBUGTREE
1013 module Binary = XML.Binary
1014 END (* IFDEF DEBUG *)