summaryrefslogtreecommitdiffstats
blob: 0b36bd238846cecf362ba30ed86336e84a9c3af5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef CPPUNIT_TOOLS_XMLELEMENT_H
#define CPPUNIT_TOOLS_XMLELEMENT_H

#include <cppunit/Portability.h>

#if CPPUNIT_NEED_DLL_DECL
#pragma warning( push )
#pragma warning( disable: 4251 )  // X needs to have dll-interface to be used by clients of class Z
#endif

#include <cppunit/portability/CppUnitDeque.h>
#include <string>


CPPUNIT_NS_BEGIN


class XmlElement;

#if CPPUNIT_NEED_DLL_DECL
//  template class CPPUNIT_API std::deque<XmlElement *>;
#endif


/*! \brief A XML Element.
 * 
 * A XML element has:
 * - a name, specified on construction,
 * - a content, specified on construction (may be empty),
 * - zero or more attributes, added with addAttribute(),
 * - zero or more child elements, added with addElement().
 */
class CPPUNIT_API XmlElement
{
public:
  /*! \brief Constructs an element with the specified name and string content.
   * \param elementName Name of the element. Must not be empty.
   * \param content Content of the element.
   */
  XmlElement( std::string elementName,
              std::string content ="" );

  /*! \brief Constructs an element with the specified name and numeric content.
   * \param elementName Name of the element. Must not be empty.
   * \param numericContent Content of the element.
   */
  XmlElement( std::string elementName,
              int numericContent );

  /*! \brief Destructs the element and its child elements.
   */
  virtual ~XmlElement();

  /*! \brief Returns the name of the element.
   * \return Name of the element.
   */
  std::string name() const;

  /*! \brief Returns the content of the element.
   * \return Content of the element.
   */
  std::string content() const;

  /*! \brief Sets the name of the element.
   * \param name New name for the element.
   */
  void setName( const std::string &name );

  /*! \brief Sets the content of the element.
   * \param content New content for the element.
   */
  void setContent( const std::string &content );

  /*! \overload void setContent( const std::string &content )
   */
  void setContent( int numericContent );

  /*! \brief Adds an attribute with the specified string value.
   * \param attributeName Name of the attribute. Must not be an empty.
   * \param value Value of the attribute.
   */
  void addAttribute( std::string attributeName,
                     std::string value );

  /*! \brief Adds an attribute with the specified numeric value.
   * \param attributeName Name of the attribute. Must not be empty.
   * \param numericValue Numeric value of the attribute.
   */
  void addAttribute( std::string attributeName,
                     int numericValue );

  /*! \brief Adds a child element to the element.
   * \param element Child element to add. Must not be \c NULL.
   */
  void addElement( XmlElement *element );

  /*! \brief Returns the number of child elements.
   * \return Number of child elements (element added with addElement()).
   */
  int elementCount() const;

  /*! \brief Returns the child element at the specified index.
   * \param index Zero based index of the element to return.
   * \returns Element at the specified index. Never \c NULL.
   * \exception std::invalid_argument if \a index < 0 or index >= elementCount().
   */
  XmlElement *elementAt( int index ) const;

  /*! \brief Returns the first child element with the specified name.
   * \param name Name of the child element to return.
   * \return First child element found which is named \a name.
   * \exception std::invalid_argument if there is no child element with the specified
   *            name.
   */
  XmlElement *elementFor( const std::string &name ) const;

  /*! \brief Returns a XML string that represents the element.
   * \param indent String of spaces representing the amount of 'indent'.
   * \return XML string that represents the element, its attributes and its
   *         child elements.
   */
  std::string toString( const std::string &indent = "" ) const;

private:
  typedef std::pair<std::string,std::string> Attribute;

  std::string attributesAsString() const;
  std::string escape( std::string value ) const;

private:
  std::string m_name;
  std::string m_content;

  typedef CppUnitDeque<Attribute> Attributes;
  Attributes m_attributes;

  typedef CppUnitDeque<XmlElement *> Elements;
  Elements m_elements;
};


CPPUNIT_NS_END

#if CPPUNIT_NEED_DLL_DECL
#pragma warning( pop )
#endif


#endif  // CPPUNIT_TOOLS_XMLELEMENT_H