Kevin Rocard | 93250d1 | 2012-07-19 17:48:30 +0200 | [diff] [blame] | 1 | /* |
David Wagner | b76c9d6 | 2014-02-05 18:30:24 +0100 | [diff] [blame] | 2 | * 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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 29 | */ |
| 30 | #include "XmlElement.h" |
| 31 | #include <libxml/tree.h> |
| 32 | #include <stdlib.h> |
Georges-Henri Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 33 | #include <sstream> |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 34 | |
| 35 | CXmlElement::CXmlElement(_xmlNode* pXmlElement) : _pXmlElement(pXmlElement) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | CXmlElement::CXmlElement() : _pXmlElement(NULL) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | void CXmlElement::setXmlElement(_xmlNode* pXmlElement) |
| 44 | { |
| 45 | _pXmlElement = pXmlElement; |
| 46 | } |
| 47 | |
| 48 | string CXmlElement::getType() const |
| 49 | { |
| 50 | return (const char*)_pXmlElement->name; |
| 51 | } |
| 52 | |
| 53 | string 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 | |
| 72 | string CXmlElement::getNameAttribute() const |
| 73 | { |
| 74 | return getAttributeString("Name"); |
| 75 | } |
| 76 | |
| 77 | bool CXmlElement::hasAttribute(const string& strAttributeName) const |
| 78 | { |
| 79 | return xmlHasProp(_pXmlElement, (const xmlChar*)strAttributeName.c_str()) != NULL; |
| 80 | } |
| 81 | |
| 82 | string 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 Mendi | 30095b3 | 2014-04-15 17:29:52 +0200 | [diff] [blame^] | 89 | if (pucXmlValue == NULL) { |
| 90 | return ""; |
| 91 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 92 | |
| 93 | string strValue((const char*)pucXmlValue); |
| 94 | |
| 95 | xmlFree(pucXmlValue); |
| 96 | |
| 97 | return strValue; |
| 98 | } |
| 99 | |
| 100 | bool CXmlElement::getAttributeBoolean(const string& strAttributeName, const string& strTrueValue) const |
| 101 | { |
| 102 | return getAttributeString(strAttributeName) == strTrueValue; |
| 103 | } |
| 104 | |
| 105 | bool CXmlElement::getAttributeBoolean(const string& strAttributeName) const |
| 106 | { |
| 107 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 108 | |
| 109 | return strAttributeValue == "true" || strAttributeValue == "1"; |
| 110 | } |
| 111 | |
| 112 | uint32_t CXmlElement::getAttributeInteger(const string &strAttributeName) const |
| 113 | { |
| 114 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 115 | |
| 116 | return strtoul(strAttributeValue.c_str(), NULL, 0); |
| 117 | } |
| 118 | |
| 119 | int32_t CXmlElement::getAttributeSignedInteger(const string &strAttributeName) const |
| 120 | { |
| 121 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 122 | |
| 123 | return strtol(strAttributeValue.c_str(), NULL, 0); |
| 124 | } |
| 125 | |
| 126 | double CXmlElement::getAttributeDouble(const string &strAttributeName) const |
| 127 | { |
| 128 | string strAttributeValue(getAttributeString(strAttributeName)); |
| 129 | |
| 130 | return strtod(strAttributeValue.c_str(), NULL); |
| 131 | } |
| 132 | |
| 133 | string CXmlElement::getTextContent() const |
| 134 | { |
| 135 | xmlChar* pucXmlContent = xmlNodeGetContent(_pXmlElement); |
Eduardo Mendi | 30095b3 | 2014-04-15 17:29:52 +0200 | [diff] [blame^] | 136 | if (pucXmlContent == NULL) { |
| 137 | return ""; |
| 138 | } |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 139 | |
| 140 | string strContent((const char*)pucXmlContent); |
| 141 | |
| 142 | xmlFree(pucXmlContent); |
| 143 | |
| 144 | return strContent; |
| 145 | } |
| 146 | |
| 147 | bool 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 | |
| 161 | bool 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 Benavoli | 6ba361d | 2011-08-31 11:23:24 +0200 | [diff] [blame] | 175 | uint32_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 Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 189 | bool 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 Benavoli | 63499d4 | 2011-10-24 18:50:03 +0200 | [diff] [blame] | 203 | void CXmlElement::setAttributeBoolean(const string& strAttributeName, bool bValue) |
| 204 | { |
| 205 | setAttributeString(strAttributeName, bValue ? "true" : "false"); |
| 206 | } |
| 207 | |
| 208 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 209 | void 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 Baron | 326a31d | 2012-06-28 12:05:09 +0200 | [diff] [blame] | 214 | void CXmlElement::setAttributeInteger(const string& strAttributeName, uint32_t uiValue) |
| 215 | { |
| 216 | ostringstream strStream; |
| 217 | strStream << uiValue; |
| 218 | setAttributeString(strAttributeName, strStream.str()); |
| 219 | } |
| 220 | |
Patrick Benavoli | 68a9128 | 2011-08-31 11:23:23 +0200 | [diff] [blame] | 221 | void CXmlElement::setNameAttribute(const string& strValue) |
| 222 | { |
| 223 | setAttributeString("Name", strValue); |
| 224 | } |
| 225 | |
| 226 | void CXmlElement::setTextContent(const string& strContent) |
| 227 | { |
| 228 | xmlAddChild(_pXmlElement, xmlNewText(BAD_CAST strContent.c_str())); |
| 229 | } |
| 230 | |
| 231 | void CXmlElement::setComment(const string& strComment) |
| 232 | { |
| 233 | xmlAddChild(_pXmlElement, xmlNewComment(BAD_CAST strComment.c_str())); |
| 234 | } |
| 235 | |
| 236 | // Child creation |
| 237 | void 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 |
| 247 | CXmlElement::CChildIterator::CChildIterator(const CXmlElement& xmlElement) : _pCurNode(xmlElement._pXmlElement->children) |
| 248 | { |
| 249 | } |
| 250 | |
| 251 | bool 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 | |