cmXMLWriter.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmXMLWriter.h"
  4. #include "cmsys/FStream.hxx"
  5. #include <cassert>
  6. cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
  7. : Output(output)
  8. , IndentationElement(1, '\t')
  9. , Level(level)
  10. , Indent(0)
  11. , ElementOpen(false)
  12. , BreakAttrib(false)
  13. , IsContent(false)
  14. {
  15. }
  16. cmXMLWriter::~cmXMLWriter()
  17. {
  18. assert(this->Indent == 0);
  19. }
  20. void cmXMLWriter::StartDocument(const char* encoding)
  21. {
  22. this->Output << R"(<?xml version="1.0" encoding=")" << encoding << "\"?>";
  23. }
  24. void cmXMLWriter::EndDocument()
  25. {
  26. assert(this->Indent == 0);
  27. this->Output << '\n';
  28. }
  29. void cmXMLWriter::StartElement(std::string const& name)
  30. {
  31. this->CloseStartElement();
  32. this->ConditionalLineBreak(!this->IsContent);
  33. this->Output << '<' << name;
  34. this->Elements.push(name);
  35. ++this->Indent;
  36. this->ElementOpen = true;
  37. this->BreakAttrib = false;
  38. }
  39. void cmXMLWriter::EndElement()
  40. {
  41. assert(this->Indent > 0);
  42. --this->Indent;
  43. if (this->ElementOpen) {
  44. this->Output << "/>";
  45. } else {
  46. this->ConditionalLineBreak(!this->IsContent);
  47. this->IsContent = false;
  48. this->Output << "</" << this->Elements.top() << '>';
  49. }
  50. this->Elements.pop();
  51. this->ElementOpen = false;
  52. }
  53. void cmXMLWriter::Element(const char* name)
  54. {
  55. this->CloseStartElement();
  56. this->ConditionalLineBreak(!this->IsContent);
  57. this->Output << '<' << name << "/>";
  58. }
  59. void cmXMLWriter::BreakAttributes()
  60. {
  61. this->BreakAttrib = true;
  62. }
  63. void cmXMLWriter::Comment(const char* comment)
  64. {
  65. this->CloseStartElement();
  66. this->ConditionalLineBreak(!this->IsContent);
  67. this->Output << "<!-- " << comment << " -->";
  68. }
  69. void cmXMLWriter::CData(std::string const& data)
  70. {
  71. this->PreContent();
  72. this->Output << "<![CDATA[" << data << "]]>";
  73. }
  74. void cmXMLWriter::Doctype(const char* doctype)
  75. {
  76. this->CloseStartElement();
  77. this->ConditionalLineBreak(!this->IsContent);
  78. this->Output << "<!DOCTYPE " << doctype << ">";
  79. }
  80. void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
  81. {
  82. this->CloseStartElement();
  83. this->ConditionalLineBreak(!this->IsContent);
  84. this->Output << "<?" << target << ' ' << data << "?>";
  85. }
  86. void cmXMLWriter::FragmentFile(const char* fname)
  87. {
  88. this->CloseStartElement();
  89. cmsys::ifstream fin(fname, std::ios::in | std::ios::binary);
  90. this->Output << fin.rdbuf();
  91. }
  92. void cmXMLWriter::SetIndentationElement(std::string const& element)
  93. {
  94. this->IndentationElement = element;
  95. }
  96. void cmXMLWriter::ConditionalLineBreak(bool condition)
  97. {
  98. if (condition) {
  99. this->Output << '\n';
  100. for (std::size_t i = 0; i < this->Indent + this->Level; ++i) {
  101. this->Output << this->IndentationElement;
  102. }
  103. }
  104. }
  105. void cmXMLWriter::PreAttribute()
  106. {
  107. assert(this->ElementOpen);
  108. this->ConditionalLineBreak(this->BreakAttrib);
  109. if (!this->BreakAttrib) {
  110. this->Output << ' ';
  111. }
  112. }
  113. void cmXMLWriter::PreContent()
  114. {
  115. this->CloseStartElement();
  116. this->IsContent = true;
  117. }
  118. void cmXMLWriter::CloseStartElement()
  119. {
  120. if (this->ElementOpen) {
  121. this->ConditionalLineBreak(this->BreakAttrib);
  122. this->Output << '>';
  123. this->ElementOpen = false;
  124. }
  125. }