]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rgw/rgw_xml.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
10 #include <include/types.h>
11 #include <common/Formatter.h>
19 typedef map
<string
, XMLObj
*>::iterator map_iter_t
;
25 void set(const XMLObjIter::map_iter_t
&_cur
, const XMLObjIter::map_iter_t
&_end
);
30 * Represents a block of XML.
31 * Give the class an XML blob, and it will parse the blob into
32 * an attr_name->value map.
33 * This really ought to be an abstract class or something; it
34 * shouldn't be the startpoint for any parsing. Look at RGWXMLParser for that.
42 multimap
<string
, XMLObj
*> children
;
43 map
<string
, string
> attr_map
;
46 XMLObj() : parent(NULL
) {}
49 bool xml_start(XMLObj
*parent
, const char *el
, const char **attr
);
50 virtual bool xml_end(const char *el
);
51 virtual void xml_handle_data(const char *s
, int len
);
54 void add_child(string el
, XMLObj
*obj
);
55 bool get_attr(string name
, string
& attr
);
56 XMLObjIter
find(string name
);
57 XMLObj
*find_first(string name
);
59 friend ostream
& operator<<(ostream
&out
, const XMLObj
&obj
);
62 struct XML_ParserStruct
;
63 class RGWXMLParser
: public XMLObj
69 vector
<XMLObj
*> objs
;
70 list
<XMLObj
*> allocated_objs
;
71 list
<XMLObj
> unallocated_objs
;
73 virtual XMLObj
*alloc_obj(const char *el
) {
78 ~RGWXMLParser() override
;
80 bool xml_start(const char *el
, const char **attr
);
81 bool xml_end(const char *el
) override
;
82 void handle_data(const char *s
, int len
);
84 bool parse(const char *buf
, int len
, int done
);
85 const char *get_xml() { return buf
; }
86 void set_failure() { success
= false; }
97 explicit err(const string
& m
) : message(m
) {}
100 class XMLParser
: public RGWXMLParser
{
103 ~XMLParser() override
{}
106 explicit RGWXMLDecoder(bufferlist
& bl
) {
107 if (!parser
.parse(bl
.c_str(), bl
.length(), 1)) {
108 cout
<< "RGWXMLDecoder::err()" << std::endl
;
109 throw RGWXMLDecoder::err("failed to parse XML input");
114 static bool decode_xml(const char *name
, T
& val
, XMLObj
*obj
, bool mandatory
= false);
117 static bool decode_xml(const char *name
, C
& container
, void (*cb
)(C
&, XMLObj
*obj
), XMLObj
*obj
, bool mandatory
= false);
120 static void decode_xml(const char *name
, T
& val
, T
& default_val
, XMLObj
*obj
);
124 void decode_xml_obj(T
& val
, XMLObj
*obj
)
129 static inline void decode_xml_obj(string
& val
, XMLObj
*obj
)
131 val
= obj
->get_data();
134 void decode_xml_obj(unsigned long long& val
, XMLObj
*obj
);
135 void decode_xml_obj(long long& val
, XMLObj
*obj
);
136 void decode_xml_obj(unsigned long& val
, XMLObj
*obj
);
137 void decode_xml_obj(long& val
, XMLObj
*obj
);
138 void decode_xml_obj(unsigned& val
, XMLObj
*obj
);
139 void decode_xml_obj(int& val
, XMLObj
*obj
);
140 void decode_xml_obj(bool& val
, XMLObj
*obj
);
141 void decode_xml_obj(bufferlist
& val
, XMLObj
*obj
);
143 void decode_xml_obj(utime_t
& val
, XMLObj
*obj
);
146 void do_decode_xml_obj(list
<T
>& l
, const string
& name
, XMLObj
*obj
)
150 XMLObjIter iter
= obj
->find(name
);
153 while ((o
= iter
.get_next())) {
155 decode_xml_obj(val
, o
);
161 void do_decode_xml_obj(vector
<T
>& l
, const string
& name
, XMLObj
*obj
)
165 XMLObjIter iter
= obj
->find(name
);
168 while (o
= iter
.get_next()) {
170 decode_xml_obj(val
, o
);
176 bool RGWXMLDecoder::decode_xml(const char *name
, T
& val
, XMLObj
*obj
, bool mandatory
)
178 XMLObjIter iter
= obj
->find(name
);
179 XMLObj
*o
= iter
.get_next();
182 string s
= "missing mandatory field " + string(name
);
190 decode_xml_obj(val
, o
);
192 string s
= string(name
) + ": ";
201 bool RGWXMLDecoder::decode_xml(const char *name
, C
& container
, void (*cb
)(C
&, XMLObj
*), XMLObj
*obj
, bool mandatory
)
205 XMLObjIter iter
= obj
->find(name
);
206 XMLObj
*o
= iter
.get_next();
209 string s
= "missing mandatory field " + string(name
);
216 decode_xml_obj(container
, cb
, o
);
218 string s
= string(name
) + ": ";
227 void RGWXMLDecoder::decode_xml(const char *name
, T
& val
, T
& default_val
, XMLObj
*obj
)
229 XMLObjIter iter
= obj
->find(name
);
230 XMLObj
*o
= iter
.get_next();
237 decode_xml_obj(val
, o
);
240 string s
= string(name
) + ": ";
247 static void encode_xml(const char *name
, const T
& val
, ceph::Formatter
*f
)
249 f
->open_object_section(name
);
254 void encode_xml(const char *name
, const string
& val
, ceph::Formatter
*f
);
255 void encode_xml(const char *name
, const char *val
, ceph::Formatter
*f
);
256 void encode_xml(const char *name
, bool val
, ceph::Formatter
*f
);
257 void encode_xml(const char *name
, int val
, ceph::Formatter
*f
);
258 void encode_xml(const char *name
, unsigned val
, ceph::Formatter
*f
);
259 void encode_xml(const char *name
, long val
, ceph::Formatter
*f
);
260 void encode_xml(const char *name
, unsigned long val
, ceph::Formatter
*f
);
261 void encode_xml(const char *name
, long long val
, ceph::Formatter
*f
);
262 void encode_xml(const char *name
, const utime_t
& val
, ceph::Formatter
*f
);
263 void encode_xml(const char *name
, const bufferlist
& bl
, ceph::Formatter
*f
);
264 void encode_xml(const char *name
, long long val
, ceph::Formatter
*f
);
265 void encode_xml(const char *name
, long long unsigned val
, ceph::Formatter
*f
);
268 static void do_encode_xml(const char *name
, const std::list
<T
>& l
, const char *entry_name
, ceph::Formatter
*f
)
270 f
->open_array_section(name
);
271 for (typename
std::list
<T
>::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
) {
272 encode_xml(entry_name
, *iter
, f
);