* rename RgwFormatter::init to RgwForamtter::reset.
Roll old reset() into base_reset().
* add write_raw_data to Formatter, to support DTDs.
Remove other DTD support code.
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
return m_ss.str().size();
}
+void JSONFormatter::write_raw_data(const char *data)
+{
+ m_ss << data;
+}
+
const char *XMLFormatter::XML_1_DTD =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
-XMLFormatter::XMLFormatter(const char *dtd, bool p)
- : m_pretty(p)
+XMLFormatter::XMLFormatter(bool pretty)
+ : m_pretty(pretty)
{
- if (dtd)
- m_dtd = dtd;
reset();
}
void XMLFormatter::reset()
{
m_ss.clear();
- m_ss << m_dtd;
- if (m_pretty) {
- m_ss << "\n";
- }
m_pending_string.clear();
m_sections.clear();
m_pending_string_name.clear();
return m_ss.str().size();
}
+void XMLFormatter::write_raw_data(const char *data)
+{
+ m_ss << data;
+}
+
void XMLFormatter::open_section(const char *name)
{
print_spaces(false);
virtual std::ostream& dump_stream(const char *name) = 0;
virtual void dump_format(const char *name, const char *fmt, ...) = 0;
virtual int get_len() const = 0;
+ virtual void write_raw_data(const char *data) = 0;
};
std::ostream& dump_stream(const char *name);
void dump_format(const char *name, const char *fmt, ...);
int get_len() const;
+ void write_raw_data(const char *data);
private:
struct json_formatter_stack_entry_d {
class XMLFormatter : public Formatter {
public:
static const char *XML_1_DTD;
- XMLFormatter(const char *dtd, bool p=false);
+ XMLFormatter(bool pretty = false);
void flush(std::ostream& os);
void reset();
std::ostream& dump_stream(const char *name);
void dump_format(const char *name, const char *fmt, ...);
int get_len() const;
+ void write_raw_data(const char *data);
private:
void open_section(const char *name);
std::deque<std::string> m_sections;
bool m_pretty;
std::string m_pending_string_name;
- std::string m_dtd;
};
}
#include "common/config.h"
#include "common/ceph_argparse.h"
+#include "common/Formatter.h"
#include "global/global_init.h"
#include "common/errno.h"
const char *delim = " ";
if (format) {
- formatter->init();
+ formatter->reset();
formatter->open_array_section("Log");
}
cerr << "could not retrieve pool info for pool_id=" << pool_id << std::endl;
return ret;
}
- formatter->init();
+ formatter->reset();
formatter->open_object_section("Pool");
formatter->dump_int("ID", pool_id);
formatter->dump_format("Bucket", "%s", info.bucket.c_str());
return true;
}
+void RGWFormatter::write_raw_data(const char *data)
+{
+ write_data("%s", data);
+}
+
void RGWFormatter::write_data(const char *fmt, ...)
{
#define LARGE_ENOUGH_LEN 128
free(p);
}
-void RGWFormatter::reset()
+void RGWFormatter::base_reset()
{
free(buf);
buf = NULL;
int len;
int max_len;
- virtual void formatter_init() = 0;
+ void base_reset();
public:
RGWFormatter() : buf(NULL), len(0), max_len(0) {}
virtual ~RGWFormatter() {}
- void init() {
- if (buf)
- free(buf);
- buf = NULL;
- len = 0;
- max_len = 0;
- formatter_init();
- }
- void reset();
- void write_data(const char *fmt, ...);
+ virtual void reset() = 0;
virtual void flush(ostream& os);
virtual int get_len() { return (len ? len - 1 : 0); } // don't include null termination in length
virtual void open_array_section(const char *name) = 0;
void dump_float(const char *name, double d) {
dump_value_int(name, "%f", d);
}
+ void write_raw_data(const char *data);
+protected:
+ void write_data(const char *fmt, ...);
private:
virtual void dump_value_int(const char *name, const char *fmt, ...) = 0;
};
#include "rgw/rgw_formats.h"
/* Plain */
-void RGWFormatter_Plain::formatter_init()
+void RGWFormatter_Plain::reset()
{
+ base_reset();
stack.clear();
min_stack_level = 0;
}
/* XML */
-void RGWFormatter_XML::formatter_init()
+void RGWFormatter_XML::reset()
{
+ base_reset();
indent = 0;
}
/* JSON */
-void RGWFormatter_JSON::formatter_init()
+void RGWFormatter_JSON::reset()
{
+ base_reset();
stack.clear();
}
class RGWFormatter_Plain : public RGWFormatter {
std::list<struct plain_stack_entry> stack;
size_t min_stack_level;
-protected:
- void formatter_init();
public:
RGWFormatter_Plain() : RGWFormatter() {}
~RGWFormatter_Plain() {}
+ void reset();
void open_array_section(const char *name);
void open_object_section(const char *name);
void close_section(const char *name);
void open_section(const char *name);
-protected:
- void formatter_init();
-
public:
- RGWFormatter_XML() : RGWFormatter() {}
+ RGWFormatter_XML()
+ : RGWFormatter()
+ {}
~RGWFormatter_XML() {}
+ void reset();
void open_array_section(const char *name) {
open_section(name);
}
std::list<struct json_stack_entry> stack;
void open_section(bool is_array);
-protected:
- void formatter_init();
-
public:
RGWFormatter_JSON() : RGWFormatter() {}
~RGWFormatter_JSON() {}
+ void reset();
void open_array_section(const char *name);
void open_object_section(const char *name);
void close_section(const char *name);
#include <errno.h>
+#include "common/Formatter.h"
#include "common/utf8.h"
#include "rgw_common.h"
#include "rgw_access.h"
CGI_PRINTF(s, "Last-Modified: %s\n", timestr);
}
-static void dump_entry(struct req_state *s, const char *val)
-{
- s->formatter->write_data("<?%s?>", val);
-}
-
-
void dump_time(struct req_state *s, const char *name, time_t *t)
{
char buf[TIME_BUF_SIZE];
{
if (!s->content_started) {
if (s->format == RGW_FORMAT_XML)
- dump_entry(s, "xml version=\"1.0\" encoding=\"UTF-8\"");
+ s->formatter->write_raw_data(XMLFormatter::XML_1_DTD);
s->content_started = true;
}
}
}
}
done:
- s->formatter->init();
+ s->formatter->reset();
}
static void line_unfold(const char *line, string& sdest)
TEST(XmlFormatter, Simple1) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.open_object_section("foo");
fmt.dump_int("a", 1);
fmt.dump_int("b", 2);
TEST(XmlFormatter, Simple2) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.open_object_section("foo");
fmt.open_object_section("bar");
fmt.dump_int("int", 0xf00000000000ll);
TEST(XmlFormatter, Empty) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.flush(oss);
ASSERT_EQ(oss.str(), "");
}
TEST(XmlFormatter, DumpStream1) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.dump_stream("blah") << "hithere";
fmt.flush(oss);
ASSERT_EQ(oss.str(), "<blah>hithere</blah>");
TEST(XmlFormatter, DumpStream2) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.open_array_section("foo");
fmt.dump_stream("blah") << "hithere";
TEST(XmlFormatter, DumpStream3) {
ostringstream oss;
- XMLFormatter fmt(NULL, false);
+ XMLFormatter fmt(false);
fmt.open_array_section("foo");
fmt.dump_stream("blah") << "hithere";
TEST(XmlFormatter, DTD) {
ostringstream oss;
- XMLFormatter fmt(XMLFormatter::XML_1_DTD, false);
+ XMLFormatter fmt(false);
+ fmt.write_raw_data(XMLFormatter::XML_1_DTD);
fmt.open_array_section("foo");
fmt.dump_stream("blah") << "hithere";
fmt.dump_float("pi", 3.14);