diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-08-16T10·32+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-08-16T10·32+0000 |
commit | 18e4ac0fc6bd1bc01d92d011e4629cacc3bec016 (patch) | |
tree | d9dccd46943c3d68595a2fc45f9ca77be776dc67 /src/libutil/xml-writer.cc | |
parent | fe101fa7851c3eccb79441ed7f5805e13934254f (diff) |
* `nix-instantiate --{eval|parse}-only --xml': print an XML
representation instead of an ATerm. * Indent XML output.
Diffstat (limited to 'src/libutil/xml-writer.cc')
-rw-r--r-- | src/libutil/xml-writer.cc | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/libutil/xml-writer.cc b/src/libutil/xml-writer.cc index e5f0d9455e40..cd37dff5608f 100644 --- a/src/libutil/xml-writer.cc +++ b/src/libutil/xml-writer.cc @@ -3,8 +3,8 @@ #include "xml-writer.hh" -XMLWriter::XMLWriter(ostream & output) - : output(output) +XMLWriter::XMLWriter(bool indent, ostream & output) + : output(output), indent(indent) { output << "<?xml version='1.0' encoding='utf-8'?>\n"; closed = false; @@ -25,13 +25,22 @@ void XMLWriter::close() } +void XMLWriter::indent_(unsigned int depth) +{ + if (!indent) return; + output << string(depth * 2, ' '); +} + + void XMLWriter::openElement(const string & name, const XMLAttrs & attrs) { assert(!closed); + indent_(pendingElems.size()); output << "<" << name; writeAttrs(attrs); output << ">"; + if (indent) output << "\n"; pendingElems.push_back(name); } @@ -39,7 +48,9 @@ void XMLWriter::openElement(const string & name, void XMLWriter::closeElement() { assert(!pendingElems.empty()); + indent_(pendingElems.size() - 1); output << "</" << pendingElems.back() << ">"; + if (indent) output << "\n"; pendingElems.pop_back(); if (pendingElems.empty()) closed = true; } @@ -49,9 +60,11 @@ void XMLWriter::writeEmptyElement(const string & name, const XMLAttrs & attrs) { assert(!closed); + indent_(pendingElems.size()); output << "<" << name; writeAttrs(attrs); output << " />"; + if (indent) output << "\n"; } |