From 83b034d47728a7519128b2777ac95fa0b558f882 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Tue, 13 Jan 2015 11:19:04 +0100 Subject: put entity escaping in XML serialization instead of DemoOutput. Also had to comment out graphviz visualization in DemoOutputTest because it did not work for me. --- src/core/XML.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'src/core') diff --git a/src/core/XML.cpp b/src/core/XML.cpp index a610daa..9c23e3b 100644 --- a/src/core/XML.cpp +++ b/src/core/XML.cpp @@ -16,6 +16,8 @@ along with this program. If not, see . */ +#include + #include #include "XML.hpp" @@ -71,6 +73,33 @@ void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) } } +static std::string escapePredefinedEntities(const std::string &input) +{ + std::stringstream ss; + for (const char &c : input) { + switch (c) { + case '<': + ss << "<"; + break; + case '>': + ss << ">"; + break; + case '&': + ss << "&"; + break; + case '\'': + ss << "'"; + break; + case '\"': + ss << """; + break; + default: + ss << c; + } + } + return std::move(ss.str()); +} + void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) { if (pretty) { @@ -78,7 +107,7 @@ void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) out << '\t'; } } - out << text; + out << escapePredefinedEntities(text); if (pretty) { out << '\n'; } -- cgit v1.2.3