--- /dev/null
+
+#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;
+ }
+}
+