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
38namespace Pt {
39
40namespace Xml {
41
42class XmlReader;
43class XmlWriter;
44class Node;
45class StartElement;
46class EndElement;
47
50class PT_XML_API XmlFormatter : public Formatter
51 , private NonCopyable
52{
53 public:
57
61
65
68
72
76 { return _writer;}
77
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
153
154 // inherit docs
156
157 // inherit docs
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
169
170 // inherit docs
172
173 // inherit docs
174 void onBeginParse(Composer& composer);
175
176 // inherit docs
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
Composes types during serialization.
Definition Composer.h:101
Formatter()
Default constructor.
Definition Formatter.h:529
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Unicode capable basic_string.
Definition Api-String.h:67
Represents a closing element tag in an XML document.
Definition EndElement.h:46
XML document node.
Definition Node.h:51
Represents the start of an element in an XML document.
Definition StartElement.h:320
~XmlFormatter()
Destructor.
bool onParseSome()
Returns true if composer completes, false if no more data available.
void onAddChar(const char *name, const Pt::Char &value, const char *id)
Formats a character value.
void onAddBinary(const char *name, const char *type, const char *value, std::size_t length, const char *id)
Formats a binary value.
void onAddBool(const char *name, bool value, const char *id)
Formats a bool value.
void onBeginParse(Composer &composer)
Begin to parse to a composer.
XmlReader * reader()
Returns the attached XmlReader or a nullptr.
Definition XmlFormatter.h:84
void onFinishMember()
Formats the end of a struct member.
void onParse()
Parse until composer completes.
void attach(XmlReader &reader)
Attach to an XmlReader.
XmlFormatter()
Default Constructor.
void onFinishElement()
Formats the end of a sequence element.
XmlFormatter(XmlWriter &writer)
Construct a formatter writing to a XmlWriter.
void onAddString(const char *, const char *type, const Pt::Char *value, const char *id)
Formats a string value.
void onAddInt8(const char *name, Pt::int8_t value, const char *id)
Formats a 8-bit integer.
void onAddInt64(const char *name, Pt::int64_t value, const char *id)
Formats a 64-bit integer.
void attach(XmlWriter &writer)
Attach to an XmlWriter.
void onFinishSequence()
Formats the end of a sequence.
void onBeginElement()
Formats the begin of a sequence element.
void onAddInt32(const char *name, Pt::int32_t value, const char *id)
Formats a 32-bit integer.
XmlFormatter(XmlReader &reader)
Construct a formatter reading from a XmlReader.
void onFinishStruct()
Formats the end of a struct.
void onAddUInt8(const char *name, Pt::uint8_t value, const char *id)
Formats a 8-bit unsigned integer.
XmlWriter * writer()
Returns the attached XmlWriter or a nullptr.
Definition XmlFormatter.h:75
void onAddUInt16(const char *name, Pt::uint16_t value, const char *id)
Formats a 16-bit unsigned integer.
void onAddUInt32(const char *name, Pt::uint32_t value, const char *id)
Formats a 32-bit unsigned integer.
void detach()
Detach from its XmlReader and XmlWriter.
void onBeginSequence(const char *name, const char *type, const char *id)
Formats the begin of a sequence.
void onAddInt16(const char *name, Pt::int16_t value, const char *id)
Formats a 16-bit integer.
void onAddUInt64(const char *name, Pt::uint64_t value, const char *id)
Formats a 64-bit unsigned integer.
void onAddFloat(const char *name, float value, const char *id)
Formats a float value.
void onBeginStruct(const char *name, const char *type, const char *id)
Formats the begin of a struct.
void onAddDouble(const char *name, double value, const char *id)
Formats a double value.
void onAddLongDouble(const char *name, long double value, const char *id)
Formats a long double value.
void onAddReference(const char *name, const char *value)
Formats a reference.
void onBeginMember(const char *name)
Formats the begin of a struct member.
Reads XML as a Stream of XML Nodes.
Definition XmlReader.h:80
Writes XML to a text stream.
Definition XmlWriter.h:53
uint_type uint16_t
Unsigned 16-bit integer type.
Definition Api-Types.h:38
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
int_type int32_t
Signed 32-bit integer type.
Definition Api-Types.h:45
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
uint_type uint64_t
Unsigned 64-bit integer type.
Definition Api-Types.h:66
int_type int16_t
Signed 16-bit integer type.
Definition Api-Types.h:31
uint_type uint8_t
Unsigned 8-bit integer type.
Definition Api-Types.h:24
int_type int8_t
Signed 8-bit integer type.
Definition Api-Types.h:17
Read and write XML ducuments.
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67