]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLTDiff.cc
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / src / CMathMLFactoryXSLTDiff.cc
diff --git a/helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLTDiff.cc b/helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLTDiff.cc
new file mode 100644 (file)
index 0000000..53fa5aa
--- /dev/null
@@ -0,0 +1,87 @@
+
+#include "dom.hh"
+#include "Diff.hh"
+#include "TNode.hh"
+#include "TDocument.hh"
+#include "CMathMLFactoryXSLTDiff.hh"
+#include "AMathMLConsumer.hh"
+
+CMathMLFactoryXSLTDiff::CMathMLFactoryXSLTDiff(ALogger& l, const DOMX::XSLTStylesheet& s)
+  : AMathMLFactory(l), style(s)
+{
+  reset();
+}
+
+void
+CMathMLFactoryXSLTDiff::reset()
+{
+  DOM::DOMImplementation di;
+  DOM::DocumentType dt;
+  result = di.createDocument(MATHML_NS_URI, "m:math", dt);
+}
+
+void
+CMathMLFactoryXSLTDiff::documentModified(TDocument& doc)
+{
+  if (TNode dirty = doc.dirtyNode()) 
+    {
+      std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
+      if (false && result.get_documentElement().hasAttribute("xref"))
+       dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
+                                        DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
+      DOM::Document res = style.apply(doc.document(), dirtyId);
+      assert(res);
+      //cout << "*** THE DIRTY FRAGMENT HAS ID = " << std::string(dirty["id"]) << endl;
+      //style.save(doc.document(), stdout);
+      cout << "*** THE CURRENT DOCUMENT:" << endl;
+      if (result) style.save(result, stdout);
+      cout << "*** THE NEW DOCUMENT:" << endl;
+      style.save(res, stdout);
+      cout << "*** THE DIFF:" << endl;
+      DOMX::Diff diff = DOMX::Diff::diff(result, res);
+      style.save(diff.document(), stdout);
+      diff.patch();
+#if 0
+      DOM::Element root = res.get_documentElement();
+      assert(root);
+      assert(root.hasAttribute("xref"));
+      if (result.get_documentElement().hasAttribute("xref"))
+       {
+         bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
+         assert(ok);
+       }
+      else
+       result = res;
+#endif
+      //style.save(result, stdout);
+
+      doc.clearDirty();
+    }
+}
+
+bool
+CMathMLFactoryXSLTDiff::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
+{
+  assert(e1);
+  assert(e2);
+  if (e1.getAttribute("xref") == id)
+    {
+      DOMX::Diff diff = DOMX::Diff::diff(e1, e2);
+      style.save(diff.document(), stdout);
+      diff.patch();
+      return true;
+    }
+  else
+    {
+      DOM::Node p = e1.get_firstChild();
+      while (p)
+       {
+         while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
+         if (p)
+           if (subst(p, id, e2)) return true;
+           else p = p.get_nextSibling();
+       }
+      return false;
+    }
+}
+