xml generator: handle '<' and '>' (#263)

This commit is contained in:
Daniel Marjamäki 2009-04-23 21:59:26 +02:00
parent 14eff64194
commit 8e15a9e79d
2 changed files with 30 additions and 3 deletions

View File

@ -118,11 +118,27 @@ std::string ErrorLogger::ErrorMessage::toXML() const
{ {
std::ostringstream xml; std::ostringstream xml;
xml << "<error"; xml << "<error";
if (!_callStack.empty())
{
xml << " file=\"" << _callStack.back().file << "\""; xml << " file=\"" << _callStack.back().file << "\"";
xml << " line=\"" << _callStack.back().line << "\""; xml << " line=\"" << _callStack.back().line << "\"";
}
xml << " id=\"" << _id << "\""; xml << " id=\"" << _id << "\"";
xml << " severity=\"" << _severity << "\""; xml << " severity=\"" << _severity << "\"";
xml << " msg=\"" << _msg << "\"";
// Replace characters in message
std::string m(_msg);
std::string::size_type pos = 0;
while ((pos = m.find_first_of("<>", pos)) != std::string::npos)
{
if (m[pos] == '<')
m.insert(pos+1, "&lt;");
if (m[pos] == '>')
m.insert(pos+1, "&gt;");
m.erase(pos, 1);
}
xml << " msg=\"" << m << "\"";
xml << "/>"; xml << "/>";
return xml.str(); return xml.str();
} }

View File

@ -50,6 +50,8 @@ private:
{ {
TEST_CASE(linenumbers); TEST_CASE(linenumbers);
// TEST_CASE(linenumbers2); // TEST_CASE(linenumbers2);
TEST_CASE(xml);
} }
void linenumbers() void linenumbers()
@ -80,6 +82,15 @@ private:
// Compare results.. // Compare results..
ASSERT_EQUALS("[file.cpp:5]: (error) Memory leak: string\n", errout.str()); ASSERT_EQUALS("[file.cpp:5]: (error) Memory leak: string\n", errout.str());
} }
void xml()
{
// Test the errorlogger..
ErrorLogger::ErrorMessage errmsg;
errmsg._msg = "ab<cd>ef";
ASSERT_EQUALS("<error id=\"\" severity=\"\" msg=\"ab&lt;cd&gt;ef\"/>", errmsg.toXML());
}
}; };
REGISTER_TEST(TestCppcheck) REGISTER_TEST(TestCppcheck)