1 /*
2  * Copyright (c) 2011-2014, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors
16  * may be used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #pragma once
31 
32 #include <stdint.h>
33 #include <string>
34 
35 struct _xmlNode;
36 struct _xmlDoc;
37 
38 class CXmlElement
39 {
40     friend class CChildIterator;
41 
42 public:
43     CXmlElement(_xmlNode *pXmlElement);
44     CXmlElement();
45 
46     // Xml element
47     void setXmlElement(_xmlNode *pXmlElement);
48 
49     // Getters
50     std::string getType() const;
51     std::string getPath() const;
52     std::string getNameAttribute() const;
53     bool hasAttribute(const std::string &strAttributeName) const;
54 
55     /** Get attribute
56      *
57      * If the attribute does not exists or there is a libxml2 error while
58      * reading it or conversion from string to T fails, false is returned. In
59      * case of failure, the content of value is the same as before calling
60      * this method.
61      *
62      * Note: if T==string, no conversion takes place.
63      *
64      * @tparam T the type of the value to retrieve
65      * @param[in] name The attribute name
66      * @param[out] value The attribute value
67      * @return true if success, false otherwise
68      */
69     template <typename T>
70     bool getAttribute(const std::string &name, T &value) const;
71 
72     std::string getTextContent() const;
73 
74     // Navigation
75     bool getChildElement(const std::string &strType, CXmlElement &childElement) const;
76     bool getChildElement(const std::string &strType, const std::string &strNameAttribute,
77                          CXmlElement &childElement) const;
78     size_t getNbChildElements() const;
79     bool getParentElement(CXmlElement &parentElement) const;
80 
81     /** Set attribute
82      *
83      * @tparam T the type of the value to retrieve
84      * @param[in] name The attribute name
85      * @param[in] value The attribute value
86      */
87     template <typename T>
88     void setAttribute(const std::string &name, const T &value);
89     /** Set attribute - special case for C-style strings
90      *
91      * @param[in] name The attribute name
92      * @param[in] value The attribute value
93      */
94     void setAttribute(const std::string &name, const char *value);
95 
96     void setNameAttribute(const std::string &strValue);
97     void setTextContent(const std::string &strContent);
98 
99     // Child creation
100     void createChild(CXmlElement &childElement, const std::string &strType);
101 
102 public:
103     // Child iteration
104     class CChildIterator
105     {
106     public:
107         CChildIterator(const CXmlElement &xmlElement);
108 
109         bool next(CXmlElement &xmlChildElement);
110 
111     private:
112         _xmlNode *_pCurNode;
113     };
114 
115 private:
116     _xmlNode *_pXmlElement;
117 };
118