node-attr.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. #include "xml-document-decl.h"
  3. #include "libxml-guards.h"
  4. #include <util/stream/str.h>
  5. #include <util/string/cast.h>
  6. namespace NXml {
  7. #define THROW(x, y) ythrow yexception() << #x << ": " << y
  8. // libxml defines unsigned char -> xmlChar,
  9. // and all functions use xmlChar.
  10. inline static const char* CAST2CHAR(const xmlChar* x) {
  11. return reinterpret_cast<const char*>(x);
  12. }
  13. inline static const xmlChar* XMLCHAR(const char* x) {
  14. return reinterpret_cast<const xmlChar*>(x);
  15. }
  16. template <class T>
  17. void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const {
  18. try {
  19. res = FromString<T>(CAST2CHAR(value.Get()));
  20. } catch (TFromStringException&) {
  21. THROW(XmlException, "Failed to convert string " << TString{TStringBuf(CAST2CHAR(value.Get())).substr(0, 50)}.Quote() << " from '" << errContext << "' to requested type");
  22. }
  23. }
  24. template <>
  25. inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const {
  26. TString tmp(CAST2CHAR(value.Get()));
  27. res.swap(tmp);
  28. }
  29. template <class T>
  30. T TNode::Attr(TZtStringBuf name) const {
  31. TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str())));
  32. if (!value) {
  33. THROW(AttributeNotFound, Path() << "@" << name);
  34. }
  35. T t;
  36. AttrInternal(value, t, name);
  37. return t;
  38. }
  39. template <class T>
  40. T TNode::Attr(TZtStringBuf name, const T& defvalue) const {
  41. TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str())));
  42. if (!attr) {
  43. return defvalue;
  44. }
  45. T t;
  46. AttrInternal(attr, t, name);
  47. return t;
  48. }
  49. template <class T>
  50. void TNode::Attr(TZtStringBuf name, T& value) const {
  51. TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str())));
  52. if (!attr) {
  53. THROW(AttributeNotFound, Path() << name);
  54. }
  55. AttrInternal(attr, value, name);
  56. }
  57. template <class T>
  58. void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const {
  59. TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str())));
  60. if (!attr) {
  61. value = defvalue;
  62. } else {
  63. AttrInternal(attr, value, name);
  64. }
  65. }
  66. template <class T>
  67. T TNode::Value() const {
  68. if (!NodePointer || xmlIsBlankNode(NodePointer)) {
  69. THROW(NodeIsBlank, Path());
  70. }
  71. TCharPtr val(xmlNodeGetContent(NodePointer));
  72. T t;
  73. AttrInternal(val, t, this->Name());
  74. return t;
  75. }
  76. template <class T>
  77. T TNode::Value(const T& defvalue) const {
  78. if (!NodePointer || xmlIsBlankNode(NodePointer)) {
  79. return defvalue;
  80. }
  81. TCharPtr val(xmlNodeGetContent(NodePointer));
  82. T t;
  83. AttrInternal(val, t, this->Name());
  84. return t;
  85. }
  86. template <class T>
  87. typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type
  88. TNode::SetValue(const T& value) {
  89. TStringStream ss;
  90. ss << value;
  91. SetValue(ss.Str());
  92. }
  93. inline void TNode::SetValue(TStringBuf value) {
  94. xmlNodeSetContent(NodePointer, XMLCHAR(""));
  95. xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size());
  96. }
  97. inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) {
  98. xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str()));
  99. if (!attr) {
  100. THROW(XmlException, "Can't set node attribute <"
  101. << name
  102. << "> to <"
  103. << value
  104. << ">");
  105. }
  106. }
  107. template <class T>
  108. typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type
  109. TNode::SetAttr(TZtStringBuf name, const T& value) {
  110. TStringStream ss;
  111. ss << value;
  112. SetAttr(name, TZtStringBuf(ss.Str()));
  113. }
  114. inline void TNode::SetAttr(TZtStringBuf name) {
  115. xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr);
  116. if (!attr) {
  117. THROW(XmlException, "Can't set node empty attribute <"
  118. << name
  119. << ">");
  120. }
  121. }
  122. inline void TNode::DelAttr(TZtStringBuf name) {
  123. if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0)
  124. THROW(XmlException, "Can't delete node attribute <"
  125. << name
  126. << ">");
  127. }
  128. template <class T>
  129. typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type
  130. TNode::AddChild(TZtStringBuf name, const T& value) {
  131. TStringStream ss;
  132. ss << value;
  133. return AddChild(name, TZtStringBuf(ss.Str()));
  134. }
  135. inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) {
  136. if (IsNull()) {
  137. THROW(XmlException, "addChild [name=" << name << ", value=" << value
  138. << "]: can't add child to null node");
  139. }
  140. xmlNode* child = nullptr;
  141. if (value.empty()) {
  142. child = xmlNewTextChild(NodePointer, nullptr, XMLCHAR(name.c_str()), nullptr);
  143. } else {
  144. child = xmlNewTextChild(
  145. NodePointer, nullptr, XMLCHAR(name.c_str()), XMLCHAR(value.c_str()));
  146. }
  147. if (!child) {
  148. THROW(XmlException, "addChild [name=" << name << ", value=" << value
  149. << "]: xmlNewTextChild returned NULL");
  150. }
  151. return TNode(DocPointer, child);
  152. }
  153. template <class T>
  154. typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type
  155. TNode::AddText(const T& value) {
  156. TStringStream ss;
  157. ss << value;
  158. return AddText(ss.Str());
  159. }
  160. inline TNode TNode::AddText(TStringBuf value) {
  161. if (IsNull()) {
  162. THROW(XmlException, "addChild [value=" << value
  163. << "]: can't add child to null node");
  164. }
  165. xmlNode* child = xmlNewTextLen((xmlChar*)value.data(), value.size());
  166. child = xmlAddChild(NodePointer, child);
  167. if (!child) {
  168. THROW(XmlException, "addChild [value=" << value
  169. << "]: xmlNewTextChild returned NULL");
  170. }
  171. return TNode(DocPointer, child);
  172. }
  173. }