mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-29 02:20:17 -06:00
cmXMLWriter: code improvement
New Indent member will be used for XML indentation
This commit is contained in:
@@ -9,6 +9,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
|
||||
: Output(output)
|
||||
, IndentationElement(1, '\t')
|
||||
, Level(level)
|
||||
, Indent(0)
|
||||
, ElementOpen(false)
|
||||
, BreakAttrib(false)
|
||||
, IsContent(false)
|
||||
@@ -17,7 +18,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
|
||||
|
||||
cmXMLWriter::~cmXMLWriter()
|
||||
{
|
||||
assert(this->Elements.empty());
|
||||
assert(this->Indent == 0);
|
||||
}
|
||||
|
||||
void cmXMLWriter::StartDocument(const char* encoding)
|
||||
@@ -27,27 +28,29 @@ void cmXMLWriter::StartDocument(const char* encoding)
|
||||
|
||||
void cmXMLWriter::EndDocument()
|
||||
{
|
||||
assert(this->Elements.empty());
|
||||
assert(this->Indent == 0);
|
||||
this->Output << '\n';
|
||||
}
|
||||
|
||||
void cmXMLWriter::StartElement(std::string const& name)
|
||||
{
|
||||
this->CloseStartElement();
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->Output << '<' << name;
|
||||
this->Elements.push(name);
|
||||
++this->Indent;
|
||||
this->ElementOpen = true;
|
||||
this->BreakAttrib = false;
|
||||
}
|
||||
|
||||
void cmXMLWriter::EndElement()
|
||||
{
|
||||
assert(!this->Elements.empty());
|
||||
assert(this->Indent > 0);
|
||||
--this->Indent;
|
||||
if (this->ElementOpen) {
|
||||
this->Output << "/>";
|
||||
} else {
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size() - 1);
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->IsContent = false;
|
||||
this->Output << "</" << this->Elements.top() << '>';
|
||||
}
|
||||
@@ -58,7 +61,7 @@ void cmXMLWriter::EndElement()
|
||||
void cmXMLWriter::Element(const char* name)
|
||||
{
|
||||
this->CloseStartElement();
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->Output << '<' << name << "/>";
|
||||
}
|
||||
|
||||
@@ -70,7 +73,7 @@ void cmXMLWriter::BreakAttributes()
|
||||
void cmXMLWriter::Comment(const char* comment)
|
||||
{
|
||||
this->CloseStartElement();
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->Output << "<!-- " << comment << " -->";
|
||||
}
|
||||
|
||||
@@ -83,14 +86,14 @@ void cmXMLWriter::CData(std::string const& data)
|
||||
void cmXMLWriter::Doctype(const char* doctype)
|
||||
{
|
||||
this->CloseStartElement();
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->Output << "<!DOCTYPE " << doctype << ">";
|
||||
}
|
||||
|
||||
void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
|
||||
{
|
||||
this->CloseStartElement();
|
||||
this->ConditionalLineBreak(!this->IsContent, this->Elements.size());
|
||||
this->ConditionalLineBreak(!this->IsContent);
|
||||
this->Output << "<?" << target << ' ' << data << "?>";
|
||||
}
|
||||
|
||||
@@ -106,11 +109,11 @@ void cmXMLWriter::SetIndentationElement(std::string const& element)
|
||||
this->IndentationElement = element;
|
||||
}
|
||||
|
||||
void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent)
|
||||
void cmXMLWriter::ConditionalLineBreak(bool condition)
|
||||
{
|
||||
if (condition) {
|
||||
this->Output << '\n';
|
||||
for (std::size_t i = 0; i < indent + this->Level; ++i) {
|
||||
for (std::size_t i = 0; i < this->Indent + this->Level; ++i) {
|
||||
this->Output << this->IndentationElement;
|
||||
}
|
||||
}
|
||||
@@ -119,7 +122,7 @@ void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent)
|
||||
void cmXMLWriter::PreAttribute()
|
||||
{
|
||||
assert(this->ElementOpen);
|
||||
this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size());
|
||||
this->ConditionalLineBreak(this->BreakAttrib);
|
||||
if (!this->BreakAttrib) {
|
||||
this->Output << ' ';
|
||||
}
|
||||
@@ -134,7 +137,7 @@ void cmXMLWriter::PreContent()
|
||||
void cmXMLWriter::CloseStartElement()
|
||||
{
|
||||
if (this->ElementOpen) {
|
||||
this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size());
|
||||
this->ConditionalLineBreak(this->BreakAttrib);
|
||||
this->Output << '>';
|
||||
this->ElementOpen = false;
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ public:
|
||||
void SetIndentationElement(std::string const& element);
|
||||
|
||||
private:
|
||||
void ConditionalLineBreak(bool condition, std::size_t indent);
|
||||
void ConditionalLineBreak(bool condition);
|
||||
|
||||
void PreAttribute();
|
||||
void PreContent();
|
||||
@@ -128,6 +128,7 @@ private:
|
||||
std::stack<std::string, std::vector<std::string>> Elements;
|
||||
std::string IndentationElement;
|
||||
std::size_t Level;
|
||||
std::size_t Indent;
|
||||
bool ElementOpen;
|
||||
bool BreakAttrib;
|
||||
bool IsContent;
|
||||
|
||||
Reference in New Issue
Block a user