xmlwriter.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * As a special exception, you may use this file as part of a free
8  * software library without restriction. Specifically, if other files
9  * instantiate templates or use macros or inline functions from this
10  * file, or you compile this file and link it with other files to
11  * produce an executable, this file does not by itself cause the
12  * resulting executable to be covered by the GNU General Public
13  * License. This exception does not however invalidate any other
14  * reasons why the executable file might be covered by the GNU Library
15  * General Public License.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 #ifndef cxxtools_Xml_XmlWriter_h
27 #define cxxtools_Xml_XmlWriter_h
28 
29 #include <cxxtools/string.h>
30 #include <cxxtools/textstream.h>
31 #include <cxxtools/xml/attribute.h>
32 #include <stack>
33 
34 namespace cxxtools {
35 
36 namespace xml {
37 
38  class XmlWriter
39  {
40  public:
41  XmlWriter();
42 
43  XmlWriter(std::ostream& os, int format = UseXmlDeclaration | UseIndent | UseEndl);
44 
45  void begin(std::ostream& os);
46 
47  void writeStartElement(const cxxtools::String& localName);
48 
49  void writeStartElement(const cxxtools::String& localName, const Attribute* attr, size_t attrCount);
50 
51  void writeStartElement(const cxxtools::String& localName, const Attributes& attr)
52  { writeStartElement(localName, &attr[0], attr.size()); }
53 
54  void writeEndElement();
55 
56  void writeElement(const cxxtools::String& localName, const cxxtools::String& content);
57 
58  void writeElement(const cxxtools::String& localName, const Attribute* attr, size_t attrCount, const cxxtools::String& content);
59 
60  void writeElement(const cxxtools::String& localName, const Attributes& attr, const cxxtools::String& content)
61  { writeElement(localName, &attr[0], attr.size(), content); }
62 
63  void writeContent(const cxxtools::String& text);
64 
65  void writeCharacters(const cxxtools::String& text);
66 
67  void flush();
68 
69  void endl();
70 
71  enum FormatFlags {
73  UseIndent = 2,
74  UseEndl = 4
75  };
76 
77  void setFormat(int f) { _flags = f; }
78 
79  void setFormatFlags(int f, bool sw = true) { if (sw) _flags |= f; else _flags &= ~f; }
80 
81  int format() const { return _flags; }
82 
83  bool useXmlDeclaration() const { return _flags & UseXmlDeclaration; }
84 
86 
87  bool useIndent() const { return _flags & UseIndent; }
88 
89  void useIndent(bool sw) { setFormatFlags(UseIndent, sw); }
90 
91  bool useEndl() const { return _flags & UseEndl; }
92 
93  void useEndl(bool sw) { setFormatFlags(UseEndl, sw); }
94 
95  void beautify(bool sw) { _flags = sw ? (UseXmlDeclaration | UseIndent | UseEndl) : 0; }
96 
97  class Element
98  {
99  public:
100  Element(XmlWriter& writer, const cxxtools::String& localName)
101  : _writer(writer)
102  { _writer.writeStartElement(localName); }
103 
104  Element(XmlWriter& writer, const cxxtools::String& localName, const Attribute* attr, size_t attrCount)
105  : _writer(writer)
106  { _writer.writeStartElement(localName, attr, attrCount); }
107 
108  Element(XmlWriter& writer, const cxxtools::String& localName, const Attributes& attr)
109  : _writer(writer)
110  { _writer.writeStartElement(localName, attr); }
111 
113  { _writer.writeEndElement(); }
114 
115  void writeContent(const String& text);
116 
117  private:
118  XmlWriter& _writer;
119  };
120 
121  private:
122  void indent(size_t size);
123  void indent();
124 
125  TextOStream _tos;
126  std::stack<cxxtools::String> _elements;
127  int _flags;
128  };
129 
130  inline void XmlWriter::indent()
131  { indent(_elements.size()); }
132 }
133 
134 }
135 
136 #endif