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