cmXMLWriter.cxx 3.16 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
#include "cmXMLWriter.h"
4

5
#include "cmsys/FStream.hxx"
6 7 8
#include <cassert>

cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
9
  : Output(output)
10
  , IndentationElement(1, '\t')
11
  , Level(level)
12
  , Indent(0)
13 14 15
  , ElementOpen(false)
  , BreakAttrib(false)
  , IsContent(false)
16 17 18 19 20
{
}

cmXMLWriter::~cmXMLWriter()
{
21
  assert(this->Indent == 0);
22 23 24 25 26 27 28 29 30
}

void cmXMLWriter::StartDocument(const char* encoding)
{
  this->Output << "<?xml version=\"1.0\" encoding=\"" << encoding << "\"?>";
}

void cmXMLWriter::EndDocument()
{
31
  assert(this->Indent == 0);
32 33 34 35 36 37
  this->Output << '\n';
}

void cmXMLWriter::StartElement(std::string const& name)
{
  this->CloseStartElement();
38
  this->ConditionalLineBreak(!this->IsContent);
39 40
  this->Output << '<' << name;
  this->Elements.push(name);
41
  ++this->Indent;
42 43 44 45 46 47
  this->ElementOpen = true;
  this->BreakAttrib = false;
}

void cmXMLWriter::EndElement()
{
48 49
  assert(this->Indent > 0);
  --this->Indent;
50
  if (this->ElementOpen) {
51
    this->Output << "/>";
52
  } else {
53
    this->ConditionalLineBreak(!this->IsContent);
54 55
    this->IsContent = false;
    this->Output << "</" << this->Elements.top() << '>';
56
  }
57 58 59 60
  this->Elements.pop();
  this->ElementOpen = false;
}

61 62 63
void cmXMLWriter::Element(const char* name)
{
  this->CloseStartElement();
64
  this->ConditionalLineBreak(!this->IsContent);
65 66 67
  this->Output << '<' << name << "/>";
}

68 69 70 71 72 73 74 75
void cmXMLWriter::BreakAttributes()
{
  this->BreakAttrib = true;
}

void cmXMLWriter::Comment(const char* comment)
{
  this->CloseStartElement();
76
  this->ConditionalLineBreak(!this->IsContent);
77 78 79 80 81 82 83 84 85
  this->Output << "<!-- " << comment << " -->";
}

void cmXMLWriter::CData(std::string const& data)
{
  this->PreContent();
  this->Output << "<![CDATA[" << data << "]]>";
}

86 87 88
void cmXMLWriter::Doctype(const char* doctype)
{
  this->CloseStartElement();
89
  this->ConditionalLineBreak(!this->IsContent);
90 91 92
  this->Output << "<!DOCTYPE " << doctype << ">";
}

93 94 95
void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
{
  this->CloseStartElement();
96
  this->ConditionalLineBreak(!this->IsContent);
97 98 99 100 101 102
  this->Output << "<?" << target << ' ' << data << "?>";
}

void cmXMLWriter::FragmentFile(const char* fname)
{
  this->CloseStartElement();
103
  cmsys::ifstream fin(fname, std::ios::in | std::ios::binary);
104 105 106
  this->Output << fin.rdbuf();
}

107 108 109 110 111
void cmXMLWriter::SetIndentationElement(std::string const& element)
{
  this->IndentationElement = element;
}

112
void cmXMLWriter::ConditionalLineBreak(bool condition)
113
{
114
  if (condition) {
115
    this->Output << '\n';
116
    for (std::size_t i = 0; i < this->Indent + this->Level; ++i) {
117 118
      this->Output << this->IndentationElement;
    }
119
  }
120 121 122 123 124
}

void cmXMLWriter::PreAttribute()
{
  assert(this->ElementOpen);
125
  this->ConditionalLineBreak(this->BreakAttrib);
126
  if (!this->BreakAttrib) {
127
    this->Output << ' ';
128
  }
129 130 131 132 133 134 135 136 137 138
}

void cmXMLWriter::PreContent()
{
  this->CloseStartElement();
  this->IsContent = true;
}

void cmXMLWriter::CloseStartElement()
{
139
  if (this->ElementOpen) {
140
    this->ConditionalLineBreak(this->BreakAttrib);
141 142
    this->Output << '>';
    this->ElementOpen = false;
143
  }
144
}