2014-01-09 13:48:43 +00:00
|
|
|
/*
|
|
|
|
* \brief Utility for generating XML
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2014-01-07
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2014-2017 Genode Labs GmbH
|
2014-01-09 13:48:43 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2014-01-09 13:48:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__UTIL__XML_GENERATOR_H_
|
|
|
|
#define _INCLUDE__UTIL__XML_GENERATOR_H_
|
|
|
|
|
|
|
|
#include <util/string.h>
|
2015-09-22 15:09:22 +00:00
|
|
|
#include <util/print_lines.h>
|
2014-01-09 13:48:43 +00:00
|
|
|
#include <base/snprintf.h>
|
|
|
|
|
|
|
|
namespace Genode { class Xml_generator; }
|
|
|
|
|
|
|
|
|
|
|
|
class Genode::Xml_generator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception type
|
|
|
|
*/
|
|
|
|
class Buffer_exceeded { };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffer descriptor where the XML output goes to
|
|
|
|
*
|
2015-03-20 16:50:41 +00:00
|
|
|
* All 'append' methods may throw a 'Buffer_exceeded' exception.
|
2014-01-09 13:48:43 +00:00
|
|
|
*/
|
|
|
|
class Out_buffer
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
char *_dst;
|
|
|
|
size_t _capacity;
|
|
|
|
size_t _used = 0;
|
|
|
|
|
|
|
|
void _check_advance(size_t const len) const {
|
|
|
|
if (_used + len > _capacity)
|
|
|
|
throw Buffer_exceeded(); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Out_buffer(char *dst, size_t capacity)
|
|
|
|
: _dst(dst), _capacity(capacity) { }
|
|
|
|
|
|
|
|
void advance(size_t const len)
|
|
|
|
{
|
|
|
|
_check_advance(len);
|
|
|
|
_used += len;
|
|
|
|
}
|
|
|
|
|
2018-09-07 09:20:13 +00:00
|
|
|
void undo_append(size_t const len) {
|
|
|
|
_used = len < _used ? _used - len : 0; }
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
/**
|
|
|
|
* Append character
|
|
|
|
*/
|
|
|
|
void append(char const c)
|
|
|
|
{
|
|
|
|
_check_advance(1);
|
|
|
|
_dst[_used] = c;
|
|
|
|
advance(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append character 'n' times
|
|
|
|
*/
|
|
|
|
void append(char const c, size_t n) {
|
|
|
|
for (; n--; append(c)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append character buffer
|
|
|
|
*/
|
|
|
|
void append(char const *src, size_t len) {
|
|
|
|
for (; len--; append(*src++)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append null-terminated string
|
|
|
|
*/
|
|
|
|
void append(char const *src) { append(src, strlen(src)); }
|
|
|
|
|
2015-09-25 13:49:33 +00:00
|
|
|
/**
|
|
|
|
* Append character, sanitize it if needed
|
|
|
|
*/
|
|
|
|
void append_sanitized(char const c)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 0: append("�"); break;
|
|
|
|
case '>': append(">"); break;
|
|
|
|
case '<': append("<"); break;
|
|
|
|
case '&': append("&"); break;
|
|
|
|
case '"': append("""); break;
|
|
|
|
case '\'': append("'"); break;
|
|
|
|
default: append(c); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Append character buffer, sanitize characters if needed
|
|
|
|
*/
|
|
|
|
void append_sanitized(char const *src, size_t len)
|
|
|
|
{
|
|
|
|
for (; len--; append_sanitized(*src++));
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
/**
|
|
|
|
* Return unused part of the buffer
|
|
|
|
*/
|
|
|
|
Out_buffer remainder() const {
|
|
|
|
return Out_buffer(_dst + _used, _capacity - _used); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert gap into already populated part of the buffer
|
|
|
|
*
|
|
|
|
* \return empty buffer spanning the gap
|
|
|
|
*/
|
2021-12-02 10:21:14 +00:00
|
|
|
Out_buffer insert_gap(size_t const at, size_t const len)
|
2014-01-09 13:48:43 +00:00
|
|
|
{
|
|
|
|
/* don't allow the insertion into non-populated part */
|
|
|
|
if (at > _used)
|
|
|
|
return Out_buffer(_dst + at, 0);
|
|
|
|
|
|
|
|
_check_advance(len);
|
|
|
|
memmove(_dst + at + len, _dst + at, _used - at);
|
|
|
|
advance(len);
|
|
|
|
|
|
|
|
return Out_buffer(_dst + at, len);
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
bool has_trailing_newline() const
|
|
|
|
{
|
|
|
|
return (_used > 1) && (_dst[_used - 1] == '\n');
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
/**
|
|
|
|
* Return number of unused bytes of the buffer
|
|
|
|
*/
|
|
|
|
size_t used() const { return _used; }
|
2015-09-22 15:09:22 +00:00
|
|
|
|
|
|
|
void discard_trailing_whitespace()
|
|
|
|
{
|
|
|
|
for (; _used > 0 && is_whitespace(_dst[_used - 1]); _used--);
|
|
|
|
}
|
2014-01-09 13:48:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class Node
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indentation level of node
|
|
|
|
*/
|
|
|
|
unsigned const _indent_level;
|
|
|
|
|
|
|
|
Node * const _parent_node = 0;
|
2018-09-07 09:20:13 +00:00
|
|
|
bool const _parent_was_indented;
|
|
|
|
bool const _parent_had_content;
|
2014-01-09 13:48:43 +00:00
|
|
|
|
|
|
|
Out_buffer _out_buffer;
|
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
bool _has_content = false;
|
|
|
|
bool _is_indented = false;
|
2014-01-09 13:48:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cursor position of next attribute to insert
|
|
|
|
*/
|
2021-12-02 10:21:14 +00:00
|
|
|
size_t _attr_offset = 0;
|
2014-01-09 13:48:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called by sub node
|
2015-09-22 15:09:22 +00:00
|
|
|
*
|
|
|
|
* \param indented if true, the returned buffer and the
|
|
|
|
* end tag start at a fresh line. This is the
|
|
|
|
* case if the content buffer contains sub
|
|
|
|
* nodes. But when inserting raw content,
|
|
|
|
* such additional whitespaces are not desired.
|
2014-01-09 13:48:43 +00:00
|
|
|
*/
|
2015-09-22 15:09:22 +00:00
|
|
|
Out_buffer _content_buffer(bool indented)
|
2014-01-09 13:48:43 +00:00
|
|
|
{
|
2015-09-22 15:09:22 +00:00
|
|
|
if (!_has_content)
|
2014-01-09 13:48:43 +00:00
|
|
|
_out_buffer.append(">");
|
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
if (indented)
|
|
|
|
_out_buffer.append("\n");
|
2014-01-09 13:48:43 +00:00
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
_has_content = true;
|
|
|
|
_is_indented = indented;
|
2014-01-09 13:48:43 +00:00
|
|
|
|
|
|
|
return _out_buffer.remainder();
|
|
|
|
}
|
|
|
|
|
2018-09-07 09:20:13 +00:00
|
|
|
void _undo_content_buffer(bool indented,
|
|
|
|
bool was_indented,
|
|
|
|
bool had_content)
|
|
|
|
{
|
|
|
|
_is_indented = was_indented;
|
|
|
|
_has_content = had_content;
|
|
|
|
|
|
|
|
if (indented)
|
|
|
|
_out_buffer.undo_append(1);
|
|
|
|
|
|
|
|
if (!_has_content)
|
|
|
|
_out_buffer.undo_append(1);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
/**
|
|
|
|
* Called by sub node
|
|
|
|
*/
|
|
|
|
void _commit_content(Out_buffer content_buffer)
|
|
|
|
{
|
|
|
|
_out_buffer.advance(content_buffer.used());
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:32:11 +00:00
|
|
|
/*
|
|
|
|
* Helper used to pass the 'fn' argument of the public 'Node'
|
|
|
|
* constructor through an ABI to the implementation of the
|
|
|
|
* private 'Node' constructor.
|
|
|
|
*/
|
|
|
|
struct _Fn : Interface { virtual void call() const = 0; };
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct _Typed_fn : _Fn
|
|
|
|
{
|
|
|
|
T const &_fn;
|
|
|
|
_Typed_fn(T const &fn) : _fn(fn) { }
|
|
|
|
void call() const override { _fn(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
Node(Xml_generator &, char const *, _Fn const &);
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
void insert_attribute(char const *name, char const *value)
|
|
|
|
{
|
|
|
|
/* ' ' + name + '=' + '"' + value + '"' */
|
|
|
|
size_t const gap = 1 + strlen(name) + 1 + 1 + strlen(value) + 1;
|
|
|
|
|
|
|
|
Out_buffer dst = _out_buffer.insert_gap(_attr_offset, gap);
|
|
|
|
dst.append(' ');
|
|
|
|
dst.append(name);
|
|
|
|
dst.append("=\"");
|
2015-09-25 13:49:33 +00:00
|
|
|
dst.append(value, strlen(value));
|
2014-01-09 13:48:43 +00:00
|
|
|
dst.append("\"");
|
|
|
|
|
|
|
|
_attr_offset += gap;
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
void append(char const *src, size_t src_len)
|
|
|
|
{
|
|
|
|
Out_buffer content_buffer = _content_buffer(false);
|
|
|
|
content_buffer.append(src, src_len);
|
2015-09-25 13:49:33 +00:00
|
|
|
_commit_content(content_buffer);
|
|
|
|
}
|
2015-09-22 15:09:22 +00:00
|
|
|
|
2019-02-15 18:27:27 +00:00
|
|
|
/**
|
|
|
|
* Append character, sanitize it if needed
|
|
|
|
*/
|
|
|
|
void append_sanitized(char const c)
|
|
|
|
{
|
|
|
|
Out_buffer content_buffer = _content_buffer(false);
|
|
|
|
content_buffer.append_sanitized(c);
|
|
|
|
_commit_content(content_buffer);
|
|
|
|
}
|
|
|
|
|
2015-09-25 13:49:33 +00:00
|
|
|
void append_sanitized(char const *src, size_t src_len)
|
|
|
|
{
|
|
|
|
Out_buffer content_buffer = _content_buffer(false);
|
|
|
|
content_buffer.append_sanitized(src, src_len);
|
2015-09-22 15:09:22 +00:00
|
|
|
_commit_content(content_buffer);
|
|
|
|
}
|
|
|
|
|
2021-03-11 20:32:11 +00:00
|
|
|
template <typename FN>
|
|
|
|
Node(Xml_generator &xml, char const *name, FN const &fn)
|
2014-01-09 13:48:43 +00:00
|
|
|
:
|
2021-03-11 20:32:11 +00:00
|
|
|
Node(xml, name, static_cast<_Fn const &>(_Typed_fn<FN>(fn)))
|
|
|
|
{ }
|
2018-09-07 09:20:13 +00:00
|
|
|
|
|
|
|
bool has_content() { return _has_content; }
|
|
|
|
bool is_indented() { return _is_indented; }
|
2014-01-09 13:48:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Out_buffer _out_buffer;
|
|
|
|
Node *_curr_node = 0;
|
|
|
|
unsigned _curr_indent = 0;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
template <typename FUNC>
|
|
|
|
Xml_generator(char *dst, size_t dst_len,
|
|
|
|
char const *name, FUNC const &func)
|
|
|
|
:
|
|
|
|
_out_buffer(dst, dst_len)
|
|
|
|
{
|
2015-06-09 19:48:50 +00:00
|
|
|
if (dst) {
|
|
|
|
node(name, func);
|
|
|
|
_out_buffer.append('\n');
|
2018-09-07 11:50:54 +00:00
|
|
|
_out_buffer.append('\0');
|
2015-06-09 19:48:50 +00:00
|
|
|
}
|
2014-01-09 13:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename FUNC>
|
|
|
|
void node(char const *name, FUNC const &func = [] () { } )
|
|
|
|
{
|
|
|
|
Node(*this, name, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void node(char const *name) { Node(*this, name, [] () { }); }
|
|
|
|
|
|
|
|
void attribute(char const *name, char const *str)
|
|
|
|
{
|
|
|
|
_curr_node->insert_attribute(name, str);
|
|
|
|
}
|
|
|
|
|
2015-11-22 11:25:23 +00:00
|
|
|
template <size_t N>
|
|
|
|
void attribute(char const *name, String<N> const &str)
|
|
|
|
{
|
|
|
|
_curr_node->insert_attribute(name, str.string());
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:36:27 +00:00
|
|
|
void attribute(char const *name, bool value)
|
|
|
|
{
|
|
|
|
_curr_node->insert_attribute(name, value ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
void attribute(char const *name, long long value)
|
|
|
|
{
|
|
|
|
char buf[64];
|
|
|
|
Genode::snprintf(buf, sizeof(buf), "%lld", value);
|
|
|
|
_curr_node->insert_attribute(name, buf);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
void attribute(char const *name, long value)
|
2015-12-16 11:36:27 +00:00
|
|
|
{
|
|
|
|
attribute(name, static_cast<long long>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void attribute(char const *name, int value)
|
|
|
|
{
|
|
|
|
attribute(name, static_cast<long long>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void attribute(char const *name, unsigned long long value)
|
2014-01-09 13:48:43 +00:00
|
|
|
{
|
|
|
|
char buf[64];
|
2015-12-16 11:36:27 +00:00
|
|
|
Genode::snprintf(buf, sizeof(buf), "%llu", value);
|
2014-01-09 13:48:43 +00:00
|
|
|
_curr_node->insert_attribute(name, buf);
|
|
|
|
}
|
|
|
|
|
2015-12-16 11:36:27 +00:00
|
|
|
void attribute(char const *name, unsigned long value)
|
|
|
|
{
|
|
|
|
attribute(name, static_cast<unsigned long long>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void attribute(char const *name, unsigned value)
|
|
|
|
{
|
|
|
|
attribute(name, static_cast<unsigned long long>(value));
|
|
|
|
}
|
|
|
|
|
2018-03-02 10:54:18 +00:00
|
|
|
void attribute(char const *name, double value)
|
|
|
|
{
|
|
|
|
String<64> buf(value);
|
|
|
|
_curr_node->insert_attribute(name, buf.string());
|
|
|
|
}
|
|
|
|
|
2015-09-22 15:09:22 +00:00
|
|
|
/**
|
|
|
|
* Append content to XML node
|
|
|
|
*
|
|
|
|
* This method must not be followed by calls of 'attribute'.
|
|
|
|
*/
|
|
|
|
void append(char const *str, size_t str_len = ~0UL)
|
|
|
|
{
|
|
|
|
_curr_node->append(str, str_len == ~0UL ? strlen(str) : str_len);
|
|
|
|
}
|
|
|
|
|
2015-09-25 13:49:33 +00:00
|
|
|
/**
|
|
|
|
* Append sanitized content to XML node
|
|
|
|
*
|
|
|
|
* This method must not be followed by calls of 'attribute'.
|
|
|
|
*/
|
|
|
|
void append_sanitized(char const *str, size_t str_len = ~0UL)
|
|
|
|
{
|
|
|
|
_curr_node->append_sanitized(str, str_len == ~0UL ? strlen(str) : str_len);
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:27:27 +00:00
|
|
|
/**
|
|
|
|
* Append printable objects to XML node as sanitized content
|
|
|
|
*
|
|
|
|
* This method must not be followed by calls of 'attribute'.
|
|
|
|
*/
|
|
|
|
template <typename... ARGS>
|
|
|
|
void append_content(ARGS &&... args)
|
|
|
|
{
|
|
|
|
struct Node_output : Genode::Output
|
|
|
|
{
|
|
|
|
Node &node; Node_output(Node &n) : node(n) { }
|
|
|
|
|
|
|
|
/******************************
|
|
|
|
** Genode::Output interface **
|
|
|
|
******************************/
|
|
|
|
|
|
|
|
void out_char(char c) override {
|
|
|
|
node.append_sanitized(c); }
|
|
|
|
|
|
|
|
void out_string(char const *str, size_t n) override {
|
|
|
|
node.append_sanitized(str, n); }
|
|
|
|
|
|
|
|
} output { *_curr_node };
|
|
|
|
|
|
|
|
Output::out_args(output, args...);
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:48:43 +00:00
|
|
|
size_t used() const { return _out_buffer.used(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _INCLUDE__UTIL__XML_GENERATOR_H_ */
|