Merge topic 'curl-tls-verify'
[kiteware-cmake.git] / Source / cmXMLWriter.h
blob49fc8646b2b1ac61f06bd78fc4d56e8ac190cd66
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #pragma once
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <chrono>
8 #include <cstddef> // IWYU pragma: keep
9 #include <ctime>
10 #include <ostream>
11 #include <stack>
12 #include <string>
13 #include <vector>
15 #include "cmXMLSafe.h"
17 class cmXMLWriter
19 public:
20 cmXMLWriter(std::ostream& output, std::size_t level = 0);
21 ~cmXMLWriter();
23 cmXMLWriter(cmXMLWriter const&) = delete;
24 cmXMLWriter& operator=(cmXMLWriter const&) = delete;
26 void StartDocument(const char* encoding = "UTF-8");
27 void EndDocument();
29 void StartElement(std::string const& name);
30 void EndElement();
32 void BreakAttributes();
34 template <typename T>
35 void Attribute(const char* name, T const& value)
37 this->PreAttribute();
38 this->Output << name << "=\"" << SafeAttribute(value) << '"';
41 void Element(const char* name);
43 template <typename T>
44 void Element(std::string const& name, T const& value)
46 this->StartElement(name);
47 this->Content(value);
48 this->EndElement();
51 template <typename T>
52 void Content(T const& content)
54 this->PreContent();
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);
70 private:
71 void ConditionalLineBreak(bool condition);
73 void PreAttribute();
74 void PreContent();
76 void CloseStartElement();
78 static cmXMLSafe SafeAttribute(const char* value) { return { value }; }
80 static cmXMLSafe SafeAttribute(std::string const& value)
82 return { value };
85 template <typename T>
86 static T SafeAttribute(T value)
88 return 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
103 * the UN*X epoch.
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)
120 return value;
123 std::ostream& Output;
124 std::stack<std::string, std::vector<std::string>> Elements;
125 std::string IndentationElement;
126 std::size_t Level;
127 std::size_t Indent = 0;
128 bool ElementOpen = false;
129 bool BreakAttrib = false;
130 bool IsContent = false;
133 class cmXMLElement; // IWYU pragma: keep
135 class cmXMLDocument
137 public:
138 cmXMLDocument(cmXMLWriter& xml)
139 : xmlwr(xml)
141 this->xmlwr.StartDocument();
143 ~cmXMLDocument() { this->xmlwr.EndDocument(); }
144 cmXMLDocument(const cmXMLDocument&) = delete;
145 cmXMLDocument& operator=(const cmXMLDocument&) = delete;
147 private:
148 friend class cmXMLElement;
149 cmXMLWriter& xmlwr;
152 class cmXMLElement
154 public:
155 cmXMLElement(cmXMLWriter& xml, const char* tag)
156 : xmlwr(xml)
158 this->xmlwr.StartElement(tag);
160 cmXMLElement(cmXMLElement& par, const char* tag)
161 : xmlwr(par.xmlwr)
163 this->xmlwr.StartElement(tag);
165 cmXMLElement(cmXMLDocument& doc, const char* tag)
166 : xmlwr(doc.xmlwr)
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);
179 return *this;
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); }
193 private:
194 cmXMLWriter& xmlwr;