-bool
-subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
-{
- assert(e1);
- assert(e2);
- if (e1.getAttribute("xref") == id)
- {
- DOM::Node parent = e1.get_parentNode();
- assert(parent);
- DOM::Node next = e1.get_nextSibling();
- parent.removeChild(e1);
- parent.insertBefore(e2, next);
- //parent.replaceChild(e2, e1);
- return true;
- }
- else
- {
- DOM::Node p = e1.get_firstChild();
- while (p)
- {
- while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
- if (p)
- if (subst(p, id, e2)) return true;
- else p = p.get_nextSibling();
- }
- return false;
- }
-}
-
-class MyListener : public TListener
-{
-public:
- MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
-
- void callback(TDocument& doc)
- {
- cout << "listener callback " << static_cast<GdomeNode*>(doc.document()) << endl;
- TNode dirty = doc.dirtyNode();
- if (dirty)
- {
- cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
- std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
- if (result)
- dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
- DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
-
- DOM::Document res = style.apply(doc.document(), dirtyId);
- assert(res);
- style.save(doc.document(), stdout);
- //style.save(res, stdout);
- if (result)
- {
- cout << "REPLACING A FRAGMENT OF THE DOCUMENT" << endl;
- DOM::Element root = res.get_documentElement();
- assert(root);
- assert(root.hasAttribute("xref"));
-
- if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
- {
- cout << "REPLACING ROOT" << endl;
- result.replaceChild(result.importNode(root, true), result.get_documentElement());
-#if 0
- // the following remove should not be necessary
- // according to the spec replaceChild should work just fine
- result.removeChild(result.get_documentElement());
- result.appendChild(result.importNode(root, true));
-#endif
- }
- else
- try
- {
- cout << "before" << endl;
- bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
- assert(ok);
- cout << "after" << endl;
- }
- catch (DOM::DOMException e)
- {
- cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
- assert(0);
- }
- }
- else
- {
- cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
- result = res;
-
- DOM::EventTarget et(result);
- assert(et);
- cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
- et.addEventListener("DOMSubtreeModified", l1, true);
-
- if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
- cerr << "c'e' stato un errore" << endl;
- }
- style.save(result, stdout);
-
- doc.clearDirty();
- }
- }
-
-private:
- const DOM::XSLTStylesheet& style;
-};
-