blob: bda39736eb6255ec60ee7de1961bfea90da41610 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
David Wagnerb76c9d62014-02-05 18:30:24 +01002 * 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.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#include "XmlElement.h"
31#include <libxml/tree.h>
32#include <stdlib.h>
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020033#include <sstream>
Patrick Benavoli68a91282011-08-31 11:23:23 +020034
35CXmlElement::CXmlElement(_xmlNode* pXmlElement) : _pXmlElement(pXmlElement)
36{
37}
38
39CXmlElement::CXmlElement() : _pXmlElement(NULL)
40{
41}
42
43void CXmlElement::setXmlElement(_xmlNode* pXmlElement)
44{
45 _pXmlElement = pXmlElement;
46}
47
48string CXmlElement::getType() const
49{
50 return (const char*)_pXmlElement->name;
51}
52
53string CXmlElement::getPath() const
54{
55 string strPathElement = "/" + getType();
56
57 if (hasAttribute("Name")) {
58
59 strPathElement += "[@Name=" + getNameAttribute() + "]";
60 }
61
62 CXmlElement parentElement;
63
64 if (getParentElement(parentElement)) {
65
66 // Done
67 return parentElement.getPath() + strPathElement;
68 }
69 return strPathElement;
70}
71
72string CXmlElement::getNameAttribute() const
73{
74 return getAttributeString("Name");
75}
76
77bool CXmlElement::hasAttribute(const string& strAttributeName) const
78{
79 return xmlHasProp(_pXmlElement, (const xmlChar*)strAttributeName.c_str()) != NULL;
80}
81
82string CXmlElement::getAttributeString(const string &strAttributeName) const
83{
84 if (!hasAttribute(strAttributeName)) {
85
86 return "";
87 }
88 xmlChar* pucXmlValue = xmlGetProp((xmlNode*)_pXmlElement, (const xmlChar*)strAttributeName.c_str());
Eduardo Mendi30095b32014-04-15 17:29:52 +020089 if (pucXmlValue == NULL) {
90 return "";
91 }
Patrick Benavoli68a91282011-08-31 11:23:23 +020092
93 string strValue((const char*)pucXmlValue);
94
95 xmlFree(pucXmlValue);
96
97 return strValue;
98}
99
100bool CXmlElement::getAttributeBoolean(const string& strAttributeName, const string& strTrueValue) const
101{
102 return getAttributeString(strAttributeName) == strTrueValue;
103}
104
105bool CXmlElement::getAttributeBoolean(const string& strAttributeName) const
106{
107 string strAttributeValue(getAttributeString(strAttributeName));
108
109 return strAttributeValue == "true" || strAttributeValue == "1";
110}
111
112uint32_t CXmlElement::getAttributeInteger(const string &strAttributeName) const
113{
114 string strAttributeValue(getAttributeString(strAttributeName));
115
116 return strtoul(strAttributeValue.c_str(), NULL, 0);
117}
118
119int32_t CXmlElement::getAttributeSignedInteger(const string &strAttributeName) const
120{
121 string strAttributeValue(getAttributeString(strAttributeName));
122
123 return strtol(strAttributeValue.c_str(), NULL, 0);
124}
125
126double CXmlElement::getAttributeDouble(const string &strAttributeName) const
127{
128 string strAttributeValue(getAttributeString(strAttributeName));
129
130 return strtod(strAttributeValue.c_str(), NULL);
131}
132
133string CXmlElement::getTextContent() const
134{
135 xmlChar* pucXmlContent = xmlNodeGetContent(_pXmlElement);
Eduardo Mendi30095b32014-04-15 17:29:52 +0200136 if (pucXmlContent == NULL) {
137 return "";
138 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200139
140 string strContent((const char*)pucXmlContent);
141
142 xmlFree(pucXmlContent);
143
144 return strContent;
145}
146
147bool CXmlElement::getChildElement(const string& strType, CXmlElement& childElement) const
148{
149 CChildIterator childIterator(*this);
150
151 while (childIterator.next(childElement)) {
152
153 if (childElement.getType() == strType) {
154
155 return true;
156 }
157 }
158 return false;
159}
160
161bool CXmlElement::getChildElement(const string& strType, const string& strNameAttribute, CXmlElement& childElement) const
162{
163 CChildIterator childIterator(*this);
164
165 while (childIterator.next(childElement)) {
166
167 if ((childElement.getType() == strType) && (childElement.getNameAttribute() == strNameAttribute)) {
168
169 return true;
170 }
171 }
172 return false;
173}
174
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200175uint32_t CXmlElement::getNbChildElements() const
176{
177 CXmlElement childElement;
178 uint32_t uiNbChildren = 0;
179
180 CChildIterator childIterator(*this);
181
182 while (childIterator.next(childElement)) {
183
184 uiNbChildren++;
185 }
186 return uiNbChildren;
187}
188
Patrick Benavoli68a91282011-08-31 11:23:23 +0200189bool CXmlElement::getParentElement(CXmlElement& parentElement) const
190{
191 _xmlNode* pXmlNode = _pXmlElement->parent;
192
193 if (pXmlNode->type == XML_ELEMENT_NODE) {
194
195 parentElement.setXmlElement(pXmlNode);
196
197 return true;
198 }
199 return false;
200}
201
202// Setters
Patrick Benavoli63499d42011-10-24 18:50:03 +0200203void CXmlElement::setAttributeBoolean(const string& strAttributeName, bool bValue)
204{
205 setAttributeString(strAttributeName, bValue ? "true" : "false");
206}
207
208
Patrick Benavoli68a91282011-08-31 11:23:23 +0200209void CXmlElement::setAttributeString(const string& strAttributeName, const string& strValue)
210{
211 xmlNewProp(_pXmlElement, BAD_CAST strAttributeName.c_str(), BAD_CAST strValue.c_str());
212}
213
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200214void CXmlElement::setAttributeInteger(const string& strAttributeName, uint32_t uiValue)
215{
216 ostringstream strStream;
217 strStream << uiValue;
218 setAttributeString(strAttributeName, strStream.str());
219}
220
Patrick Benavoli68a91282011-08-31 11:23:23 +0200221void CXmlElement::setNameAttribute(const string& strValue)
222{
223 setAttributeString("Name", strValue);
224}
225
226void CXmlElement::setTextContent(const string& strContent)
227{
228 xmlAddChild(_pXmlElement, xmlNewText(BAD_CAST strContent.c_str()));
229}
230
231void CXmlElement::setComment(const string& strComment)
232{
233 xmlAddChild(_pXmlElement, xmlNewComment(BAD_CAST strComment.c_str()));
234}
235
236// Child creation
237void CXmlElement::createChild(CXmlElement& childElement, const string& strType)
238{
239#ifdef LIBXML_TREE_ENABLED
240 xmlNodePtr pChildNode = xmlNewChild(_pXmlElement, NULL, BAD_CAST strType.c_str(), NULL);
241
242 childElement.setXmlElement(pChildNode);
243#endif
244}
245
246// Child iteration
247CXmlElement::CChildIterator::CChildIterator(const CXmlElement& xmlElement) : _pCurNode(xmlElement._pXmlElement->children)
248{
249}
250
251bool CXmlElement::CChildIterator::next(CXmlElement& xmlChildElement)
252{
253 while (_pCurNode) {
254
255 if (_pCurNode->type == XML_ELEMENT_NODE) {
256
257 xmlChildElement.setXmlElement(_pCurNode);
258
259 _pCurNode = _pCurNode->next;
260
261 return true;
262 }
263 _pCurNode = _pCurNode->next;
264 }
265
266 return false;
267}
268