4 #include "TDocument.hh"
5 #include "TPushParser.hh"
6 #include "TPushLexer.hh"
7 #include "TDictionary.hh"
8 #include "TListener.hh"
9 #include <GdomeSmartDOMXSLT.hh>
11 TDictionary dictionary;
15 subst(const DOM::Element& e1, const DOM::GdomeString& id, const DOM::Element& e2)
19 if (e1.getAttribute("xref") == id)
21 DOM::Node parent = e1.get_parentNode();
23 parent.replaceChild(e2, e1);
28 DOM::Node p = e1.get_firstChild();
31 while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
33 if (subst(p, id, e2)) return true;
34 else p = p.get_nextSibling();
42 subst(const DOM::Node& parent, const DOM::GdomeString& id, const DOM::Element& newElem)
47 DOM::Node p = parent.get_firstChild();
50 while (p && p.get_nodeType() != DOM::Node::ELEMENT_NODE) p = p.get_nextSibling();
55 if (el.getAttribute("xref") == id)
57 parent.replaceChild(el, newElem);
60 else if (subst(el, id, newElem))
63 p = p.get_nextSibling();
71 class MyListener : public TListener
74 MyListener(const DOM::XSLTStylesheet& s) : style(s) { };
76 void callback(TDocument& doc)
78 TNode dirty = doc.dirtyNode();
81 cout << "recreating subtree with id " << std::string(dirty["id"]) << endl;
82 std::vector< std::pair<DOM::GdomeString, DOM::GdomeString> > dirtyId;
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);
88 style.save(res, stdout);
91 DOM::Element root = res.get_documentElement();
93 assert(root.hasAttribute("xref"));
95 if (result.get_documentElement().getAttribute("xref") == root.getAttribute("xref"))
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));
105 cout << "before" << endl;
106 bool ok = subst(result.get_documentElement(), root.getAttribute("xref"), result.importNode(root, true));
108 cout << "after" << endl;
110 catch (DOM::DOMException e)
112 cerr << "exception " << e.code << " " << e.msg << endl;
124 const DOM::XSLTStylesheet& style;
127 main(int argc, char* argv[])
131 cerr << "specify a string, please" << endl;
135 cout << "loading the dictionary..." << endl;
136 dictionary.load("dictionary.xml");
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);
143 MyListener listener(style);
144 TPushParser parser(dictionary, listener);
145 TPushLexer lexer(parser);
147 std::string s = argv[1];
148 for (unsigned long i = 0; i < s.length(); i++)
152 cout << "finished" << endl;
153 di.saveDocumentToFile(result, "result.xml", GDOME_SAVE_LIBXML_INDENT);
155 cout << "done" << endl;
157 parser.document().serialize("output.xml");