mirror of
https://github.com/NixOS/nix
synced 2025-06-27 12:41:15 +02:00
* `nix-instantiate --{eval|parse}-only --xml': print an XML
representation instead of an ATerm. * Indent XML output.
This commit is contained in:
parent
fe101fa785
commit
18e4ac0fc6
5 changed files with 82 additions and 13 deletions
|
@ -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";
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,13 +18,14 @@ private:
|
|||
|
||||
ostream & output;
|
||||
|
||||
bool indent;
|
||||
bool closed;
|
||||
|
||||
list<string> pendingElems;
|
||||
|
||||
public:
|
||||
|
||||
XMLWriter(ostream & output);
|
||||
XMLWriter(bool indent, ostream & output);
|
||||
~XMLWriter();
|
||||
|
||||
void close();
|
||||
|
@ -40,6 +41,8 @@ public:
|
|||
|
||||
private:
|
||||
void writeAttrs(const XMLAttrs & attrs);
|
||||
|
||||
void indent_(unsigned int depth);
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue