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