cmXMLWriter.h 4.4 KB

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