summaryrefslogtreecommitdiff
path: root/Source/cmXMLWriter.cxx
blob: ec2646759b97487c8c96227f593a8338df23a692 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2015 Daniel Pfeifer <daniel@pfeifer-mail.de>

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
#include "cmXMLWriter.h"

#include "cmXMLSafe.h"

#include <cassert>
#include <fstream>

cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
: Output(output)
, Level(level)
, ElementOpen(false)
, BreakAttrib(false)
, IsContent(false)
{
}

cmXMLWriter::~cmXMLWriter()
{
  assert(this->Elements.empty());
}

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

void cmXMLWriter::EndDocument()
{
  assert(this->Elements.empty());
  this->Output << '\n';
}

void cmXMLWriter::StartElement(std::string const& name)
{
  this->CloseStartElement();
  this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
  this->Output << '<' << name;
  this->Elements.push(name);
  this->ElementOpen = true;
  this->BreakAttrib = false;
}

void cmXMLWriter::EndElement()
{
  assert(!this->Elements.empty());
  if (this->ElementOpen)
    {
    this->Output << "/>";
    }
  else
    {
    this->ConditionalLineBreak(!this->IsContent, this->Elements.size() - 1);
    this->IsContent = false;
    this->Output << "</" << this->Elements.top() << '>';
    }
  this->Elements.pop();
  this->ElementOpen = false;
}

void cmXMLWriter::Element(const char* name)
{
  this->CloseStartElement();
  this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
  this->Output << '<' << name << "/>";
}

void cmXMLWriter::BreakAttributes()
{
  this->BreakAttrib = true;
}

void cmXMLWriter::Comment(const char* comment)
{
  this->CloseStartElement();
  this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
  this->Output << "<!-- " << comment << " -->";
}

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

void cmXMLWriter::Doctype(const char* doctype)
{
  this->CloseStartElement();
  this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
  this->Output << "<!DOCTYPE " << doctype << ">";
}

void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
{
  this->CloseStartElement();
  this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
  this->Output << "<?" << target << ' ' << data << "?>";
}

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

void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent)
{
  if (condition)
    {
    this->Output << '\n' << std::string(indent + this->Level, '\t');
    }
}

void cmXMLWriter::PreAttribute()
{
  assert(this->ElementOpen);
  this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size());
  if (!this->BreakAttrib)
    {
    this->Output << ' ';
    }
}

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

void cmXMLWriter::CloseStartElement()
{
  if (this->ElementOpen)
    {
    this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size());
    this->Output << '>';
    this->ElementOpen = false;
    }
}