]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/src/CMathMLFactoryXSLTDiff.cc
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / src / CMathMLFactoryXSLTDiff.cc
1
2 #include "dom.hh"
3 #include "Diff.hh"
4 #include "TNode.hh"
5 #include "TDocument.hh"
6 #include "CMathMLFactoryXSLTDiff.hh"
7 #include "AMathMLConsumer.hh"
8
9 CMathMLFactoryXSLTDiff::CMathMLFactoryXSLTDiff(ALogger& l, const DOMX::XSLTStylesheet& s)
10   : AMathMLFactory(l), style(s)
11 {
12   reset();
13 }
14
15 void
16 CMathMLFactoryXSLTDiff::reset()
17 {
18   DOM::DOMImplementation di;
19   DOM::DocumentType dt;
20   result = di.createDocument(MATHML_NS_URI, "m:math", dt);
21 }
22
23 void
24 CMathMLFactoryXSLTDiff::documentModified(TDocument& doc)
25 {
26   if (TNode dirty = doc.dirtyNode()) 
27     {
28       std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
29       if (false && result.get_documentElement().hasAttribute("xref"))
30         dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
31                                          DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
32       DOM::Document res = style.apply(doc.document(), dirtyId);
33       assert(res);
34       //cout << "*** THE DIRTY FRAGMENT HAS ID = " << std::string(dirty["id"]) << endl;
35       //style.save(doc.document(), stdout);
36       cout << "*** THE CURRENT DOCUMENT:" << endl;
37       if (result) style.save(result, stdout);
38       cout << "*** THE NEW DOCUMENT:" << endl;
39       style.save(res, stdout);
40       cout << "*** THE DIFF:" << endl;
41       DOMX::Diff diff = DOMX::Diff::diff(result, res);
42       style.save(diff.document(), stdout);
43       diff.patch();
44 #if 0
45       DOM::Element root = res.get_documentElement();
46       assert(root);
47       assert(root.hasAttribute("xref"));
48       if (result.get_documentElement().hasAttribute("xref"))
49         {
50           bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
51           assert(ok);
52         }
53       else
54         result = res;
55 #endif
56       //style.save(result, stdout);
57
58       doc.clearDirty();
59     }
60 }
61
62 bool
63 CMathMLFactoryXSLTDiff::subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
64 {
65   assert(e1);
66   assert(e2);
67   if (e1.getAttribute("xref") == id)
68     {
69       DOMX::Diff diff = DOMX::Diff::diff(e1, e2);
70       style.save(diff.document(), stdout);
71       diff.patch();
72       return true;
73     }
74   else
75     {
76       DOM::Node p = e1.get_firstChild();
77       while (p)
78         {
79           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
80           if (p)
81             if (subst(p, id, e2)) return true;
82             else p = p.get_nextSibling();
83         }
84       return false;
85     }
86 }
87