]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc
* added show/hide cursro methods
[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   cout << "passo di qui" << endl;
20   std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
21   if (TNode dirty = doc.dirtyNode())
22     if (result.get_documentElement().hasAttribute("xref"))
23       dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
24                                        DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
25   DOM::Document res = style.apply(doc.document(), dirtyId);
26   assert(res);
27   style.save(doc.document(), stdout);
28
29   DOM::Element root = res.get_documentElement();
30   assert(root);
31   assert(root.hasAttribute("xref"));
32
33   if (result.get_documentElement().hasAttribute("xref"))
34     {
35       bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
36       assert(ok);
37     }
38   else
39     result.replaceChild(result.importNode(root, true), result.get_documentElement());
40
41   style.save(result, stdout);
42
43   doc.clearDirty();
44 }
45
46 bool
47 CMathMLFactoryXSLT::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
48 {
49   assert(e1);
50   assert(e2);
51   if (e1.getAttribute("xref") == id)
52     {
53       DOM::Node parent = e1.get_parentNode();
54       assert(parent);
55       parent.replaceChild(e2, e1);
56       return true;
57     }
58   else
59     {
60       DOM::Node p = e1.get_firstChild();
61       while (p)
62         {
63           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
64           if (p)
65             if (subst(p, id, e2)) return true;
66             else p = p.get_nextSibling();
67         }
68       return false;
69     }
70 }
71