LCOV - code coverage report
Current view: top level - xml - xml_pugi.cc (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 101 208 48.6 %
Date: 2026-11-16 02:06:09 Functions: 19 36 52.8 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
       3             :  * Copyright Contributors to the OpenSDN project
       4             :  */
       5             : 
       6             : #include <boost/algorithm/string/replace.hpp>
       7             : #include "xml/xml_base.h"
       8             : #include "xml/xml_pugi.h"
       9             : #include "base/logging.h"
      10             : 
      11             : // This is internal implementation detail specific to this lib to make
      12             : // exposed methods efficient.
      13             : 
      14             : //XmlPugi::tmp_("");
      15             : pugi::xml_attribute XmlPugi::GAttr;
      16             : pugi::xml_node XmlPugi::GNode;
      17             : 
      18      313625 : XmlPugi::XmlPugi() : writer_(this), node_(GNode), attrib_(GAttr) {
      19      313624 : }
      20             : 
      21      627254 : XmlPugi::~XmlPugi() {
      22      313627 :     doc_.reset();
      23      627253 : }
      24             : 
      25           0 : pugi::xml_node XmlPugi::RootNode() {
      26           0 :     return(doc_.root());
      27             : }
      28             : 
      29      129199 : const char *XmlPugi::ReadNode(const std::string &name) {
      30      129199 :     PugiPredicate p1(name);
      31             : 
      32      129199 :     pugi::xml_node node = doc_.find_node(p1);
      33      129199 :     if (IsNull(node)) {
      34          16 :         return NULL;
      35             :     }
      36             : 
      37      129183 :     SetContext(node);
      38      129183 :     if (node.type() != pugi::node_pi) {
      39      255496 :         for (node = node.first_child(); node; node = node.next_sibling()) {
      40             :             // traverse to first plain character data or char data,
      41             :             // skipping XML comments
      42      252641 :             if (node.type() == pugi::node_pcdata ||
      43      126313 :                 node.type() == pugi::node_cdata)
      44          15 :                break;
      45             :         }
      46             :     }
      47             : 
      48      129181 :     return node.value();
      49             : 
      50      129198 : }
      51             : 
      52      241708 : pugi::xml_node XmlPugi::FindNode(const std::string &name) {
      53      241708 :     PugiPredicate p1(name);
      54             : 
      55      241708 :     pugi::xml_node node = doc_.find_node(p1);
      56      241708 :     return node;
      57      241708 : }
      58             : 
      59           0 : const char *XmlPugi::ReadNodeName(const std::string &name) {
      60           0 :     PugiPredicate p1(name);
      61             : 
      62           0 :     pugi::xml_node node = doc_.find_node(p1);
      63           0 :     SetContext(node);
      64             : 
      65           0 :     return node.name();
      66           0 : }
      67             : 
      68           0 : const char *XmlPugi::ReadNodeValue() {
      69           0 :     pugi::xml_node node = node_;
      70             : 
      71           0 :     if (node.type() != pugi::node_pi) {
      72           0 :         for (node = node.first_child(); node; node = node.next_sibling()) {
      73             :             // traverse to first plain character data or char data,
      74             :             // skipping XML comments
      75           0 :             if (node.type() == pugi::node_pcdata ||
      76           0 :                 node.type() == pugi::node_cdata)
      77           0 :                break;
      78             :         }
      79             :     }
      80           0 :     return node.value();
      81             : }
      82             : 
      83           0 : const char *XmlPugi::ReadChildNode() {
      84           0 :     pugi::xml_node tmp = (node_ == GNode) ? doc_.first_child() :
      85           0 :                           node_.first_child();
      86           0 :     SetContext(tmp);
      87           0 :     return tmp.value();
      88             : }
      89             : 
      90        2216 : const char *XmlPugi::ReadChildNodeName() {
      91        2216 :     pugi::xml_node tmp = (node_ == GNode) ? doc_.first_child() :
      92        2216 :                           node_.first_child();
      93        2216 :     SetContext(tmp);
      94        4432 :     return tmp.name();
      95             : }
      96             : 
      97           0 : const char *XmlPugi::ReadNextNode() {
      98           0 :     pugi::xml_node tmp = node_.next_sibling();
      99             : 
     100           0 :     SetContext(tmp);
     101           0 :     return tmp.value();
     102             : }
     103             : 
     104           0 : const char *XmlPugi::ReadNextNodeName() {
     105           0 :     pugi::xml_node tmp = node_.next_sibling();
     106             : 
     107           0 :     SetContext(tmp);
     108           0 :     return tmp.name();
     109             : }
     110             : 
     111           0 : void XmlPugi::RewindNode() {
     112           0 :     SetContext(node_);
     113           0 : }
     114             : 
     115      257521 : const char *XmlPugi::ReadAttrib(const std::string &str) {
     116      257521 :     pugi::xml_attribute tmp = node_.attribute(str.c_str());
     117      257523 :     SetContext(node_, tmp);
     118      515046 :     return tmp.value();
     119             : }
     120             : 
     121           0 : const char *XmlPugi::ReadFirstAttrib() {
     122           0 :     pugi::xml_attribute tmp = node_.first_attribute();
     123           0 :     SetContext(node_, tmp);
     124           0 :     return tmp.value();
     125             : }
     126             : 
     127           0 : const char *XmlPugi::ReadNextAttrib() {
     128           0 :     pugi::xml_attribute tmp = attrib_.next_attribute();
     129           0 :     SetContext(node_, tmp);
     130           0 :     return tmp.value();
     131             : }
     132             : 
     133           0 : void XmlPugi::RewindAttrib() {
     134           0 :     SetContext(node_, GAttr);
     135           0 : }
     136             : 
     137           0 : const char *XmlPugi::ReadParentName() {
     138           0 :     pugi::xml_node tmp = node_.parent();
     139           0 :     SetContext(tmp);
     140           0 :     return tmp.name();
     141             : }
     142             : 
     143         243 : int XmlPugi::WriteDoc(uint8_t *buf) {
     144         243 :     buf_tmp_ = buf; // this will be used by writer_->write
     145         243 :     ts_ = 0;
     146         243 :     doc_.save(writer_, "", pugi::format_default, pugi::encoding_utf8);
     147             : 
     148         243 :     if (ts_ == 0) return -1;
     149         243 :     return static_cast<int>(ts_);
     150             : }
     151             : 
     152           0 : int XmlPugi::WriteRawDoc(uint8_t *buf) {
     153           0 :     buf_tmp_ = buf; // this will be used by writer_->write
     154           0 :     ts_ = 0;
     155           0 :     doc_.save(writer_, "", pugi::format_raw | pugi::format_no_declaration);
     156             : 
     157           0 :     if (ts_ == 0) return -1;
     158           0 :     return static_cast<int>(ts_);
     159             : }
     160             : 
     161         505 : void XmlPugi::PrintDoc(std::ostream& os) const {
     162         505 :     doc_.print(os, " ", pugi::format_raw | pugi::format_no_declaration);
     163         505 : }
     164             : 
     165           0 : void XmlPugi::PrintDocFormatted(std::ostream& os) const {
     166           0 :     doc_.print(os, " ", pugi::format_indent | pugi::format_no_declaration);
     167           0 : }
     168             : 
     169      519206 : void XmlPugi::SetContext(pugi::xml_node node, pugi::xml_attribute attrib) {
     170      519206 :     node_ = node;
     171      519206 :     attrib_ = attrib;
     172      519206 : }
     173             : 
     174      123466 : int XmlPugi::LoadDoc(const std::string &document) {
     175      123466 :     RewindDoc();
     176      123466 :     doc_.reset();
     177             : 
     178      123466 :     pugi::xml_parse_result ret = doc_.load_buffer(document.c_str(), document.size(),
     179             :                                                  pugi::parse_default,
     180             :                                                  pugi::encoding_utf8);
     181      123466 :     if (ret == false) {
     182           0 :         LOG(DEBUG, "XML doc load failed, code: " << ret << " " << ret.description());
     183           0 :         LOG(DEBUG, "Error offset: " << ret.offset << " (error at [..." << (document.c_str() + ret.offset) << "]");
     184           0 :         LOG(DEBUG, "Document: " << document);
     185           0 :         return -1;
     186             :     }
     187      123466 :     return 0;
     188             : }
     189             : 
     190      123466 : void XmlPugi::RewindDoc() {
     191      123466 :     SetContext();
     192      123466 : }
     193             : 
     194           0 : void XmlPugi::AppendDoc(const std::string &node_name, XmlBase *a_doc) {
     195           0 :     std::string str;
     196           0 :     pugi::xml_node node_s;
     197           0 :     pugi::xml_node node1;
     198           0 :     pugi::xml_node node2;
     199             : 
     200           0 :     PugiPredicate p1(node_name);
     201           0 :     node1 = doc_.find_node(p1);
     202           0 :     SetContext(node1);
     203             : 
     204           0 :     XmlPugi *xp = static_cast<XmlPugi *>(a_doc);
     205           0 :     node_s = xp->doc_.first_child();
     206           0 :     if (!IsNull(node1)) {
     207           0 :         node2 = node1.parent().append_copy(node_s);
     208           0 :         SetContext(node2);
     209             :     }
     210             : 
     211           0 : }
     212             : 
     213         295 : int XmlPugi::AddNode(const std::string &key, const std::string &value) {
     214         295 :     pugi::xml_node node;
     215             : 
     216         295 :     if (IsNull(node_)) {
     217         295 :         node = doc_.append_child(key.c_str());
     218             :     } else {
     219           0 :         node = node_.parent().append_child(key.c_str());
     220             :     }
     221         295 :     if (value != "") {
     222           0 :         node.text().set(value.c_str());
     223             :     }
     224             : 
     225         295 :     SetContext(node);
     226         295 :     return 0;
     227             : }
     228             : 
     229         244 : int XmlPugi::DeleteNode(const std::string &key) {
     230         244 :     PugiPredicate p1(key);
     231         244 :     pugi::xml_node node = doc_.find_node(p1);
     232         244 :     if (IsNull(node))
     233           0 :         return -1;
     234             : 
     235         244 :     node.parent().remove_child(key.c_str());
     236         244 :     return 0;
     237         244 : }
     238             : 
     239           0 : int XmlPugi::ModifyNode(const std::string &key, const std::string &value) {
     240           0 :     PugiPredicate p1(key);
     241           0 :     pugi::xml_node node = doc_.find_node(p1);
     242           0 :     if (IsNull(node))
     243           0 :         return -1;
     244             : 
     245           0 :     node.text().set(value.c_str());
     246             : 
     247           0 :     SetContext(node);
     248           0 :     return 0;
     249           0 : }
     250             : 
     251        1630 : int XmlPugi::AddChildNode(const std::string &key, const std::string &value) {
     252        1630 :     pugi::xml_node node;
     253             : 
     254        1630 :     if (IsNull(node_)) {
     255           0 :         node = doc_.append_child(key.c_str());
     256             :     } else {
     257        1630 :         node = node_.append_child(key.c_str());
     258             :     }
     259        1630 :     if (value != "") {
     260          32 :         node.text().set(value.c_str());
     261             :     }
     262             : 
     263        1630 :     SetContext(node);
     264        1630 :     return 0;
     265             : }
     266             : 
     267           0 : int XmlPugi::AddChildNodeAfter(const std::string &node_name,
     268             :                                const std::string &key,
     269             :                                const std::string &value) {
     270             : 
     271           0 :     PugiPredicate p1(node_name);
     272           0 :     pugi::xml_node node1 = doc_.find_node(p1);
     273           0 :     SetContext(node1);
     274             : 
     275           0 :     if (!IsNull(node1)) {
     276           0 :         AddChildNode(key, value);
     277             :     }
     278             : 
     279           0 :     return 0;
     280           0 : }
     281             : 
     282             : 
     283        2483 : int XmlPugi::AddAttribute(const std::string &key, const std::string &value) {
     284        2483 :     if (IsNull(node_))
     285           0 :         return -1;
     286             : 
     287        2483 :     pugi::xml_attribute attrib;
     288             : 
     289        2483 :     if (IsNull(attrib_) ) {
     290        1617 :         attrib = node_.append_attribute(key.c_str()) = value.c_str();
     291             :     } else {
     292         866 :         attrib =
     293         866 :             node_.insert_attribute_after(key.c_str(), attrib_) = value.c_str();
     294             :     }
     295             : 
     296        2483 :     SetContext(node_, attrib);
     297        2483 :     return 0;
     298             : }
     299             : 
     300           0 : int XmlPugi::DeleteAttribute(const std::string &key) {
     301           0 :     if (IsNull(node_))
     302           0 :         return -1;
     303           0 :     pugi::xml_attribute tmp = node_.attribute(key.c_str());
     304             :     bool res;
     305           0 :     if (IsNull(tmp)) {
     306           0 :         return -1;
     307             :     } else {
     308           0 :          pugi::xml_attribute attrib = tmp.next_attribute();
     309           0 :          res = node_.remove_attribute(tmp);
     310           0 :          SetContext(node_, attrib);
     311           0 :          return (res ? -1 : 0);
     312             :     }
     313             :     return 0;
     314             : }
     315             : 
     316        2424 : int XmlPugi::ModifyAttribute(const std::string &key, const std::string &value) {
     317        2424 :     if (node_.type() == pugi::node_null)
     318           0 :         return -1;
     319             : 
     320             :     //ReadAttrib(key);
     321        2424 :     pugi::xml_attribute tmp = node_.attribute(key.c_str());
     322        2424 :     if (IsNull(tmp)) {
     323           8 :         return -1;
     324             :     } else {
     325        2416 :         tmp.set_value(value.c_str());
     326        2416 :         SetContext(node_, tmp);
     327             :     }
     328             : 
     329        2416 :     return 0;
     330             : }
     331             : 
     332         243 : void XmlPugi::xmpp_buf_write::write(const void *data, size_t sz) {
     333         243 :     ref->SetBuf(data, sz);
     334         243 : }
     335             : 
     336         243 : void XmlPugi::SetBuf(const void *buf, size_t sz) {
     337         243 :     memcpy(buf_tmp_ + ts_, buf, sz);
     338         243 :     ts_ += sz;
     339         243 :     *(buf_tmp_+ts_) = '\0';
     340         243 : }

Generated by: LCOV version 1.14