]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
More changes towards using common/Formatter in rgw
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 4 Aug 2011 20:57:33 +0000 (13:57 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Thu, 4 Aug 2011 21:05:59 +0000 (14:05 -0700)
* 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>
src/common/Formatter.cc
src/common/Formatter.h
src/rgw/rgw_admin.cc
src/rgw/rgw_common.cc
src/rgw/rgw_common.h
src/rgw/rgw_formats.cc
src/rgw/rgw_formats.h
src/rgw/rgw_rest.cc
src/test/formatter.cc

index 83c16e4365e06d6f97328c7771a32a1802e2d756..f7d50dca4e42422d15321fdad1d9cc09d41f210a 100644 (file)
@@ -200,14 +200,17 @@ int JSONFormatter::get_len() const
   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();
 }
 
@@ -222,10 +225,6 @@ void XMLFormatter::flush(std::ostream& os)
 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();
@@ -314,6 +313,11 @@ int XMLFormatter::get_len() const
   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);
index c1f4fd016b83e164fcb0dcc0f231e07ee9f358f0..ec306765dddeb040f57fe101e5d8406670081d73 100644 (file)
@@ -30,6 +30,7 @@ class Formatter {
   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;
 };
 
 
@@ -49,6 +50,7 @@ class JSONFormatter : public Formatter {
   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 {
@@ -72,7 +74,7 @@ class JSONFormatter : public Formatter {
 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();
@@ -86,6 +88,7 @@ class XMLFormatter : public Formatter {
   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);
@@ -97,7 +100,6 @@ class XMLFormatter : public Formatter {
   std::deque<std::string> m_sections;
   bool m_pretty;
   std::string m_pending_string_name;
-  std::string m_dtd;
 };
 
 }
index 5a5eb63e31959dd88b2a3be58f0156c0b8a6aa13..f60efe579ed25c94e89d1435e0939f3ee3b2b0fd 100644 (file)
@@ -8,6 +8,7 @@ using namespace std;
 
 #include "common/config.h"
 #include "common/ceph_argparse.h"
+#include "common/Formatter.h"
 #include "global/global_init.h"
 #include "common/errno.h"
 
@@ -835,7 +836,7 @@ int main(int argc, char **argv)
     const char *delim = " ";
 
     if (format) {
-      formatter->init();
+      formatter->reset();
       formatter->open_array_section("Log");
     }
 
@@ -904,7 +905,7 @@ int main(int argc, char **argv)
       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());
index da4e376dd51bb13a96a21f61b4bc912e5d2829f2..3968565beae6938c74eea42d69a1497421d5facf 100644 (file)
@@ -324,6 +324,11 @@ bool url_decode(string& src_str, string& dest_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
@@ -384,7 +389,7 @@ done_free:
     free(p);
 }
 
-void RGWFormatter::reset()
+void RGWFormatter::base_reset()
 {
   free(buf);
   buf = NULL;
index 7406395502a55f1b84c8a8cfae7b07cbc44e2754..9df966c713d2000c36999a3d5f74db4468a9fb58 100644 (file)
@@ -362,20 +362,11 @@ protected:
   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;
@@ -391,6 +382,9 @@ public:
   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;
 };
index 6644de5176703e324cfcbea906093a0d01753972..dd6dc6e2ebbea1d3c7910f4489ea83d0d54ea587 100644 (file)
@@ -17,8 +17,9 @@
 #include "rgw/rgw_formats.h"
 
 /* Plain */
-void RGWFormatter_Plain::formatter_init()
+void RGWFormatter_Plain::reset()
 {
+  base_reset();
   stack.clear();
   min_stack_level = 0;
 }
@@ -90,8 +91,9 @@ void RGWFormatter_Plain::dump_format(const char *name, const char *fmt, ...)
 
 /* XML */
 
-void RGWFormatter_XML::formatter_init()
+void RGWFormatter_XML::reset()
 {
+  base_reset();
   indent = 0;
 }
 
@@ -135,8 +137,9 @@ void RGWFormatter_XML::dump_format(const char *name, const char *fmt, ...)
 
 /* JSON */
 
-void RGWFormatter_JSON::formatter_init()
+void RGWFormatter_JSON::reset()
 {
+  base_reset();
   stack.clear();
 }
 
index e1f280dfccb1cbbcfcb761e72318bc47dd374c22..723c7f36eb61bea10ce74749d7f2d89944ea1b39 100644 (file)
@@ -9,13 +9,12 @@ struct plain_stack_entry {
 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);
@@ -29,13 +28,13 @@ class RGWFormatter_XML : public RGWFormatter {
 
   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);
   }
@@ -57,13 +56,11 @@ class RGWFormatter_JSON : public RGWFormatter {
   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);
index 4468f72ab2e3045d7351508bf5dc8cfb687b8f5a..4361bec228d4f3020fee7b6a63c87cb983f94004 100644 (file)
@@ -1,5 +1,6 @@
 #include <errno.h>
 
+#include "common/Formatter.h"
 #include "common/utf8.h"
 #include "rgw_common.h"
 #include "rgw_access.h"
@@ -113,12 +114,6 @@ void dump_last_modified(struct req_state *s, time_t t)
   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];
@@ -146,7 +141,7 @@ void dump_start(struct req_state *s)
 {
   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;
   }
 }
@@ -507,7 +502,7 @@ void init_entities_from_header(struct req_state *s)
     }
   }
 done:
-  s->formatter->init();
+  s->formatter->reset();
 }
 
 static void line_unfold(const char *line, string& sdest)
index a8f3f39b34907e31ddef136b90e7f606d02b43dd..eec69bf7ad1c7658ec22d3d849e1657ca88fdd3c 100644 (file)
@@ -58,7 +58,7 @@ TEST(JsonFormatter, Empty) {
 
 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);
@@ -70,7 +70,7 @@ TEST(XmlFormatter, Simple1) {
 
 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);
@@ -90,14 +90,14 @@ TEST(XmlFormatter, Simple2) {
 
 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>");
@@ -105,7 +105,7 @@ TEST(XmlFormatter, DumpStream1) {
 
 TEST(XmlFormatter, DumpStream2) {
   ostringstream oss;
-  XMLFormatter fmt(NULL, false);
+  XMLFormatter fmt(false);
 
   fmt.open_array_section("foo");
   fmt.dump_stream("blah") << "hithere";
@@ -116,7 +116,7 @@ TEST(XmlFormatter, DumpStream2) {
 
 TEST(XmlFormatter, DumpStream3) {
   ostringstream oss;
-  XMLFormatter fmt(NULL, false);
+  XMLFormatter fmt(false);
 
   fmt.open_array_section("foo");
   fmt.dump_stream("blah") << "hithere";
@@ -128,8 +128,9 @@ TEST(XmlFormatter, DumpStream3) {
 
 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);