1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmXMLWriter.h"
7 #include "cmsys/FStream.hxx"
9 cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
11 , IndentationElement(1, '\t')
16 cmXMLWriter::~cmXMLWriter()
18 assert(this->Indent == 0);
21 void cmXMLWriter::StartDocument(const char* encoding)
23 this->Output << R"(<?xml version="1.0" encoding=")" << encoding << "\"?>";
26 void cmXMLWriter::EndDocument()
28 assert(this->Indent == 0);
32 void cmXMLWriter::StartElement(std::string const& name)
34 this->CloseStartElement();
35 this->ConditionalLineBreak(!this->IsContent);
36 this->Output << '<' << name;
37 this->Elements.push(name);
39 this->ElementOpen = true;
40 this->BreakAttrib = false;
43 void cmXMLWriter::EndElement()
45 assert(this->Indent > 0);
47 if (this->ElementOpen) {
50 this->ConditionalLineBreak(!this->IsContent);
51 this->IsContent = false;
52 this->Output << "</" << this->Elements.top() << '>';
55 this->ElementOpen = false;
58 void cmXMLWriter::Element(const char* name)
60 this->CloseStartElement();
61 this->ConditionalLineBreak(!this->IsContent);
62 this->Output << '<' << name << "/>";
65 void cmXMLWriter::BreakAttributes()
67 this->BreakAttrib = true;
70 void cmXMLWriter::Comment(const char* comment)
72 this->CloseStartElement();
73 this->ConditionalLineBreak(!this->IsContent);
74 this->Output << "<!-- " << comment << " -->";
77 void cmXMLWriter::CData(std::string const& data)
80 this->Output << "<![CDATA[" << data << "]]>";
83 void cmXMLWriter::Doctype(const char* doctype)
85 this->CloseStartElement();
86 this->ConditionalLineBreak(!this->IsContent);
87 this->Output << "<!DOCTYPE " << doctype << ">";
90 void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
92 this->CloseStartElement();
93 this->ConditionalLineBreak(!this->IsContent);
94 this->Output << "<?" << target << ' ' << data << "?>";
97 void cmXMLWriter::FragmentFile(const char* fname)
99 this->CloseStartElement();
100 cmsys::ifstream fin(fname, std::ios::in | std::ios::binary);
101 this->Output << fin.rdbuf();
104 void cmXMLWriter::SetIndentationElement(std::string const& element)
106 this->IndentationElement = element;
109 void cmXMLWriter::ConditionalLineBreak(bool condition)
112 this->Output << '\n';
113 for (std::size_t i = 0; i < this->Indent + this->Level; ++i) {
114 this->Output << this->IndentationElement;
119 void cmXMLWriter::PreAttribute()
121 assert(this->ElementOpen);
122 this->ConditionalLineBreak(this->BreakAttrib);
123 if (!this->BreakAttrib) {
128 void cmXMLWriter::PreContent()
130 this->CloseStartElement();
131 this->IsContent = true;
134 void cmXMLWriter::CloseStartElement()
136 if (this->ElementOpen) {
137 this->ConditionalLineBreak(this->BreakAttrib);
139 this->ElementOpen = false;