~ubuntu-branches/ubuntu/jaunty/psi/jaunty

« back to all changes in this revision

Viewing changes to third-party/cppunit/cppunit/src/cppunit/XmlElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <cppunit/tools/StringTools.h>
 
2
#include <cppunit/tools/XmlElement.h>
 
3
#include <stdexcept>
 
4
 
 
5
 
 
6
CPPUNIT_NS_BEGIN
 
7
 
 
8
  
 
9
XmlElement::XmlElement( std::string elementName,
 
10
                        std::string content ) 
 
11
  : m_name( elementName )
 
12
  , m_content( content )
 
13
{
 
14
}
 
15
 
 
16
    
 
17
XmlElement::XmlElement( std::string elementName,
 
18
                        int numericContent )
 
19
  : m_name( elementName )
 
20
{
 
21
  setContent( numericContent );
 
22
}
 
23
 
 
24
 
 
25
XmlElement::~XmlElement()
 
26
{
 
27
  Elements::iterator itNode = m_elements.begin();
 
28
  while ( itNode != m_elements.end() )
 
29
  {
 
30
    XmlElement *element = *itNode++;
 
31
    delete element;
 
32
  }
 
33
}
 
34
 
 
35
 
 
36
std::string 
 
37
XmlElement::name() const
 
38
{
 
39
  return m_name;
 
40
}
 
41
 
 
42
 
 
43
std::string 
 
44
XmlElement::content() const
 
45
{
 
46
  return m_content;
 
47
}
 
48
 
 
49
 
 
50
void 
 
51
XmlElement::setName( const std::string &name )
 
52
{
 
53
  m_name = name;
 
54
}
 
55
 
 
56
 
 
57
void 
 
58
XmlElement::setContent( const std::string &content )
 
59
{
 
60
  m_content = content;
 
61
}
 
62
 
 
63
 
 
64
void 
 
65
XmlElement::setContent( int numericContent )
 
66
{
 
67
  m_content = StringTools::toString( numericContent );
 
68
}
 
69
 
 
70
 
 
71
void 
 
72
XmlElement::addAttribute( std::string attributeName,
 
73
                          std::string value  )
 
74
{
 
75
  m_attributes.push_back( Attribute( attributeName, value ) );
 
76
}
 
77
 
 
78
 
 
79
void 
 
80
XmlElement::addAttribute( std::string attributeName,
 
81
                          int numericValue )
 
82
{
 
83
  addAttribute( attributeName, StringTools::toString( numericValue ) );
 
84
}
 
85
 
 
86
 
 
87
void 
 
88
XmlElement::addElement( XmlElement *node )
 
89
{
 
90
  m_elements.push_back( node );
 
91
}
 
92
 
 
93
 
 
94
int 
 
95
XmlElement::elementCount() const
 
96
{
 
97
  return m_elements.size();
 
98
}
 
99
 
 
100
 
 
101
XmlElement *
 
102
XmlElement::elementAt( int index ) const
 
103
{
 
104
  if ( index < 0  ||  index >= elementCount() )
 
105
    throw std::invalid_argument( "XmlElement::elementAt(), out of range index" );
 
106
 
 
107
  return m_elements[ index ];
 
108
}
 
109
 
 
110
 
 
111
XmlElement *
 
112
XmlElement::elementFor( const std::string &name ) const
 
113
{
 
114
  Elements::const_iterator itElement = m_elements.begin();
 
115
  for ( ; itElement != m_elements.end(); ++itElement )
 
116
  {
 
117
    if ( (*itElement)->name() == name )
 
118
      return *itElement;
 
119
  }
 
120
 
 
121
  throw std::invalid_argument( "XmlElement::elementFor(), not matching child element found" );
 
122
  return NULL;  // make some compilers happy.
 
123
}
 
124
 
 
125
 
 
126
std::string 
 
127
XmlElement::toString( const std::string &indent ) const
 
128
{
 
129
  std::string element( indent );
 
130
  element += "<";
 
131
  element += m_name;
 
132
  if ( !m_attributes.empty() )
 
133
  {
 
134
    element += " ";
 
135
    element += attributesAsString();
 
136
  }
 
137
  element += ">";
 
138
 
 
139
  if ( !m_elements.empty() )
 
140
  {
 
141
    element += "\n";
 
142
 
 
143
    std::string subNodeIndent( indent + "  " );
 
144
    Elements::const_iterator itNode = m_elements.begin();
 
145
    while ( itNode != m_elements.end() )
 
146
    {
 
147
      const XmlElement *node = *itNode++;
 
148
      element += node->toString( subNodeIndent );
 
149
    }
 
150
 
 
151
    element += indent;
 
152
  }
 
153
 
 
154
  if ( !m_content.empty() )
 
155
  {
 
156
    element += escape( m_content );
 
157
    if ( !m_elements.empty() )
 
158
    {
 
159
      element += "\n";
 
160
      element += indent;
 
161
    }
 
162
  }
 
163
 
 
164
  element += "</";
 
165
  element += m_name;
 
166
  element += ">\n";
 
167
 
 
168
  return element;
 
169
}
 
170
 
 
171
 
 
172
std::string 
 
173
XmlElement::attributesAsString() const
 
174
{
 
175
  std::string attributes;
 
176
  Attributes::const_iterator itAttribute = m_attributes.begin();
 
177
  while ( itAttribute != m_attributes.end() )
 
178
  {
 
179
    if ( !attributes.empty() )
 
180
       attributes += " ";
 
181
 
 
182
    const Attribute &attribute = *itAttribute++;
 
183
    attributes += attribute.first;
 
184
    attributes += "=\"";
 
185
    attributes += escape( attribute.second );
 
186
    attributes += "\"";
 
187
  }
 
188
  return attributes;
 
189
}
 
190
 
 
191
 
 
192
std::string 
 
193
XmlElement::escape( std::string value ) const
 
194
{
 
195
  std::string escaped;
 
196
  for ( unsigned int index =0; index < value.length(); ++index )
 
197
  {
 
198
    char c = value[index ];
 
199
    switch ( c )    // escape all predefined XML entity (safe?)
 
200
    {
 
201
    case '<': 
 
202
      escaped += "&lt;";
 
203
      break;
 
204
    case '>': 
 
205
      escaped += "&gt;";
 
206
      break;
 
207
    case '&': 
 
208
      escaped += "&amp;";
 
209
      break;
 
210
    case '\'': 
 
211
      escaped += "&apos;";
 
212
      break;
 
213
    case '"': 
 
214
      escaped += "&quot;";
 
215
      break;
 
216
    default:
 
217
      escaped += c;
 
218
    }
 
219
  }
 
220
  
 
221
  return escaped;
 
222
}
 
223
 
 
224
 
 
225
CPPUNIT_NS_END
 
226