xml-document-decl.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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 value if exists
  281. */
  282. template <class T>
  283. TMaybe<T> TryAttr(TZtStringBuf name) const;
  284. /**
  285. * get node attribute
  286. * @param name: attribute name
  287. * returns default value if attribute not found
  288. */
  289. template <class T>
  290. T Attr(TZtStringBuf name, const T& defvalue) const;
  291. /**
  292. * get node attribute
  293. * @param name: attribute name
  294. * @param value: return-value
  295. * throws exception if attribute not found
  296. */
  297. template <class T>
  298. void Attr(TZtStringBuf name, T& value) const;
  299. /**
  300. * get node attribute
  301. * @param name: attribute name
  302. * @param defvalue: default value
  303. * @param value: return-value
  304. * returns default value if attribute not found, attr value otherwise
  305. */
  306. template <class T>
  307. void Attr(TZtStringBuf name, T& value, const T& defvalue) const;
  308. /**
  309. * get node value (text)
  310. * @throws exception if node is blank
  311. */
  312. template <class T>
  313. T Value() const;
  314. /**
  315. * get node value
  316. * @param defvalue: default value
  317. * returns default value if node is blank
  318. */
  319. template <class T>
  320. T Value(const T& defvalue) const;
  321. /**
  322. * set node value
  323. * @param value: new text value
  324. */
  325. template <class T>
  326. typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type
  327. SetValue(const T& value);
  328. void SetValue(TStringBuf value);
  329. /**
  330. * set/reset node attribute value,
  331. * if attribute does not exist, it'll be created
  332. * @param name: attribute name
  333. * @param value: attribute value
  334. */
  335. template<class T>
  336. typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type
  337. SetAttr(TZtStringBuf name, const T& value);
  338. void SetAttr(TZtStringBuf name, TZtStringBuf value);
  339. void SetAttr(TZtStringBuf name);
  340. /**
  341. * delete node attribute
  342. * @param name: attribute name
  343. */
  344. void DelAttr(TZtStringBuf name);
  345. /**
  346. * set node application data
  347. * @param priv: new application data pointer
  348. */
  349. void SetPrivate(void* priv);
  350. /**
  351. * @return application data pointer, passed by SetPrivate
  352. */
  353. void* GetPrivate() const;
  354. /**
  355. * get node name
  356. */
  357. TString Name() const;
  358. /**
  359. * get node xpath
  360. */
  361. TString Path() const;
  362. /**
  363. * get node xml representation
  364. */
  365. TString ToString(TZtStringBuf enc = "") const {
  366. TStringStream s;
  367. Save(s, enc);
  368. return s.Str();
  369. }
  370. void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const;
  371. void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const;
  372. /**
  373. * get pointer to internal node
  374. */
  375. xmlNode* GetPtr();
  376. const xmlNode* GetPtr() const;
  377. /**
  378. * check if node is text-only node
  379. */
  380. bool IsText() const;
  381. /**
  382. * unlink node from parent and free
  383. */
  384. void Remove();
  385. /**
  386. * constructs null node
  387. */
  388. TNode()
  389. : NodePointer(nullptr)
  390. , DocPointer(nullptr)
  391. {
  392. }
  393. private:
  394. friend class TConstNodes;
  395. TNode(xmlDoc* doc, xmlNode* node)
  396. : NodePointer(node)
  397. , DocPointer(doc)
  398. {
  399. }
  400. TNode Find(xmlNode* start, TZtStringBuf name);
  401. template <class T>
  402. void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const;
  403. void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const;
  404. xmlNode* NodePointer;
  405. xmlDoc* DocPointer;
  406. };
  407. class TConstNode {
  408. public:
  409. friend class TDocument;
  410. friend class TConstNodes;
  411. friend class TNode;
  412. /**
  413. * check if node is null
  414. */
  415. bool IsNull() const {
  416. return ActualNode.IsNull();
  417. }
  418. bool IsElementNode() const {
  419. return ActualNode.IsElementNode();
  420. }
  421. TConstNode Parent() const {
  422. return ActualNode.Parent();
  423. }
  424. /**
  425. * Create xpath context to be used later for fast xpath evaluation.
  426. * @param nss: explicitly specify XML namespaces to use and their prefixes
  427. */
  428. TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const {
  429. return ActualNode.CreateXPathContext(nss);
  430. }
  431. /**
  432. * get all element nodes matching given xpath expression
  433. * @param xpath: xpath expression
  434. * @param quiet: don't throw exception if zero nodes found
  435. * @param ns: explicitly specify XML namespaces to use and their prefixes
  436. *
  437. * For historical reasons, this only works for *element* nodes.
  438. * Use the XPath function if you need other kinds of nodes.
  439. */
  440. TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  441. return ActualNode.Nodes(xpath, quiet, ns);
  442. }
  443. /**
  444. * get all element nodes matching given xpath expression
  445. * @param xpath: xpath expression
  446. * @param quiet: don't throw exception if zero nodes found
  447. * @param ctxt: reusable xpath context
  448. *
  449. * For historical reasons, this only works for *element* nodes.
  450. * Use the XPath function if you need other kinds of nodes.
  451. */
  452. TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  453. return ActualNode.Nodes(xpath, quiet, ctxt);
  454. }
  455. /**
  456. * get all nodes matching given xpath expression
  457. * @param xpath: xpath expression
  458. * @param quiet: don't throw exception if zero nodes found
  459. * @param ns: explicitly specify XML namespaces to use and their prefixes
  460. */
  461. TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  462. return ActualNode.XPath(xpath, quiet, ns);
  463. }
  464. /**
  465. * get all nodes matching given xpath expression
  466. * @param xpath: xpath expression
  467. * @param quiet: don't throw exception if zero nodes found
  468. * @param ctxt: reusable xpath context
  469. */
  470. TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  471. return ActualNode.XPath(xpath, quiet, ctxt);
  472. }
  473. /**
  474. * get the first element node matching given xpath expression
  475. * @param xpath: path to node (from current node)
  476. * @param quiet: don't throw exception if node not found,
  477. * return null node (@see IsNull())
  478. * @param ns: explicitly specify XML namespaces to use and their prefixes
  479. *
  480. * For historical reasons, this only works for *element* nodes.
  481. * Use the XPath function if you need other kinds of nodes.
  482. */
  483. TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const {
  484. return ActualNode.Node(xpath, quiet, ns);
  485. }
  486. /**
  487. * get the first element node matching given xpath expression
  488. * @param xpath: path to node (from current node)
  489. * @param quiet: don't throw exception if node not found,
  490. * return null node (@see IsNull())
  491. * @param ctxt: reusable xpath context
  492. *
  493. * For historical reasons, this only works for *element* nodes.
  494. * Use the XPath function if you need other kinds of nodes.
  495. */
  496. TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
  497. return ActualNode.Node(xpath, quiet, ctxt);
  498. }
  499. TConstNode FirstChild(TZtStringBuf name) const {
  500. return ActualNode.FirstChild(name);
  501. }
  502. TConstNode FirstChild() const {
  503. return ActualNode.FirstChild();
  504. }
  505. /**
  506. * get node neighbour
  507. * @param name: neighbour name
  508. * throws exception if no neighbour found
  509. */
  510. TConstNode NextSibling(TZtStringBuf name) const {
  511. return ActualNode.NextSibling(name);
  512. }
  513. TConstNode NextSibling() const {
  514. return ActualNode.NextSibling();
  515. }
  516. /**
  517. * get node attribute
  518. * @param name: attribute name
  519. * returns value if exists
  520. */
  521. template <class T>
  522. TMaybe<T> TryAttr(TZtStringBuf name) const {
  523. return ActualNode.TryAttr<T>(name);
  524. }
  525. /**
  526. * get node attribute
  527. * @param name: attribute name
  528. * throws exception if attribute not found
  529. */
  530. template <class T>
  531. T Attr(TZtStringBuf name) const {
  532. return ActualNode.Attr<T>(name);
  533. }
  534. /**
  535. * get node attribute
  536. * @param name: attribute name
  537. * returns default value if attribute not found
  538. */
  539. template <class T>
  540. T Attr(TZtStringBuf name, const T& defvalue) const {
  541. return ActualNode.Attr(name, defvalue);
  542. }
  543. /**
  544. * get node attribute
  545. * @param name: attribute name
  546. * @param value: return-value
  547. * throws exception if attribute not found
  548. */
  549. template <class T>
  550. void Attr(TZtStringBuf name, T& value) const {
  551. return ActualNode.Attr(name, value);
  552. }
  553. /**
  554. * get node attribute
  555. * @param name: attribute name
  556. * @param defvalue: default value
  557. * @param value: return-value
  558. * returns default value if attribute not found, attr value otherwise
  559. */
  560. template <class T>
  561. void Attr(TZtStringBuf name, T& value, const T& defvalue) const {
  562. return ActualNode.Attr(name, value, defvalue);
  563. }
  564. /**
  565. * get node value (text)
  566. * @throws exception if node is blank
  567. */
  568. template <class T>
  569. T Value() const {
  570. return ActualNode.Value<T>();
  571. }
  572. /**
  573. * get node value
  574. * @param defvalue: default value
  575. * returns default value if node is blank
  576. */
  577. template <class T>
  578. T Value(const T& defvalue) const {
  579. return ActualNode.Value(defvalue);
  580. }
  581. /**
  582. * get node name
  583. */
  584. TString Name() const {
  585. return ActualNode.Name();
  586. }
  587. /**
  588. * @return application data pointer, passed by SetPrivate
  589. */
  590. void* GetPrivate() const {
  591. return ActualNode.GetPrivate();
  592. }
  593. /**
  594. * get pointer to internal node
  595. */
  596. const xmlNode* GetPtr() const {
  597. return ActualNode.GetPtr();
  598. }
  599. /**
  600. * check if node is text-only node
  601. */
  602. bool IsText() const {
  603. return ActualNode.IsText();
  604. }
  605. /**
  606. * get node xpath
  607. */
  608. TString Path() const {
  609. return ActualNode.Path();
  610. }
  611. /**
  612. * get node xml representation
  613. */
  614. TString ToString(TZtStringBuf enc = "") const {
  615. return ActualNode.ToString(enc);
  616. }
  617. TConstNode() = default;
  618. TConstNode(TNode node)
  619. : ActualNode(node)
  620. {
  621. }
  622. TNode ConstCast() const {
  623. return ActualNode;
  624. }
  625. private:
  626. TNode ActualNode;
  627. };
  628. }