cmXMLWriter.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Daniel Pfeifer <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #ifndef cmXMLWiter_h
  11. #define cmXMLWiter_h
  12. #include "cmStandardIncludes.h"
  13. #include "cmXMLSafe.h"
  14. #include <ostream>
  15. #include <stack>
  16. #include <string>
  17. #include <vector>
  18. class cmXMLWriter
  19. {
  20. public:
  21. cmXMLWriter(std::ostream& output, std::size_t level = 0);
  22. ~cmXMLWriter();
  23. void StartDocument(const char* encoding = "UTF-8");
  24. void EndDocument();
  25. void StartElement(std::string const& name);
  26. void EndElement();
  27. void BreakAttributes();
  28. template <typename T>
  29. void Attribute(const char* name, T const& value)
  30. {
  31. this->PreAttribute();
  32. this->Output << name << "=\"" << SafeAttribute(value) << '"';
  33. }
  34. void Element(const char* name);
  35. template <typename T>
  36. void Element(std::string const& name, T const& value)
  37. {
  38. this->StartElement(name);
  39. this->Content(value);
  40. this->EndElement();
  41. }
  42. template <typename T>
  43. void Content(T const& content)
  44. {
  45. this->PreContent();
  46. this->Output << SafeContent(content);
  47. }
  48. void Comment(const char* comment);
  49. void CData(std::string const& data);
  50. void Doctype(const char* doctype);
  51. void ProcessingInstruction(const char* target, const char* data);
  52. void FragmentFile(const char* fname);
  53. private:
  54. cmXMLWriter(const cmXMLWriter&);
  55. cmXMLWriter& operator=(const cmXMLWriter&);
  56. void ConditionalLineBreak(bool condition, std::size_t indent);
  57. void PreAttribute();
  58. void PreContent();
  59. void CloseStartElement();
  60. private:
  61. static cmXMLSafe SafeAttribute(const char* value)
  62. {
  63. return cmXMLSafe(value);
  64. }
  65. static cmXMLSafe SafeAttribute(std::string const& value)
  66. {
  67. return cmXMLSafe(value);
  68. }
  69. template <typename T>
  70. static T SafeAttribute(T value)
  71. {
  72. return value;
  73. }
  74. static cmXMLSafe SafeContent(const char* value)
  75. {
  76. return cmXMLSafe(value).Quotes(false);
  77. }
  78. static cmXMLSafe SafeContent(std::string const& value)
  79. {
  80. return cmXMLSafe(value).Quotes(false);
  81. }
  82. template <typename T>
  83. static T SafeContent(T value)
  84. {
  85. return value;
  86. }
  87. private:
  88. std::ostream& Output;
  89. std::stack<std::string, std::vector<std::string> > Elements;
  90. std::size_t Level;
  91. bool ElementOpen;
  92. bool BreakAttrib;
  93. bool IsContent;
  94. };
  95. #endif