4 #include "TDocument.hh"
5 #include "TPushParser.hh"
6 #include "TPushLexer.hh"
7 #include "TDictionary.hh"
8 #include "TListener.hh"
9 #include <GdomeSmartDOMXSLT.hh>
13 class MyResultListener : public DOM::EventListener
16 MyResultListener(const std::string& s) : msg(s) { };
18 virtual void handleEvent(const DOM::Event&);
21 const std::string msg;
25 MyResultListener::handleEvent(const DOM::Event& ev)
27 cout << "RECEIVED EVENT: " << ev.get_type() << " " << msg << " ";
28 const DOM::MutationEvent& me(ev);
30 const DOM::Node target(me.get_target());
32 cout << "target = " << target.get_nodeName() << " " << target.get_nodeType() << endl;
35 MyResultListener l1("?");
37 TDictionary dictionary;
40 extern void *parseMathMLFile(char *);
43 subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
47 if (e1.getAttribute("xref") == id)
49 DOM::Node parent = e1.get_parentNode();
51 DOM::Node next = e1.get_nextSibling();
52 parent.removeChild(e1);
53 parent.insertBefore(e2, next);
54 //parent.replaceChild(e2, e1);
59 DOM::Node p = e1.get_firstChild();
62 while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
64 if (subst(p, id, e2)) return true;
65 else p = p.get_nextSibling();
71 class MyListener : public TListener
74 MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
76 void callback(TDocument& doc)
78 cout << "listener callback " << static_cast<GdomeNode*>(doc.document()) << endl;
79 TNode dirty = doc.dirtyNode();
82 cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
83 std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
85 dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
86 DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
87 DOM::Document res = style.apply(doc.document(), dirtyId);
89 style.save(doc.document(), stdout);
90 style.save(res, stdout);
93 cout << "REPLACING A FRAGMENT OF THE DOCUMENT" << endl;
94 DOM::Element root = res.get_documentElement();
96 assert(root.hasAttribute("xref"));
98 if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
100 cout << "REPLACING ROOT" << endl;
101 // the following remove should not be necessary
102 // according to the spec replaceChild should work just fine
103 result.removeChild(result.get_documentElement());
104 result.appendChild(result.importNode(root, true));
109 cout << "before" << endl;
110 bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
112 cout << "after" << endl;
114 catch (DOM::DOMException e)
116 cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
122 cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
125 DOM::EventTarget et(result);
127 cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
128 et.addEventListener("DOMSubtreeModified", l1, true);
130 if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
131 cerr << "c'e' stato un errore" << endl;
139 const DOM::XSLTStylesheet& style;
144 Context(const std::string& s, TPushLexer& l) : buffer(s), i(0), lexer(l) { };
148 if (i < buffer.length())
150 cout << "document is " << static_cast<GdomeNode*>(result) << endl;
151 lexer.push(buffer[i++]);
153 else lexer.push('\n');
162 edit_timeout(Context* data)
172 push_char(Context* context, gchar ch)
175 cout << "*** SENDING " << ch << endl;
176 context->lexer.push(ch);
180 main(int argc, char* argv[])
182 cout << "loading the dictionary..." << endl;
183 dictionary.load("dictionary.xml");
185 cout << "loading the stylesheet..." << endl;
186 DOM::DOMImplementation di;
187 DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
188 DOM::XSLTStylesheet style(docStyle);
190 MyListener listener(style);
191 TPushParser parser(dictionary, listener);
192 TPushLexer lexer(parser);
201 DOM::Document doc = parser.document().document();
202 std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > np;
203 result = style.apply(doc, np);
204 style.save(result, stdout);
207 Context context("", lexer);
209 cout << "passing context " << &context << endl;
210 GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context);
213 GUI_unload_document();