Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 2 | * 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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 22 | * CREATED: 2011-06-01 |
| 23 | * UPDATED: 2011-07-27 |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 24 | */ |
| 25 | #include "XmlElement.h" |
| 26 | #include <libxml/tree.h> |
| 27 | #include <stdlib.h> |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame^] | 28 | #include <sstream> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | |
| 30 | CXmlElement::CXmlElement(_xmlNode* pXmlElement) : _pXmlElement(pXmlElement) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | CXmlElement::CXmlElement() : _pXmlElement(NULL) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | void CXmlElement::setXmlElement(_xmlNode* pXmlElement) |
| 39 | { |
| 40 | _pXmlElement = pXmlElement; |
| 41 | } |
| 42 | |
| 43 | string CXmlElement::getType() const |
| 44 | { |
| 45 | return (const char*)_pXmlElement->name; |
| 46 | } |
| 47 | |
| 48 | string 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 | |
| 67 | string CXmlElement::getNameAttribute() const |
| 68 | { |
| 69 | return getAttributeString("Name"); |
| 70 | } |
| 71 | |
| 72 | bool CXmlElement::hasAttribute(const string& strAttributeName) const |
| 73 | { |
| 74 | return xmlHasProp(_pXmlElement, (const xmlChar*)strAttributeName.c_str()) != NULL; |
| 75 | } |
| 76 | |
| 77 | string 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 | |
| 92 | bool CXmlElement::getAttributeBoolean(const string& strAttributeName, const string& strTrueValue) const |
| 93 | { |
| 94 | return getAttributeString(strAttributeName) == strTrueValue; |
| 95 | } |
| 96 | |
| 97 | bool CXmlElement::getAttributeBoolean(const string& strAttributeName) const |
| 98 | { |
| 99 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 100 | |
| 101 | return strAttributeValue == "true" || strAttributeValue == "1"; |
| 102 | } |
| 103 | |
| 104 | uint32_t CXmlElement::getAttributeInteger(const string &strAttributeName) const |
| 105 | { |
| 106 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 107 | |
| 108 | return strtoul(strAttributeValue.c_str(), NULL, 0); |
| 109 | } |
| 110 | |
| 111 | int32_t CXmlElement::getAttributeSignedInteger(const string &strAttributeName) const |
| 112 | { |
| 113 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 114 | |
| 115 | return strtol(strAttributeValue.c_str(), NULL, 0); |
| 116 | } |
| 117 | |
| 118 | double CXmlElement::getAttributeDouble(const string &strAttributeName) const |
| 119 | { |
| 120 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 121 | |
| 122 | return strtod(strAttributeValue.c_str(), NULL); |
| 123 | } |
| 124 | |
| 125 | string 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 | |
| 136 | bool 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 | |
| 150 | bool 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 Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 164 | uint32_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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 178 | bool 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 Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 192 | void CXmlElement::setAttributeBoolean(const string& strAttributeName, bool bValue) |
| 193 | { |
| 194 | setAttributeString(strAttributeName, bValue ? "true" : "false"); |
| 195 | } |
| 196 | |
| 197 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 198 | void 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 Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame^] | 203 | void CXmlElement::setAttributeInteger(const string& strAttributeName, uint32_t uiValue) |
| 204 | { |
| 205 | ostringstream strStream; |
| 206 | strStream << uiValue; |
| 207 | setAttributeString(strAttributeName, strStream.str()); |
| 208 | } |
| 209 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 210 | void CXmlElement::setNameAttribute(const string& strValue) |
| 211 | { |
| 212 | setAttributeString("Name", strValue); |
| 213 | } |
| 214 | |
| 215 | void CXmlElement::setTextContent(const string& strContent) |
| 216 | { |
| 217 | xmlAddChild(_pXmlElement, xmlNewText(BAD_CAST strContent.c_str())); |
| 218 | } |
| 219 | |
| 220 | void CXmlElement::setComment(const string& strComment) |
| 221 | { |
| 222 | xmlAddChild(_pXmlElement, xmlNewComment(BAD_CAST strComment.c_str())); |
| 223 | } |
| 224 | |
| 225 | // Child creation |
| 226 | void 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 |
| 236 | CXmlElement::CChildIterator::CChildIterator(const CXmlElement& xmlElement) : _pCurNode(xmlElement._pXmlElement->children) |
| 237 | { |
| 238 | } |
| 239 | |
| 240 | bool 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 | |