XmlFormatter.h
1 /*
2  * Copyright (C) 2008-2012 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef Pt_Xml_XmlFormatter_h
29 #define Pt_Xml_XmlFormatter_h
30 
31 #include <Pt/Xml/Api.h>
32 #include <Pt/Xml/XmlWriter.h>
33 #include <Pt/Xml/XmlReader.h>
34 #include <Pt/String.h>
35 #include <Pt/NonCopyable.h>
36 #include <Pt/Formatter.h>
37 
38 namespace Pt {
39 
40 namespace Xml {
41 
42 class XmlReader;
43 class XmlWriter;
44 class Node;
45 class StartElement;
46 class EndElement;
47 
50 class PT_XML_API XmlFormatter : public Formatter
51  , private NonCopyable
52 {
53  public:
56  XmlFormatter();
57 
60  explicit XmlFormatter(XmlWriter& writer);
61 
64  explicit XmlFormatter(XmlReader& reader);
65 
67  ~XmlFormatter();
68 
71  void attach(XmlWriter& writer);
72 
76  { return _writer;}
77 
80  void attach(XmlReader& reader);
81 
85  { return _reader;}
86 
89  void detach();
90 
91  protected:
92  // inherit docs
93  void onAddString(const char*, const char* type,
94  const Pt::Char* value, const char* id);
95 
96  // inherit docs
97  void onAddBool(const char* name, bool value,
98  const char* id);
99 
100  // inherit docs
101  void onAddChar(const char* name, const Pt::Char& value,
102  const char* id);
103 
104  // inherit docs
105  void onAddInt8(const char* name, Pt::int8_t value, const char* id);
106 
107  // inherit docs
108  void onAddInt16(const char* name, Pt::int16_t value, const char* id);
109 
110  // inherit docs
111  void onAddInt32(const char* name, Pt::int32_t value, const char* id);
112 
113  // inherit docs
114  void onAddInt64(const char* name, Pt::int64_t value, const char* id);
115 
116  // inherit docs
117  void onAddUInt8(const char* name, Pt::uint8_t value, const char* id);
118 
119  // inherit docs
120  void onAddUInt16(const char* name, Pt::uint16_t value, const char* id);
121 
122  // inherit docs
123  void onAddUInt32(const char* name, Pt::uint32_t value, const char* id);
124 
125  // inherit docs
126  void onAddUInt64(const char* name, Pt::uint64_t value, const char* id);
127 
128  // inherit docs
129  void onAddFloat(const char* name, float value,
130  const char* id);
131 
132  // inherit docs
133  void onAddDouble(const char* name, double value,
134  const char* id);
135 
136  // inherit docs
137  void onAddLongDouble(const char* name, long double value,
138  const char* id);
139 
140  // inherit docs
141  void onAddBinary(const char* name, const char* type,
142  const char* value, std::size_t length, const char* id);
143 
144  // inherit docs
145  void onAddReference(const char* name, const char* value);
146 
147  // inherit docs
148  void onBeginSequence(const char* name, const char* type,
149  const char* id);
150 
151  // inherit docs
152  void onBeginElement();
153 
154  // inherit docs
155  void onFinishElement();
156 
157  // inherit docs
158  void onFinishSequence();
159 
160  // inherit docs
161  void onBeginStruct(const char* name, const char* type,
162  const char* id);
163 
164  // inherit docs
165  void onBeginMember(const char* name);
166 
167  // inherit docs
168  void onFinishMember();
169 
170  // inherit docs
171  void onFinishStruct();
172 
173  // inherit docs
174  void onBeginParse(Composer& composer);
175 
176  // inherit docs
177  bool onParseSome();
178 
179  // inherit docs
180  void onParse();
181 
182  protected:
184  void addValue(const char*, const char* type,
185  const Pt::Char* value, const char* id);
186 
188  void OnBegin(const Node& node);
189 
191  void OnReferenceBegin(const Node& node);
192 
194  void OnMemberBegin(const Node& node);
195 
197  void OnValue(const Node& node);
198 
200  void OnMemberEnd(const Node& node);
201 
203  void beginXmlMember(const StartElement& se);
204 
206  void finishXmlMember(const EndElement& ee);
207 
208  private:
210  XmlWriter* _writer;
211 
213  XmlReader* _reader;
214 
216  Pt::String _value;
217 
219  int _valueType;
220 
222  typedef void (XmlFormatter::*ProcessNode)(const Node&);
223 
225  ProcessNode _processNode;
226 
228  Composer* _composer;
229 };
230 
231 } // namespace Xml
232 
233 } // namespace Pt
234 
235 #endif
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
Represents a closing element tag in an XML document.
Definition: EndElement.h:44
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Composes types during serialization.
Definition: Composer.h:42
Reads XML as a Stream of XML Nodes.
Definition: XmlReader.h:79
Writes XML to a text stream.
Definition: XmlWriter.h:52
XmlReader * reader()
Returns the attached XmlReader or a nullptr.
Definition: XmlFormatter.h:84
int_type int32_t
Signed 32-bit integer type.
Definition: Types.h:36
Unicode character type.
Definition: String.h:66
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
int_type int8_t
Signed 8-bit integer type.
Definition: Types.h:12
uint_type uint64_t
Unsigned 64-bit integer type.
Definition: Types.h:54
Unicode capable basic_string.
Definition: String.h:42
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
uint_type uint8_t
Unsigned 8-bit integer type.
Definition: Types.h:18
XML document node.
Definition: Node.h:50
Format objects or data to XML.
Definition: XmlFormatter.h:50
Support for serialization to different formats.
Definition: Formatter.h:45
Represents the start of an element in an XML document.
Definition: StartElement.h:318
XmlWriter * writer()
Returns the attached XmlWriter or a nullptr.
Definition: XmlFormatter.h:75