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>
16 typedef map<string, XMLObj *>::iterator map_iter_t;
22 void set(const XMLObjIter::map_iter_t &_cur, const XMLObjIter::map_iter_t &_end);
27 * Represents a block of XML.
28 * Give the class an XML blob, and it will parse the blob into
29 * an attr_name->value map.
30 * This really ought to be an abstract class or something; it
31 * shouldn't be the startpoint for any parsing. Look at RGWXMLParser for that.
39 multimap<string, XMLObj *> children;
40 map<string, string> attr_map;
43 XMLObj() : parent(NULL) {}
46 bool xml_start(XMLObj *parent, const char *el, const char **attr);
47 virtual bool xml_end(const char *el);
48 virtual void xml_handle_data(const char *s, int len);
51 void add_child(string el, XMLObj *obj);
52 bool get_attr(string name, string& attr);
53 XMLObjIter find(string name);
54 XMLObj *find_first(string name);
56 friend ostream& operator<<(ostream &out, const XMLObj &obj);
59 struct XML_ParserStruct;
60 class RGWXMLParser : public XMLObj
66 vector<XMLObj *> objs;
67 list<XMLObj *> allocated_objs;
68 list<XMLObj> unallocated_objs;
70 virtual XMLObj *alloc_obj(const char *el) {
75 ~RGWXMLParser() override;
77 bool xml_start(const char *el, const char **attr);
78 bool xml_end(const char *el) override;
79 void handle_data(const char *s, int len);
81 bool parse(const char *buf, int len, int done);
82 const char *get_xml() { return buf; }
83 void set_failure() { success = false; }
94 explicit err(const string& m) : message(m) {}
97 class XMLParser : public RGWXMLParser {
100 ~XMLParser() override {}
103 explicit RGWXMLDecoder(bufferlist& bl) {
104 if (!parser.parse(bl.c_str(), bl.length(), 1)) {
105 cout << "RGWXMLDecoder::err()" << std::endl;
106 throw RGWXMLDecoder::err("failed to parse XML input");
111 static bool decode_xml(const char *name, T& val, XMLObj *obj, bool mandatory = false);
114 static bool decode_xml(const char *name, C& container, void (*cb)(C&, XMLObj *obj), XMLObj *obj, bool mandatory = false);
117 static void decode_xml(const char *name, T& val, T& default_val, XMLObj *obj);
121 void decode_xml_obj(T& val, XMLObj *obj)
126 static inline void decode_xml_obj(string& val, XMLObj *obj)
128 val = obj->get_data();
131 void decode_xml_obj(unsigned long long& val, XMLObj *obj);
132 void decode_xml_obj(long long& val, XMLObj *obj);
133 void decode_xml_obj(unsigned long& val, XMLObj *obj);
134 void decode_xml_obj(long& val, XMLObj *obj);
135 void decode_xml_obj(unsigned& val, XMLObj *obj);
136 void decode_xml_obj(int& val, XMLObj *obj);
137 void decode_xml_obj(bool& val, XMLObj *obj);
138 void decode_xml_obj(bufferlist& val, XMLObj *obj);
140 void decode_xml_obj(utime_t& val, XMLObj *obj);
143 void do_decode_xml_obj(list<T>& l, const string& name, XMLObj *obj)
147 XMLObjIter iter = obj->find(name);
150 while ((o = iter.get_next())) {
152 decode_xml_obj(val, o);
158 void do_decode_xml_obj(vector<T>& l, const string& name, XMLObj *obj)
162 XMLObjIter iter = obj->find(name);
165 while (o = iter.get_next()) {
167 decode_xml_obj(val, o);
173 bool RGWXMLDecoder::decode_xml(const char *name, T& val, XMLObj *obj, bool mandatory)
175 XMLObjIter iter = obj->find(name);
176 XMLObj *o = iter.get_next();
179 string s = "missing mandatory field " + string(name);
187 decode_xml_obj(val, o);
189 string s = string(name) + ": ";
198 bool RGWXMLDecoder::decode_xml(const char *name, C& container, void (*cb)(C&, XMLObj *), XMLObj *obj, bool mandatory)
202 XMLObjIter iter = obj->find(name);
203 XMLObj *o = iter.get_next();
206 string s = "missing mandatory field " + string(name);
213 decode_xml_obj(container, cb, o);
215 string s = string(name) + ": ";
224 void RGWXMLDecoder::decode_xml(const char *name, T& val, T& default_val, XMLObj *obj)
226 XMLObjIter iter = obj->find(name);
227 XMLObj *o = iter.get_next();
234 decode_xml_obj(val, o);
237 string s = string(name) + ": ";
244 static void encode_xml(const char *name, const T& val, ceph::Formatter *f)
246 f->open_object_section(name);
251 void encode_xml(const char *name, const string& val, ceph::Formatter *f);
252 void encode_xml(const char *name, const char *val, ceph::Formatter *f);
253 void encode_xml(const char *name, bool val, ceph::Formatter *f);
254 void encode_xml(const char *name, int val, ceph::Formatter *f);
255 void encode_xml(const char *name, unsigned val, ceph::Formatter *f);
256 void encode_xml(const char *name, long val, ceph::Formatter *f);
257 void encode_xml(const char *name, unsigned long val, ceph::Formatter *f);
258 void encode_xml(const char *name, long long val, ceph::Formatter *f);
259 void encode_xml(const char *name, const utime_t& val, ceph::Formatter *f);
260 void encode_xml(const char *name, const bufferlist& bl, ceph::Formatter *f);
261 void encode_xml(const char *name, long long val, ceph::Formatter *f);
262 void encode_xml(const char *name, long long unsigned val, ceph::Formatter *f);
265 static void do_encode_xml(const char *name, const std::list<T>& l, const char *entry_name, ceph::Formatter *f)
267 f->open_array_section(name);
268 for (typename std::list<T>::const_iterator iter = l.begin(); iter != l.end(); ++iter) {
269 encode_xml(entry_name, *iter, f);