cmXMLWriter.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, std::size_t indent);
  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. bool ElementOpen;
  103. bool BreakAttrib;
  104. bool IsContent;
  105. };
  106. #endif