Formatter.h
1 /*
2  * Copyright (C) 2009 by Dr. 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 
29 #ifndef Pt_XmlRpc_Formatter_h
30 #define Pt_XmlRpc_Formatter_h
31 
32 #include <Pt/XmlRpc/Api.h>
33 #include <Pt/Xml/XmlReader.h>
34 #include <Pt/Composer.h>
35 #include <Pt/Formatter.h>
36 #include <Pt/String.h>
37 #include <Pt/NonCopyable.h>
38 #include <Pt/Types.h>
39 #include <string>
40 #include <iostream>
41 
42 namespace Pt {
43 
44 namespace XmlRpc {
45 
48 class PT_XMLRPC_API Formatter : public Pt::Formatter
49  , private NonCopyable
50 {
51  public:
52  Formatter(std::basic_ostream<Char>& os);
53 
54  ~Formatter();
55 
56  void attach(Xml::XmlReader& reader);
57 
58  void attach(std::basic_ostream<Char>& os);
59 
63  bool advance(const Pt::Xml::Node& node);
64 
65  protected:
66  void onAddString(const char* name, const char* type,
67  const Pt::Char* value, const char* id);
68 
69  void onAddBool(const char* name, bool value,
70  const char* id);
71 
72  void onAddChar(const char* name, const Pt::Char& value,
73  const char* id);
74 
75  void onAddInt8(const char* name, Pt::int8_t value,
76  const char* id);
77 
78  void onAddInt16(const char* name, Pt::int16_t value,
79  const char* id);
80 
81  void onAddInt32(const char* name, Pt::int32_t value,
82  const char* id);
83 
84  void onAddInt64(const char* name, Pt::int64_t value,
85  const char* id);
86 
87  void onAddUInt8(const char* name, Pt::uint8_t value, const char* id);
88 
89  void onAddUInt16(const char* name, Pt::uint16_t value, const char* id);
90 
91  void onAddUInt32(const char* name, Pt::uint32_t value, const char* id);
92 
93  void onAddUInt64(const char* name, Pt::uint64_t value, const char* id);
94 
95  void onAddFloat(const char* name, float value,
96  const char* id);
97 
98  void onAddDouble(const char* name, double value,
99  const char* id);
100 
101  void onAddLongDouble(const char* name, long double value,
102  const char* id);
103 
104  void onAddBinary(const char* name, const char* type,
105  const char* value, std::size_t length, const char* id);
106 
107  void onAddReference(const char* name, const char* id);
108 
109  void onBeginSequence(const char* name, const char* type,
110  const char* id);
111 
112  virtual void onBeginElement();
113 
114  virtual void onFinishElement();
115 
116  void onFinishSequence();
117 
118  void onBeginStruct(const char* name, const char* type,
119  const char* id);
120 
121  void onBeginMember(const char* name);
122 
123  void onFinishMember();
124 
125  void onFinishStruct();
126 
127  protected:
128  void onBeginParse(Composer& composer);
129 
130  bool onParseSome();
131 
132  void onParse();
133 
134  private:
135  enum State
136  {
137  OnParam,
138  OnValueBegin,
139  OnValueEnd,
140  OnBoolBegin,
141  OnIntBegin,
142  OnDoubleBegin,
143  OnStringBegin,
144  OnScalar,
145  OnScalarEnd,
146  OnStructBegin,
147  OnMemberBegin,
148  OnNameBegin,
149  OnName,
150  OnNameEnd,
151  OnStructEnd,
152  OnArrayBegin,
153  OnDataBegin,
154  OnDataEnd,
155  OnArrayEnd
156  };
157 
158  Xml::XmlReader* _reader;
159  State _state;
160  Composer* _composer;
161 
162  std::basic_ostream<Char>* _os;
163  Pt::String _str;
164 
165  Pt::varint_t _r1;
166  Pt::varint_t _r2;
167 };
168 
169 } // namespace XmlRpc
170 
171 } // namespace Pt
172 
173 #endif // Pt_XmlRpc_Formatter_h
uint_type uint16_t
Unsigned 16-bit integer type.
Definition: Types.h:30
int_type int16_t
Signed 16-bit integer type.
Definition: Types.h:24
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
Support for serialization to different formats.
Definition: Formatter.h:45