cmake/Source/cmXMLWriter.h

196 lines
4.4 KiB
C
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2021-09-14 00:13:48 +02:00
#pragma once
2015-08-17 11:37:30 +02:00
2018-01-26 17:06:56 +01:00
#include "cmConfigure.h" // IWYU pragma: keep
2016-07-09 11:21:54 +02:00
2018-04-23 21:13:27 +02:00
#include <chrono>
2020-08-30 11:54:41 +02:00
#include <cstddef> // IWYU pragma: keep
2018-04-23 21:13:27 +02:00
#include <ctime>
2015-08-17 11:37:30 +02:00
#include <ostream>
#include <stack>
#include <string>
#include <vector>
2020-02-01 23:06:01 +01:00
#include "cmXMLSafe.h"
2015-08-17 11:37:30 +02:00
class cmXMLWriter
{
public:
cmXMLWriter(std::ostream& output, std::size_t level = 0);
~cmXMLWriter();
2019-11-11 23:01:05 +01:00
cmXMLWriter(cmXMLWriter const&) = delete;
cmXMLWriter& operator=(cmXMLWriter const&) = delete;
2015-08-17 11:37:30 +02:00
void StartDocument(const char* encoding = "UTF-8");
void EndDocument();
void StartElement(std::string const& name);
void EndElement();
void BreakAttributes();
template <typename T>
void Attribute(const char* name, T const& value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
this->PreAttribute();
this->Output << name << "=\"" << SafeAttribute(value) << '"';
2016-07-09 11:21:54 +02:00
}
void Element(const char* name);
2015-08-17 11:37:30 +02:00
template <typename T>
void Element(std::string const& name, T const& value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
this->StartElement(name);
this->Content(value);
this->EndElement();
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
template <typename T>
void Content(T const& content)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
this->PreContent();
this->Output << SafeContent(content);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
void Comment(const char* comment);
void CData(std::string const& data);
2016-07-09 11:21:54 +02:00
void Doctype(const char* doctype);
2015-08-17 11:37:30 +02:00
void ProcessingInstruction(const char* target, const char* data);
void FragmentFile(const char* fname);
2017-07-20 19:35:53 +02:00
void SetIndentationElement(std::string const& element);
2015-08-17 11:37:30 +02:00
2017-07-20 19:35:53 +02:00
private:
2018-08-09 18:06:22 +02:00
void ConditionalLineBreak(bool condition);
2015-08-17 11:37:30 +02:00
void PreAttribute();
void PreContent();
void CloseStartElement();
2020-02-01 23:06:01 +01:00
static cmXMLSafe SafeAttribute(const char* value) { return { value }; }
2015-08-17 11:37:30 +02:00
static cmXMLSafe SafeAttribute(std::string const& value)
2016-07-09 11:21:54 +02:00
{
2020-02-01 23:06:01 +01:00
return { value };
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
template <typename T>
static T SafeAttribute(T value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
return value;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
static cmXMLSafe SafeContent(const char* value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
return cmXMLSafe(value).Quotes(false);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
static cmXMLSafe SafeContent(std::string const& value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
return cmXMLSafe(value).Quotes(false);
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2018-04-23 21:13:27 +02:00
/*
* Convert a std::chrono::system::time_point to the number of seconds since
* the UN*X epoch.
*
* It would be tempting to convert a time_point to number of seconds by
* using time_since_epoch(). Unfortunately the C++11 standard does not
* specify what the epoch of the system_clock must be.
* Therefore we must assume it is an arbitrary point in time. Instead of this
* method, it is recommended to convert it by means of the to_time_t method.
*/
static std::time_t SafeContent(
std::chrono::system_clock::time_point const& value)
{
return std::chrono::system_clock::to_time_t(value);
}
2015-08-17 11:37:30 +02:00
template <typename T>
static T SafeContent(T value)
2016-07-09 11:21:54 +02:00
{
2015-08-17 11:37:30 +02:00
return value;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
std::ostream& Output;
2018-01-26 17:06:56 +01:00
std::stack<std::string, std::vector<std::string>> Elements;
2017-07-20 19:35:53 +02:00
std::string IndentationElement;
2015-08-17 11:37:30 +02:00
std::size_t Level;
2018-08-09 18:06:22 +02:00
std::size_t Indent;
2015-08-17 11:37:30 +02:00
bool ElementOpen;
bool BreakAttrib;
bool IsContent;
};
2018-08-09 18:06:22 +02:00
class cmXMLElement; // IWYU pragma: keep
class cmXMLDocument
{
public:
cmXMLDocument(cmXMLWriter& xml)
: xmlwr(xml)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.StartDocument();
2018-08-09 18:06:22 +02:00
}
2021-09-14 00:13:48 +02:00
~cmXMLDocument() { this->xmlwr.EndDocument(); }
2019-11-11 23:01:05 +01:00
cmXMLDocument(const cmXMLDocument&) = delete;
cmXMLDocument& operator=(const cmXMLDocument&) = delete;
2018-08-09 18:06:22 +02:00
private:
friend class cmXMLElement;
cmXMLWriter& xmlwr;
};
class cmXMLElement
{
public:
cmXMLElement(cmXMLWriter& xml, const char* tag)
: xmlwr(xml)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.StartElement(tag);
2018-08-09 18:06:22 +02:00
}
cmXMLElement(cmXMLElement& par, const char* tag)
: xmlwr(par.xmlwr)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.StartElement(tag);
2018-08-09 18:06:22 +02:00
}
cmXMLElement(cmXMLDocument& doc, const char* tag)
: xmlwr(doc.xmlwr)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.StartElement(tag);
2018-08-09 18:06:22 +02:00
}
2021-09-14 00:13:48 +02:00
~cmXMLElement() { this->xmlwr.EndElement(); }
2018-08-09 18:06:22 +02:00
2019-11-11 23:01:05 +01:00
cmXMLElement(const cmXMLElement&) = delete;
cmXMLElement& operator=(const cmXMLElement&) = delete;
2018-08-09 18:06:22 +02:00
template <typename T>
cmXMLElement& Attribute(const char* name, T const& value)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.Attribute(name, value);
2018-08-09 18:06:22 +02:00
return *this;
}
template <typename T>
void Content(T const& content)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.Content(content);
2018-08-09 18:06:22 +02:00
}
template <typename T>
void Element(std::string const& name, T const& value)
{
2021-09-14 00:13:48 +02:00
this->xmlwr.Element(name, value);
2018-08-09 18:06:22 +02:00
}
2021-09-14 00:13:48 +02:00
void Comment(const char* comment) { this->xmlwr.Comment(comment); }
2018-08-09 18:06:22 +02:00
private:
cmXMLWriter& xmlwr;
};