From 4ea0c7c65d9e627acb73524afdc7f51ac02aa25d Mon Sep 17 00:00:00 2001 From: Radoslaw Zarzynski Date: Thu, 22 Oct 2015 18:56:14 +0200 Subject: [PATCH] rgw: XMLFormatter may print XML tags lowercased and underscored now. Signed-off-by: Radoslaw Zarzynski --- src/common/Formatter.cc | 40 ++++++++++++++++++++++++++++++++++++---- src/common/Formatter.h | 3 ++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/common/Formatter.cc b/src/common/Formatter.cc index 9b5b20303e85c..f5ce058a2f74f 100644 --- a/src/common/Formatter.cc +++ b/src/common/Formatter.cc @@ -21,6 +21,7 @@ #include "common/escape.h" #include "include/buffer.h" +#include #include #include #include @@ -32,6 +33,9 @@ #include +static char tolower_underscore(const char b) { + return ' ' == b ? '_' : std::tolower(b); +} // ----------------------- namespace ceph { @@ -317,8 +321,9 @@ void JSONFormatter::write_raw_data(const char *data) const char *XMLFormatter::XML_1_DTD = ""; -XMLFormatter::XMLFormatter(bool pretty) -: m_pretty(pretty) +XMLFormatter::XMLFormatter(bool pretty, bool lowercased_underscored) +: m_pretty(pretty), + m_lowercased_underscored(lowercased_underscored) { reset(); } @@ -379,6 +384,10 @@ void XMLFormatter::close_section() finish_pending_string(); std::string section = m_sections.back(); + if (m_lowercased_underscored) { + std::transform(section.begin(), section.end(), section.begin(), + tolower_underscore); + } m_sections.pop_back(); print_spaces(); m_ss << ""; @@ -389,6 +398,9 @@ void XMLFormatter::close_section() void XMLFormatter::dump_unsigned(const char *name, uint64_t u) { std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } print_spaces(); m_ss << "<" << e << ">" << u << ""; if (m_pretty) @@ -398,6 +410,9 @@ void XMLFormatter::dump_unsigned(const char *name, uint64_t u) void XMLFormatter::dump_int(const char *name, int64_t u) { std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } print_spaces(); m_ss << "<" << e << ">" << u << ""; if (m_pretty) @@ -407,6 +422,9 @@ void XMLFormatter::dump_int(const char *name, int64_t u) void XMLFormatter::dump_float(const char *name, double d) { std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } print_spaces(); m_ss << "<" << e << ">" << d << ""; if (m_pretty) @@ -416,6 +434,9 @@ void XMLFormatter::dump_float(const char *name, double d) void XMLFormatter::dump_string(const char *name, const std::string& s) { std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } print_spaces(); m_ss << "<" << e << ">" << escape_xml_str(s.c_str()) << ""; if (m_pretty) @@ -425,6 +446,9 @@ void XMLFormatter::dump_string(const char *name, const std::string& s) void XMLFormatter::dump_string_with_attrs(const char *name, const std::string& s, const FormatterAttrs& attrs) { std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } std::string attrs_str; get_attrs_str(&attrs, attrs_str); print_spaces(); @@ -447,6 +471,9 @@ void XMLFormatter::dump_format_va(const char* name, const char *ns, bool quoted, vsnprintf(buf, LARGE_SIZE, fmt, ap); std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } print_spaces(); if (ns) { m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << ""; @@ -490,10 +517,15 @@ void XMLFormatter::open_section_in_ns(const char *name, const char *ns, const Fo get_attrs_str(attrs, attrs_str); } + std::string e(name); + if (m_lowercased_underscored) { + std::transform(e.begin(), e.end(), e.begin(), tolower_underscore); + } + if (ns) { - m_ss << "<" << name << attrs_str << " xmlns=\"" << ns << "\">"; + m_ss << "<" << e << attrs_str << " xmlns=\"" << ns << "\">"; } else { - m_ss << "<" << name << attrs_str << ">"; + m_ss << "<" << e << attrs_str << ">"; } if (m_pretty) m_ss << "\n"; diff --git a/src/common/Formatter.h b/src/common/Formatter.h index 3c145c8912b2c..3784bdb4cfcd3 100644 --- a/src/common/Formatter.h +++ b/src/common/Formatter.h @@ -128,7 +128,7 @@ namespace ceph { class XMLFormatter : public Formatter { public: static const char *XML_1_DTD; - XMLFormatter(bool pretty = false); + XMLFormatter(bool pretty = false, bool lowercased_underscored = false); void flush(std::ostream& os); void reset(); @@ -160,6 +160,7 @@ namespace ceph { std::stringstream m_ss, m_pending_string; std::deque m_sections; bool m_pretty; + bool m_lowercased_underscored; std::string m_pending_string_name; }; -- 2.39.5