]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc
Debug code removed.
[helm.git] / helm / DEVEL / mathml_editor / src / CMathMLFactoryXSLT.cc
1
2 #include "dom.hh"
3 #include "TNode.hh"
4 #include "TDocument.hh"
5 #include "CMathMLFactoryXSLT.hh"
6 #include "AMathMLConsumer.hh"
7
8 CMathMLFactoryXSLT::CMathMLFactoryXSLT(ALogger& l, const DOMX::XSLTStylesheet& s)
9   : AMathMLFactory(l), style(s)
10 {
11   DOM::DOMImplementation di;
12   DOM::DocumentType dt;
13   result = di.createDocument(MATHML_NS_URI, "m:math", dt);
14 }
15
16 void
17 CMathMLFactoryXSLT::documentModified(TDocument& doc)
18 {
19   std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
20   if (TNode dirty = doc.dirtyNode())
21     if (result.get_documentElement().hasAttribute("xref"))
22       dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
23                                        DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
24   DOM::Document res = style.apply(doc.document(), dirtyId);
25   assert(res);
26   style.save(doc.document(), stdout);
27
28   DOM::Element root = res.get_documentElement();
29   assert(root);
30
31   if (result.get_documentElement().hasAttribute("xref"))
32     {
33       bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
34       assert(ok);
35     }
36   else
37     result.replaceChild(result.importNode(root, true), result.get_documentElement());
38
39   style.save(result, stdout);
40
41   doc.clearDirty();
42 }
43
44 bool
45 CMathMLFactoryXSLT::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
46 {
47   assert(e1);
48   assert(e2);
49   if (e1.getAttribute("xref") == id)
50     {
51       DOM::Node parent = e1.get_parentNode();
52       assert(parent);
53       parent.replaceChild(e2, e1);
54       return true;
55     }
56   else
57     {
58       DOM::Node p = e1.get_firstChild();
59       while (p)
60         {
61           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
62           if (p)
63             if (subst(p, id, e2)) return true;
64             else p = p.get_nextSibling();
65         }
66       return false;
67     }
68 }
69