summaryrefslogtreecommitdiff
path: root/src/cppunit/XmlElement.cpp
diff options
context:
space:
mode:
authorBaptiste Lepilleur <gaiacrtn@free.fr>2002-05-23 17:38:39 +0000
committerBaptiste Lepilleur <gaiacrtn@free.fr>2002-05-23 17:38:39 +0000
commit5ad4640702a80078748b38ebaeda37e69dce1189 (patch)
tree9d1ecf8d28f0e9397c2f90565d96ccda6d08b98e /src/cppunit/XmlElement.cpp
parent7edd0684368ed3c43fe2707d1d34d6b7590d9fd6 (diff)
downloadcppunit-5ad4640702a80078748b38ebaeda37e69dce1189.tar.gz
Include/cppunit/XmlOutputter.
include/cppunit/XmlOutputter.h: * src/cppunit/XmlOutputter.cpp: extracted class XmlOutputter::Node to XmlElement. Extracted xml 'prolog' generation to XmlDocument. * include/cppunit/tools/XmlElement.h: * src/cppunit/tools/XmlElement.cpp: added, extracted from XmlOutputter::Node. * include/cppunit/tools/XmlDocument.h: * src/cppunit/tools/XmlDocument.cpp: added, extracted from XmlOutputter. Handle XML document prolog (encoding & style-sheet) and manage the root element. * src/DllPlugInTester/DllPlugInTester.cpp: bug fix, flag --xsl was ignored. * examples/cppunittest/XmlOutputterTest.h: * examples/cppunittest/XmlOutputterTest.cpp: updated for XmlOuputter changes. extracted tests for XmlOutputter::Node to XmlElementTest * examples/cppunittest/XmlElementTest.h: * examples/cppunittest/XmlElementTest.cpp: added, tests extracted from XmlOutputterTest.
Diffstat (limited to 'src/cppunit/XmlElement.cpp')
-rw-r--r--src/cppunit/XmlElement.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/src/cppunit/XmlElement.cpp b/src/cppunit/XmlElement.cpp
new file mode 100644
index 0000000..4a01405
--- /dev/null
+++ b/src/cppunit/XmlElement.cpp
@@ -0,0 +1,161 @@
+#include <cppunit/tools/XmlElement.h>
+
+
+namespace CppUnit
+{
+
+
+XmlElement::XmlElement( std::string elementName,
+ std::string content )
+ : m_name( elementName )
+ , m_content( content )
+{
+}
+
+
+XmlElement::XmlElement( std::string elementName,
+ int numericContent )
+ : m_name( elementName )
+{
+ m_content = asString( numericContent );
+}
+
+
+XmlElement::~XmlElement()
+{
+ Elements::iterator itNode = m_elements.begin();
+ while ( itNode != m_elements.end() )
+ delete *itNode++;
+}
+
+
+void
+XmlElement::addAttribute( std::string attributeName,
+ std::string value )
+{
+ m_attributes.push_back( Attribute( attributeName, value ) );
+}
+
+
+void
+XmlElement::addAttribute( std::string attributeName,
+ int numericValue )
+{
+ addAttribute( attributeName, asString( numericValue ) );
+}
+
+
+void
+XmlElement::addNode( XmlElement *node )
+{
+ m_elements.push_back( node );
+}
+
+
+std::string
+XmlElement::toString( const std::string &indent ) const
+{
+ std::string element( indent );
+ element += "<";
+ element += m_name;
+ if ( !m_attributes.empty() )
+ {
+ element += " ";
+ element += attributesAsString();
+ }
+ element += ">";
+
+ if ( !m_elements.empty() )
+ {
+ element += "\n";
+
+ std::string subNodeIndent( indent + " " );
+ Elements::const_iterator itNode = m_elements.begin();
+ while ( itNode != m_elements.end() )
+ {
+ const XmlElement *node = *itNode++;
+ element += node->toString( subNodeIndent );
+ }
+
+ element += indent;
+ }
+
+ if ( !m_content.empty() )
+ {
+ element += escape( m_content );
+ if ( !m_elements.empty() )
+ {
+ element += "\n";
+ element += indent;
+ }
+ }
+
+ element += "</";
+ element += m_name;
+ element += ">\n";
+
+ return element;
+}
+
+
+std::string
+XmlElement::attributesAsString() const
+{
+ std::string attributes;
+ Attributes::const_iterator itAttribute = m_attributes.begin();
+ while ( itAttribute != m_attributes.end() )
+ {
+ const Attribute &attribute = *itAttribute++;
+ attributes += attribute.first;
+ attributes += "=\"";
+ attributes += escape( attribute.second );
+ attributes += "\"";
+ }
+ return attributes;
+}
+
+
+std::string
+XmlElement::escape( std::string value ) const
+{
+ std::string escaped;
+ for ( int index =0; index < value.length(); ++index )
+ {
+ char c = value[index ];
+ switch ( c ) // escape all predefined XML entity (safe?)
+ {
+ case '<':
+ escaped += "&lt;";
+ break;
+ case '>':
+ escaped += "&gt;";
+ break;
+ case '&':
+ escaped += "&amp;";
+ break;
+ case '\'':
+ escaped += "&apos;";
+ break;
+ case '"':
+ escaped += "&quot;";
+ break;
+ default:
+ escaped += c;
+ }
+ }
+
+ return escaped;
+}
+
+// should be somewhere else... Future CppUnit::String ?
+std::string
+XmlElement::asString( int value )
+{
+ OStringStream stream;
+ stream << value;
+ return stream.str();
+}
+
+
+} // namespace CppUnit
+