cmXMLWriter.h 4.4 KB

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