cmXMLWriter: code improvement

New Indent member will be used for XML indentation
This commit is contained in:
Vitaly Stakhovsky
2018-04-03 17:28:08 -04:00
parent 561238bb6f
commit 050ddfb3f3
2 changed files with 18 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
: Output(output) : Output(output)
, IndentationElement(1, '\t') , IndentationElement(1, '\t')
, Level(level) , Level(level)
, Indent(0)
, ElementOpen(false) , ElementOpen(false)
, BreakAttrib(false) , BreakAttrib(false)
, IsContent(false) , IsContent(false)
@@ -17,7 +18,7 @@ cmXMLWriter::cmXMLWriter(std::ostream& output, std::size_t level)
cmXMLWriter::~cmXMLWriter() cmXMLWriter::~cmXMLWriter()
{ {
assert(this->Elements.empty()); assert(this->Indent == 0);
} }
void cmXMLWriter::StartDocument(const char* encoding) void cmXMLWriter::StartDocument(const char* encoding)
@@ -27,27 +28,29 @@ void cmXMLWriter::StartDocument(const char* encoding)
void cmXMLWriter::EndDocument() void cmXMLWriter::EndDocument()
{ {
assert(this->Elements.empty()); assert(this->Indent == 0);
this->Output << '\n'; this->Output << '\n';
} }
void cmXMLWriter::StartElement(std::string const& name) void cmXMLWriter::StartElement(std::string const& name)
{ {
this->CloseStartElement(); this->CloseStartElement();
this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); this->ConditionalLineBreak(!this->IsContent);
this->Output << '<' << name; this->Output << '<' << name;
this->Elements.push(name); this->Elements.push(name);
++this->Indent;
this->ElementOpen = true; this->ElementOpen = true;
this->BreakAttrib = false; this->BreakAttrib = false;
} }
void cmXMLWriter::EndElement() void cmXMLWriter::EndElement()
{ {
assert(!this->Elements.empty()); assert(this->Indent > 0);
--this->Indent;
if (this->ElementOpen) { if (this->ElementOpen) {
this->Output << "/>"; this->Output << "/>";
} else { } else {
this->ConditionalLineBreak(!this->IsContent, this->Elements.size() - 1); this->ConditionalLineBreak(!this->IsContent);
this->IsContent = false; this->IsContent = false;
this->Output << "</" << this->Elements.top() << '>'; this->Output << "</" << this->Elements.top() << '>';
} }
@@ -58,7 +61,7 @@ void cmXMLWriter::EndElement()
void cmXMLWriter::Element(const char* name) void cmXMLWriter::Element(const char* name)
{ {
this->CloseStartElement(); this->CloseStartElement();
this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); this->ConditionalLineBreak(!this->IsContent);
this->Output << '<' << name << "/>"; this->Output << '<' << name << "/>";
} }
@@ -70,7 +73,7 @@ void cmXMLWriter::BreakAttributes()
void cmXMLWriter::Comment(const char* comment) void cmXMLWriter::Comment(const char* comment)
{ {
this->CloseStartElement(); this->CloseStartElement();
this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); this->ConditionalLineBreak(!this->IsContent);
this->Output << "<!-- " << comment << " -->"; this->Output << "<!-- " << comment << " -->";
} }
@@ -83,14 +86,14 @@ void cmXMLWriter::CData(std::string const& data)
void cmXMLWriter::Doctype(const char* doctype) void cmXMLWriter::Doctype(const char* doctype)
{ {
this->CloseStartElement(); this->CloseStartElement();
this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); this->ConditionalLineBreak(!this->IsContent);
this->Output << "<!DOCTYPE " << doctype << ">"; this->Output << "<!DOCTYPE " << doctype << ">";
} }
void cmXMLWriter::ProcessingInstruction(const char* target, const char* data) void cmXMLWriter::ProcessingInstruction(const char* target, const char* data)
{ {
this->CloseStartElement(); this->CloseStartElement();
this->ConditionalLineBreak(!this->IsContent, this->Elements.size()); this->ConditionalLineBreak(!this->IsContent);
this->Output << "<?" << target << ' ' << data << "?>"; this->Output << "<?" << target << ' ' << data << "?>";
} }
@@ -106,11 +109,11 @@ void cmXMLWriter::SetIndentationElement(std::string const& element)
this->IndentationElement = element; this->IndentationElement = element;
} }
void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent) void cmXMLWriter::ConditionalLineBreak(bool condition)
{ {
if (condition) { if (condition) {
this->Output << '\n'; 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; this->Output << this->IndentationElement;
} }
} }
@@ -119,7 +122,7 @@ void cmXMLWriter::ConditionalLineBreak(bool condition, std::size_t indent)
void cmXMLWriter::PreAttribute() void cmXMLWriter::PreAttribute()
{ {
assert(this->ElementOpen); assert(this->ElementOpen);
this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size()); this->ConditionalLineBreak(this->BreakAttrib);
if (!this->BreakAttrib) { if (!this->BreakAttrib) {
this->Output << ' '; this->Output << ' ';
} }
@@ -134,7 +137,7 @@ void cmXMLWriter::PreContent()
void cmXMLWriter::CloseStartElement() void cmXMLWriter::CloseStartElement()
{ {
if (this->ElementOpen) { if (this->ElementOpen) {
this->ConditionalLineBreak(this->BreakAttrib, this->Elements.size()); this->ConditionalLineBreak(this->BreakAttrib);
this->Output << '>'; this->Output << '>';
this->ElementOpen = false; this->ElementOpen = false;
} }

View File

@@ -67,7 +67,7 @@ public:
void SetIndentationElement(std::string const& element); void SetIndentationElement(std::string const& element);
private: private:
void ConditionalLineBreak(bool condition, std::size_t indent); void ConditionalLineBreak(bool condition);
void PreAttribute(); void PreAttribute();
void PreContent(); void PreContent();
@@ -128,6 +128,7 @@ private:
std::stack<std::string, std::vector<std::string>> Elements; std::stack<std::string, std::vector<std::string>> Elements;
std::string IndentationElement; std::string IndentationElement;
std::size_t Level; std::size_t Level;
std::size_t Indent;
bool ElementOpen; bool ElementOpen;
bool BreakAttrib; bool BreakAttrib;
bool IsContent; bool IsContent;