Formatter: add a dump_format_ns to base Formatter

`dump_format_ns` is a generic formatter to dump a simple format along
with a namespace. It is `dump_format` with an optional ns. This also
extends the XML formatter with this functionality. This allows creation
of xml tags with ns and a specified format. The JSON Format doesn't
define a ns, and here the functionality is exactly same as that of
`dump_format`

Signed-off-by: Abhishek Lekshmanan <abhishek.lekshmanan@ril.com>
This commit is contained in:
Abhishek Lekshmanan 2014-08-27 12:02:19 +05:30
parent 4dbc4c412b
commit 2fb51b2b58
2 changed files with 38 additions and 0 deletions

View File

@ -255,6 +255,21 @@ void JSONFormatter::dump_format(const char *name, const char *fmt, ...)
print_quoted_string(buf);
}
void JSONFormatter:: dump_format_ns(const char *name, const char *ns, const char *fmt, ...)
{
// ignore the namespace for now
char buf[LARGE_SIZE];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, LARGE_SIZE, fmt, ap);
va_end(ap);
print_name(name);
print_quoted_string(buf);
}
void JSONFormatter::dump_format_unquoted(const char *name, const char *fmt, ...)
{
char buf[LARGE_SIZE];
@ -417,6 +432,26 @@ void XMLFormatter::dump_format(const char *name, const char *fmt, ...)
m_ss << "\n";
}
void XMLFormatter::dump_format_ns(const char* name, const char *ns, const char *fmt, ...)
{
char buf[LARGE_SIZE];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, LARGE_SIZE, fmt, ap);
va_end(ap);
std::string e(name);
print_spaces();
if (ns) {
m_ss << "<" << e << " xmlns=\"" << ns << "\">" << buf << "</" << e << ">";
} else {
m_ss << "<" << e << ">" << buf << "</" << e << ">";
}
if (m_pretty)
m_ss << "\n";
}
void XMLFormatter::dump_format_unquoted(const char *name, const char *fmt, ...)
{
char buf[LARGE_SIZE];

View File

@ -49,6 +49,7 @@ class Formatter {
}
virtual std::ostream& dump_stream(const char *name) = 0;
virtual void dump_format(const char *name, const char *fmt, ...) = 0;
virtual void dump_format_ns(const char *name, const char *ns, const char *fmt, ...) = 0;
virtual void dump_format_unquoted(const char *name, const char *fmt, ...) = 0;
virtual int get_len() const = 0;
virtual void write_raw_data(const char *data) = 0;
@ -85,6 +86,7 @@ class JSONFormatter : public Formatter {
std::ostream& dump_stream(const char *name);
void dump_format(const char *name, const char *fmt, ...);
void dump_format_unquoted(const char *name, const char *fmt, ...);
void dump_format_ns(const char*name, const char *ns, const char *fmt, ...);
int get_len() const;
void write_raw_data(const char *data);
@ -125,6 +127,7 @@ class XMLFormatter : public Formatter {
void dump_string(const char *name, std::string s);
std::ostream& dump_stream(const char *name);
void dump_format(const char *name, const char *fmt, ...);
void dump_format_ns(const char *name, const char *ns, const char *fmt, ...);
void dump_format_unquoted(const char *name, const char *fmt, ...);
int get_len() const;
void write_raw_data(const char *data);