xmlwriter.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <config.h>
  2. #include <stdexcept>
  3. #include <sstream>
  4. #include "xmlwriter.hpp"
  5. XmlWriter::XmlWriter(std::ostream& outstream) :
  6. out(outstream),
  7. indent(0),
  8. closetag(),
  9. lasttag(),
  10. sections()
  11. {
  12. }
  13. XmlWriter::~XmlWriter()
  14. {
  15. if(sections.size() > 0) {
  16. std::cerr << "WARNING: NOT CLOSED: ";
  17. for(std::vector<std::string>::iterator i = sections.begin();
  18. i != sections.end(); ++i)
  19. std::cerr << *i << " ";
  20. std::cerr << "\n";
  21. }
  22. closeTag();
  23. }
  24. void XmlWriter::openTag(const char* name)
  25. {
  26. newLine();
  27. out << "<" << name;
  28. closetag = ">";
  29. indent++;
  30. sections.push_back(name);
  31. }
  32. void XmlWriter::closeTag(const char* name)
  33. {
  34. if(sections.size() == 0)
  35. throw std::runtime_error("got closeSection without prior openSection.");
  36. const std::string& lastsection = sections.back();
  37. if (lastsection != name) {
  38. std::ostringstream msg;
  39. msg << "mismatch in open/closeSection. Expected '"
  40. << lastsection << "' got '" << name << "'";
  41. throw std::runtime_error(msg.str());
  42. }
  43. sections.pop_back();
  44. indent--;
  45. newLine();
  46. // XXX: We should check for consistency here
  47. out << "</" << name;
  48. closetag = ">" ;
  49. }
  50. void XmlWriter::writeTag(const char* name)
  51. {
  52. newLine();
  53. out << "<" << name;
  54. closetag = "/>";
  55. lasttag = name;
  56. }
  57. void XmlWriter::newLine()
  58. {
  59. if(closetag != "") {
  60. closeTag();
  61. for (int i=0;i<indent;i++)
  62. out << "\t";
  63. }
  64. }
  65. void XmlWriter::closeTag()
  66. {
  67. if (closetag != "")
  68. out << closetag << "\n";
  69. }