1
#include <cppunit/tools/StringTools.h>
2
#include <cppunit/tools/XmlElement.h>
9
XmlElement::XmlElement( std::string elementName,
11
: m_name( elementName )
12
, m_content( content )
17
XmlElement::XmlElement( std::string elementName,
19
: m_name( elementName )
21
setContent( numericContent );
25
XmlElement::~XmlElement()
27
Elements::iterator itNode = m_elements.begin();
28
while ( itNode != m_elements.end() )
30
XmlElement *element = *itNode++;
37
XmlElement::name() const
44
XmlElement::content() const
51
XmlElement::setName( const std::string &name )
58
XmlElement::setContent( const std::string &content )
65
XmlElement::setContent( int numericContent )
67
m_content = StringTools::toString( numericContent );
72
XmlElement::addAttribute( std::string attributeName,
75
m_attributes.push_back( Attribute( attributeName, value ) );
80
XmlElement::addAttribute( std::string attributeName,
83
addAttribute( attributeName, StringTools::toString( numericValue ) );
88
XmlElement::addElement( XmlElement *node )
90
m_elements.push_back( node );
95
XmlElement::elementCount() const
97
return m_elements.size();
102
XmlElement::elementAt( int index ) const
104
if ( index < 0 || index >= elementCount() )
105
throw std::invalid_argument( "XmlElement::elementAt(), out of range index" );
107
return m_elements[ index ];
112
XmlElement::elementFor( const std::string &name ) const
114
Elements::const_iterator itElement = m_elements.begin();
115
for ( ; itElement != m_elements.end(); ++itElement )
117
if ( (*itElement)->name() == name )
121
throw std::invalid_argument( "XmlElement::elementFor(), not matching child element found" );
122
return NULL; // make some compilers happy.
127
XmlElement::toString( const std::string &indent ) const
129
std::string element( indent );
132
if ( !m_attributes.empty() )
135
element += attributesAsString();
139
if ( !m_elements.empty() )
143
std::string subNodeIndent( indent + " " );
144
Elements::const_iterator itNode = m_elements.begin();
145
while ( itNode != m_elements.end() )
147
const XmlElement *node = *itNode++;
148
element += node->toString( subNodeIndent );
154
if ( !m_content.empty() )
156
element += escape( m_content );
157
if ( !m_elements.empty() )
173
XmlElement::attributesAsString() const
175
std::string attributes;
176
Attributes::const_iterator itAttribute = m_attributes.begin();
177
while ( itAttribute != m_attributes.end() )
179
if ( !attributes.empty() )
182
const Attribute &attribute = *itAttribute++;
183
attributes += attribute.first;
185
attributes += escape( attribute.second );
193
XmlElement::escape( std::string value ) const
196
for ( unsigned int index =0; index < value.length(); ++index )
198
char c = value[index ];
199
switch ( c ) // escape all predefined XML entity (safe?)