driver.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // file : tests/roundtrip/driver.cxx
  2. // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
  3. // license : MIT; see accompanying LICENSE file
  4. #include <string>
  5. #include <fstream>
  6. #include <cassert>
  7. #include <iostream>
  8. #include <xml/parser>
  9. #include <xml/serializer>
  10. using namespace std;
  11. using namespace xml;
  12. const bool trace = false;
  13. int
  14. main (int argc, char* argv[])
  15. {
  16. if (argc != 2)
  17. {
  18. cerr << "usage: " << argv[0] << " <xml-file>" << endl;
  19. return 1;
  20. }
  21. try
  22. {
  23. ifstream ifs;
  24. ifs.exceptions (ifstream::badbit | ifstream::failbit);
  25. ifs.open (argv[1], ifstream::in | ifstream::binary);
  26. parser p (ifs,
  27. argv[1],
  28. parser::receive_default |
  29. parser::receive_attributes_event |
  30. parser::receive_namespace_decls);
  31. serializer s (cout, "out", 0);
  32. bool in_attr (false);
  33. for (parser::event_type e (p.next ()); e != parser::eof; e = p.next ())
  34. {
  35. switch (e)
  36. {
  37. case parser::start_element:
  38. {
  39. if (trace)
  40. cerr << p.line () << ':' << p.column () << ": " << e << " "
  41. << p.namespace_() << (p.namespace_().empty () ? "" : "#")
  42. << p.prefix () << (p.prefix ().empty () ? "" : ":")
  43. << p.name () << endl;
  44. s.start_element (p.qname ());
  45. break;
  46. }
  47. case parser::end_element:
  48. {
  49. if (trace)
  50. cerr << p.line () << ':' << p.column () << ": " << e << " "
  51. << p.namespace_() << (p.namespace_().empty () ? "" : "#")
  52. << p.prefix () << (p.prefix ().empty () ? "" : ":")
  53. << p.name () << endl;
  54. s.end_element ();
  55. break;
  56. }
  57. case parser::start_namespace_decl:
  58. {
  59. if (trace)
  60. cerr << " " << p.prefix () << "->" << p.namespace_ () << endl;
  61. s.namespace_decl (p.namespace_ (), p.prefix ());
  62. break;
  63. }
  64. case parser::end_namespace_decl:
  65. {
  66. if (trace)
  67. cerr << " " << p.prefix () << "-x" << endl;
  68. break;
  69. }
  70. case parser::start_attribute:
  71. {
  72. if (trace)
  73. cerr << " " << p.qname () << "=";
  74. s.start_attribute (p.qname ());
  75. in_attr = true;
  76. break;
  77. }
  78. case parser::end_attribute:
  79. {
  80. s.end_attribute ();
  81. in_attr = false;
  82. break;
  83. }
  84. case parser::characters:
  85. {
  86. if (trace)
  87. {
  88. if (!in_attr)
  89. cerr << p.line () << ':' << p.column () << ": " << e << " ";
  90. cerr << "'" << p.value () << "'" << endl;
  91. }
  92. s.characters (p.value ());
  93. break;
  94. }
  95. default:
  96. break;
  97. }
  98. }
  99. }
  100. catch (const ios_base::failure&)
  101. {
  102. cerr << "io failure" << endl;
  103. return 1;
  104. }
  105. catch (const xml::exception& e)
  106. {
  107. cerr << e.what () << endl;
  108. return 1;
  109. }
  110. }