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