XmlWriter reverts to XML 1.0.

Character encodings that are not valid in XML 1.0 are instead written using C-style escapes
This commit is contained in:
Phil Nash 2017-02-06 16:00:05 +00:00
parent 7db4d8d90c
commit 1f271c9944
5 changed files with 36 additions and 40 deletions

View File

@ -57,8 +57,11 @@ namespace Catch {
default: default:
// Escape control chars - based on contribution by @espenalb in PR #465 and // Escape control chars - based on contribution by @espenalb in PR #465 and
// by @mrpi PR #588 // by @mrpi PR #588
if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) {
os << "&#x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>( c ) << ';'; // see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
os << "\\x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2)
<< static_cast<int>( c );
}
else else
os << c; os << c;
} }
@ -112,20 +115,17 @@ namespace Catch {
XmlWriter() XmlWriter()
: m_tagIsOpen( false ), : m_tagIsOpen( false ),
m_needsNewline( false ), m_needsNewline( false ),
m_os( &Catch::cout() ) m_os( Catch::cout() )
{ {
// We encode control characters, which requires writeDeclaration();
// XML 1.1
// see http://stackoverflow.com/questions/404107/why-are-control-characters-illegal-in-xml-1-0
*m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n";
} }
XmlWriter( std::ostream& os ) XmlWriter( std::ostream& os )
: m_tagIsOpen( false ), : m_tagIsOpen( false ),
m_needsNewline( false ), m_needsNewline( false ),
m_os( &os ) m_os( os )
{ {
*m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n"; writeDeclaration();
} }
~XmlWriter() { ~XmlWriter() {
@ -136,7 +136,7 @@ namespace Catch {
XmlWriter& startElement( std::string const& name ) { XmlWriter& startElement( std::string const& name ) {
ensureTagClosed(); ensureTagClosed();
newlineIfNecessary(); newlineIfNecessary();
stream() << m_indent << '<' << name; m_os << m_indent << '<' << name;
m_tags.push_back( name ); m_tags.push_back( name );
m_indent += " "; m_indent += " ";
m_tagIsOpen = true; m_tagIsOpen = true;
@ -153,25 +153,25 @@ namespace Catch {
newlineIfNecessary(); newlineIfNecessary();
m_indent = m_indent.substr( 0, m_indent.size()-2 ); m_indent = m_indent.substr( 0, m_indent.size()-2 );
if( m_tagIsOpen ) { if( m_tagIsOpen ) {
stream() << "/>"; m_os << "/>";
m_tagIsOpen = false; m_tagIsOpen = false;
} }
else { else {
stream() << m_indent << "</" << m_tags.back() << ">"; m_os << m_indent << "</" << m_tags.back() << ">";
} }
stream() << std::endl; m_os << std::endl;
m_tags.pop_back(); m_tags.pop_back();
return *this; return *this;
} }
XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) { XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) {
if( !name.empty() && !attribute.empty() ) if( !name.empty() && !attribute.empty() )
stream() << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; m_os << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"';
return *this; return *this;
} }
XmlWriter& writeAttribute( std::string const& name, bool attribute ) { XmlWriter& writeAttribute( std::string const& name, bool attribute ) {
stream() << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"';
return *this; return *this;
} }
@ -187,8 +187,8 @@ namespace Catch {
bool tagWasOpen = m_tagIsOpen; bool tagWasOpen = m_tagIsOpen;
ensureTagClosed(); ensureTagClosed();
if( tagWasOpen && indent ) if( tagWasOpen && indent )
stream() << m_indent; m_os << m_indent;
stream() << XmlEncode( text ); m_os << XmlEncode( text );
m_needsNewline = true; m_needsNewline = true;
} }
return *this; return *this;
@ -196,39 +196,35 @@ namespace Catch {
XmlWriter& writeComment( std::string const& text ) { XmlWriter& writeComment( std::string const& text ) {
ensureTagClosed(); ensureTagClosed();
stream() << m_indent << "<!--" << text << "-->"; m_os << m_indent << "<!--" << text << "-->";
m_needsNewline = true; m_needsNewline = true;
return *this; return *this;
} }
XmlWriter& writeBlankLine() { XmlWriter& writeBlankLine() {
ensureTagClosed(); ensureTagClosed();
stream() << '\n'; m_os << '\n';
return *this; return *this;
} }
void setStream( std::ostream& os ) {
m_os = &os;
}
private: private:
XmlWriter( XmlWriter const& ); XmlWriter( XmlWriter const& );
void operator=( XmlWriter const& ); void operator=( XmlWriter const& );
std::ostream& stream() { void writeDeclaration() {
return *m_os; m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
} }
void ensureTagClosed() { void ensureTagClosed() {
if( m_tagIsOpen ) { if( m_tagIsOpen ) {
stream() << ">\n"; m_os << ">\n";
m_tagIsOpen = false; m_tagIsOpen = false;
} }
} }
void newlineIfNecessary() { void newlineIfNecessary() {
if( m_needsNewline ) { if( m_needsNewline ) {
stream() << '\n'; m_os << '\n';
m_needsNewline = false; m_needsNewline = false;
} }
} }
@ -237,7 +233,7 @@ namespace Catch {
bool m_needsNewline; bool m_needsNewline;
std::vector<std::string> m_tags; std::vector<std::string> m_tags;
std::string m_indent; std::string m_indent;
std::ostream* m_os; std::ostream& m_os;
}; };
} }

