From 66eccd131232e49fdb0790ee5b48cc41829157fe Mon Sep 17 00:00:00 2001 From: Claudio Sacerdoti Coen Date: Fri, 25 Jul 2003 15:19:02 +0000 Subject: [PATCH] Highlighting of changed parts. --- helm/gTopLevel/xmlDiff.ml | 161 ++++++++++++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 16 deletions(-) diff --git a/helm/gTopLevel/xmlDiff.ml b/helm/gTopLevel/xmlDiff.ml index 3d9bb39ac..e35a9ec5e 100644 --- a/helm/gTopLevel/xmlDiff.ml +++ b/helm/gTopLevel/xmlDiff.ml @@ -23,9 +23,58 @@ * 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 highlight_node (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#setAttributeNS + ~namespaceURI:(Some (Gdome.domString xmldiffns)) + ~qualifiedName:(Gdome.domString "xmldiff:type") + ~value:(Gdome.domString "fake") ; + let parent = + match n#get_parentNode with + None -> assert false + | Some p -> p + in + ignore (highlighter#appendChild n) ; + ignore (parent#appendChild (highlighter :> Gdome.node)) + in + let rec find_mstylable_node n = + match n#get_namespaceURI, n#get_localName with + Some nu, Some ln when + nu#to_string = mathmlns && + let ln = ln#to_string in + ln <> "mtr" && ln <> "mtd" -> n + | Some nu, Some ln when + nu#to_string = mathmlns && + let ln = ln#to_string in + ln = "mtr" || ln = "mtd" -> +prerr_endline "@@@ find_mstylable_node scendo"; + let true_child = + match n#get_firstChild with + None -> assert false + | Some n -> n + in + find_mstylable_node true_child + | _,_ -> +prerr_endline ("@@@ find_mstylable_node salgo da " ^ match n#get_localName with Some n -> n#to_string | None -> "_") ; + match n#get_parentNode with + None -> assert false + | Some p -> find_mstylable_node p + in + highlight (find_mstylable_node n) +;; + let update_dom ~(from : Gdome.document) (d : Gdome.document) = let rec aux (p: Gdome.node) (f: Gdome.node) (t: Gdome.node) = (* Perche' non andava? @@ -56,7 +105,7 @@ prerr_endline ("XML_DIFF: preservo il nodo "^ nu#to_string ^ ":" ^ln#to_string); let processed = ref [] in for i = 0 to flen -1 do match fattrs#item i with - None -> assert false + None -> () (* CSC: sigh, togliere un nodo rompe fa decrescere la lunghezza ==> passare a un while *) | Some attr -> match attr#get_namespaceURI with None -> @@ -65,8 +114,12 @@ prerr_endline ("XML_DIFF: preservo il nodo "^ nu#to_string ^ ":" ^ln#to_string); let name = attr#get_nodeName in match tattrs#getNamedItem ~name with None -> -myprerr_endline ("XML_DIFF: rimuovo l'attributo " ^ name#to_string); - ignore (f#removeChild attr) +prerr_endline ("XML_DIFF: DOM 1; rimuovo l'attributo " ^ name#to_string); +(* CSC: questo non mi toglieva solo l'attributo, ma anche altri nodi qui + e la' ;-( + ignore (f#removeChild attr) +*) + ignore (fattrs#removeNamedItem ~name) | Some attr' -> processed := (None,Some name)::!processed ; @@ -81,7 +134,7 @@ myprerr_endline ("XML_DIFF: rimuovo l'attributo " ^ name#to_string); prerr_endline ("XML_DIFF: DOM 1; preservo l'attributo " ^ name#to_string); () | Some v1, Some v2 -> -myprerr_endline ("XML_DIFF: DOM 1; rimpiazzo l'attributo " ^ name#to_string ^ ": old value=" ^ v1#to_string ^ ", new value=" ^ v2#to_string); +prerr_endline ("XML_DIFF: DOM 1; rimpiazzo l'attributo " ^ name#to_string ^ ": old value=" ^ v1#to_string ^ ", new value=" ^ v2#to_string); let attr'' = from#importNode attr' true in ignore (fattrs#setNamedItem attr'') | _,_ -> assert false @@ -96,8 +149,14 @@ myprerr_endline ("XML_DIFF: DOM 1; rimpiazzo l'attributo " ^ name#to_string ^ ": tattrs#getNamedItemNS ~namespaceURI ~localName with None -> -myprerr_endline ("XML_DIFF: rimuovo l'attributo " ^ localName#to_string); +prerr_endline ("XML_DIFF: DOM 2; rimuovo l'attributo " ^ namespaceURI#to_string ^ ":" ^ localName#to_string); +(* CSC: questo non mi toglieva solo l'attributo, ma anche altri nodi qui + e la' ;-( ignore (f#removeChild attr) +*) + ignore + (fattrs#removeNamedItemNS + ~namespaceURI ~localName) | Some attr' -> processed := (Some namespaceURI,Some localName)::!processed ; @@ -112,7 +171,7 @@ myprerr_endline ("XML_DIFF: rimuovo l'attributo " ^ localName#to_string); prerr_endline ("XML_DIFF: DOM 2; preservo l'attributo " ^ namespaceURI#to_string ^ ":" ^ localName#to_string); () | Some _, Some _ -> -myprerr_endline ("XML_DIFF: DOM 2; rimpiazzo l'attributo " ^ namespaceURI#to_string ^ ":" ^ localName#to_string); +prerr_endline ("XML_DIFF: DOM 2; rimpiazzo l'attributo " ^ namespaceURI#to_string ^ ":" ^ localName#to_string); let attr'' = from#importNode attr' true in ignore (fattrs#setNamedItem attr'') | _,_ -> assert false @@ -125,10 +184,10 @@ let debugs = ref "" in let namespaceURI,localName = match attr#get_namespaceURI with None -> -debugs := ("XML_DIFF: DOM 1.0; creo un nuovo attributo " ^ attr#get_nodeName#to_string) ; +debugs := ("XML_DIFF: DOM 1; creo un nuovo attributo " ^ attr#get_nodeName#to_string) ; None,attr#get_nodeName | Some namespaceURI as v -> -debugs := ("XML_DIFF: DOM 2.0; creo un nuovo attributo " ^ namespaceURI#to_string ^ ":" ^ match attr#get_localName with Some v -> v#to_string | None -> assert false); +debugs := ("XML_DIFF: DOM 2; creo un nuovo attributo " ^ namespaceURI#to_string ^ ":" ^ match attr#get_localName with Some v -> v#to_string | None -> assert false); v, match attr#get_localName with None -> assert false | Some v -> v @@ -153,7 +212,7 @@ debugs := ("XML_DIFF: DOM 2.0; creo un nuovo attributo " ^ namespaceURI#to_strin ) !processed) then let attr' = from#importNode attr false in -myprerr_endline !debugs ; +prerr_endline !debugs ; ignore (fattrs#setNamedItem attr') done | _,_ -> assert false @@ -168,14 +227,15 @@ prerr_endline "XML_DIFF: processo una coppia di figli" ; aux f he1 he2 ; dumb_diff (tl1,tl2) | [],tl2 -> -myprerr_endline "XML_DIFF: appendo i nodi residui" ; +prerr_endline ("XML_DIFF: appendo i nodi residui " ^ String.concat ", " (List.map (function n -> match n#get_localName with Some s -> s#to_string | None -> "#" ^ (match n#get_nodeValue with Some s -> s#to_string | None -> "_") ^ "#") tl2)) ; List.iter (function n -> let n' = from#importNode n true in - ignore (f#appendChild n') + ignore (f#appendChild n') ; + highlight_node from n' ) tl2 | tl1,[] -> -myprerr_endline "XML_DIFF: cancello i nodi residui" ; +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 = @@ -200,17 +260,86 @@ match n#get_nodeType with GdomeNodeTypeT.ATTRIBUTE_NODE -> prerr_endline "EUREKA 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: 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) | t1,t2,_,_,_,_ when (t1 = GdomeNodeTypeT.ELEMENT_NODE || t1 = GdomeNodeTypeT.TEXT_NODE) && (t2 = GdomeNodeTypeT.ELEMENT_NODE || t2 = GdomeNodeTypeT.TEXT_NODE) -> - let t' = from#importNode t true in -myprerr_endline "XML_DIFF: importo il nodo" ; + if + t1 = GdomeNodeTypeT.ELEMENT_NODE && + ((new Gdome.element_of_node f)#getAttributeNS + ~namespaceURI:(Gdome.domString xmldiffns) + ~localName:(Gdome.domString "type"))#to_string = "fake" + then + let true_child = + match f#get_firstChild with + None -> assert false + | Some n -> n + 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 + else + let t' = from#importNode t true in +prerr_endline ("%%% XML_DIFF: importo il nodo " ^ match t'#get_localName with Some n -> n#to_string | None -> "_") ; (* 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) + ignore (p#replaceChild ~newChild:t' ~oldChild:f) ; + highlight_node from t' | _,_,_,_,_,_ -> assert false in try -- 2.39.2