1 #include <libxml++/libxml++.h>
7 void print_indentation(unsigned int indentation)
9 for(unsigned int i = 0; i < indentation; ++i)
15 //! get child element by id
17 \return pointer to element having id or null on error
18 \todo find libxmlpp pendant
20 const xmlpp::Element* get_element_by_id(const xmlpp::Element* element, const std::string &id)
22 const xmlpp::Attribute* cid = element->get_attribute("id");
23 if ( cid && ( cid->get_value() == id)) return element;
25 //Recurse through child nodes:
26 xmlpp::Node::NodeList list = element->get_children();
27 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
29 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(*iter);
31 const xmlpp::Element* match = get_element_by_id(element, id);
32 if (match) return match;
38 //! get namespace by id
40 \return namespace name or empty string on error
42 std::string get_namespace(const xmlpp::Element* root, const std::string &id)
45 const xmlpp::Element* element(get_element_by_id(root, id));
46 if ((!element)||(!element->get_attribute("name"))) return error;
47 return element->get_attribute("name")->get_value();
52 \return type name or empty string on error
54 std::string get_type(const xmlpp::Element* root, const std::string &id)
57 const xmlpp::Element* element(get_element_by_id(root, id));
58 if (!element) return error;
60 // TODO: not yet complete
61 // if we recurse - when do we stop?
62 // if it is a typedef? yes? (hmm if the typedef is in the file parsed this will not work)
64 // TODO: const and reference types (perhaps it is good enough to remove the const and &?)
65 // non-const reference types/pointers (output error message? not yet supported)
67 std::string tag(element->get_name());
68 if (tag=="ReferenceType") {
69 assert(element->get_attribute("type"));
70 std::string ret(get_type(root, element->get_attribute("type")->get_value()));
71 if (ret==error) return error;
73 }else if (tag=="CvQualifiedType") {
74 assert(element->get_attribute("type"));
75 std::string ret(get_type(root, element->get_attribute("type")->get_value()));
76 if (ret==error) return error;
77 return std::string("const ")+ret;
80 assert(element->get_attribute("name"));
81 assert(element->get_attribute("context"));
82 return get_namespace(root, element->get_attribute("context")->get_value())+"::"+element->get_attribute("name")->get_value();
87 typedef std::map<std::string, std::string> Args;
94 std::string ret_classname() const {
97 std::string cmd_classname() const {
102 std::ostream &operator<<(std::ostream &o, const t2n_procedure::Args &args) {
103 for (t2n_procedure::Args::const_iterator it=args.begin();it!=args.end();++it) {
104 if (it!=args.begin()) o << ", ";
105 o << it->second << " " << it->first;
110 std::ostream &operator<<(std::ostream &o, const t2n_procedure &f) {
111 o << f.ret_type << " " << f.name << "(" << f.args << ")";
118 Parser(const std::string &fname) : m_fname(fname) {}
120 std::list<t2n_procedure> get_procedures() {
121 xmlpp::DomParser parser;
122 // parser.set_validate();
123 parser.set_substitute_entities(); //We just want the text to be resolved/unescaped automatically.
124 parser.parse_file(m_fname);
128 const xmlpp::Node* pNode = parser.get_document()->get_root_node(); //deleted by DomParser.
129 const xmlpp::Element* root = dynamic_cast<const xmlpp::Element*>(pNode);
137 std::list<t2n_procedure> m_procedures;
139 void parse_function(const xmlpp::Element* root, const xmlpp::Node* node) {
140 const xmlpp::Element* element = dynamic_cast<const xmlpp::Element*>(node);
141 if (!element) return;
143 const xmlpp::Attribute* attributes = element->get_attribute("attributes");
144 const xmlpp::Attribute* name = element->get_attribute("name");
145 const xmlpp::Attribute* returns = element->get_attribute("returns");
146 if ((!attributes)||(!name)||(!returns)) return;
148 // check wether the procedure is marked (TODO: improve)
149 // attributes are speparated by spaces?
150 if (attributes->get_value().find("gccxml(libt2n")==std::string::npos) return;
152 // TODO: extract command group
153 // something like: sed 's,gccxml(libt2n-\([a-z]*}),\1,'
157 // we need the return type
158 f.ret_type=get_type(root, returns->get_value());
159 f.name=name->get_value();
161 xmlpp::Node::NodeList list = node->get_children("Argument");
162 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
164 const xmlpp::Element* arg = dynamic_cast<const xmlpp::Element*>(*iter);
166 assert(arg->get_name() == "Argument");
167 assert(arg->get_attribute("name"));
168 assert(arg->get_attribute("type"));
169 assert(f.args.find(arg->get_attribute("name")->get_value())==f.args.end());
170 f.args[arg->get_attribute("name")->get_value()]=get_type(root, arg->get_attribute("type")->get_value());
173 m_procedures.push_back(f);
176 void visit_node(const xmlpp::Element* root, const xmlpp::Node* node = NULL, unsigned int indentation = 0)
178 if (!node) node=root;
179 std::cout << std::endl; //Separate nodes by an empty line.
181 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
182 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
183 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
185 if(nodeText && nodeText->is_white_space()) //Let's ignore the indenting - you don't always want to do this.
188 std::string nodename = node->get_name();
190 if(!nodeText && !nodeComment && !nodename.empty()) //Let's not say "name: text".
192 print_indentation(indentation);
193 // std::cout << "Node name = " << node->get_name() << std::endl;
194 // std::cout << "Node name = " << nodename << std::endl;
195 if (node->get_name() == "Function") {
196 parse_function(root, node);
199 else if(nodeText) //Let's say when it's text. - e.g. let's say what that white space is.
201 print_indentation(indentation);
202 std::cout << "Text Node" << std::endl;
205 //Treat the various node types differently:
208 print_indentation(indentation);
209 std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
213 print_indentation(indentation);
214 std::cout << "comment = " << nodeComment->get_content() << std::endl;
218 print_indentation(indentation);
219 std::cout << "content = " << nodeContent->get_content() << std::endl;
221 else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
223 //A normal Element node:
225 //line() works only for ElementNodes.
226 print_indentation(indentation);
227 std::cout << " line = " << node->get_line() << std::endl;
230 const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
231 for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
233 const xmlpp::Attribute* attribute = *iter;
234 print_indentation(indentation);
235 std::cout << " Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
238 const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
241 std::cout << "title found: =" << attribute->get_value() << std::endl;
248 //Recurse through child nodes:
249 xmlpp::Node::NodeList list = node->get_children();
250 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
252 visit_node(root, *iter, indentation + 2); //recursive
259 std::set<std::string>
260 used_groups(const std::list<t2n_procedure> &funcs) {
261 typedef std::set<std::string> Groups;
263 for (std::list<t2n_procedure>::const_iterator it=funcs.begin();it!=funcs.end();++it)
264 // since we use std::set each group is inserted only once
265 groups.insert(it->group);
269 void output_common_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
270 std::set<std::string> groups(used_groups(procs));
272 o << "#include \"codegen-stubhead.hxx\"\n";
273 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
274 o << "class cmd_group_" << *it << " : public libt2n::command\n"
277 << " friend class boost::serialization::access;\n"
278 << " template<class Archive>\n"
279 << " void serialize(Archive & ar, const unsigned int version)\n"
280 << " {ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);}\n"
284 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
285 o << "class " << it->ret_classname() << " : public libt2n::result\n"
288 << " " << it->ret_type << " res;\n"
289 << " friend class boost::serialization::access;\n"
290 << " template<class Archive>\n"
291 << " void serialize(Archive & ar, const unsigned int version)\n"
293 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);\n"
294 << " ar & BOOST_SERIALIZATION_NVP(res);\n"
297 << " " << it->ret_classname() << "(const " << it->ret_type << " &_res) : res(_res) {}\n"
298 << " " << it->ret_type << " get_data() { return res; }\n"
301 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
302 o << "class " << it->cmd_classname() << " : public " << "cmd_group_" << it->group << "\n"
305 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
306 o << " " << ait->second << " " << ait->first << ";\n";
308 o << " friend class boost::serialization::access;\n"
309 << " template<class Archive>\n"
310 << " void serialize(Archive & ar, const unsigned int version)\n"
312 << " ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_" << it->group << ");\n";
313 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
314 o << " ar & BOOST_SERIALIZATION_NVP(" << ait->first << ");\n";
317 // default constructor
321 << " " << it->cmd_classname() << "() {}\n";
323 // constructor taking all arguments
324 o << " " << it->cmd_classname() << "(";
325 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
326 if (ait!=it->args.begin()) o << ", ";
327 o << ait->second << " _" << ait->first;
330 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
331 if (ait!=it->args.begin()) o << ", ";
332 o << ait->first << "(_" << ait->first << ")";
335 << " libt2n::result* operator()();\n"
340 void output_common_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp) {
341 std::set<std::string> groups(used_groups(procs));
343 o << "#include \"" << common_hpp << "\"\n"
344 << "#include <boost/serialization/export.hxx>\n"
346 << "/* register types with boost serialization */\n";
347 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
348 o << "BOOST_CLASS_EXPORT(cmd_group_" << *it << ")\n";
350 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
351 o << "BOOST_CLASS_EXPORT("<<it->ret_classname()<<")\n"
352 << "BOOST_CLASS_EXPORT("<<it->cmd_classname()<<")\n";
356 void output_client_hpp(std::ostream &o, const std::list<t2n_procedure> &procs) {
357 std::set<std::string> groups(used_groups(procs));
359 o << "#include <command_client.hxx>\n";
361 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
362 o << "class cmd_group_" << *it << "_client : public libt2n::command_client\n"
365 << "cmd_group_" << *it << "_client(libt2n::client_connection &_c,\n"
366 << " long long _command_timeout_usec=command_timeout_usec_default,\n"
367 << " long long _hello_timeout_usec=hello_timeout_usec_default)\n"
368 << " : libt2n::command_client(_c,_command_timeout_usec,_hello_timeout_usec)\n"
370 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
371 if (pit->group==*it) {
372 o << " " << *pit << ";\n";
379 void output_client_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp, const std::string &client_hpp) {
380 std::set<std::string> groups(used_groups(procs));
382 o << "#include \"" << client_hpp << "\"\n"
383 << "#include \"" << common_hpp << "\"\n"
385 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
386 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return NULL; }\n";
389 for (std::set<std::string>::iterator it=groups.begin();it!=groups.end();++it) {
390 for (std::list<t2n_procedure>::const_iterator pit=procs.begin();pit!=procs.end();++pit) {
391 if (pit->group==*it) {
392 o << pit->ret_type << " cmd_group_" << *it << "_client::" << pit->name << "(" << pit->args << ")\n"
394 << " libt2n::result_container rc;\n"
395 << " send_command(new " << pit->cmd_classname() << "(";
396 for (t2n_procedure::Args::const_iterator ait=pit->args.begin();ait!=pit->args.end();++ait) {
397 if (ait!=pit->args.begin()) o << ", ";
401 << " " << pit->ret_classname() << "* res=dynamic_cast<" << pit->ret_classname() << "*>(rc.get_result());\n"
402 << " if (!res) throw libt2n::t2n_communication_error(\"result object of wrong type\");\n"
403 << " return res->get_data();\n"
409 // include in this compilation unit to ensure the compilation unit is used
411 // http://www.google.de/search?q=g%2B%2B+static+initializer+in+static+library
412 o << "#include \"" << common_cpp << "\"\n";
415 void output_server_cpp(std::ostream &o, const std::list<t2n_procedure> &procs, const std::string &common_hpp, const std::string &common_cpp) {
416 o << "#include \"" << common_hpp << "\"\n";
418 for (std::list<t2n_procedure>::const_iterator it=procs.begin();it!=procs.end();++it) {
420 o << "libt2n::result* " << it->cmd_classname() << "::operator()() { return new " << it->ret_classname() << "(" << it->name << "(";
421 for (t2n_procedure::Args::const_iterator ait=it->args.begin();ait!=it->args.end();++ait) {
422 if (ait!=it->args.begin()) o << ", ";
427 o << "#include \"" << common_cpp << "\"\n";
432 toupper(std::string s) {
433 for (unsigned i=0; i<s.length(); ++i) s[i]=toupper(s[i]);
438 replace(std::string s, char f, char r) {
439 for (unsigned i=0; i<s.length(); ++i) if (s[i]==f) s[i]=r;
443 struct header_file : public std::ofstream
445 header_file(const char* fname) : std::ofstream(fname) {
446 std::cerr << "create header: '" << fname << "'" << std::endl;
447 std::string macro(replace(toupper(fname),'.','_'));
448 *this << "// automatically generated code - do not edit\n" << std::endl;
449 *this << "#ifndef " << macro << "\n"
450 << "#define " << macro << "\n";
453 *this << "#endif" << std::endl;
457 struct cpp_file : public std::ofstream
459 cpp_file(const char* fname) : std::ofstream(fname) {
460 std::cerr << "create cpp: '" << fname << "'" << std::endl;
461 *this << "// automatically generated code - do not edit\n" << std::endl;
466 main(int argc, char* argv[])
468 std::string filepath;
470 filepath = argv[1]; //Allow the user to specify a different XML file to parse.
472 filepath = "example.xml";
474 Parser parser(filepath);
475 std::list<t2n_procedure> procedures(parser.get_procedures());
477 std::cerr << "Procedures:" << std::endl;
478 for (std::list<t2n_procedure>::const_iterator it=procedures.begin();it!=procedures.end();++it)
479 std::cerr << *it << ";" << std::endl;
481 std::set<std::string> groups(used_groups(procedures));
482 std::cerr << "Used groups:" << std::endl;
483 for (std::set<std::string>::const_iterator it=groups.begin();it!=groups.end();++it)
484 std::cerr << *it << std::endl;
486 std::string prefix("test_");
487 std::string common_hpp_fname(prefix+"common.hxx");
488 std::string common_cpp_fname(prefix+"common.cpp");
489 std::string client_hpp_fname(prefix+"client.hxx");
490 std::string client_cpp_fname(prefix+"client.cpp");
491 std::string server_cpp_fname(prefix+"client.cpp");
493 header_file common_hpp(common_hpp_fname.c_str());
494 output_common_hpp(common_hpp, procedures);
496 cpp_file common_cpp(common_cpp_fname.c_str());
497 output_common_cpp(common_cpp, procedures, "common-stub.hxx");
499 header_file client_hpp(client_hpp_fname.c_str());
500 output_client_hpp(client_hpp, procedures);
502 cpp_file client_cpp(client_cpp_fname.c_str());
503 output_client_cpp(client_cpp, procedures, common_hpp_fname, common_cpp_fname, client_hpp_fname);
505 cpp_file server_cpp(server_cpp_fname.c_str());
506 output_server_cpp(server_cpp, procedures, common_hpp_fname, common_cpp_fname);