]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
XmlFormatter: add DTD option
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Aug 2011 00:05:53 +0000 (17:05 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Wed, 3 Aug 2011 00:05:53 +0000 (17:05 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/Formatter.cc
src/common/Formatter.h
src/test/formatter.cc

index 60b050c3a85cb38e7927aee87affeb18ee8bb76f..83c16e4365e06d6f97328c7771a32a1802e2d756 100644 (file)
@@ -200,9 +200,14 @@ int JSONFormatter::get_len() const
   return m_ss.str().size();
 }
 
-XMLFormatter::XMLFormatter(bool p)
+const char *XMLFormatter::XML_1_DTD = 
+  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
+
+XMLFormatter::XMLFormatter(const char *dtd, bool p)
   : m_pretty(p)
 {
+  if (dtd)
+    m_dtd = dtd;
   reset();
 }
 
@@ -217,6 +222,10 @@ 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();
index 2efa1dd5bfb648b7532a0721f7aac1cfcb24e513..c1f4fd016b83e164fcb0dcc0f231e07ee9f358f0 100644 (file)
@@ -71,7 +71,8 @@ class JSONFormatter : public Formatter {
 
 class XMLFormatter : public Formatter {
  public:
-  XMLFormatter(bool p=false);
+  static const char *XML_1_DTD;
+  XMLFormatter(const char *dtd, bool p=false);
 
   void flush(std::ostream& os);
   void reset();
@@ -96,6 +97,7 @@ class XMLFormatter : public Formatter {
   std::deque<std::string> m_sections;
   bool m_pretty;
   std::string m_pending_string_name;
+  std::string m_dtd;
 };
 
 }
index c34980d565eac6c9ed2d2996050d3379729912af..a8f3f39b34907e31ddef136b90e7f606d02b43dd 100644 (file)
@@ -58,7 +58,7 @@ TEST(JsonFormatter, Empty) {
 
 TEST(XmlFormatter, Simple1) {
   ostringstream oss;
-  XMLFormatter fmt(false);
+  XMLFormatter fmt(NULL, 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(false);
+  XMLFormatter fmt(NULL, 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(false);
+  XMLFormatter fmt(NULL, false);
   fmt.flush(oss);
   ASSERT_EQ(oss.str(), "");
 }
 
 TEST(XmlFormatter, DumpStream1) {
   ostringstream oss;
-  XMLFormatter fmt(false);
+  XMLFormatter fmt(NULL, 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(false);
+  XMLFormatter fmt(NULL, 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(false);
+  XMLFormatter fmt(NULL, false);
 
   fmt.open_array_section("foo");
   fmt.dump_stream("blah") << "hithere";
@@ -125,3 +125,16 @@ TEST(XmlFormatter, DumpStream3) {
   fmt.flush(oss);
   ASSERT_EQ(oss.str(), "<foo><blah>hithere</blah><pi>3.14</pi></foo>");
 }
+
+TEST(XmlFormatter, DTD) {
+  ostringstream oss;
+  XMLFormatter fmt(XMLFormatter::XML_1_DTD, false);
+
+  fmt.open_array_section("foo");
+  fmt.dump_stream("blah") << "hithere";
+  fmt.dump_float("pi", 3.14);
+  fmt.close_section();
+  fmt.flush(oss);
+  ASSERT_EQ(oss.str(), "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+    "<foo><blah>hithere</blah><pi>3.14</pi></foo>");
+}