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 result.replaceChild(result.importNode(root, true), result.get_documentElement());
103 // the following remove should not be necessary
104 // according to the spec replaceChild should work just fine
105 result.removeChild(result.get_documentElement());
106 result.appendChild(result.importNode(root, true));
112 cout << "before" << endl;
113 bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
115 cout << "after" << endl;
117 catch (DOM::DOMException e)
119 cout << "!!!!!!!!!!!!!!!! EXCEPTION " << e.code << " " << e.msg << endl;
125 cout << "SETTING THE DOCUMENT FOR THE FIRST TIME" << endl;
128 DOM::EventTarget et(result);
130 cout << "SETTING EVENT LISTENER (EDITOR) ON " << static_cast<GdomeNode*>(result) << endl;
131 et.addEventListener("DOMSubtreeModified", l1, true);
133 if (GUI_load_document(gdome_cast_doc(static_cast<GdomeNode*>(result))) < 0)
134 cerr << "c'e' stato un errore" << endl;
136 style.save(result, stdout);
143 const DOM::XSLTStylesheet& style;
148 Context(const std::string& s, TPushLexer& l) : buffer(s), i(0), lexer(l) { };
152 if (i < buffer.length())
154 cout << "document is " << static_cast<GdomeNode*>(result) << endl;
155 lexer.push(buffer[i++]);
157 else lexer.push('\n');
166 edit_timeout(Context* data)
176 push_char(Context* context, gchar ch)
179 cout << "*** SENDING " << ch << endl;
180 context->lexer.push(ch);
184 main(int argc, char* argv[])
186 cout << "loading the dictionary..." << endl;
187 dictionary.load("dictionary.xml");
189 cout << "loading the stylesheet..." << endl;
190 DOM::DOMImplementation di;
191 DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
192 DOM::XSLTStylesheet style(docStyle);
194 MyListener listener(style);
195 TPushParser parser(dictionary, listener);
196 TPushLexer lexer(parser);
205 DOM::Document doc = parser.document().document();
206 std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > np;
207 result = style.apply(doc, np);
208 style.save(result, stdout);
211 Context context("", lexer);
213 cout << "passing context " << &context << endl;
214 GUI_init(&argc, &argv, "mathmleditor", 500, 600, &context);
217 GUI_unload_document();