1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
8 #include <cstddef> // IWYU pragma: keep
15 #include "cmXMLSafe.h"
20 cmXMLWriter(std::ostream
& output
, std::size_t level
= 0);
23 cmXMLWriter(cmXMLWriter
const&) = delete;
24 cmXMLWriter
& operator=(cmXMLWriter
const&) = delete;
26 void StartDocument(const char* encoding
= "UTF-8");
29 void StartElement(std::string
const& name
);
32 void BreakAttributes();
35 void Attribute(const char* name
, T
const& value
)
38 this->Output
<< name
<< "=\"" << SafeAttribute(value
) << '"';
41 void Element(const char* name
);
44 void Element(std::string
const& name
, T
const& value
)
46 this->StartElement(name
);
52 void Content(T
const& content
)
55 this->Output
<< SafeContent(content
);
58 void Comment(const char* comment
);
60 void CData(std::string
const& data
);
62 void Doctype(const char* doctype
);
64 void ProcessingInstruction(const char* target
, const char* data
);
66 void FragmentFile(const char* fname
);
68 void SetIndentationElement(std::string
const& element
);
71 void ConditionalLineBreak(bool condition
);
76 void CloseStartElement();
78 static cmXMLSafe
SafeAttribute(const char* value
) { return { value
}; }
80 static cmXMLSafe
SafeAttribute(std::string
const& value
)
86 static T
SafeAttribute(T value
)
91 static cmXMLSafe
SafeContent(const char* value
)
93 return cmXMLSafe(value
).Quotes(false);
96 static cmXMLSafe
SafeContent(std::string
const& value
)
98 return cmXMLSafe(value
).Quotes(false);
102 * Convert a std::chrono::system::time_point to the number of seconds since
105 * It would be tempting to convert a time_point to number of seconds by
106 * using time_since_epoch(). Unfortunately the C++11 standard does not
107 * specify what the epoch of the system_clock must be.
108 * Therefore we must assume it is an arbitrary point in time. Instead of this
109 * method, it is recommended to convert it by means of the to_time_t method.
111 static std::time_t SafeContent(
112 std::chrono::system_clock::time_point
const& value
)
114 return std::chrono::system_clock::to_time_t(value
);
117 template <typename T
>
118 static T
SafeContent(T value
)
123 std::ostream
& Output
;
124 std::stack
<std::string
, std::vector
<std::string
>> Elements
;
125 std::string IndentationElement
;
127 std::size_t Indent
= 0;
128 bool ElementOpen
= false;
129 bool BreakAttrib
= false;
130 bool IsContent
= false;
133 class cmXMLElement
; // IWYU pragma: keep
138 cmXMLDocument(cmXMLWriter
& xml
)
141 this->xmlwr
.StartDocument();
143 ~cmXMLDocument() { this->xmlwr
.EndDocument(); }
144 cmXMLDocument(const cmXMLDocument
&) = delete;
145 cmXMLDocument
& operator=(const cmXMLDocument
&) = delete;
148 friend class cmXMLElement
;
155 cmXMLElement(cmXMLWriter
& xml
, const char* tag
)
158 this->xmlwr
.StartElement(tag
);
160 cmXMLElement(cmXMLElement
& par
, const char* tag
)
163 this->xmlwr
.StartElement(tag
);
165 cmXMLElement(cmXMLDocument
& doc
, const char* tag
)
168 this->xmlwr
.StartElement(tag
);
170 ~cmXMLElement() { this->xmlwr
.EndElement(); }
172 cmXMLElement(const cmXMLElement
&) = delete;
173 cmXMLElement
& operator=(const cmXMLElement
&) = delete;
175 template <typename T
>
176 cmXMLElement
& Attribute(const char* name
, T
const& value
)
178 this->xmlwr
.Attribute(name
, value
);
181 template <typename T
>
182 void Content(T
const& content
)
184 this->xmlwr
.Content(content
);
186 template <typename T
>
187 void Element(std::string
const& name
, T
const& value
)
189 this->xmlwr
.Element(name
, value
);
191 void Comment(const char* comment
) { this->xmlwr
.Comment(comment
); }