View File

@ -8106,9 +8106,9 @@ MiscTests.cpp:<line number>
MiscTests.cpp:<line number>: MiscTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( encode( "[\x01]" ) == "[&#x01;]" ) REQUIRE( encode( "[\x01]" ) == "[\\x01]" )
with expansion: with expansion:
"[&#x01;]" == "[&#x01;]" "[\x01]" == "[\x01]"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
XmlEncode XmlEncode
@ -8119,9 +8119,9 @@ MiscTests.cpp:<line number>
MiscTests.cpp:<line number>: MiscTests.cpp:<line number>:
PASSED: PASSED:
REQUIRE( encode( "[\x7F]" ) == "[&#x7F;]" ) REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" )
with expansion: with expansion:
"[&#x7F;]" == "[&#x7F;]" "[\x7F]" == "[\x7F]"
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
atomic if atomic if

View File

@ -1,4 +1,4 @@
<?xml version="1.1" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<testsuites> <testsuites>
<testsuite name="<exe-name>" errors="13" failures="68" tests="931" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}"> <testsuite name="<exe-name>" errors="13" failures="68" tests="931" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
<testcase classname="global" name="# A test name that starts with a #" time="{duration}"/> <testcase classname="global" name="# A test name that starts with a #" time="{duration}"/>

View File

@ -1,4 +1,4 @@
<?xml version="1.1" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Catch name="<exe-name>"> <Catch name="<exe-name>">
<Group name="<exe-name>"> <Group name="<exe-name>">
<TestCase name="# A test name that starts with a #"> <TestCase name="# A test name that starts with a #">
@ -8600,10 +8600,10 @@ there"
<Section name="string with control char (1)"> <Section name="string with control char (1)">
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
encode( "[\x01]" ) == "[&amp;#x01;]" encode( "[\x01]" ) == "[\\x01]"
</Original> </Original>
<Expanded> <Expanded>
"[&amp;#x01;]" == "[&amp;#x01;]" "[\x01]" == "[\x01]"
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/> <OverallResults successes="1" failures="0" expectedFailures="0"/>
@ -8611,10 +8611,10 @@ there"
<Section name="string with control char (x7F)"> <Section name="string with control char (x7F)">
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
encode( "[\x7F]" ) == "[&amp;#x7F;]" encode( "[\x7F]" ) == "[\\x7F]"
</Original> </Original>
<Expanded> <Expanded>
"[&amp;#x7F;]" == "[&amp;#x7F;]" "[\x7F]" == "[\x7F]"
</Expanded> </Expanded>
</Expression> </Expression>
<OverallResults successes="1" failures="0" expectedFailures="0"/> <OverallResults successes="1" failures="0" expectedFailures="0"/>

View File

@ -458,10 +458,10 @@ TEST_CASE( "XmlEncode" ) {
REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't &quot;quote&quot; me on that" ); REQUIRE( encode( stringWithQuotes, Catch::XmlEncode::ForAttributes ) == "don't &quot;quote&quot; me on that" );
} }
SECTION( "string with control char (1)" ) { SECTION( "string with control char (1)" ) {
REQUIRE( encode( "[\x01]" ) == "[&#x01;]" ); REQUIRE( encode( "[\x01]" ) == "[\\x01]" );
} }
SECTION( "string with control char (x7F)" ) { SECTION( "string with control char (x7F)" ) {
REQUIRE( encode( "[\x7F]" ) == "[&#x7F;]" ); REQUIRE( encode( "[\x7F]" ) == "[\\x7F]" );
} }
} }