blob: 11b9a94446d4f05c2379c3a137472d8124c117eb [file] [log] [blame]
Patrick Benavoli68a91282011-08-31 11:23:23 +02001/* <auto_header>
2 * <FILENAME>
3 *
4 * INTEL CONFIDENTIAL
5 * Copyright © 2011 Intel
6 * Corporation All Rights Reserved.
7 *
8 * The source code contained or described herein and all documents related to
9 * the source code ("Material") are owned by Intel Corporation or its suppliers
10 * or licensors. Title to the Material remains with Intel Corporation or its
11 * suppliers and licensors. The Material contains trade secrets and proprietary
12 * and confidential information of Intel or its suppliers and licensors. The
13 * Material is protected by worldwide copyright and trade secret laws and
14 * treaty provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed, or
16 * disclosed in any way without Intel’s prior express written permission.
17 *
18 * No license under any patent, copyright, trade secret or other intellectual
19 * property right is granted to or conferred upon you by disclosure or delivery
20 * of the Materials, either expressly, by implication, inducement, estoppel or
21 * otherwise. Any license under such intellectual property rights must be
22 * express and approved by Intel in writing.
23 *
24 * AUTHOR: Patrick Benavoli (patrickx.benavoli@intel.com)
25 * CREATED: 2011-06-01
26 * UPDATED: 2011-07-27
27 *
28 *
29 * </auto_header>
30 */
31#include "XmlElement.h"
32#include <libxml/tree.h>
33#include <stdlib.h>
34
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());
89
90 string strValue((const char*)pucXmlValue);
91
92 xmlFree(pucXmlValue);
93
94 return strValue;
95}
96
97bool CXmlElement::getAttributeBoolean(const string& strAttributeName, const string& strTrueValue) const
98{
99 return getAttributeString(strAttributeName) == strTrueValue;
100}
101
102bool CXmlElement::getAttributeBoolean(const string& strAttributeName) const
103{
104 string strAttributeValue(getAttributeString(strAttributeName));
105
106 return strAttributeValue == "true" || strAttributeValue == "1";
107}
108
109uint32_t CXmlElement::getAttributeInteger(const string &strAttributeName) const
110{
111 string strAttributeValue(getAttributeString(strAttributeName));
112
113 return strtoul(strAttributeValue.c_str(), NULL, 0);
114}
115
116int32_t CXmlElement::getAttributeSignedInteger(const string &strAttributeName) const
117{
118 string strAttributeValue(getAttributeString(strAttributeName));
119
120 return strtol(strAttributeValue.c_str(), NULL, 0);
121}
122
123double CXmlElement::getAttributeDouble(const string &strAttributeName) const
124{
125 string strAttributeValue(getAttributeString(strAttributeName));
126
127 return strtod(strAttributeValue.c_str(), NULL);
128}
129
130string CXmlElement::getTextContent() const
131{
132 xmlChar* pucXmlContent = xmlNodeGetContent(_pXmlElement);
133
134 string strContent((const char*)pucXmlContent);
135
136 xmlFree(pucXmlContent);
137
138 return strContent;
139}
140
141bool CXmlElement::getChildElement(const string& strType, CXmlElement& childElement) const
142{
143 CChildIterator childIterator(*this);
144
145 while (childIterator.next(childElement)) {
146
147 if (childElement.getType() == strType) {
148
149 return true;
150 }
151 }
152 return false;
153}
154
155bool CXmlElement::getChildElement(const string& strType, const string& strNameAttribute, CXmlElement& childElement) const
156{
157 CChildIterator childIterator(*this);
158
159 while (childIterator.next(childElement)) {
160
161 if ((childElement.getType() == strType) && (childElement.getNameAttribute() == strNameAttribute)) {
162
163 return true;
164 }
165 }
166 return false;
167}
168
Patrick Benavoli6ba361d2011-08-31 11:23:24 +0200169uint32_t CXmlElement::getNbChildElements() const
170{
171 CXmlElement childElement;
172 uint32_t uiNbChildren = 0;
173
174 CChildIterator childIterator(*this);
175
176 while (childIterator.next(childElement)) {
177
178 uiNbChildren++;
179 }
180 return uiNbChildren;
181}
182
Patrick Benavoli68a91282011-08-31 11:23:23 +0200183bool CXmlElement::getParentElement(CXmlElement& parentElement) const
184{
185 _xmlNode* pXmlNode = _pXmlElement->parent;
186
187 if (pXmlNode->type == XML_ELEMENT_NODE) {
188
189 parentElement.setXmlElement(pXmlNode);
190
191 return true;
192 }
193 return false;
194}
195
196// Setters
197void CXmlElement::setAttributeString(const string& strAttributeName, const string& strValue)
198{
199 xmlNewProp(_pXmlElement, BAD_CAST strAttributeName.c_str(), BAD_CAST strValue.c_str());
200}
201
202void CXmlElement::setNameAttribute(const string& strValue)
203{
204 setAttributeString("Name", strValue);
205}
206
207void CXmlElement::setTextContent(const string& strContent)
208{
209 xmlAddChild(_pXmlElement, xmlNewText(BAD_CAST strContent.c_str()));
210}
211
212void CXmlElement::setComment(const string& strComment)
213{
214 xmlAddChild(_pXmlElement, xmlNewComment(BAD_CAST strComment.c_str()));
215}
216
217// Child creation
218void CXmlElement::createChild(CXmlElement& childElement, const string& strType)
219{
220#ifdef LIBXML_TREE_ENABLED
221 xmlNodePtr pChildNode = xmlNewChild(_pXmlElement, NULL, BAD_CAST strType.c_str(), NULL);
222
223 childElement.setXmlElement(pChildNode);
224#endif
225}
226
227// Child iteration
228CXmlElement::CChildIterator::CChildIterator(const CXmlElement& xmlElement) : _pCurNode(xmlElement._pXmlElement->children)
229{
230}
231
232bool CXmlElement::CChildIterator::next(CXmlElement& xmlChildElement)
233{
234 while (_pCurNode) {
235
236 if (_pCurNode->type == XML_ELEMENT_NODE) {
237
238 xmlChildElement.setXmlElement(_pCurNode);
239
240 _pCurNode = _pCurNode->next;
241
242 return true;
243 }
244 _pCurNode = _pCurNode->next;
245 }
246
247 return false;
248}
249