cmXMLWriter.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <ostream>
  8. #include <stack>
  9. #include <string>
  10. #include <vector>
  11. class cmXMLWriter
  12. {
  13. public:
  14. cmXMLWriter(std::ostream& output, std::size_t level = 0);
  15. ~cmXMLWriter();
  16. void StartDocument(const char* encoding = "UTF-8");
  17. void EndDocument();
  18. void StartElement(std::string const& name);
  19. void EndElement();
  20. void BreakAttributes();
  21. template <typename T>
  22. void Attribute(const char* name, T const& value)
  23. {
  24. this->PreAttribute();
  25. this->Output << name << "=\"" << SafeAttribute(value) << '"';
  26. }
  27. void Element(const char* name);
  28. template <typename T>
  29. void Element(std::string const& name, T const& value)
  30. {
  31. this->StartElement(name);
  32. this->Content(value);
  33. this->EndElement();
  34. }
  35. template <typename T>
  36. void Content(T const& content)
  37. {
  38. this->PreContent();
  39. this->Output << SafeContent(content);
  40. }
  41. void Comment(const char* comment);
  42. void CData(std::string const& data);
  43. void Doctype(const char* doctype);
  44. void ProcessingInstruction(const char* target, const char* data);
  45. void FragmentFile(const char* fname);
  46. void SetIndentationElement(std::string const& element);
  47. private:
  48. cmXMLWriter(const cmXMLWriter&);
  49. cmXMLWriter& operator=(const cmXMLWriter&);
  50. void ConditionalLineBreak(bool condition, std::size_t indent);
  51. void PreAttribute();
  52. void PreContent();
  53. void CloseStartElement();
  54. private:
  55. static cmXMLSafe SafeAttribute(const char* value)
  56. {
  57. return cmXMLSafe(value);
  58. }
  59. static cmXMLSafe SafeAttribute(std::string const& value)
  60. {
  61. return cmXMLSafe(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. template <typename T>
  77. static T SafeContent(T value)
  78. {
  79. return value;
  80. }
  81. private:
  82. std::ostream& Output;
  83. std::stack<std::string, std::vector<std::string> > Elements;
  84. std::string IndentationElement;
  85. std::size_t Level;
  86. bool ElementOpen;
  87. bool BreakAttrib;
  88. bool IsContent;
  89. };
  90. #endif