mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-10-31 12:17:11 +01:00 
			
		
		
		
	| @@ -22,12 +22,10 @@ namespace Catch { | ||||
|     class XmlEncode { | ||||
|     public: | ||||
|         enum ForWhat { ForTextNodes, ForAttributes }; | ||||
|         enum XmlVersion { _1_0, _1_1 }; | ||||
|  | ||||
|         XmlEncode( XmlVersion xmlVersion, std::string const& str, ForWhat forWhat = ForTextNodes ) | ||||
|         XmlEncode( std::string const& str, ForWhat forWhat = ForTextNodes ) | ||||
|         :   m_str( str ), | ||||
|             m_forWhat( forWhat ), | ||||
|             m_xmlVersion( xmlVersion ) | ||||
|             m_forWhat( forWhat ) | ||||
|         {} | ||||
|  | ||||
|         void encodeTo( std::ostream& os ) const { | ||||
| @@ -59,15 +57,8 @@ namespace Catch { | ||||
|                     default: | ||||
|                         // Escape control chars - based on contribution by @espenalb in PR #465 and | ||||
|                         // by @mrpi PR #588 | ||||
|                         if ( ( c >= 0 && c < '\x09' ) || ( c > '\x0D' && c < '\x20') || c=='\x7F' ) { | ||||
|                             if( m_xmlVersion == _1_0 ) | ||||
|                                 // 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 | ||||
|                                 os << "&#x" << std::uppercase << std::hex << std::setfill('0') << std::setw(2) | ||||
|                                    << static_cast<int>( c ) << ';'; | ||||
|                         } | ||||
|                         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 ) << ';'; | ||||
|                         else | ||||
|                             os << c; | ||||
|                 } | ||||
| @@ -82,7 +73,6 @@ namespace Catch { | ||||
|     private: | ||||
|         std::string m_str; | ||||
|         ForWhat m_forWhat; | ||||
|         XmlVersion m_xmlVersion; | ||||
|     }; | ||||
|  | ||||
|     class XmlWriter { | ||||
| @@ -122,19 +112,20 @@ namespace Catch { | ||||
|         XmlWriter() | ||||
|         :   m_tagIsOpen( false ), | ||||
|             m_needsNewline( false ), | ||||
|             m_os( Catch::cout() ), | ||||
|             m_xmlVersion( XmlEncode::_1_0 ) | ||||
|             m_os( &Catch::cout() ) | ||||
|         { | ||||
|             writeDeclaration(); | ||||
|             // We encode control characters, which requires | ||||
|             // 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 ) | ||||
|         :   m_tagIsOpen( false ), | ||||
|             m_needsNewline( false ), | ||||
|             m_os( os ), | ||||
|             m_xmlVersion( XmlEncode::_1_0 ) | ||||
|             m_os( &os ) | ||||
|         { | ||||
|             writeDeclaration(); | ||||
|             *m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n"; | ||||
|         } | ||||
|  | ||||
|         ~XmlWriter() { | ||||
| @@ -145,7 +136,7 @@ namespace Catch { | ||||
|         XmlWriter& startElement( std::string const& name ) { | ||||
|             ensureTagClosed(); | ||||
|             newlineIfNecessary(); | ||||
|             m_os << m_indent << '<' << name; | ||||
|             stream() << m_indent << '<' << name; | ||||
|             m_tags.push_back( name ); | ||||
|             m_indent += "  "; | ||||
|             m_tagIsOpen = true; | ||||
| @@ -162,11 +153,11 @@ namespace Catch { | ||||
|             newlineIfNecessary(); | ||||
|             m_indent = m_indent.substr( 0, m_indent.size()-2 ); | ||||
|             if( m_tagIsOpen ) { | ||||
|                 m_os << "/>\n"; | ||||
|                 stream() << "/>\n"; | ||||
|                 m_tagIsOpen = false; | ||||
|             } | ||||
|             else { | ||||
|                 m_os << m_indent << "</" << m_tags.back() << ">\n"; | ||||
|                 stream() << m_indent << "</" << m_tags.back() << ">\n"; | ||||
|             } | ||||
|             m_tags.pop_back(); | ||||
|             return *this; | ||||
| @@ -174,12 +165,12 @@ namespace Catch { | ||||
|  | ||||
|         XmlWriter& writeAttribute( std::string const& name, std::string const& attribute ) { | ||||
|             if( !name.empty() && !attribute.empty() ) | ||||
|                 m_os << ' ' << name << "=\"" << XmlEncode( m_xmlVersion, attribute, XmlEncode::ForAttributes ) << '"'; | ||||
|                 stream() << ' ' << name << "=\"" << XmlEncode( attribute, XmlEncode::ForAttributes ) << '"'; | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
|         XmlWriter& writeAttribute( std::string const& name, bool attribute ) { | ||||
|             m_os << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; | ||||
|             stream() << ' ' << name << "=\"" << ( attribute ? "true" : "false" ) << '"'; | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
| @@ -195,8 +186,8 @@ namespace Catch { | ||||
|                 bool tagWasOpen = m_tagIsOpen; | ||||
|                 ensureTagClosed(); | ||||
|                 if( tagWasOpen && indent ) | ||||
|                     m_os << m_indent; | ||||
|                 m_os << XmlEncode( m_xmlVersion, text ); | ||||
|                     stream() << m_indent; | ||||
|                 stream() << XmlEncode( text ); | ||||
|                 m_needsNewline = true; | ||||
|             } | ||||
|             return *this; | ||||
| @@ -204,42 +195,39 @@ namespace Catch { | ||||
|  | ||||
|         XmlWriter& writeComment( std::string const& text ) { | ||||
|             ensureTagClosed(); | ||||
|             m_os << m_indent << "<!--" << text << "-->"; | ||||
|             stream() << m_indent << "<!--" << text << "-->"; | ||||
|             m_needsNewline = true; | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
|         XmlWriter& writeBlankLine() { | ||||
|             ensureTagClosed(); | ||||
|             m_os << '\n'; | ||||
|             stream() << '\n'; | ||||
|             return *this; | ||||
|         } | ||||
|  | ||||
|         void setStream( std::ostream& os ) { | ||||
|             m_os = &os; | ||||
|         } | ||||
|  | ||||
|     private: | ||||
|         XmlWriter( XmlWriter const& ); | ||||
|         void operator=( XmlWriter const& ); | ||||
|  | ||||
|         void writeDeclaration() { | ||||
|             switch( m_xmlVersion ) { | ||||
|                 case XmlEncode::_1_0: | ||||
|                     m_os << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; | ||||
|                     break; | ||||
|                 case XmlEncode::_1_1: | ||||
|                     m_os << "<?xml version=\"1.1\" encoding=\"UTF-8\"?>\n"; | ||||
|                     break; | ||||
|             } | ||||
|         std::ostream& stream() { | ||||
|             return *m_os; | ||||
|         } | ||||
|  | ||||
|         void ensureTagClosed() { | ||||
|             if( m_tagIsOpen ) { | ||||
|                 m_os << ">\n"; | ||||
|                 stream() << ">\n"; | ||||
|                 m_tagIsOpen = false; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         void newlineIfNecessary() { | ||||
|             if( m_needsNewline ) { | ||||
|                 m_os << '\n'; | ||||
|                 stream() << '\n'; | ||||
|                 m_needsNewline = false; | ||||
|             } | ||||
|         } | ||||
| @@ -248,8 +236,7 @@ namespace Catch { | ||||
|         bool m_needsNewline; | ||||
|         std::vector<std::string> m_tags; | ||||
|         std::string m_indent; | ||||
|         std::ostream& m_os; | ||||
|         XmlEncode::XmlVersion m_xmlVersion; | ||||
|         std::ostream* m_os; | ||||
|     }; | ||||
|  | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Phil Nash
					Phil Nash