]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc
* code cleanup
[helm.git] / helm / DEVEL / mathml_editor / src / CMathMLFactoryXSLT.cc
diff --git a/helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc b/helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc
new file mode 100644 (file)
index 0000000..244d364
--- /dev/null
@@ -0,0 +1,72 @@
+
+#include "dom.hh"
+#include "TNode.hh"
+#include "TDocument.hh"
+#include "CMathMLFactoryXSLT.hh"
+#include "AMathMLConsumer.hh"
+
+void
+CMathMLFactoryXSLT::documentModified(TDocument& doc)
+{
+  if (TNode dirty = doc.dirtyNode()) 
+    {
+      std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
+      if (result)
+       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);
+      //style.save(doc.document(), stdout);
+      //style.save(res, stdout);
+      if (result)
+       {
+         DOM::Element root = res.get_documentElement();
+         assert(root);
+         assert(root.hasAttribute("xref"));
+
+         try
+           {
+             bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
+             assert(ok);
+           }
+         catch (DOM::DOMException& e)
+           {
+             cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
+             assert(0);
+           }
+       }
+      else
+       result = res;
+
+      doc.clearDirty();
+
+      if (consumer) consumer->documentModified(result);
+    }
+}
+
+bool
+CMathMLFactoryXSLT::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
+{
+  assert(e1);
+  assert(e2);
+  if (e1.getAttribute("xref") == id)
+    {
+      DOM::Node parent = e1.get_parentNode();
+      assert(parent);
+      parent.replaceChild(e2, e1);
+      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;
+    }
+}
+