xml-document-decl.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #pragma once
  2. #include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h>
  3. #include <util/generic/string.h>
  4. #include <util/generic/vector.h>
  5. #include <util/stream/output.h>
  6. #include <util/stream/str.h>
  7. #include <algorithm>
  8. #include "libxml-guards.h"
  9. namespace NXml {
  10. class TNode;
  11. class TConstNodes;
  12. class TConstNode;
  13. using TXPathContext = xmlXPathContext;
  14. class TDocument {
  15. public:
  16. enum Source {
  17. File,
  18. String,
  19. RootName,
  20. };
  21. public:
  22. /**
  23. * create TDocument
  24. * @param source: filename, XML string, or name for the root element (depends on @src)
  25. * @param src: source type: File | String | RootName
  26. * throws if file not found or cannot be parsed
  27. */
  28. TDocument(const TString& source, Source type = File);
  29. public:
  30. TDocument(const TDocument& that) = delete;
  31. TDocument& operator=(const TDocument& that) = delete;
  32. TDocument(TDocument&& that);
  33. TDocument& operator=(TDocument&& that);
  34. /**
  35. * get root element
  36. */
  37. TNode Root();
  38. TConstNode Root() const;
  39. void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const {
  40. int bufferSize = 0;
  41. xmlChar* xmlBuff = nullptr;
  42. const char* encoding = enc.size() ? enc.data() : Doc->encoding ? nullptr : "UTF-8";
  43. xmlDocDumpFormatMemoryEnc(Doc.Get(), &xmlBuff, &bufferSize, encoding, shouldFormat);
  44. TCharPtr xmlCharBuffPtr(xmlBuff);
  45. stream.Write(xmlBuff, bufferSize);
  46. }
  47. TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const {
  48. TStringStream s;
  49. Save(s, enc, shouldFormat);
  50. return s.Str();
  51. }
  52. void Swap(TDocument& that) {
  53. std::swap(this->Doc, that.Doc);
  54. }
  55. xmlDocPtr GetImpl() {
  56. return Doc.Get();
  57. }
  58. private:
  59. void ParseFile(const TString& file);
  60. void ParseString(TZtStringBuf xml);
  61. TDocument(TDocHolder doc)
  62. : Doc(std::move(doc))
  63. {
  64. }
  65. TDocHolder Doc;
  66. };
  67. struct TNamespaceForXPath {
  68. TString Prefix;
  69. TString Url;
  70. };
  71. typedef TVector<TNamespaceForXPath> TNamespacesForXPath;
  72. class TConstNodes {
  73. private:
  74. struct TConstNodesRef {
  75. explicit TConstNodesRef(TConstNodes& n)
  76. : r_(n)
  77. {
  78. }
  79. TConstNodes& r_;
  80. };
  81. public:
  82. TConstNodes(const TConstNodes& nodes);
  83. TConstNodes& operator=(const TConstNodes& nodes);
  84. TConstNodes(TConstNodesRef ref);
  85. TConstNodes& operator=(TConstNodesRef ref);
  86. operator TConstNodesRef();
  87. /**
  88. * get node by id
  89. * @param number: node id
  90. */
  91. TConstNode operator[](size_t number) const;
  92. /**
  93. * get number of nodes
  94. */
  95. size_t Size() const {
  96. return SizeValue;
  97. }
  98. size_t size() const {
  99. return SizeValue;
  100. }
  101. struct TNodeIter {
  102. const TConstNodes& Nodes;
  103. size_t Index;
  104. TConstNode operator*() const;
  105. bool operator==(const TNodeIter& other) const {
  106. return Index == other.Index;
  107. }
  108. bool operator!=(const TNodeIter& other) const {
  109. return !(*this == other);
  110. }
  111. TNodeIter operator++() {
  112. Index++;
  113. return *this;
  114. }
  115. };
  116. TNodeIter begin() const {
  117. return TNodeIter{*this, 0};
  118. }
  119. TNodeIter end() const {
  120. return TNodeIter{*this, size()};
  121. }
  122. private:
  123. friend class TDocument;
  124. friend class TConstNode;
  125. friend class TNode;
  126. TConstNodes(xmlDoc* doc, TXPathObjectPtr obj);
  127. size_t SizeValue;
  128. xmlDoc* Doc;
  129. TXPathObjectPtr Obj;
  130. };
  131. class TNode {
  132. public:
  133. friend class TDocument;
  134. friend class TConstNode;
  135. friend class TTextReader;
  136. /**
  137. * check if node is null
  138. */
  139. bool IsNull() const;
  140. /**
  141. * check if node is element node
  142. */
  143. bool IsElementNode() const;
  144. /**
  145. * Create xpath context to be used later for fast xpath evaluation.
  146. * @param nss: explicitly specify XML namespaces to use and their prefixes
  147. *
  148. * For better performance, when you need to evaluate several xpath expressions,
  149. * it makes sense to create a context, load namespace prefixes once
  150. * and use the context several times in Node(), Nodes(), XPath() function calls for several nodes.
  151. * The context may be used with any node of the current document, but
  152. * cannot be shared between different XML documents.
  153. */
  154. TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const;
  155. /**
  156. * get all element nodes matching given xpath expression
  157. * @param xpath: xpath expression
  158. * @param quiet: don't throw exception if zero nodes found
  159. * @param ns: explicitly specify XML namespaces to use and their prefixes
  160. *
  161. * For historical reasons, this only works for *element* nodes.
  162. * Use the XPath function if you need other kinds of nodes.
  163. */
  164. TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const;
  165. /**
  166. * get all element nodes matching given xpath expression
  167. * @param xpath: xpath expression
  168. * @param quiet: don't throw exception if zero nodes found
  169. * @param ctxt: reusable xpath context
  170. *
  171. * For historical reasons, this only works for *element* nodes.
  172. * Use the XPath function if you need other kinds of nodes.
  173. */
  174. TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const;
  175. /**
  176. * get all nodes matching given xpath expression
  177. * @param xpath: xpath expression
  178. * @param quiet: don't throw exception if zero nodes found
  179. * @param ns: explicitly specify XML namespaces to use and their prefixes
  180. */
  181. TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const;
  182. /**
  183. * get all nodes matching given xpath expression
  184. * @param xpath: xpath expression
  185. * @param quiet: don't throw exception if zero nodes found
  186. * @param ctxt: reusable xpath context
  187. */
  188. TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const;
  189. /**
  190. * get the first element node matching given xpath expression
  191. * @param xpath: path to node (from current node)
  192. * @param quiet: don't throw exception if node not found,
  193. * return null node (@see IsNull())
  194. * @param ns: explicitly specify XML namespaces to use and their prefixes
  195. *
  196. * For historical reasons, this only works for *element* nodes.
  197. * Use the XPath function if you need other kinds of nodes.
  198. */
  199. /// @todo: quiet should be default, empty nodeset is not an error
  200. TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath());
  201. TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const;
  202. /**
  203. * get the first element node matching given xpath expression
  204. * @param xpath: path to node (from current node)
  205. * @param quiet: don't throw exception if node not found,
  206. * return null node (@see IsNull())
  207. * @param ctxt: reusable xpath context
  208. *
  209. * For historical reasons, this only works for *element* nodes.
  210. * Use the XPath function if you need other kinds of nodes.
  211. */
  212. TNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt);
  213. TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const;
  214. /**
  215. * get node first child
  216. * @param name: child name
  217. * @note if name is empty, returns the first child node of type "element"
  218. * @note returns null node if no child found
  219. */
  220. TNode FirstChild(TZtStringBuf name);
  221. TConstNode FirstChild(TZtStringBuf name) const;
  222. TNode FirstChild();
  223. TConstNode FirstChild() const;
  224. /**
  225. * get parent node
  226. * throws exception if has no parent
  227. */
  228. TNode Parent();
  229. TConstNode Parent() const;
  230. /**
  231. * get node neighbour
  232. * @param name: neighbour name
  233. * @note if name is empty, returns the next sibling node of type "element"
  234. * @node returns null node if no neighbour found
  235. */
  236. TNode NextSibling(TZtStringBuf name);
  237. TConstNode NextSibling(TZtStringBuf name) const;
  238. TNode NextSibling();
  239. TConstNode NextSibling() const;
  240. /**
  241. * create child node
  242. * @param name: child name
  243. * returns new empty node
  244. */
  245. TNode AddChild(TZtStringBuf name);
  246. /**
  247. * create child node with given value
  248. * @param name: child name
  249. * @param value: node value
  250. */
  251. template <class T>
  252. typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type
  253. AddChild(TZtStringBuf name, const T& value);
  254. TNode AddChild(TZtStringBuf name, TZtStringBuf value);
  255. /**
  256. * add child node, making recursive copy of original
  257. * @param node: node to copy from
  258. * returns added node
  259. */
  260. TNode AddChild(const TConstNode& node);
  261. /**
  262. * create text child node
  263. * @param name: child name
  264. * @param value: node value
  265. */
  266. template <class T>
  267. typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type
  268. AddText(const T& value);
  269. TNode AddText(TStringBuf value);
  270. /**
  271. * get node attribute
  272. * @param name: attribute name
  273. * throws exception if attribute not found
  274. */
  275. template <class T>
  276. T Attr(TZtStringBuf name) const;
  277. /**
  278. * get node attribute
  279. * @param name: attribute name
  280. * returns default value if attribute not found
  281. */
  282. template <class T>
  283. T Attr(TZtStringBuf name, const T& defvalue) const;
  284. /**
  285. * get node attribute
  286. * @param name: attribute name
  287. * @param value: return-value
  288. * throws exception if attribute not found
  289. */
  290. template <class T>
  291. void Attr(TZtStringBuf name, T& value) const;
  292. /**
  293. * get node attribute
  294. * @param name: attribute name
  295. * @param defvalue: default value
  296. * @param value: return-value
  297. * returns default value if attribute not found, attr value otherwise
  298. */
  299. template <class T>
  300. void Attr(TZtStringBuf name, T& value, const T& defvalue) const;
  301. /**
  302. * get node value (text)
  303. * @throws exception if node is blank
  304. */
  305. template <class T>
  306. T Value() const;
  307. /**
  308. * get node value
  309. * @param defvalue: default value
  310. * returns default value if node is blank
  311. */
  312. template <class T>
  313. T Value(const T& defvalue) const;
  314. /**
  315. * set node value
  316. * @param value: new text value
  317. */
  318. template <class T>
  319. typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type
  320. SetValue(const T& value);
  321. void SetValue(TStringBuf value);
  322. /**
  323. * set/reset node attribute value,
  324. * if attribute does not exist, it'll be created
  325. * @param name: attribute name
  326. * @param value: attribute value
  327. */
  328. template<class T>
  329. typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type
  330. SetAttr(TZtStringBuf name, const T& value);
  331. void SetAttr(TZtStringBuf name, TZtStringBuf value);
  332. void SetAttr(TZtStringBuf name);
  333. /**
  334. * delete node attribute
  335. * @param name: attribute name
  336. */
  337. void DelAttr(TZtStringBuf name);
  338. /**
  339. * set node application data
  340. * @param priv: new application data pointer
  341. */
  342. void SetPrivate(void* priv);
  343. /**
  344. * @return application data pointer, passed by SetPrivate
  345. */
  346. void* GetPrivate() const;
  347. /**
  348. * get node name
  349. */
  350. TString Name() const;
  351. /**
  352. * get node xpath
  353. */
  354. TString Path() const;
  355. /**
  356. * get node xml representation
  357. */
  358. TString ToString(TZtStringBuf enc = "") const {
  359. TStringStream s;
  360. Save(s, enc);
  361. return s.Str();
  362. }
  363. void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const;
  364. void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const;
  365. /**
  366. * get pointer to internal node
  367. */
  368. xmlNode* GetPtr();
  369. const xmlNode* GetPtr() const;
  370. /**
  371. * check if node is text-only node
  372. */
  373. bool IsText() const;
  374. /**
  375. * unlink node from parent and free
  376. */
  377. void Remove();
  378. /**
  379. * constructs null node
  380. */
  381. TNode()
  382. : NodePointer(nullptr)
  383. , DocPointer(nullptr)
  384. {
  385. }
  386. private:
  387. friend class TConstNodes;
  388. TNode(xmlDoc* doc, xmlNode* node)
  389. : NodePointer(node)
  390. , DocPointer(doc)
  391. {
  392. }
  393. TNode Find(xmlNode* start, TZtStringBuf name);
  394. template <class T>
  395. void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const;
  396. void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const;
  397. xmlNode* NodePointer;
  398. xmlDoc* DocPointer;
  399. };
  400. class TConstNode {
  401. public:
  402. friend class TDocument;
  403. friend class TConstNodes;
  404. friend class TNode;
  405. /**
  406. * check if node is null
  407. */
  408. bool IsNull() const {
  409. return ActualNode.IsNull();
  410. }
  411. bool IsElementNode() const {
  412. return ActualNode.IsElementNode();
  413. }
  414. TConstNode Parent() const {
  415. return ActualNode.Parent();
  416. }
  417. /**
  418. * Create xpath context to be used later for fast xpath evaluation.
  419. * @param nss: explicitly specify XML namespaces to use and their prefixes
  420. */
  421. TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const {
  422. return ActualNode.CreateXPathContext(nss);
  423. }
  424. /**
  425. * get all element nodes matching given xpath expression
  426. * @param xpath: xpath expression
  427. * @param quiet: don't throw exception if zero nodes found
  428. * @param ns: explicitly specify XML namespaces to use and their prefixes
  429. *
  430. * For historical reasons, this only works for *element* nodes.
  431. * Use the XPath function if you need other kinds of nodes.
  432. */
  433. TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  434. return ActualNode.Nodes(xpath, quiet, ns);
  435. }
  436. /**
  437. * get all element nodes matching given xpath expression
  438. * @param xpath: xpath expression
  439. * @param quiet: don't throw exception if zero nodes found
  440. * @param ctxt: reusable xpath context
  441. *
  442. * For historical reasons, this only works for *element* nodes.
  443. * Use the XPath function if you need other kinds of nodes.
  444. */
  445. TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  446. return ActualNode.Nodes(xpath, quiet, ctxt);
  447. }
  448. /**
  449. * get all nodes matching given xpath expression
  450. * @param xpath: xpath expression
  451. * @param quiet: don't throw exception if zero nodes found
  452. * @param ns: explicitly specify XML namespaces to use and their prefixes
  453. */
  454. TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  455. return ActualNode.XPath(xpath, quiet, ns);
  456. }
  457. /**
  458. * get all nodes matching given xpath expression
  459. * @param xpath: xpath expression
  460. * @param quiet: don't throw exception if zero nodes found
  461. * @param ctxt: reusable xpath context
  462. */
  463. TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  464. return ActualNode.XPath(xpath, quiet, ctxt);
  465. }
  466. /**
  467. * get the first element node matching given xpath expression
  468. * @param xpath: path to node (from current node)
  469. * @param quiet: don't throw exception if node not found,
  470. * return null node (@see IsNull())
  471. * @param ns: explicitly specify XML namespaces to use and their prefixes
  472. *
  473. * For historical reasons, this only works for *element* nodes.
  474. * Use the XPath function if you need other kinds of nodes.
  475. */
  476. TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  477. return ActualNode.Node(xpath, quiet, ns);
  478. }
  479. /**
  480. * get the first element node matching given xpath expression
  481. * @param xpath: path to node (from current node)
  482. * @param quiet: don't throw exception if node not found,
  483. * return null node (@see IsNull())
  484. * @param ctxt: reusable xpath context
  485. *
  486. * For historical reasons, this only works for *element* nodes.
  487. * Use the XPath function if you need other kinds of nodes.
  488. */
  489. TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  490. return ActualNode.Node(xpath, quiet, ctxt);
  491. }
  492. TConstNode FirstChild(TZtStringBuf name) const {
  493. return ActualNode.FirstChild(name);
  494. }
  495. TConstNode FirstChild() const {
  496. return ActualNode.FirstChild();
  497. }
  498. /**
  499. * get node neighbour
  500. * @param name: neighbour name
  501. * throws exception if no neighbour found
  502. */
  503. TConstNode NextSibling(TZtStringBuf name) const {
  504. return ActualNode.NextSibling(name);
  505. }
  506. TConstNode NextSibling() const {
  507. return ActualNode.NextSibling();
  508. }
  509. /**
  510. * get node attribute
  511. * @param name: attribute name
  512. * throws exception if attribute not found
  513. */
  514. template <class T>
  515. T Attr(TZtStringBuf name) const {
  516. return ActualNode.Attr<T>(name);
  517. }
  518. /**
  519. * get node attribute
  520. * @param name: attribute name
  521. * returns default value if attribute not found
  522. */
  523. template <class T>
  524. T Attr(TZtStringBuf name, const T& defvalue) const {
  525. return ActualNode.Attr(name, defvalue);
  526. }
  527. /**
  528. * get node attribute
  529. * @param name: attribute name
  530. * @param value: return-value
  531. * throws exception if attribute not found
  532. */
  533. template <class T>
  534. void Attr(TZtStringBuf name, T& value) const {
  535. return ActualNode.Attr(name, value);
  536. }
  537. /**
  538. * get node attribute
  539. * @param name: attribute name
  540. * @param defvalue: default value
  541. * @param value: return-value
  542. * returns default value if attribute not found, attr value otherwise
  543. */
  544. template <class T>
  545. void Attr(TZtStringBuf name, T& value, const T& defvalue) const {
  546. return ActualNode.Attr(name, value, defvalue);
  547. }
  548. /**
  549. * get node value (text)
  550. * @throws exception if node is blank
  551. */
  552. template <class T>
  553. T Value() const {
  554. return ActualNode.Value<T>();
  555. }
  556. /**
  557. * get node value
  558. * @param defvalue: default value
  559. * returns default value if node is blank
  560. */
  561. template <class T>
  562. T Value(const T& defvalue) const {
  563. return ActualNode.Value(defvalue);
  564. }
  565. /**
  566. * get node name
  567. */
  568. TString Name() const {
  569. return ActualNode.Name();
  570. }
  571. /**
  572. * @return application data pointer, passed by SetPrivate
  573. */
  574. void* GetPrivate() const {
  575. return ActualNode.GetPrivate();
  576. }
  577. /**
  578. * get pointer to internal node
  579. */
  580. const xmlNode* GetPtr() const {
  581. return ActualNode.GetPtr();
  582. }
  583. /**
  584. * check if node is text-only node
  585. */
  586. bool IsText() const {
  587. return ActualNode.IsText();
  588. }
  589. /**
  590. * get node xpath
  591. */
  592. TString Path() const {
  593. return ActualNode.Path();
  594. }
  595. /**
  596. * get node xml representation
  597. */
  598. TString ToString(TZtStringBuf enc = "") const {
  599. return ActualNode.ToString(enc);
  600. }
  601. TConstNode() = default;
  602. TConstNode(TNode node)
  603. : ActualNode(node)
  604. {
  605. }
  606. TNode ConstCast() const {
  607. return ActualNode;
  608. }
  609. private:
  610. TNode ActualNode;
  611. };
  612. }