cmXMLWriter.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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) { return { value }; }
  59. static cmXMLSafe SafeAttribute(std::string const& value)
  60. {
  61. return { value };
  62. }
  63. template <typename T>
  64. static T SafeAttribute(T value)
  65. {
  66. return value;
  67. }
  68. static cmXMLSafe SafeContent(const char* value)
  69. {
  70. return cmXMLSafe(value).Quotes(false);
  71. }
  72. static cmXMLSafe SafeContent(std::string const& value)
  73. {
  74. return cmXMLSafe(value).Quotes(false);
  75. }
  76. /*
  77. * Convert a std::chrono::system::time_point to the number of seconds since
  78. * the UN*X epoch.
  79. *
  80. * It would be tempting to convert a time_point to number of seconds by
  81. * using time_since_epoch(). Unfortunately the C++11 standard does not
  82. * specify what the epoch of the system_clock must be.
  83. * Therefore we must assume it is an arbitrary point in time. Instead of this
  84. * method, it is recommended to convert it by means of the to_time_t method.
  85. */
  86. static std::time_t SafeContent(
  87. std::chrono::system_clock::time_point const& value)
  88. {
  89. return std::chrono::system_clock::to_time_t(value);
  90. }
  91. template <typename T>
  92. static T SafeContent(T value)
  93. {
  94. return value;
  95. }
  96. private:
  97. std::ostream& Output;
  98. std::stack<std::string, std::vector<std::string>> Elements;
  99. std::string IndentationElement;
  100. std::size_t Level;
  101. std::size_t Indent;
  102. bool ElementOpen;
  103. bool BreakAttrib;
  104. bool IsContent;
  105. };
  106. class cmXMLElement; // IWYU pragma: keep
  107. class cmXMLDocument
  108. {
  109. public:
  110. cmXMLDocument(cmXMLWriter& xml)
  111. : xmlwr(xml)
  112. {
  113. xmlwr.StartDocument();
  114. }
  115. ~cmXMLDocument() { xmlwr.EndDocument(); }
  116. cmXMLDocument(const cmXMLDocument&) = delete;
  117. cmXMLDocument& operator=(const cmXMLDocument&) = delete;
  118. private:
  119. friend class cmXMLElement;
  120. cmXMLWriter& xmlwr;
  121. };
  122. class cmXMLElement
  123. {
  124. public:
  125. cmXMLElement(cmXMLWriter& xml, const char* tag)
  126. : xmlwr(xml)
  127. {
  128. xmlwr.StartElement(tag);
  129. }
  130. cmXMLElement(cmXMLElement& par, const char* tag)
  131. : xmlwr(par.xmlwr)
  132. {
  133. xmlwr.StartElement(tag);
  134. }
  135. cmXMLElement(cmXMLDocument& doc, const char* tag)
  136. : xmlwr(doc.xmlwr)
  137. {
  138. xmlwr.StartElement(tag);
  139. }
  140. ~cmXMLElement() { xmlwr.EndElement(); }
  141. cmXMLElement(const cmXMLElement&) = delete;
  142. cmXMLElement& operator=(const cmXMLElement&) = delete;
  143. template <typename T>
  144. cmXMLElement& Attribute(const char* name, T const& value)
  145. {
  146. xmlwr.Attribute(name, value);
  147. return *this;
  148. }
  149. template <typename T>
  150. void Content(T const& content)
  151. {
  152. xmlwr.Content(content);
  153. }
  154. template <typename T>
  155. void Element(std::string const& name, T const& value)
  156. {
  157. xmlwr.Element(name, value);
  158. }
  159. void Comment(const char* comment) { xmlwr.Comment(comment); }
  160. private:
  161. cmXMLWriter& xmlwr;
  162. };
  163. #endif