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