+ let next t = nodei ( (inode t) + 1 )
+ let next2 t = nodei ( (inode t) + 2 )
+ let next3 t = nodei ( (inode t) + 3 )
+
+ let print_xml_fast2 =
+ let _ = init_stack () in
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let tree = tree.doc in
+ let fin = tree_closing tree t in
+ let rec loop_tag t tag =
+ if t <= fin then
+ if tree_is_open tree t then
+ (* opening tag *)
+ if tag == Tag.pcdata then
+ begin
+ output_string outc (text_get_cached_text tree (tree_my_text_unsafe tree t));
+ loop (next2 t) (* skip closing $ *)
+ end
+ else
+ let tagstr = tag_str tag in
+ let _ = output_char outc '<';
+ output_string outc tagstr in
+ let t' = next t in
+ if tree_is_open tree t' then
+ let _ = push tagstr in
+ let tag' = tree_tag_id tree t' in
+ if tag' == Tag.attribute then let t'' = loop_attr (next t') 0 in
+ output_string outc ">"; loop t'' else (output_string outc ">";loop_tag t' tag')
+ else (* closing with no content *)
+ let _ = output_string outc "/>" in
+ loop (next t')
+ else
+ begin
+ (* closing tag *)
+ output_string outc "</";
+ output_string outc (pop());
+ output_char outc '>';
+ loop (next t);
+ end
+ and loop t = loop_tag t (tree_tag_id tree t)
+ and loop_attr t n =
+ if tree_is_open tree t then
+ let attname = att_str (tree_tag_id tree t) in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ let t = next t in (* open $@ *)
+ output_string outc (text_get_cached_text tree (tree_my_text_unsafe tree t));
+ output_char outc '"';
+ loop_attr (next3 t) (n+1)
+ else
+ next t (* close @ *)
+ in loop t
+
+ let print_xml_fast =
+ let h = Hashtbl.create MED_H_SIZE in
+ let tag_str t = try Hashtbl.find h t with
+ Not_found -> let s = Tag.to_string t in
+ Hashtbl.add h t s;s
+ in
+ let h_att = Hashtbl.create MED_H_SIZE in
+ let att_str t = try Hashtbl.find h_att t with
+ Not_found -> let s = Tag.to_string t in
+ let attname = String.sub s 3 ((String.length s) -3) in
+ Hashtbl.add h_att t attname;attname
+ in fun outc tree t ->
+ let rec loop ?(print_right=true) t =
+ if t != nil
+ then
+ let tagid = tree_tag_id tree.doc t in
+ if tagid==Tag.pcdata
+ then
+ begin
+ let tid = tree_my_text_unsafe tree.doc t in
+ output_string outc (text_get_cached_text tree.doc tid);
+ if print_right
+ then loop (next_sibling tree t);
+ end
+ else
+ let tagstr = tag_str tagid in
+ let l = first_child tree t
+ and r = next_sibling tree t
+ in
+ output_char outc '<';
+ output_string outc tagstr;
+ if l == nil then output_string outc "/>"
+ else
+ if (tag tree l) == Tag.attribute then
+ begin
+ loop_attributes (first_child tree l);
+ if (next_sibling tree l) == nil then output_string outc "/>"
+ else
+ begin
+ output_char outc '>';
+ loop (next_sibling tree l);
+ output_string outc "</";
+ output_string outc tagstr;
+ output_char outc '>';
+ end;
+ end
+ else
+ begin
+ output_char outc '>';
+ loop l;
+ output_string outc "</";
+ output_string outc tagstr;
+ output_char outc '>';
+ end;
+ if print_right then loop r
+ and loop_attributes a =
+ if a != nil
+ then
+ let attname = att_str (tag tree a) in
+ let fsa = first_child tree a in
+ let tid = tree_my_text_unsafe tree.doc fsa in
+ output_char outc ' ';
+ output_string outc attname;
+ output_string outc "=\"";
+ output_string outc (text_get_cached_text tree.doc tid);
+ output_char outc '"';
+ loop_attributes (next_sibling tree a)
+ in
+ loop ~print_right:false t
+
+
+ let print_xml_fast outc tree t =
+ if (tag tree t) = Tag.document_node then
+ print_xml_fast outc tree (first_child tree t)
+ else print_xml_fast outc tree t
+
+let tags_children t tag =
+ let a,_,_,_ = Hashtbl.find t.ttable tag in a
+let tags_below t tag =
+ let _,a,_,_ = Hashtbl.find t.ttable tag in a
+let tags_siblings t tag =
+ let _,_,a,_ = Hashtbl.find t.ttable tag in a
+let tags_after t tag =
+ let _,_,_,a = Hashtbl.find t.ttable tag in a
+
+
+let tags t tag = Hashtbl.find t.ttable tag
+
+
+let rec binary_parent t n =
+ let r =
+ if tree_is_first_child t.doc n
+ then tree_parent t.doc n
+ else tree_prev_sibling t.doc n
+ in if tree_tag_id t.doc r = Tag.pcdata then
+ binary_parent t r
+ else r
+
+let doc_ids t n = tree_doc_ids t.doc n
+
+let subtree_tags t tag = ();
+ fun n -> if n == nil then 0 else
+ tree_subtree_tags t.doc n tag
+
+let get_text t n =
+ let tid = tree_my_text t.doc n in
+ if tid == nulldoc then "" else
+ text_get_cached_text t.doc tid
+
+
+let dump_tree fmt tree =
+ let rec loop t n =
+ if t != nil then
+ let tag = (tree_tag_id tree.doc t ) in
+ let tagstr = Tag.to_string tag in
+ let tab = String.make n ' ' in
+
+ if tag == Tag.pcdata || tag == Tag.attribute_data
+ then
+ Format.fprintf fmt "%s<%s>%s</%s>\n"
+ tab tagstr (text_get_cached_text tree.doc (tree_my_text tree.doc t)) tagstr
+ else begin
+ Format.fprintf fmt "%s<%s>\n" tab tagstr;
+ loop (tree_first_child tree.doc t) (n+2);
+ Format.fprintf fmt "%s</%s>\n%!" tab tagstr;
+ end;
+ loop (tree_next_sibling tree.doc t) n
+ in
+ loop root 0
+;;
+
+
+let print_xml_fast3 t = tree_print_xml_fast3 t.doc