]> matita.cs.unibo.it Git - helm.git/blob - helm/DEVEL/mathml_editor/test/main.cc
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / test / main.cc
1
2 #include "TNode.hh"
3 #include "TToken.hh"
4 #include "TDocument.hh"
5 #include "TPushParser.hh"
6 #include "TPushLexer.hh"
7 #include "TDictionary.hh"
8 #include "TListener.hh"
9 #include <GdomeSmartDOMXSLT.hh>
10
11 TDictionary dictionary;
12 DOM::Document result;
13
14 bool
15 subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
16 {
17   assert(e1);
18   assert(e2);
19   if (e1.getAttribute("xref") == id)
20     {
21       DOM::Node parent = e1.get_parentNode();
22       assert(parent);
23       parent.replaceChild(e2, e1);
24       return true;
25     }
26   else
27     {
28       DOM::Node p = e1.get_firstChild();
29       while (p)
30         {
31           while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
32           if (p)
33             if (subst(p, id, e2)) return true;
34             else p = p.get_nextSibling();
35         }
36       return false;
37     }
38 }
39
40 #if 0
41 bool
42 subst(const DOM::Node& parent, const DOM::GdomeString& id, const DOM::Element& newElem)
43 {
44   assert(parent);
45   assert(newElem);
46
47   DOM::Node p = parent.get_firstChild();
48   while (p)
49     {
50       while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
51       if (p)
52         {
53           DOM::Element el = p;
54           assert(el);
55           if (el.getAttribute("xref") == id)
56             {
57               parent.replaceChild(el, newElem);
58               return true;
59             }
60           else if (subst(el, id, newElem))
61             return true;
62           else
63             p = p.get_nextSibling();
64         }
65     }
66
67   return false;
68 }
69 #endif
70
71 class MyListener : public TListener
72 {
73 public:
74   MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
75
76   void callback(TDocument& doc)
77   {
78     TNode dirty = doc.dirtyNode();
79     if (dirty) 
80       {
81         cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
82         std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
83         if (result)
84           dirtyId.push_back(std::make_pair(DOM::GdomeString("id"),
85                                            DOM::GdomeString("'" + std::string(dirty["id"]) + "'")));
86         DOM::Document res = style.apply(doc.document(), dirtyId);
87         assert(res);
88         style.save(res, stdout);
89         if (result)
90           {
91             DOM::Element root = res.get_documentElement();
92             assert(root);
93             assert(root.hasAttribute("xref"));
94
95             if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
96               {
97                 // the following remove should not be necessary
98                 // according to the spec replaceChild should work just fine
99                 result.removeChild(result.get_documentElement());
100                 result.appendChild(result.importNode(root, true));
101               }
102             else
103               try
104                 {
105                   cout << "before" << endl;
106                   bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
107                   assert(ok);
108                   cout << "after" << endl;
109                 }
110               catch (DOM::DOMException e)
111                 {
112                   cerr << "exception " << e.code << " " << e.msg << endl;
113                   assert(0);
114                 }
115           }
116         else
117           result = res;
118
119         doc.clearDirty();
120       }
121   }
122
123 private:
124   const DOM::XSLTStylesheet& style;
125 };
126
127 main(int argc, char* argv[])
128 {
129   if (argc != 2)
130     {
131       cerr << "specify a string, please" << endl;
132       return -1;
133     }
134
135   cout << "loading the dictionary..." << endl;
136   dictionary.load("dictionary.xml");
137
138   cout << "loading the stylesheet..." << endl;
139   DOM::DOMImplementation di;
140   DOM::Document docStyle = di.createDocumentFromURI("./xsl/tml-mmlp.xsl");
141   DOM::XSLTStylesheet style(docStyle);
142   
143   MyListener listener(style);
144   TPushParser parser(dictionary, listener);
145   TPushLexer lexer(parser);
146
147   std::string s = argv[1];
148   for (unsigned long i = 0; i < s.length(); i++)
149     lexer.push(s[i]);
150   lexer.push('\n');
151   
152   cout << "finished" << endl;
153   di.saveDocumentToFile(result, "result.xml", GDOME_SAVE_LIBXML_INDENT);
154
155   cout << "done" << endl;
156
157   parser.document().serialize("output.xml");
158 }