# HG changeset patch # User František Kučera # Date 1546865576 -3600 # Node ID edbebc6163e3e4d627a2fda296fd6754eda7d13f # Parent f21d6ae71bf0913b7b2aff36917738ecdd60dfc3 indentation diff -r f21d6ae71bf0 -r edbebc6163e3 include/relpipe/xmlwriter/XMLWriter.h --- a/include/relpipe/xmlwriter/XMLWriter.h Sun Jan 06 22:15:37 2019 +0100 +++ b/include/relpipe/xmlwriter/XMLWriter.h Mon Jan 07 13:52:56 2019 +0100 @@ -53,6 +53,7 @@ ostream& output; wstring_convert> convertor; // XML output will be always in UTF-8 vector treePosition; + bool lastWasTextNode = false; const string escapeXmlText(const wstring& value) { // TODO: really bad performance → rewrite @@ -114,19 +115,31 @@ } } + void writeIndentation(bool resetState = true) { + if (lastWasTextNode) { + if (resetState) lastWasTextNode = false; + } else { + output << endl; + for (int i = 0; i < treePosition.size(); i++) { + output << INDENT; + } + } + } + public: XMLWriter(std::ostream& output) : output(output) { - output << "" << endl; + output << ""; } virtual ~XMLWriter() { + output << endl; output.flush(); } void writeStartElement(const wstring& name, const vector& attributes = {}) { - // FIXME: indentation checkName(name); + writeIndentation(); treePosition.push_back(name); output << "<" << convertor.to_bytes(name); writeAttributes(attributes); @@ -134,15 +147,16 @@ } void writeEndElement() { - // FIXME: indentation if (treePosition.empty()) throw RelpipeXMLWriterException(L"unable to close element – all elements are already closed"); - output << ""; + wstring name = treePosition.back(); treePosition.pop_back(); + writeIndentation(); + output << ""; } void writeEmptyElement(const wstring& name, const vector& attributes = {}) { - // FIXME: indentation checkName(name); + writeIndentation(); output << "<" << convertor.to_bytes(name); writeAttributes(attributes); output << "/>"; @@ -157,12 +171,14 @@ void writeCharacters(const wstring& text) { output << escapeXmlText(text); + lastWasTextNode = true; } void writeComment(const wstring& text, bool addSpaces = true) { - output << addSpaces ? "" : "-->"; + output << (addSpaces ? " -->" : "-->"); } };