Merge branch 'nix'.
[jackhill/guix/guix.git] / nix / libutil / xml-writer.hh
1 #pragma once
2
3 #include <iostream>
4 #include <string>
5 #include <list>
6 #include <map>
7
8
9 namespace nix {
10
11 using std::string;
12 using std::map;
13 using std::list;
14
15
16 typedef map<string, string> XMLAttrs;
17
18
19 class XMLWriter
20 {
21 private:
22
23 std::ostream & output;
24
25 bool indent;
26 bool closed;
27
28 list<string> pendingElems;
29
30 public:
31
32 XMLWriter(bool indent, std::ostream & output);
33 ~XMLWriter();
34
35 void close();
36
37 void openElement(const string & name,
38 const XMLAttrs & attrs = XMLAttrs());
39 void closeElement();
40
41 void writeEmptyElement(const string & name,
42 const XMLAttrs & attrs = XMLAttrs());
43
44 private:
45 void writeAttrs(const XMLAttrs & attrs);
46
47 void indent_(unsigned int depth);
48 };
49
50
51 class XMLOpenElement
52 {
53 private:
54 XMLWriter & writer;
55 public:
56 XMLOpenElement(XMLWriter & writer, const string & name,
57 const XMLAttrs & attrs = XMLAttrs())
58 : writer(writer)
59 {
60 writer.openElement(name, attrs);
61 }
62 ~XMLOpenElement()
63 {
64 writer.closeElement();
65 }
66 };
67
68
69 }