]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/gTopLevel/xmlDiff.ml
Debugging stuff removed.
[helm.git] / helm / gTopLevel / xmlDiff.ml
index e35a9ec5ee1b107c646a85f44e98f0ad34c723c6..2b9806c44b2de0f3c39ca40c88af25b0f9d3e71d 100644 (file)
  * http://cs.unibo.it/helm/.
  *)
 
+let myprerr_endline = prerr_endline;;
 let prerr_endline _ = ();;
 
 let mathmlns = "http://www.w3.org/1998/Math/MathML";;
 let xmldiffns = "http://helm.cs.unibo.it/XmlDiff";;
+let helmns = "http://www.cs.unibo.it/helm";;
 
-let highlight_node (doc: Gdome.document) (n: Gdome.node) =
+type highlighted_nodes = Gdome.node list;;
+
+let rec make_visible (n: Gdome.node) =
+ match n#get_parentNode with
+    None -> ()
+  | Some p ->
+     match p#get_namespaceURI, p#get_localName with
+        Some nu, Some ln when
+         nu#to_string = mathmlns && ln#to_string = "maction" ->
+          (new Gdome.element_of_node p)#setAttribute
+            ~name:(Gdome.domString "selection")
+             ~value:(Gdome.domString "2") ;
+          make_visible p
+      | _,_ -> make_visible p
+;;
+
+let highlight_node ?(color="yellow") (doc: Gdome.document) (n: Gdome.node) =
  let highlight (n: Gdome.node) =
   let highlighter =
    doc#createElementNS
     ~namespaceURI:(Some (Gdome.domString mathmlns))
     ~qualifiedName:(Gdome.domString "m:mstyle")
   in
-   highlighter#setAttribute ~name:(Gdome.domString "background")
-    ~value:(Gdome.domString "yellow") ;
+   highlighter#setAttribute ~name:(Gdome.domString "mathbackground")
+    ~value:(Gdome.domString color) ;
    highlighter#setAttributeNS
     ~namespaceURI:(Some (Gdome.domString xmldiffns))
     ~qualifiedName:(Gdome.domString "xmldiff:type")
@@ -46,8 +64,10 @@ let highlight_node (doc: Gdome.document) (n: Gdome.node) =
        None -> assert false
      | Some p -> p
    in
+    ignore
+     (parent#replaceChild ~oldChild:n ~newChild:(highlighter :> Gdome.node)) ;
     ignore (highlighter#appendChild n) ;
-    ignore (parent#appendChild (highlighter :> Gdome.node))
+    (highlighter :> Gdome.node)
  in
   let rec find_mstylable_node n =
    match n#get_namespaceURI, n#get_localName with
@@ -72,7 +92,63 @@ prerr_endline ("@@@ find_mstylable_node salgo da  " ^ match n#get_localName with
          None -> assert false
        | Some p -> find_mstylable_node p
   in
-   highlight (find_mstylable_node n)
+   let highlighter = highlight (find_mstylable_node n) in
+    make_visible highlighter ;
+    highlighter
+;;
+
+let iter_children ~f (n:Gdome.node) =
+ let rec aux =
+  function
+     None -> ()
+   | Some n ->
+      let sibling = n#get_nextSibling in
+       (f n) ;
+       aux sibling
+ in
+  aux n#get_firstChild
+;;
+
+let highlight_nodes ~xrefs (doc:Gdome.document) =
+ let highlighted = ref [] in
+ let rec aux (n:Gdome.element) =
+  if
+   List.mem
+    (n#getAttributeNS ~namespaceURI:(Gdome.domString helmns)
+     ~localName:(Gdome.domString "xref"))#to_string
+    xrefs
+  then
+   highlighted :=
+    (highlight_node ~color:"#00ff00"(* light green *) doc (n :> Gdome.node))::
+    !highlighted ;
+   iter_children (n :> Gdome.node)
+    ~f:(function n ->
+         if n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE then
+          aux (new Gdome.element_of_node n))
+ in
+  aux doc#get_documentElement ;
+  !highlighted
+;;
+
+let dim_nodes =
+ List.iter 
+  (function (n : Gdome.node) ->
+    assert
+     (n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE &&
+      ((new Gdome.element_of_node n)#getAttributeNS
+        ~namespaceURI:(Gdome.domString xmldiffns)
+        ~localName:(Gdome.domString "type"))#to_string = "fake") ;
+    let true_child =
+     match n#get_firstChild with
+        None -> assert false
+      | Some n -> n in
+    let p =
+     match n#get_parentNode with
+        None -> assert false
+      | Some n -> n
+    in
+     ignore (p#replaceChild ~oldChild:n ~newChild:true_child)
+  )
 ;;
 
 let update_dom ~(from : Gdome.document) (d : Gdome.document) =
@@ -217,8 +293,10 @@ prerr_endline !debugs ;
               done
           | _,_ -> assert false
         end ;
+(* debugging only
         let fchildren = f#get_childNodes in
         let tchildren = t#get_childNodes in
+*)
          let rec dumb_diff =
           function
              [],[] -> ()
@@ -232,37 +310,39 @@ prerr_endline ("XML_DIFF: appendo i nodi residui " ^ String.concat ", " (List.ma
                (function n ->
                  let n' = from#importNode n true in
                    ignore (f#appendChild n') ;
-                   highlight_node from n'
+                   ignore (highlight_node from n')
                ) tl2
            | tl1,[] ->
 prerr_endline ("XML_DIFF: cancello i nodi residui " ^ String.concat ", " (List.map (function n ->  match n#get_localName with Some s -> s#to_string | None -> "_") tl1)) ;
               List.iter (function n -> ignore (f#removeChild n)) tl1
          in
-          let node_list_of_nodeList nl =
-           let rec aux =
-            match nl#item ~index:i with
+          let node_list_of_nodeList n =
+           let rec aux =
+            function
                None -> []
              | Some n when
                    n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE
                 or n#get_nodeType = GdomeNodeTypeT.TEXT_NODE ->
-                 n::(aux (i+1))
+                 n::(aux n#get_nextSibling)
              | Some n ->
 prerr_endline ("XML_DIFF: mi sto perdendo i nodi di tipo " ^ string_of_int (Obj.magic n#get_nodeType)) ;
-               aux (i+1)
+               aux n#get_nextSibling
            in
-            aux 0
+            aux n#get_firstChild
           in
+(* debugging stuff only
 for i = 0 to fchildren#get_length - 1 do
 match fchildren#item i with
 None -> prerr_endline "EUREKA: ma siamo matti?"
 |Some n ->
 match n#get_nodeType with GdomeNodeTypeT.ATTRIBUTE_NODE -> prerr_endline "EUREKA attr" | GdomeNodeTypeT.CDATA_SECTION_NODE -> prerr_endline "EUREKA text" | GdomeNodeTypeT.DOCUMENT_FRAGMENT_NODE -> prerr_endline "EUREKA document fragment" | _ -> prerr_endline ("EUREKA " ^ string_of_int (Obj.magic n#get_nodeType))
 done ;
-prerr_endline("XML_DIFF: from ha " ^ string_of_int fchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList fchildren)) ^ " figli");
-prerr_endline("XML_DIFF: to   ha " ^ string_of_int tchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList tchildren)) ^ " figli");
+prerr_endline("XML_DIFF: from ha " ^ string_of_int fchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList f)) ^ " figli");
+prerr_endline("XML_DIFF: to   ha " ^ string_of_int tchildren#get_length ^ " nodi, ovvero " ^ string_of_int (List.length (node_list_of_nodeList t)) ^ " figli");
 prerr_endline ("XML_DIFF: chiamo dumb_diff su " ^ String.concat ", " (List.map (function n ->  match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList fchildren)) ^ " e " ^ String.concat ", " (List.map (function n ->  match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList tchildren))) ;
+*)
            dumb_diff
-            (node_list_of_nodeList fchildren, node_list_of_nodeList tchildren)
+            (node_list_of_nodeList f, node_list_of_nodeList t)
    | t1,t2,_,_,_,_ when
       (t1 = GdomeNodeTypeT.ELEMENT_NODE || t1 = GdomeNodeTypeT.TEXT_NODE) &&
       (t2 = GdomeNodeTypeT.ELEMENT_NODE || t2 = GdomeNodeTypeT.TEXT_NODE) ->
@@ -279,56 +359,6 @@ prerr_endline ("XML_DIFF: chiamo dumb_diff su " ^ String.concat ", " (List.map (
         in
          begin
 prerr_endline ("%%% CANCELLO HIGHLIGHTER " ^ (match f#get_localName with Some s -> s#to_string | None -> "_") ^ " CON DENTRO " ^ (match true_child#get_localName with Some s -> s#to_string | None -> "_")) ;
-(********* PROVE E RIPROVE
-let old_true_child = true_child in
-let true_child =
-   (from#createElementNS
-    ~namespaceURI:(Some (Gdome.domString mathmlns))
-    ~qualifiedName:(Gdome.domString "m:mrow") :> Gdome.node) ;
-in
-ignore (f#removeChild old_true_child) ;
-ignore (true_child#appendChild old_true_child) ;
-          ignore (p#replaceChild ~oldChild:f ~newChild:true_child) ;
-(*
-ignore (true_child#appendChild old_true_child) ;
-          ignore (p#replaceChild ~oldChild:f ~newChild:true_child) ;
-*)
-prerr_endline ("%%% RICORSIONE SU " ^ (match true_child#get_localName with Some s -> s#to_string | None -> "_") ^ "/" ^ (match old_true_child#get_localName with Some s -> s#to_string | None -> "_") ^ " E " ^ (match old_true_child#get_localName with Some s -> s#to_string | None -> "_")) ;
-let fchildren = old_true_child#get_childNodes in
-let l = ref [] in
-for i = 0 to fchildren#get_length -1 do
- l := !l @ [ match (fchildren#item i) with None -> "?" | Some n -> match n#get_localName with Some s -> s#to_string | None -> "_" ]
-done ;
-let tchildren = t#get_childNodes in
-let l2 = ref [] in
-for i = 0 to tchildren#get_length -1 do
- l2 := !l2 @ [ match (tchildren#item i) with None -> "?" | Some n -> match n#get_localName with Some s -> s#to_string | None -> "_" ]
-done ;
-prerr_endline ("%%% Il primo nodo ha i seguenti figli: " ^ String.concat "," !l) ;
-prerr_endline ("%%% Il secondo nodo ha i seguenti figli: " ^ String.concat "," !l2) ;
-
-let fchildren = old_true_child#get_childNodes in
-let tchildren = t#get_childNodes in
-let node_list_of_nodeList nl =
-let rec aux i =
- match nl#item ~index:i with
-    None -> []
-  | Some n when
-        n#get_nodeType = GdomeNodeTypeT.ELEMENT_NODE
-     or n#get_nodeType = GdomeNodeTypeT.TEXT_NODE ->
-      n::(aux (i+1))
-  | Some n ->
-prerr_endline ("### XML_DIFF: mi sto perdendo i nodi di tipo " ^ string_of_int (Obj.magic n#get_nodeType)) ;
-    aux (i+1)
-in
- aux 0
-          in
-prerr_endline ("RRR XML_DIFF: chiamo dumb_diff su " ^ String.concat ", " (List.map (function n ->  match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList fchildren)) ^ " e " ^ String.concat ", " (List.map (function n ->  match n#get_localName with Some s -> s#to_string | None -> "_") (node_list_of_nodeList tchildren))) ;
-          aux true_child old_true_child t
-(*
-          aux p true_child t
-*)
-*****)
           ignore (p#replaceChild ~oldChild:f ~newChild:true_child) ;
           aux p true_child t
          end
@@ -339,7 +369,7 @@ prerr_endline ("%%% XML_DIFF: importo il nodo " ^ match t'#get_localName with So
 prerr_endline ("Rimpiazzo" ^ (match f#get_namespaceURI with Some s -> s#to_string) ^ ":" ^ (match f#get_localName with Some s -> s#to_string) ^ " con " ^ (match t#get_namespaceURI with Some s -> s#to_string) ^ ":" ^ (match t#get_localName with Some s -> s#to_string) ^ " in " ^ (match p#get_localName with Some s -> s#to_string | None -> "_")) ;
 *)
          ignore (p#replaceChild ~newChild:t' ~oldChild:f) ;
-         highlight_node from t'
+         ignore (highlight_node from t')
    | _,_,_,_,_,_ -> assert false
  in
   try
@@ -348,32 +378,28 @@ prerr_endline ("Rimpiazzo" ^ (match f#get_namespaceURI with Some s -> s#to_strin
     (d#get_documentElement :> Gdome.node)
   with
      (GdomeInit.DOMException (e,msg) as ex) ->
-(*
+(* CSC: Non si puo' per problemi di linking ;-(
     let module E = GdomeDOMExceptionT in
 *)
        prerr_endline
         ("DOM EXCEPTION: " ^ msg ^ " --- " ^
-string_of_int (Obj.magic e)) ;
-       raise ex
- (*
-        match e with
-           E.NO_ERR -> "NO_ERR"
-         | E.INDEX_SIZE_ERR -> "INDEX_SIZE_ERR"
-         | E.DOMSTRING_SIZE_ERR -> "DOMSTRING_SIZE_ERR"
-         | E.HIERARCHY_REQUEST_ERR -> "HIERARCHY_REQUEST_ERR"
-         | E.WRONG_DOCUMENT_ERR -> "WRONG_DOCUMENT_ERR"
-         | E.INVALID_CHARACTER_ERR -> "INVALID_CHARACTER_ERR"
-         | E.NO_DATA_ALLOWED_ERR -> "NO_DATA_ALLOWER_ERR"
-         | E.NO_MODIFICATION_ALLOWED_ERR -> "NO_MODIFICATION_ALLOWED_ERR"
-         | E.NOT_FOUND_ERR -> "NOT_FOUND_ERR"
-         | E.NOT_SUPPORTED_ERR -> "NOT_SUPPORTED_ERR"
-         | E.INUSE_ATTRIBUTE_ERR -> "INUSE_ATTRIBUTE_ERR"
-         | E.INVALID_STATE_ERR -> "INVALID_STATE_ERR"
-         | E.SYNTAX_ERR -> "SYNTAX_ERR"
-         | E.INVALID_MODIFICATION_ERR -> "INVALID_MODIFICATION_ERR"
-         | E.NAMESPACE_ERR -> "NAMESPACE_ERR"
-         | E.INVALID_ACCESS_ERR -> "INVALID_ACCESS_ERR"
-*)
+          match e with
+             GdomeDOMExceptionT.NO_ERR -> "NO_ERR"
+           | GdomeDOMExceptionT.INDEX_SIZE_ERR -> "INDEX_SIZE_ERR"
+           | GdomeDOMExceptionT.DOMSTRING_SIZE_ERR -> "DOMSTRING_SIZE_ERR"
+           | GdomeDOMExceptionT.HIERARCHY_REQUEST_ERR -> "HIERARCHY_REQUEST_ERR"
+           | GdomeDOMExceptionT.WRONG_DOCUMENT_ERR -> "WRONG_DOCUMENT_ERR"
+           | GdomeDOMExceptionT.INVALID_CHARACTER_ERR -> "INVALID_CHARACTER_ERR"
+           | GdomeDOMExceptionT.NO_DATA_ALLOWED_ERR -> "NO_DATA_ALLOWER_ERR"
+           | GdomeDOMExceptionT.NO_MODIFICATION_ALLOWED_ERR -> "NO_MODIFICATION_ALLOWED_ERR"
+           | GdomeDOMExceptionT.NOT_FOUND_ERR -> "NOT_FOUND_ERR"
+           | GdomeDOMExceptionT.NOT_SUPPORTED_ERR -> "NOT_SUPPORTED_ERR"
+           | GdomeDOMExceptionT.INUSE_ATTRIBUTE_ERR -> "INUSE_ATTRIBUTE_ERR"
+           | GdomeDOMExceptionT.INVALID_STATE_ERR -> "INVALID_STATE_ERR"
+           | GdomeDOMExceptionT.SYNTAX_ERR -> "SYNTAX_ERR"
+           | GdomeDOMExceptionT.INVALID_MODIFICATION_ERR -> "INVALID_MODIFICATION_ERR"
+           | GdomeDOMExceptionT.NAMESPACE_ERR -> "NAMESPACE_ERR"
+           | GdomeDOMExceptionT.INVALID_ACCESS_ERR -> "INVALID_ACCESS_ERR")
   | e ->
     prerr_endline "PROBLEMA" ;
     raise e