X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2FDEVEL%2Fmathml_editor%2Ftest%2Feditor.cc;h=9d548289c6a2625491060ef5b83ae543a492c369;hb=8335f6c45ef87edd700b898e91489eb9cb551304;hp=265d35e53477249bda2226930c436ce8e8a62666;hpb=89262281b6e83bd2321150f81f1a0583645eb0c8;p=helm.git diff --git a/helm/DEVEL/mathml_editor/test/editor.cc b/helm/DEVEL/mathml_editor/test/editor.cc index 265d35e53..9d548289c 100644 --- a/helm/DEVEL/mathml_editor/test/editor.cc +++ b/helm/DEVEL/mathml_editor/test/editor.cc @@ -1,142 +1,35 @@ -#include "TNode.hh" -#include "TToken.hh" -#include "TDocument.hh" +#include "dom.hh" #include "TPushParser.hh" #include "TPushLexer.hh" #include "TDictionary.hh" -#include "TListener.hh" -#include +#include "CLoggerConsole.hh" +#include "CMathMLFactoryXSLT.hh" +#include "AMathMLConsumer.hh" #include "guiGTK.h" -class MyResultListener : public DOM::EventListener -{ -public: - MyResultListener(const std::string& s) : msg(s) { }; - - virtual void handleEvent(const DOM::Event&); - -private: - const std::string msg; -}; - -void -MyResultListener::handleEvent(const DOM::Event& ev) -{ - cout << "RECEIVED EVENT: " << ev.get_type() << " " << msg << " "; - const DOM::MutationEvent& me(ev); - assert(me); - const DOM::Node target(me.get_target()); - assert(target); - cout << "target = " << target.get_nodeName() << " " << target.get_nodeType() << endl; -} - -MyResultListener l1("?"); - TDictionary dictionary; -DOM::Document result; extern void *parseMathMLFile(char *); -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 +class CMathMLConsumer : public AMathMLConsumer { public: - MyListener(const DOM::XSLTStylesheet& s) : style(s) { }; + CMathMLConsumer(void) { firstTime = true; }; - void callback(TDocument& doc) + virtual void documentModified(const DOM::Document& result) { - cout << "listener callback " << static_cast(doc.document()) << endl; - TNode dirty = doc.dirtyNode(); - if (dirty) + if (firstTime) { - cout << "recreating subtree with id " << std::string(dirty["id"]) << endl; - std::vector< std::pair > 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; - // 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)); - } - 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(result) << endl; - et.addEventListener("DOMSubtreeModified", l1, true); - - if (GUI_load_document(gdome_cast_doc(static_cast(result))) < 0) - cerr << "c'e' stato un errore" << endl; - } - - doc.clearDirty(); + if (GUI_load_document(gdome_cast_doc(static_cast(result))) < 0) + cerr << "c'e' stato un errore" << endl; + firstTime = false; } } private: - const DOM::XSLTStylesheet& style; + bool firstTime; }; struct Context @@ -145,11 +38,7 @@ struct Context void send(void) { - if (i < buffer.length()) - { - cout << "document is " << static_cast(result) << endl; - lexer.push(buffer[i++]); - } + if (i < buffer.length()) lexer.push(buffer[i++]); else lexer.push('\n'); } @@ -179,17 +68,19 @@ push_char(Context* context, gchar ch) main(int argc, char* argv[]) { - cout << "loading the dictionary..." << endl; + CLoggerConsole logger; + logger.info("loading the dictionary..."); dictionary.load("dictionary.xml"); - cout << "loading the stylesheet..." << endl; + logger.info("loading the stylesheet..."); DOM::DOMImplementation di; DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl"); DOM::XSLTStylesheet style(docStyle); - - MyListener listener(style); - TPushParser parser(dictionary, listener); - TPushLexer lexer(parser); + + CMathMLConsumer consumer; + CMathMLFactoryXSLT factory(logger, consumer, style); + TPushParser parser(logger, factory, dictionary); + TPushLexer lexer(logger, parser); #if 0 lexer.push('$'); @@ -206,7 +97,6 @@ main(int argc, char* argv[]) Context context("", lexer); - cout << "passing context " << &context << endl; GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context); GUI_run(); GUI_uninit();