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/xml/api.h>
30 #include <cxxtools/string.h>
31 #include <cxxtools/textstream.h>
32 #include <stack>
33 
34 namespace cxxtools {
35 
36 namespace xml {
37 
38  class Attribute;
39 
41  {
42  public:
43  XmlWriter();
44 
45  XmlWriter(std::ostream& os, int format = UseXmlDeclaration | UseIndent | UseEndl);
46 
47  ~XmlWriter();
48 
49  void begin(std::ostream& os);
50 
51  void writeStartElement(const cxxtools::String& prefix, const cxxtools::String& localName, const cxxtools::String& ns);
52 
53  void writeStartElement(const cxxtools::String& localName);
54 
55  void writeStartElement(const cxxtools::String& localName, const Attribute* attr, size_t attrCount);
56 
57  void writeEndElement();
58 
59  void writeElement(const cxxtools::String& localName, const cxxtools::String& content);
60 
61  void writeElement(const cxxtools::String& localName, const Attribute* attr, size_t attrCount, const cxxtools::String& content);
62 
63  void writeCharacters(const cxxtools::String& text);
64 
65  void flush();
66 
67  void endl();
68 
69  enum FormatFlags {
70  UseXmlDeclaration = 1,
71  UseIndent = 2,
72  UseEndl = 4
73  };
74 
75  void setFormat(int f) { _flags = f; }
76 
77  void setFormatFlags(int f, bool sw = true) { if (sw) _flags |= f; else _flags &= ~f; }
78 
79  int format() const { return _flags; }
80 
81  bool useXmlDeclaration() const { return _flags & UseXmlDeclaration; }
82 
83  void useXmlDeclaration(bool sw) { setFormatFlags(UseXmlDeclaration, sw); }
84 
85  bool useIndent() const { return _flags & UseIndent; }
86 
87  void useIndent(bool sw) { setFormatFlags(UseIndent, sw); }
88 
89  bool useEndl() const { return _flags & UseEndl; }
90 
91  void useEndl(bool sw) { setFormatFlags(UseEndl, sw); }
92 
93  private:
94  TextOStream _tos;
95  std::stack<cxxtools::String> _elements;
96  int _flags;
97  };
98 
99 }
100 
101 }
102 
103 #endif