cmXMLWriter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst 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(char const* 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(char const* name, T const& value)
  27. {
  28. this->PreAttribute();
  29. this->Output << name << "=\"" << SafeAttribute(value) << '"';
  30. }
  31. void Element(char const* 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(char const* comment);
  46. void CData(std::string const& data);
  47. void Doctype(char const* doctype);
  48. void ProcessingInstruction(char const* target, char const* data);
  49. void FragmentFile(char const* 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(char const* 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(char const* 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(std::chrono::system_clock::time_point value)
  85. {
  86. return std::chrono::system_clock::to_time_t(value);
  87. }
  88. template <typename T>
  89. static T SafeContent(T value)
  90. {
  91. return value;
  92. }
  93. std::ostream& Output;
  94. std::stack<std::string, std::vector<std::string>> Elements;
  95. std::string IndentationElement;
  96. std::size_t Level;
  97. std::size_t Indent = 0;
  98. bool ElementOpen = false;
  99. bool BreakAttrib = false;
  100. bool IsContent = false;
  101. };
  102. class cmXMLElement; // IWYU pragma: keep
  103. class cmXMLDocument
  104. {
  105. public:
  106. cmXMLDocument(cmXMLWriter& xml)
  107. : xmlwr(xml)
  108. {
  109. this->xmlwr.StartDocument();
  110. }
  111. ~cmXMLDocument() { this->xmlwr.EndDocument(); }
  112. cmXMLDocument(cmXMLDocument const&) = delete;
  113. cmXMLDocument& operator=(cmXMLDocument const&) = delete;
  114. private:
  115. friend class cmXMLElement;
  116. cmXMLWriter& xmlwr;
  117. };
  118. class cmXMLElement
  119. {
  120. public:
  121. cmXMLElement(cmXMLWriter& xml, char const* tag)
  122. : xmlwr(xml)
  123. {
  124. this->xmlwr.StartElement(tag);
  125. }
  126. cmXMLElement(cmXMLElement& par, char const* tag)
  127. : xmlwr(par.xmlwr)
  128. {
  129. this->xmlwr.StartElement(tag);
  130. }
  131. cmXMLElement(cmXMLDocument& doc, char const* tag)
  132. : xmlwr(doc.xmlwr)
  133. {
  134. this->xmlwr.StartElement(tag);
  135. }
  136. ~cmXMLElement() { this->xmlwr.EndElement(); }
  137. cmXMLElement(cmXMLElement const&) = delete;
  138. cmXMLElement& operator=(cmXMLElement const&) = delete;
  139. template <typename T>
  140. cmXMLElement& Attribute(char const* name, T const& value)
  141. {
  142. this->xmlwr.Attribute(name, value);
  143. return *this;
  144. }
  145. template <typename T>
  146. void Content(T const& content)
  147. {
  148. this->xmlwr.Content(content);
  149. }
  150. template <typename T>
  151. void Element(std::string const& name, T const& value)
  152. {
  153. this->xmlwr.Element(name, value);
  154. }
  155. void Comment(char const* comment) { this->xmlwr.Comment(comment); }
  156. private:
  157. cmXMLWriter& xmlwr;
  158. };