cmXMLWriter.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. template <typename T>
  35. void Element(std::string const& name, T const& value)
  36. {
  37. this->StartElement(name);
  38. this->Content(value);
  39. this->EndElement();
  40. }
  41. template <typename T>
  42. void Content(T const& content)
  43. {
  44. this->PreContent();
  45. this->Output << SafeContent(content);
  46. }
  47. void Comment(const char* comment);
  48. void CData(std::string const& data);
  49. void ProcessingInstruction(const char* target, const char* data);
  50. void FragmentFile(const char* fname);
  51. private:
  52. cmXMLWriter(const cmXMLWriter&);
  53. cmXMLWriter& operator=(const cmXMLWriter&);
  54. void ConditionalLineBreak(bool condition, std::size_t indent);
  55. void PreAttribute();
  56. void PreContent();
  57. void CloseStartElement();
  58. private:
  59. static cmXMLSafe SafeAttribute(const char* value)
  60. {
  61. return cmXMLSafe(value);
  62. }
  63. static cmXMLSafe SafeAttribute(std::string const& value)
  64. {
  65. return cmXMLSafe(value);
  66. }
  67. template <typename T>
  68. static T SafeAttribute(T value)
  69. {
  70. return value;
  71. }
  72. static cmXMLSafe SafeContent(const char* value)
  73. {
  74. return cmXMLSafe(value).Quotes(false);
  75. }
  76. static cmXMLSafe SafeContent(std::string const& value)
  77. {
  78. return cmXMLSafe(value).Quotes(false);
  79. }
  80. template <typename T>
  81. static T SafeContent(T value)
  82. {
  83. return value;
  84. }
  85. private:
  86. std::ostream& Output;
  87. std::stack<std::string, std::vector<std::string> > Elements;
  88. std::size_t Level;
  89. bool ElementOpen;
  90. bool BreakAttrib;
  91. bool IsContent;
  92. };
  93. #endif