From: Colin Patrick McCabe Date: Wed, 3 Aug 2011 00:05:53 +0000 (-0700) Subject: XmlFormatter: add DTD option X-Git-Tag: v0.33~30 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=cec04ca9deb4828c63dc428a5328f2a05753ff3a;p=ceph.git XmlFormatter: add DTD option Signed-off-by: Colin McCabe --- diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 60b050c3a85c..83c16e4365e0 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -200,9 +200,14 @@ int JSONFormatter::get_len() const return m_ss.str().size(); } -XMLFormatter::XMLFormatter(bool p) +const char *XMLFormatter::XML_1_DTD = + "\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(); diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 2efa1dd5bfb6..c1f4fd016b83 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -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 m_sections; bool m_pretty; std::string m_pending_string_name; + std::string m_dtd; }; } diff --git a/src/test/formatter.cc b/src/test/formatter.cc index c34980d565ea..a8f3f39b3490 100644 --- a/src/test/formatter.cc +++ b/src/test/formatter.cc @@ -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(), "hithere"); @@ -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(), "hithere3.14"); } + +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(), "\n" + "hithere3.14"); +}