]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/ocaml/c_mathml_editor.cc
* this is a large commit
[helm.git] / helm / DEVEL / mathml_editor / ocaml / c_mathml_editor.cc
1
2 #include <GdomeSmartDOMXSLT.hh>
3
4 #include "ALogger.hh"
5 #include "TDictionary.hh"
6 #include "CMathMLFactoryXSLT.hh"
7 #include "TPushLexer.hh"
8 #include "TPushParser.hh"
9
10 class CCallbackLogger : public ALogger
11 {
12 public:
13   CCallbackLogger(void (*)(int, const char*, void*), void*);
14   virtual ~CCallbackLogger() { };
15
16 protected:
17   virtual void message(Level, const std::string&);
18
19 private:
20   void (*callback)(int, const char*, void*);
21   void* user_data;
22 };
23
24 CCallbackLogger::CCallbackLogger(void (*cb)(int, const char*, void*), void* data) : callback(cb), user_data(data)
25 {
26   assert(callback);
27 }
28
29 void
30 CCallbackLogger::message(Level l, const std::string& s)
31 {
32   assert(callback);
33   callback(l, s.c_str(), user_data);
34 }
35
36 struct Editor
37 {
38   Editor(const DOM::Document&, const DOM::Document&, const DOM::Document&, void (*)(int, const char*, void*), void*);
39   ~Editor();
40
41   ALogger*        logger;
42   TDictionary*    dictionary;
43   DOMX::XSLTStylesheet* tml_mml;
44   DOMX::XSLTStylesheet* tml_tex;
45   AMathMLFactory* factory;
46   TPushParser*    parser;
47   APushLexer*     lexer;
48 };
49
50 Editor::Editor(const DOM::Document& dict, const DOM::Document& mml, const DOM::Document& tex,
51                void (*cb)(int, const char*, void*), void* data)
52 {
53   assert(cb);
54   logger = new CCallbackLogger(cb, data);
55   dictionary = new TDictionary(*logger);
56   dictionary->load(DOM::Document(dict));
57   tml_mml = new DOMX::XSLTStylesheet(mml);
58   tml_tex = new DOMX::XSLTStylesheet(tex);
59   factory = new CMathMLFactoryXSLT(*logger, *tml_mml);
60   parser = new TPushParser(*logger, *factory, *dictionary);
61   lexer = new TPushLexer(*logger, *parser);
62 }
63
64 Editor::~Editor()
65 {
66   delete lexer;
67   delete parser;
68   delete factory;
69   delete tml_tex;
70   delete tml_mml;
71   delete dictionary;
72   delete logger;
73 }
74
75 extern "C" Editor*
76 c_mathml_editor_new(GdomeDocument* dictionary,
77                     GdomeDocument* tml_mml,
78                     GdomeDocument* tml_tex,
79                     void (*log_message_cb)(int, const char*, void*),
80                     void* user_data)
81 {
82   return new Editor(DOM::Document(dictionary),
83                     DOM::Document(tml_mml),
84                     DOM::Document(tml_tex), log_message_cb, user_data);
85 }
86
87 extern "C" void
88 c_mathml_editor_destroy(Editor* editor)
89 {
90   assert(editor);
91   delete editor;
92 }
93
94 extern "C" int
95 c_mathml_editor_freeze(Editor* editor)
96 {
97   assert(editor);
98   return editor->parser->freeze();
99 }
100
101 extern "C" int
102 c_mathml_editor_thaw(Editor* editor)
103 {
104   assert(editor);
105   return editor->parser->thaw();
106 }
107
108 extern "C" void
109 c_mathml_editor_push(Editor* editor, char ch)
110 {
111   assert(editor);
112   editor->lexer->push(ch);
113 }
114
115 extern "C" void
116 c_mathml_editor_drop(Editor* editor, int alt)
117 {
118   assert(editor);
119   editor->lexer->drop(alt != 0);
120 }
121
122 extern "C" char*
123 c_mathml_editor_get_tex(const Editor* editor)
124 {
125   assert(editor);
126   DOM::Document res = editor->tml_tex->apply(editor->parser->document());
127   DOM::Element root = res.get_documentElement();
128   return strdup(std::string(root.get_nodeValue()).c_str());
129 }
130
131 extern "C" void
132 c_mathml_editor_reset(Editor* editor)
133 {
134   assert(editor);
135   editor->parser->reset();
136 }
137
138 extern "C" GdomeDocument*
139 c_mathml_editor_get_tml(const Editor* editor)
140 {
141   assert(editor);
142   GdomeNode* n = editor->parser->document().cloneNode(true).gdome_object();
143   GdomeDocument* doc = gdome_cast_doc(n);
144   assert(n && doc);
145   return doc;
146 }
147
148 extern "C" GdomeDocument*
149 c_mathml_editor_get_mml(const Editor* editor)
150 {
151   assert(editor);
152   GdomeNode* n = editor->factory->document().gdome_object();
153   GdomeDocument* doc = gdome_cast_doc(n);
154   assert(n && doc);
155   return doc;
156 }
157