blob: 9f3f84ad364e1905ce13dd4e1f7dddd780966d60 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
3 * Copyright © 2011 Intel
4 * Corporation All Rights Reserved.
5 *
6 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
15 *
16 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
21 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "XmlElement.h"
26#include <libxml/tree.h>
27#include <stdlib.h>
Georges-Henri Baron326a31d2012-06-28 12:05:09 +020028#include <sstream>
Patrick Benavoli68a91282011-08-31 11:23:23 +020029
30CXmlElement::CXmlElement(_xmlNode* pXmlElement) : _pXmlElement(pXmlElement)
31{
32}
33
34CXmlElement::CXmlElement() : _pXmlElement(NULL)
35{
36}
37
38void CXmlElement::setXmlElement(_xmlNode* pXmlElement)
39{
40 _pXmlElement = pXmlElement;
41}
42
43string CXmlElement::getType() const
44{
45 return (const char*)_pXmlElement->name;
46}
47
48string CXmlElement::getPath() const
49{
50 string strPathElement = "/" + getType();
51
52 if (hasAttribute("Name")) {
53
54 strPathElement += "[@Name=" + getNameAttribute() + "]";
55 }
56
57 CXmlElement parentElement;
58
59 if (getParentElement(parentElement)) {
60
61 // Done
62 return parentElement.getPath() + strPathElement;
63 }
64 return strPathElement;
65}
66
67string CXmlElement::getNameAttribute() const
68{
69 return getAttributeString("Name");
70}
71
72bool CXmlElement::hasAttribute(const string& strAttributeName) const
73{
74 return xmlHasProp(_pXmlElement, (const xmlChar*)strAttributeName.c_str()) != NULL;
75}
76
77string CXmlElement::getAttributeString(const string &strAttributeName) const
78{
79 if (!hasAttribute(strAttributeName)) {
80
81 return "";
82 }
83 xmlChar* pucXmlValue = xmlGetProp((xmlNode*)_pXmlElement, (const xmlChar*)strAttributeName.c_str());
84
85 string strValue((const char*)pucXmlValue);
86
87 xmlFree(pucXmlValue);
88
89 return strValue;
90}
91
92bool CXmlElement::getAttributeBoolean(const string& strAttributeName, const string& strTrueValue) const
93{
94 return getAttributeString(strAttributeName) == strTrueValue;
95}
96
97bool CXmlElement::getAttributeBoolean(const string& strAttributeName) const
98{
99 string strAttributeValue(getAttributeString(strAttributeName));
100
101 return strAttributeValue == "true" || strAttributeValue == "1";
102}
103
104uint32_t CXmlElement::getAttributeInteger(const string &strAttributeName) const
105{
106 string strAttributeValue(getAttributeString(strAttributeName));
107
108 return strtoul(strAttributeValue.c_str(), NULL, 0);
109}
110
111int32_t CXmlElement::getAttributeSignedInteger(const string &strAttributeName) const
112{
113 string strAttributeValue(getAttributeString(strAttributeName));
114
115 return strtol(strAttributeValue.c_str(), NULL, 0);
116}
117
118double CXmlElement::getAttributeDouble(const string &strAttributeName) const
119{
120 string strAttributeValue(getAttributeString(strAttributeName));
121
122 return strtod(strAttributeValue.c_str(), NULL);
123}
124
125string CXmlElement::getTextContent() const
126{
127 xmlChar* pucXmlContent = xmlNodeGetContent(_pXmlElement);
128
129 string strContent((const char*)pucXmlContent);
130
131 xmlFree(pucXmlContent);
132
133 return strContent;
134}
135
136bool CXmlElement::getChildElement(const string& strType, CXmlElement& childElement) const
137{
138 CChildIterator childIterator(*this);
139
140 while (childIterator.next(childElement)) {
141
142 if (childElement.getType() == strType) {
143
144 return true;
145 }
146 }
147 return false;
148}
149
150bool CXmlElement::getChildElement(const string& strType, const string& strNameAttribute, CXmlElement& childElement) const
151{
152 CChildIterator childIterator(*this);
153
154 while (childIterator.next(childElement)) {
155
156 if ((childElement.getType() == strType) && (childElement.getNameAttribute() == strNameAttribute)) {
157
158 return true;
159 }
160 }
161 return false;
162}
163
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200164uint32_t CXmlElement::getNbChildElements() const
165{
166 CXmlElement childElement;
167 uint32_t uiNbChildren = 0;
168
169 CChildIterator childIterator(*this);
170
171 while (childIterator.next(childElement)) {
172
173 uiNbChildren++;
174 }
175 return uiNbChildren;
176}
177
Patrick Benavoli68a91282011-08-31 11:23:23 +0200178bool CXmlElement::getParentElement(CXmlElement& parentElement) const
179{
180 _xmlNode* pXmlNode = _pXmlElement->parent;
181
182 if (pXmlNode->type == XML_ELEMENT_NODE) {
183
184 parentElement.setXmlElement(pXmlNode);
185
186 return true;
187 }
188 return false;
189}
190
191// Setters
Patrick Benavoli63499d42011-10-24 18:50:03 +0200192void CXmlElement::setAttributeBoolean(const string& strAttributeName, bool bValue)
193{
194 setAttributeString(strAttributeName, bValue ? "true" : "false");
195}
196
197
Patrick Benavoli68a91282011-08-31 11:23:23 +0200198void CXmlElement::setAttributeString(const string& strAttributeName, const string& strValue)
199{
200 xmlNewProp(_pXmlElement, BAD_CAST strAttributeName.c_str(), BAD_CAST strValue.c_str());
201}
202
Georges-Henri Baron326a31d2012-06-28 12:05:09 +0200203void CXmlElement::setAttributeInteger(const string& strAttributeName, uint32_t uiValue)
204{
205 ostringstream strStream;
206 strStream << uiValue;
207 setAttributeString(strAttributeName, strStream.str());
208}
209
Patrick Benavoli68a91282011-08-31 11:23:23 +0200210void CXmlElement::setNameAttribute(const string& strValue)
211{
212 setAttributeString("Name", strValue);
213}
214
215void CXmlElement::setTextContent(const string& strContent)
216{
217 xmlAddChild(_pXmlElement, xmlNewText(BAD_CAST strContent.c_str()));
218}
219
220void CXmlElement::setComment(const string& strComment)
221{
222 xmlAddChild(_pXmlElement, xmlNewComment(BAD_CAST strComment.c_str()));
223}
224
225// Child creation
226void CXmlElement::createChild(CXmlElement& childElement, const string& strType)
227{
228#ifdef LIBXML_TREE_ENABLED
229 xmlNodePtr pChildNode = xmlNewChild(_pXmlElement, NULL, BAD_CAST strType.c_str(), NULL);
230
231 childElement.setXmlElement(pChildNode);
232#endif
233}
234
235// Child iteration
236CXmlElement::CChildIterator::CChildIterator(const CXmlElement& xmlElement) : _pCurNode(xmlElement._pXmlElement->children)
237{
238}
239
240bool CXmlElement::CChildIterator::next(CXmlElement& xmlChildElement)
241{
242 while (_pCurNode) {
243
244 if (_pCurNode->type == XML_ELEMENT_NODE) {
245
246 xmlChildElement.setXmlElement(_pCurNode);
247
248 _pCurNode = _pCurNode->next;
249
250 return true;
251 }
252 _pCurNode = _pCurNode->next;
253 }
254
255 return false;
256}
257