]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLT.cc
* assertion too strict 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   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
32   if (result.get_documentElement().hasAttribute("xref"))
33     {
34       bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
35       assert(ok);
36     }
37   else
38     result.replaceChild(result.importNode(root, true), result.get_documentElement());
39
40   style.save(result, stdout);
41
42   doc.clearDirty();
43 }
44
45 bool
46 CMathMLFactoryXSLT::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
47 {
48   assert(e1);
49   assert(e2);
50   if (e1.getAttribute("xref") == id)
51     {
52       DOM::Node parent = e1.get_parentNode();
53       assert(parent);
54       parent.replaceChild(e2, e1);
55       return true;
56     }
57   else
58     {
59       DOM::Node p = e1.get_firstChild();
60       while (p)
61         {
62           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
63           if (p)
64             if (subst(p, id, e2)) return true;
65             else p = p.get_nextSibling();
66         }
67       return false;
68     }
69 }
70