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