cmXMLWriter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <chrono>
  6. #include <cstddef> // IWYU pragma: keep
  7. #include <ctime>
  8. #include <ostream>
  9. #include <stack>
  10. #include <string>
  11. #include <vector>
  12. #include "cmXMLSafe.h"
  13. class cmXMLWriter
  14. {
  15. public:
  16. cmXMLWriter(std::ostream& output, std::size_t level = 0);
  17. ~cmXMLWriter();
  18. cmXMLWriter(cmXMLWriter const&) = delete;
  19. cmXMLWriter& operator=(cmXMLWriter const&) = delete;
  20. void StartDocument(const char* encoding = "UTF-8");
  21. void EndDocument();
  22. void StartElement(std::string const& name);
  23. void EndElement();
  24. void BreakAttributes();
  25. template <typename T>
  26. void Attribute(const char* name, T const& value)
  27. {
  28. this->PreAttribute();
  29. this->Output << name << "=\"" << SafeAttribute(value) << '"';
  30. }
  31. void Element(const char* name);
  32. template <typename T>
  33. void Element(std::string const& name, T const& value)
  34. {
  35. this->StartElement(name);
  36. this->Content(value);
  37. this->EndElement();
  38. }
  39. template <typename T>
  40. void Content(T const& content)
  41. {
  42. this->PreContent();
  43. this->Output << SafeContent(content);
  44. }
  45. void Comment(const char* comment);
  46. void CData(std::string const& data);
  47. void Doctype(const char* doctype);
  48. void ProcessingInstruction(const char* target, const char* data);
  49. void FragmentFile(const char* fname);
  50. void SetIndentationElement(std::string const& element);
  51. private:
  52. void ConditionalLineBreak(bool condition);
  53. void PreAttribute();
  54. void PreContent();
  55. void CloseStartElement();
  56. static cmXMLSafe SafeAttribute(const char* value) { return { value }; }
  57. static cmXMLSafe SafeAttribute(std::string const& value)
  58. {
  59. return { value };
  60. }
  61. template <typename T>
  62. static T SafeAttribute(T value)
  63. {
  64. return value;
  65. }
  66. static cmXMLSafe SafeContent(const char* value)
  67. {
  68. return cmXMLSafe(value).Quotes(false);
  69. }
  70. static cmXMLSafe SafeContent(std::string const& value)
  71. {
  72. return cmXMLSafe(value).Quotes(false);
  73. }
  74. /*
  75. * Convert a std::chrono::system::time_point to the number of seconds since
  76. * the UN*X epoch.
  77. *
  78. * It would be tempting to convert a time_point to number of seconds by
  79. * using time_since_epoch(). Unfortunately the C++11 standard does not
  80. * specify what the epoch of the system_clock must be.
  81. * Therefore we must assume it is an arbitrary point in time. Instead of this
  82. * method, it is recommended to convert it by means of the to_time_t method.
  83. */
  84. static std::time_t SafeContent(
  85. std::chrono::system_clock::time_point const& value)
  86. {
  87. return std::chrono::system_clock::to_time_t(value);
  88. }
  89. template <typename T>
  90. static T SafeContent(T value)
  91. {
  92. return value;
  93. }
  94. std::ostream& Output;
  95. std::stack<std::string, std::vector<std::string>> Elements;
  96. std::string IndentationElement;
  97. std::size_t Level;
  98. std::size_t Indent;
  99. bool ElementOpen;
  100. bool BreakAttrib;
  101. bool IsContent;
  102. };
  103. class cmXMLElement; // IWYU pragma: keep
  104. class cmXMLDocument
  105. {
  106. public:
  107. cmXMLDocument(cmXMLWriter& xml)
  108. : xmlwr(xml)
  109. {
  110. this->xmlwr.StartDocument();
  111. }
  112. ~cmXMLDocument() { this->xmlwr.EndDocument(); }
  113. cmXMLDocument(const cmXMLDocument&) = delete;
  114. cmXMLDocument& operator=(const cmXMLDocument&) = delete;
  115. private:
  116. friend class cmXMLElement;
  117. cmXMLWriter& xmlwr;
  118. };
  119. class cmXMLElement
  120. {
  121. public:
  122. cmXMLElement(cmXMLWriter& xml, const char* tag)
  123. : xmlwr(xml)
  124. {
  125. this->xmlwr.StartElement(tag);
  126. }
  127. cmXMLElement(cmXMLElement& par, const char* tag)
  128. : xmlwr(par.xmlwr)
  129. {
  130. this->xmlwr.StartElement(tag);
  131. }
  132. cmXMLElement(cmXMLDocument& doc, const char* tag)
  133. : xmlwr(doc.xmlwr)
  134. {
  135. this->xmlwr.StartElement(tag);
  136. }
  137. ~cmXMLElement() { this->xmlwr.EndElement(); }
  138. cmXMLElement(const cmXMLElement&) = delete;
  139. cmXMLElement& operator=(const cmXMLElement&) = delete;
  140. template <typename T>
  141. cmXMLElement& Attribute(const char* name, T const& value)
  142. {
  143. this->xmlwr.Attribute(name, value);
  144. return *this;
  145. }
  146. template <typename T>
  147. void Content(T const& content)
  148. {
  149. this->xmlwr.Content(content);
  150. }
  151. template <typename T>
  152. void Element(std::string const& name, T const& value)
  153. {
  154. this->xmlwr.Element(name, value);
  155. }
  156. void Comment(const char* comment) { this->xmlwr.Comment(comment); }
  157. private:
  158. cmXMLWriter& xmlwr;
  159. };