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: 69 208 33.2 %
Date: 2026-11-23 02:04:52 Functions: 15 36 41.7 %
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        9671 : XmlPugi::XmlPugi() : writer_(this), node_(GNode), attrib_(GAttr) {
      19        9666 : }
      20             : 
      21       19342 : XmlPugi::~XmlPugi() {
      22        9671 :     doc_.reset();
      23       19338 : }
      24             : 
      25           0 : pugi::xml_node XmlPugi::RootNode() {
      26           0 :     return(doc_.root());
      27             : }
      28             : 
      29        8401 : const char *XmlPugi::ReadNode(const std::string &name) {
      30        8401 :     PugiPredicate p1(name);
      31             : 
      32        8402 :     pugi::xml_node node = doc_.find_node(p1);
      33        8402 :     if (IsNull(node)) {
      34         320 :         return NULL;
      35             :     }
      36             : 
      37        8082 :     SetContext(node);
      38        8082 :     if (node.type() != pugi::node_pi) {
      39       15000 :         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       13852 :             if (node.type() == pugi::node_pcdata ||
      43        6918 :                 node.type() == pugi::node_cdata)
      44          15 :                break;
      45             :         }
      46             :     }
      47             : 
      48        8078 :     return node.value();
      49             : 
      50        8401 : }
      51             : 
      52        6866 : pugi::xml_node XmlPugi::FindNode(const std::string &name) {
      53        6866 :     PugiPredicate p1(name);
      54             : 
      55        6866 :     pugi::xml_node node = doc_.find_node(p1);
      56        6866 :     return node;
      57        6866 : }
      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        1322 : const char *XmlPugi::ReadChildNodeName() {
      91        1322 :     pugi::xml_node tmp = (node_ == GNode) ? doc_.first_child() :
      92        1322 :                           node_.first_child();
      93        1322 :     SetContext(tmp);
      94        2644 :     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       16294 : const char *XmlPugi::ReadAttrib(const std::string &str) {
     116       16294 :     pugi::xml_attribute tmp = node_.attribute(str.c_str());
     117       16290 :     SetContext(node_, tmp);
     118       32579 :     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          39 : int XmlPugi::WriteDoc(uint8_t *buf) {
     144          39 :     buf_tmp_ = buf; // this will be used by writer_->write
     145          39 :     ts_ = 0;
     146          39 :     doc_.save(writer_, "", pugi::format_default, pugi::encoding_utf8);
     147             : 
     148          39 :     if (ts_ == 0) return -1;
     149          39 :     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         131 : void XmlPugi::PrintDoc(std::ostream& os) const {
     162         131 :     doc_.print(os, " ", pugi::format_raw | pugi::format_no_declaration);
     163         131 : }
     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       31286 : void XmlPugi::SetContext(pugi::xml_node node, pugi::xml_attribute attrib) {
     170       31286 :     node_ = node;
     171       31286 :     attrib_ = attrib;
     172       31286 : }
     173             : 
     174        4616 : int XmlPugi::LoadDoc(const std::string &document) {
     175        4616 :     RewindDoc();
     176        4617 :     doc_.reset();
     177             : 
     178        4616 :     pugi::xml_parse_result ret = doc_.load_buffer(document.c_str(), document.size(),
     179             :                                                  pugi::parse_default,
     180             :                                                  pugi::encoding_utf8);
     181        4617 :     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        4617 :     return 0;
     188             : }
     189             : 
     190        4616 : void XmlPugi::RewindDoc() {
     191        4616 :     SetContext();
     192        4617 : }
     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           0 : int XmlPugi::AddNode(const std::string &key, const std::string &value) {
     214           0 :     pugi::xml_node node;
     215             : 
     216           0 :     if (IsNull(node_)) {
     217           0 :         node = doc_.append_child(key.c_str());
     218             :     } else {
     219           0 :         node = node_.parent().append_child(key.c_str());
     220             :     }
     221           0 :     if (value != "") {
     222           0 :         node.text().set(value.c_str());
     223             :     }
     224             : 
     225           0 :     SetContext(node);
     226           0 :     return 0;
     227             : }
     228             : 
     229           0 : int XmlPugi::DeleteNode(const std::string &key) {
     230           0 :     PugiPredicate p1(key);
     231           0 :     pugi::xml_node node = doc_.find_node(p1);
     232           0 :     if (IsNull(node))
     233           0 :         return -1;
     234             : 
     235           0 :     node.parent().remove_child(key.c_str());
     236           0 :     return 0;
     237           0 : }
     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           0 : int XmlPugi::AddChildNode(const std::string &key, const std::string &value) {
     252           0 :     pugi::xml_node node;
     253             : 
     254           0 :     if (IsNull(node_)) {
     255           0 :         node = doc_.append_child(key.c_str());
     256             :     } else {
     257           0 :         node = node_.append_child(key.c_str());
     258             :     }
     259           0 :     if (value != "") {
     260           0 :         node.text().set(value.c_str());
     261             :     }
     262             : 
     263           0 :     SetContext(node);
     264           0 :     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           0 : int XmlPugi::AddAttribute(const std::string &key, const std::string &value) {
     284           0 :     if (IsNull(node_))
     285           0 :         return -1;
     286             : 
     287           0 :     pugi::xml_attribute attrib;
     288             : 
     289           0 :     if (IsNull(attrib_) ) {
     290           0 :         attrib = node_.append_attribute(key.c_str()) = value.c_str();
     291             :     } else {
     292           0 :         attrib =
     293           0 :             node_.insert_attribute_after(key.c_str(), attrib_) = value.c_str();
     294             :     }
     295             : 
     296           0 :     SetContext(node_, attrib);
     297           0 :     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         981 : int XmlPugi::ModifyAttribute(const std::string &key, const std::string &value) {
     317         981 :     if (node_.type() == pugi::node_null)
     318           0 :         return -1;
     319             : 
     320             :     //ReadAttrib(key);
     321         981 :     pugi::xml_attribute tmp = node_.attribute(key.c_str());
     322         981 :     if (IsNull(tmp)) {
     323           0 :         return -1;
     324             :     } else {
     325         981 :         tmp.set_value(value.c_str());
     326         981 :         SetContext(node_, tmp);
     327             :     }
     328             : 
     329         981 :     return 0;
     330             : }
     331             : 
     332          39 : void XmlPugi::xmpp_buf_write::write(const void *data, size_t sz) {
     333          39 :     ref->SetBuf(data, sz);
     334          39 : }
     335             : 
     336          39 : void XmlPugi::SetBuf(const void *buf, size_t sz) {
     337          39 :     memcpy(buf_tmp_ + ts_, buf, sz);
     338          39 :     ts_ += sz;
     339          39 :     *(buf_tmp_+ts_) = '\0';
     340          39 : }

Generated by: LCOV version 1.14