XmlElement.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef CPPUNIT_TOOLS_XMLELEMENT_H
  2. #define CPPUNIT_TOOLS_XMLELEMENT_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/portability/CppUnitDeque.h>
  9. #include <string>
  10. CPPUNIT_NS_BEGIN
  11. class XmlElement;
  12. #if CPPUNIT_NEED_DLL_DECL
  13. // template class CPPUNIT_API std::deque<XmlElement *>;
  14. #endif
  15. /*! \brief A XML Element.
  16. *
  17. * A XML element has:
  18. * - a name, specified on construction,
  19. * - a content, specified on construction (may be empty),
  20. * - zero or more attributes, added with addAttribute(),
  21. * - zero or more child elements, added with addElement().
  22. */
  23. class CPPUNIT_API XmlElement
  24. {
  25. public:
  26. /*! \brief Constructs an element with the specified name and string content.
  27. * \param elementName Name of the element. Must not be empty.
  28. * \param content Content of the element.
  29. */
  30. XmlElement( std::string elementName,
  31. std::string content ="" );
  32. /*! \brief Constructs an element with the specified name and numeric content.
  33. * \param elementName Name of the element. Must not be empty.
  34. * \param numericContent Content of the element.
  35. */
  36. XmlElement( std::string elementName,
  37. int numericContent );
  38. /*! \brief Destructs the element and its child elements.
  39. */
  40. virtual ~XmlElement();
  41. /*! \brief Returns the name of the element.
  42. * \return Name of the element.
  43. */
  44. std::string name() const;
  45. /*! \brief Returns the content of the element.
  46. * \return Content of the element.
  47. */
  48. std::string content() const;
  49. /*! \brief Sets the name of the element.
  50. * \param name New name for the element.
  51. */
  52. void setName( const std::string &name );
  53. /*! \brief Sets the content of the element.
  54. * \param content New content for the element.
  55. */
  56. void setContent( const std::string &content );
  57. /*! \overload void setContent( const std::string &content )
  58. */
  59. void setContent( int numericContent );
  60. /*! \brief Adds an attribute with the specified string value.
  61. * \param attributeName Name of the attribute. Must not be an empty.
  62. * \param value Value of the attribute.
  63. */
  64. void addAttribute( std::string attributeName,
  65. std::string value );
  66. /*! \brief Adds an attribute with the specified numeric value.
  67. * \param attributeName Name of the attribute. Must not be empty.
  68. * \param numericValue Numeric value of the attribute.
  69. */
  70. void addAttribute( std::string attributeName,
  71. int numericValue );
  72. /*! \brief Adds a child element to the element.
  73. * \param element Child element to add. Must not be \c NULL.
  74. */
  75. void addElement( XmlElement *element );
  76. /*! \brief Returns the number of child elements.
  77. * \return Number of child elements (element added with addElement()).
  78. */
  79. int elementCount() const;
  80. /*! \brief Returns the child element at the specified index.
  81. * \param index Zero based index of the element to return.
  82. * \returns Element at the specified index. Never \c NULL.
  83. * \exception std::invalid_argument if \a index < 0 or index >= elementCount().
  84. */
  85. XmlElement *elementAt( int index ) const;
  86. /*! \brief Returns the first child element with the specified name.
  87. * \param name Name of the child element to return.
  88. * \return First child element found which is named \a name.
  89. * \exception std::invalid_argument if there is no child element with the specified
  90. * name.
  91. */
  92. XmlElement *elementFor( const std::string &name ) const;
  93. /*! \brief Returns a XML string that represents the element.
  94. * \param indent String of spaces representing the amount of 'indent'.
  95. * \return XML string that represents the element, its attributes and its
  96. * child elements.
  97. */
  98. std::string toString( const std::string &indent = "" ) const;
  99. private:
  100. typedef std::pair<std::string,std::string> Attribute;
  101. std::string attributesAsString() const;
  102. std::string escape( std::string value ) const;
  103. private:
  104. std::string m_name;
  105. std::string m_content;
  106. typedef CppUnitDeque<Attribute> Attributes;
  107. Attributes m_attributes;
  108. typedef CppUnitDeque<XmlElement *> Elements;
  109. Elements m_elements;
  110. };
  111. CPPUNIT_NS_END
  112. #if CPPUNIT_NEED_DLL_DECL
  113. #pragma warning( pop )
  114. #endif
  115. #endif // CPPUNIT_TOOLS_XMLELEMENT_H