diff options
author | myltsev <myltsev@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:03 +0300 |
commit | 9166d66c30c23c9e85a7c88185a068987148d23f (patch) | |
tree | 421bdcec5755d9e441c485560aab5ab8d74c7475 /library/cpp/xml/document | |
parent | 8d3a5ed3a188a34167eaee54f1691ce5c9edf2f3 (diff) | |
download | ydb-9166d66c30c23c9e85a7c88185a068987148d23f.tar.gz |
Restoring authorship annotation for <myltsev@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/xml/document')
-rw-r--r-- | library/cpp/xml/document/README | 36 | ||||
-rw-r--r-- | library/cpp/xml/document/libxml-guards.h | 18 | ||||
-rw-r--r-- | library/cpp/xml/document/node-attr.h | 80 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 302 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.cpp | 118 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.h | 4 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document_ut.cpp | 112 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-textreader.cpp | 10 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-textreader.h | 14 | ||||
-rw-r--r-- | library/cpp/xml/document/ya.make | 2 |
10 files changed, 348 insertions, 348 deletions
diff --git a/library/cpp/xml/document/README b/library/cpp/xml/document/README index b2649523d8..c7396aec30 100644 --- a/library/cpp/xml/document/README +++ b/library/cpp/xml/document/README @@ -1,20 +1,20 @@ -A wrapper around the DOM interface of libxml2. - -The standard way to use it is as follows: - +A wrapper around the DOM interface of libxml2. + +The standard way to use it is as follows: + #include <library/cpp/xml/document/xml-document.h> - ... - - // open a document - NXml::TDocument xml("filename.xml"); - - // get a nodeset from an XPath query - NXml::TConstNodes nodes = xml.Root().Nodes("xpath/expression/here"); - - // iterate over the nodeset - for (size_t i = 0; i < nodes.size(); ++i) { + ... + + // open a document + NXml::TDocument xml("filename.xml"); + + // get a nodeset from an XPath query + NXml::TConstNodes nodes = xml.Root().Nodes("xpath/expression/here"); + + // iterate over the nodeset + for (size_t i = 0; i < nodes.size(); ++i) { using namespace NXml; - TConstNode& node = nodes[i]; + TConstNode& node = nodes[i]; // query node TString name = node.Name(); TString lang = node.Attr<TString>("lang"); @@ -26,8 +26,8 @@ The standard way to use it is as follows: node.SetAttr("x", 2); node.SetValue(5); node.AddText(" apples"); - } - + } + // edit documents with copy-paste NXml::TDocument xml2("<xpath><node/></xpath>", NXml::TDocument::String); NXml::TNode place = xml2.Root().Node("xpath/node"); @@ -39,4 +39,4 @@ The standard way to use it is as follows: TString modifiedDoc = xml2.ToString("ISO-8559-1"); -See xml-document_ut.cpp for more examples. +See xml-document_ut.cpp for more examples. diff --git a/library/cpp/xml/document/libxml-guards.h b/library/cpp/xml/document/libxml-guards.h index 4188cecff1..619ca23ce7 100644 --- a/library/cpp/xml/document/libxml-guards.h +++ b/library/cpp/xml/document/libxml-guards.h @@ -2,10 +2,10 @@ #include <library/cpp/xml/init/ptr.h> #include <util/generic/ptr.h> -#include <libxml/xmlstring.h> -#include <libxml/tree.h> -#include <libxml/xpath.h> -#include <libxml/uri.h> +#include <libxml/xmlstring.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> +#include <libxml/uri.h> #include <libxml/xmlsave.h> namespace NXml { @@ -36,15 +36,15 @@ namespace NXml { } - typedef TxmlXPathContextPtr TXPathContextPtr; - typedef TxmlXPathObjectPtr TXPathObjectPtr; + typedef TxmlXPathContextPtr TXPathContextPtr; + typedef TxmlXPathObjectPtr TXPathObjectPtr; typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; typedef TxmlDocHolder TDocHolder; - typedef TxmlURIPtr TURIPtr; - typedef TxmlNodePtr TNodePtr; + typedef TxmlURIPtr TURIPtr; + typedef TxmlNodePtr TNodePtr; typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; - typedef TxmlParserCtxtPtr TParserCtxtPtr; + typedef TxmlParserCtxtPtr TParserCtxtPtr; typedef TAutoPtr<xmlSaveCtxt, NDetail::TSaveCtxtPtrTraits> TSaveCtxtPtr; } diff --git a/library/cpp/xml/document/node-attr.h b/library/cpp/xml/document/node-attr.h index 6e74403943..2642eb96f9 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -1,24 +1,24 @@ #pragma once #include "xml-document-decl.h" -#include "libxml-guards.h" +#include "libxml-guards.h" #include <util/stream/str.h> #include <util/string/cast.h> namespace NXml { #define THROW(x, y) ythrow yexception() << #x << ": " << y - // libxml defines unsigned char -> xmlChar, - // and all functions use xmlChar. + // libxml defines unsigned char -> xmlChar, + // and all functions use xmlChar. inline static const char* CAST2CHAR(const xmlChar* x) { return reinterpret_cast<const char*>(x); } inline static const xmlChar* XMLCHAR(const char* x) { return reinterpret_cast<const xmlChar*>(x); } - + template <class T> - void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { + void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { try { res = FromString<T>(CAST2CHAR(value.Get())); } catch (TFromStringException&) { @@ -27,16 +27,16 @@ namespace NXml { } template <> - inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { + inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { TString tmp(CAST2CHAR(value.Get())); res.swap(tmp); } template <class T> - T TNode::Attr(TZtStringBuf name) const { - TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + T TNode::Attr(TZtStringBuf name) const { + TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!value) { - THROW(AttributeNotFound, Path() << "@" << name); + THROW(AttributeNotFound, Path() << "@" << name); } T t; @@ -45,8 +45,8 @@ namespace NXml { } template <class T> - T TNode::Attr(TZtStringBuf name, const T& defvalue) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + T TNode::Attr(TZtStringBuf name, const T& defvalue) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { return defvalue; } @@ -57,18 +57,18 @@ namespace NXml { } template <class T> - void TNode::Attr(TZtStringBuf name, T& value) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + void TNode::Attr(TZtStringBuf name, T& value) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { - THROW(AttributeNotFound, Path() << name); + THROW(AttributeNotFound, Path() << name); } AttrInternal(attr, value, name); } template <class T> - void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { - TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); + void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { + TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { value = defvalue; @@ -80,10 +80,10 @@ namespace NXml { template <class T> T TNode::Value() const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { - THROW(NodeIsBlank, Path()); + THROW(NodeIsBlank, Path()); } - TCharPtr val(xmlNodeGetContent(NodePointer)); + TCharPtr val(xmlNodeGetContent(NodePointer)); T t; AttrInternal(val, t, this->Name()); return t; @@ -95,26 +95,26 @@ namespace NXml { return defvalue; } - TCharPtr val(xmlNodeGetContent(NodePointer)); + TCharPtr val(xmlNodeGetContent(NodePointer)); T t; AttrInternal(val, t, this->Name()); return t; } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type - TNode::SetValue(const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type + TNode::SetValue(const T& value) { TStringStream ss; ss << value; - SetValue(ss.Str()); - } - - inline void TNode::SetValue(TStringBuf value) { + SetValue(ss.Str()); + } + + inline void TNode::SetValue(TStringBuf value) { xmlNodeSetContent(NodePointer, XMLCHAR("")); - xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); + xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); } - inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { + inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); if (!attr) { @@ -127,14 +127,14 @@ namespace NXml { } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type - TNode::SetAttr(TZtStringBuf name, const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type + TNode::SetAttr(TZtStringBuf name, const T& value) { TStringStream ss; ss << value; - SetAttr(name, TZtStringBuf(ss.Str())); + SetAttr(name, TZtStringBuf(ss.Str())); } - inline void TNode::SetAttr(TZtStringBuf name) { + inline void TNode::SetAttr(TZtStringBuf name) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); if (!attr) { @@ -144,7 +144,7 @@ namespace NXml { } } - inline void TNode::DelAttr(TZtStringBuf name) { + inline void TNode::DelAttr(TZtStringBuf name) { if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0) THROW(XmlException, "Can't delete node attribute <" << name @@ -152,14 +152,14 @@ namespace NXml { } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type - TNode::AddChild(TZtStringBuf name, const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type + TNode::AddChild(TZtStringBuf name, const T& value) { TStringStream ss; ss << value; - return AddChild(name, TZtStringBuf(ss.Str())); + return AddChild(name, TZtStringBuf(ss.Str())); } - inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { + inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: can't add child to null node"); @@ -179,18 +179,18 @@ namespace NXml { << "]: xmlNewTextChild returned NULL"); } - return TNode(DocPointer, child); + return TNode(DocPointer, child); } template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type - TNode::AddText(const T& value) { + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type + TNode::AddText(const T& value) { TStringStream ss; ss << value; return AddText(ss.Str()); } - inline TNode TNode::AddText(TStringBuf value) { + inline TNode TNode::AddText(TStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [value=" << value << "]: can't add child to null node"); diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6..7b900939ab 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -1,19 +1,19 @@ #pragma once -#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> - +#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> + #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/stream/output.h> #include <util/stream/str.h> #include <algorithm> -#include "libxml-guards.h" +#include "libxml-guards.h" namespace NXml { - class TNode; + class TNode; class TConstNodes; - class TConstNode; + class TConstNode; using TXPathContext = xmlXPathContext; @@ -24,11 +24,11 @@ namespace NXml { String, RootName, }; - + public: /** - * create TDocument - * @param source: filename, XML string, or name for the root element (depends on @src) + * create TDocument + * @param source: filename, XML string, or name for the root element (depends on @src) * @param src: source type: File | String | RootName * throws if file not found or cannot be parsed */ @@ -44,10 +44,10 @@ namespace NXml { /** * get root element */ - TNode Root(); - TConstNode Root() const; + TNode Root(); + TConstNode Root() const; - void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { int bufferSize = 0; xmlChar* xmlBuff = nullptr; const char* encoding = enc.size() ? enc.data() : Doc->encoding ? nullptr : "UTF-8"; @@ -56,14 +56,14 @@ namespace NXml { stream.Write(xmlBuff, bufferSize); } - TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { + TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { TStringStream s; Save(s, enc, shouldFormat); return s.Str(); } void Swap(TDocument& that) { - std::swap(this->Doc, that.Doc); + std::swap(this->Doc, that.Doc); } xmlDocPtr GetImpl() { @@ -72,7 +72,7 @@ namespace NXml { private: void ParseFile(const TString& file); - void ParseString(TZtStringBuf xml); + void ParseString(TZtStringBuf xml); TDocument(TDocHolder doc) : Doc(std::move(doc)) @@ -111,7 +111,7 @@ namespace NXml { * get node by id * @param number: node id */ - TConstNode operator[](size_t number) const; + TConstNode operator[](size_t number) const; /** * get number of nodes @@ -123,50 +123,50 @@ namespace NXml { return SizeValue; } - struct TNodeIter { - const TConstNodes& Nodes; - size_t Index; + struct TNodeIter { + const TConstNodes& Nodes; + size_t Index; TConstNode operator*() const; bool operator==(const TNodeIter& other) const { - return Index == other.Index; - } + return Index == other.Index; + } bool operator!=(const TNodeIter& other) const { - return !(*this == other); - } - TNodeIter operator++() { - Index++; - return *this; - } - }; + return !(*this == other); + } + TNodeIter operator++() { + Index++; + return *this; + } + }; TNodeIter begin() const { return TNodeIter{*this, 0}; } TNodeIter end() const { return TNodeIter{*this, size()}; } - + private: friend class TDocument; - friend class TConstNode; - friend class TNode; + friend class TConstNode; + friend class TNode; TConstNodes(xmlDoc* doc, TXPathObjectPtr obj); - size_t SizeValue; - xmlDoc* Doc; - TXPathObjectPtr Obj; + size_t SizeValue; + xmlDoc* Doc; + TXPathObjectPtr Obj; }; class TNode { public: friend class TDocument; - friend class TConstNode; + friend class TConstNode; friend class TTextReader; /** * check if node is null */ - bool IsNull() const; + bool IsNull() const; /** * check if node is element node @@ -186,15 +186,15 @@ namespace NXml { TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const; /** - * get all element nodes matching given xpath expression + * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; /** * get all element nodes matching given xpath expression @@ -205,37 +205,37 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; - - /** - * get all nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found - * @param ns: explicitly specify XML namespaces to use and their prefixes - */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** + * get all nodes matching given xpath expression + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found + * @param ns: explicitly specify XML namespaces to use and their prefixes + */ + TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + + /** * get all nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ctxt: reusable xpath context */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** - * get the first element node matching given xpath expression + * get the first element node matching given xpath expression * @param xpath: path to node (from current node) * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ /// @todo: quiet should be default, empty nodeset is not an error - TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()); - TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; + TNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()); + TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; /** * get the first element node matching given xpath expression @@ -247,8 +247,8 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt); - TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; + TNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt); + TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** * get node first child @@ -256,8 +256,8 @@ namespace NXml { * @note if name is empty, returns the first child node of type "element" * @note returns null node if no child found */ - TNode FirstChild(TZtStringBuf name); - TConstNode FirstChild(TZtStringBuf name) const; + TNode FirstChild(TZtStringBuf name); + TConstNode FirstChild(TZtStringBuf name) const; TNode FirstChild(); TConstNode FirstChild() const; @@ -275,8 +275,8 @@ namespace NXml { * @note if name is empty, returns the next sibling node of type "element" * @node returns null node if no neighbour found */ - TNode NextSibling(TZtStringBuf name); - TConstNode NextSibling(TZtStringBuf name) const; + TNode NextSibling(TZtStringBuf name); + TConstNode NextSibling(TZtStringBuf name) const; TNode NextSibling(); TConstNode NextSibling() const; @@ -286,7 +286,7 @@ namespace NXml { * @param name: child name * returns new empty node */ - TNode AddChild(TZtStringBuf name); + TNode AddChild(TZtStringBuf name); /** * create child node with given value @@ -294,11 +294,11 @@ namespace NXml { * @param value: node value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type - AddChild(TZtStringBuf name, const T& value); - - TNode AddChild(TZtStringBuf name, TZtStringBuf value); + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type + AddChild(TZtStringBuf name, const T& value); + TNode AddChild(TZtStringBuf name, TZtStringBuf value); + /** * add child node, making recursive copy of original * @param node: node to copy from @@ -312,18 +312,18 @@ namespace NXml { * @param value: node value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type - AddText(const T& value); - - TNode AddText(TStringBuf value); + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type + AddText(const T& value); + TNode AddText(TStringBuf value); + /** * get node attribute * @param name: attribute name * throws exception if attribute not found */ template <class T> - T Attr(TZtStringBuf name) const; + T Attr(TZtStringBuf name) const; /** * get node attribute @@ -331,7 +331,7 @@ namespace NXml { * returns default value if attribute not found */ template <class T> - T Attr(TZtStringBuf name, const T& defvalue) const; + T Attr(TZtStringBuf name, const T& defvalue) const; /** * get node attribute @@ -340,7 +340,7 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - void Attr(TZtStringBuf name, T& value) const; + void Attr(TZtStringBuf name, T& value) const; /** * get node attribute @@ -350,14 +350,14 @@ namespace NXml { * returns default value if attribute not found, attr value otherwise */ template <class T> - void Attr(TZtStringBuf name, T& value, const T& defvalue) const; + void Attr(TZtStringBuf name, T& value, const T& defvalue) const; /** * get node value (text) * @throws exception if node is blank */ template <class T> - T Value() const; + T Value() const; /** * get node value @@ -365,37 +365,37 @@ namespace NXml { * returns default value if node is blank */ template <class T> - T Value(const T& defvalue) const; + T Value(const T& defvalue) const; /** * set node value * @param value: new text value */ template <class T> - typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type - SetValue(const T& value); - - void SetValue(TStringBuf value); + typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type + SetValue(const T& value); + void SetValue(TStringBuf value); + /** * set/reset node attribute value, * if attribute does not exist, it'll be created * @param name: attribute name * @param value: attribute value */ - template<class T> - typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type - SetAttr(TZtStringBuf name, const T& value); - - void SetAttr(TZtStringBuf name, TZtStringBuf value); + template<class T> + typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type + SetAttr(TZtStringBuf name, const T& value); - void SetAttr(TZtStringBuf name); + void SetAttr(TZtStringBuf name, TZtStringBuf value); + void SetAttr(TZtStringBuf name); + /** * delete node attribute * @param name: attribute name */ - void DelAttr(TZtStringBuf name); + void DelAttr(TZtStringBuf name); /** * set node application data @@ -421,13 +421,13 @@ namespace NXml { /** * get node xml representation */ - TString ToString(TZtStringBuf enc = "") const { + TString ToString(TZtStringBuf enc = "") const { TStringStream s; Save(s, enc); return s.Str(); } - void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; - void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; + void SaveAsHtml(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = false) const; /** * get pointer to internal node @@ -448,7 +448,7 @@ namespace NXml { /** * constructs null node */ - TNode() + TNode() : NodePointer(nullptr) , DocPointer(nullptr) { @@ -457,33 +457,33 @@ namespace NXml { private: friend class TConstNodes; - TNode(xmlDoc* doc, xmlNode* node) - : NodePointer(node) - , DocPointer(doc) + TNode(xmlDoc* doc, xmlNode* node) + : NodePointer(node) + , DocPointer(doc) { } - TNode Find(xmlNode* start, TZtStringBuf name); + TNode Find(xmlNode* start, TZtStringBuf name); template <class T> - void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; + void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; - void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; + void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; - xmlNode* NodePointer; - xmlDoc* DocPointer; + xmlNode* NodePointer; + xmlDoc* DocPointer; }; class TConstNode { public: friend class TDocument; friend class TConstNodes; - friend class TNode; + friend class TNode; /** * check if node is null */ bool IsNull() const { - return ActualNode.IsNull(); + return ActualNode.IsNull(); } bool IsElementNode() const { @@ -491,9 +491,9 @@ namespace NXml { } TConstNode Parent() const { - return ActualNode.Parent(); - } - + return ActualNode.Parent(); + } + /** * Create xpath context to be used later for fast xpath evaluation. * @param nss: explicitly specify XML namespaces to use and their prefixes @@ -503,16 +503,16 @@ namespace NXml { } /** - * get all element nodes matching given xpath expression + * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.Nodes(xpath, quiet, ns); + TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.Nodes(xpath, quiet, ns); } /** @@ -524,42 +524,42 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.Nodes(xpath, quiet, ctxt); } /** - * get all nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found + * get all nodes matching given xpath expression + * @param xpath: xpath expression + * @param quiet: don't throw exception if zero nodes found * @param ns: explicitly specify XML namespaces to use and their prefixes - */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.XPath(xpath, quiet, ns); - } - - /** + */ + TConstNodes XPath(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.XPath(xpath, quiet, ns); + } + + /** * get all nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found * @param ctxt: reusable xpath context */ - TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.XPath(xpath, quiet, ctxt); } /** - * get the first element node matching given xpath expression + * get the first element node matching given xpath expression * @param xpath: path to node (from current node) * @param quiet: don't throw exception if node not found, * return null node (@see IsNull()) * @param ns: explicitly specify XML namespaces to use and their prefixes - * - * For historical reasons, this only works for *element* nodes. - * Use the XPath function if you need other kinds of nodes. + * + * For historical reasons, this only works for *element* nodes. + * Use the XPath function if you need other kinds of nodes. */ - TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { - return ActualNode.Node(xpath, quiet, ns); + TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { + return ActualNode.Node(xpath, quiet, ns); } /** @@ -572,12 +572,12 @@ namespace NXml { * For historical reasons, this only works for *element* nodes. * Use the XPath function if you need other kinds of nodes. */ - TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return ActualNode.Node(xpath, quiet, ctxt); } - TConstNode FirstChild(TZtStringBuf name) const { - return ActualNode.FirstChild(name); + TConstNode FirstChild(TZtStringBuf name) const { + return ActualNode.FirstChild(name); } TConstNode FirstChild() const { @@ -589,8 +589,8 @@ namespace NXml { * @param name: neighbour name * throws exception if no neighbour found */ - TConstNode NextSibling(TZtStringBuf name) const { - return ActualNode.NextSibling(name); + TConstNode NextSibling(TZtStringBuf name) const { + return ActualNode.NextSibling(name); } TConstNode NextSibling() const { @@ -603,8 +603,8 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - T Attr(TZtStringBuf name) const { - return ActualNode.Attr<T>(name); + T Attr(TZtStringBuf name) const { + return ActualNode.Attr<T>(name); } /** @@ -613,8 +613,8 @@ namespace NXml { * returns default value if attribute not found */ template <class T> - T Attr(TZtStringBuf name, const T& defvalue) const { - return ActualNode.Attr(name, defvalue); + T Attr(TZtStringBuf name, const T& defvalue) const { + return ActualNode.Attr(name, defvalue); } /** @@ -624,8 +624,8 @@ namespace NXml { * throws exception if attribute not found */ template <class T> - void Attr(TZtStringBuf name, T& value) const { - return ActualNode.Attr(name, value); + void Attr(TZtStringBuf name, T& value) const { + return ActualNode.Attr(name, value); } /** @@ -636,8 +636,8 @@ namespace NXml { * returns default value if attribute not found, attr value otherwise */ template <class T> - void Attr(TZtStringBuf name, T& value, const T& defvalue) const { - return ActualNode.Attr(name, value, defvalue); + void Attr(TZtStringBuf name, T& value, const T& defvalue) const { + return ActualNode.Attr(name, value, defvalue); } /** @@ -646,7 +646,7 @@ namespace NXml { */ template <class T> T Value() const { - return ActualNode.Value<T>(); + return ActualNode.Value<T>(); } /** @@ -656,7 +656,7 @@ namespace NXml { */ template <class T> T Value(const T& defvalue) const { - return ActualNode.Value(defvalue); + return ActualNode.Value(defvalue); } /** @@ -691,28 +691,28 @@ namespace NXml { * get node xpath */ TString Path() const { - return ActualNode.Path(); + return ActualNode.Path(); } /** * get node xml representation */ - TString ToString(TZtStringBuf enc = "") const { + TString ToString(TZtStringBuf enc = "") const { return ActualNode.ToString(enc); } TConstNode() = default; - TConstNode(TNode node) - : ActualNode(node) + TConstNode(TNode node) + : ActualNode(node) { } TNode ConstCast() const { - return ActualNode; - } - + return ActualNode; + } + private: - TNode ActualNode; + TNode ActualNode; }; } diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 18a554d732..23c16cf495 100644 --- a/library/cpp/xml/document/xml-document.cpp +++ b/library/cpp/xml/document/xml-document.cpp @@ -75,7 +75,7 @@ namespace NXml { Doc = std::move(doc); } - void TDocument::ParseString(TZtStringBuf xml) { + void TDocument::ParseString(TZtStringBuf xml) { TParserCtxtPtr pctx(xmlNewParserCtxt()); if (pctx.Get() == nullptr) THROW(XmlException, "Can't create parser context"); @@ -89,19 +89,19 @@ namespace NXml { } TNode TDocument::Root() { - xmlNode* r = xmlDocGetRootElement(Doc.Get()); + xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); - return TNode(Doc.Get(), r); + return TNode(Doc.Get(), r); } TConstNode TDocument::Root() const { - xmlNode* r = xmlDocGetRootElement(Doc.Get()); + xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); - return TConstNode(TNode(Doc.Get(), r)); + return TConstNode(TNode(Doc.Get(), r)); } bool TNode::IsNull() const { @@ -126,12 +126,12 @@ namespace NXml { return ctx; } - TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return XPath(xpath, quiet, *ctxt); } - TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes TNode::XPath(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { if (xmlXPathSetContextNode(NodePointer, &ctxt) != 0) THROW(XmlException, "Can't set xpath context node, probably the context is associated with another document"); @@ -141,59 +141,59 @@ namespace NXml { TConstNodes nodes(DocPointer, obj); - if (nodes.Size() == 0 && !quiet) + if (nodes.Size() == 0 && !quiet) THROW(NodeNotFound, xpath); return nodes; } - TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Nodes(xpath, quiet, *ctxt); } - TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { TConstNodes nodes = XPath(xpath, quiet, ctxt); - if (nodes.Size() != 0 && !nodes[0].IsElementNode()) - THROW(XmlException, "xpath points to non-element nodes: " << xpath); - return nodes; - } + if (nodes.Size() != 0 && !nodes[0].IsElementNode()) + THROW(XmlException, "xpath points to non-element nodes: " << xpath); + return nodes; + } - TNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) { + TNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); } - TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); } - TNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) { + TNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) { TConstNodes n = Nodes(xpath, quiet, ctxt); - if (n.Size() == 0 && !quiet) + if (n.Size() == 0 && !quiet) THROW(NodeNotFound, xpath); - if (n.Size() == 0) - return TNode(); + if (n.Size() == 0) + return TNode(); else return n[0].ConstCast(); } - TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { + TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const { return const_cast<TNode*>(this)->Node(xpath, quiet, ctxt); } - TNode TNode::FirstChild(TZtStringBuf name) { - if (IsNull()) + TNode TNode::FirstChild(TZtStringBuf name) { + if (IsNull()) THROW(XmlException, "Node is null"); - return Find(NodePointer->children, name); + return Find(NodePointer->children, name); } - TConstNode TNode::FirstChild(TZtStringBuf name) const { - return const_cast<TNode*>(this)->FirstChild(name); + TConstNode TNode::FirstChild(TZtStringBuf name) const { + return const_cast<TNode*>(this)->FirstChild(name); } TNode TNode::FirstChild() { @@ -218,15 +218,15 @@ namespace NXml { return const_cast<TNode*>(this)->Parent(); } - TNode TNode::NextSibling(TZtStringBuf name) { - if (IsNull()) + TNode TNode::NextSibling(TZtStringBuf name) { + if (IsNull()) THROW(XmlException, "Node is null"); - return Find(NodePointer->next, name); + return Find(NodePointer->next, name); } - TConstNode TNode::NextSibling(TZtStringBuf name) const { - return const_cast<TNode*>(this)->NextSibling(name); + TConstNode TNode::NextSibling(TZtStringBuf name) const { + return const_cast<TNode*>(this)->NextSibling(name); } TNode TNode::NextSibling() { @@ -242,7 +242,7 @@ namespace NXml { /* NOTE: by default child will inherit it's parent ns */ - TNode TNode::AddChild(TZtStringBuf name) { + TNode TNode::AddChild(TZtStringBuf name) { return AddChild(name, ""); } @@ -262,12 +262,12 @@ namespace NXml { return NodePointer->_private; } - TNode TNode::Find(xmlNode* start, TZtStringBuf name) { + TNode TNode::Find(xmlNode* start, TZtStringBuf name) { for (; start; start = start->next) if (start->type == XML_ELEMENT_NODE && (name.empty() || !xmlStrcmp(start->name, XMLCHAR(name.c_str())))) - return TNode(DocPointer, start); + return TNode(DocPointer, start); - return TNode(); + return TNode(); } TString TNode::Name() const { @@ -278,7 +278,7 @@ namespace NXml { } TString TNode::Path() const { - TCharPtr path(xmlGetNodePath(NodePointer)); + TCharPtr path(xmlGetNodePath(NodePointer)); if (!!path) return CAST2CHAR(path.Get()); else @@ -313,7 +313,7 @@ namespace NXml { return len; } - void TNode::SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const { + void TNode::SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const { const char* encoding = enc.size() ? enc.data() : "utf-8"; TSaveCtxtPtr ctx(xmlSaveToIO(XmlWriteToOstream, /* close */ nullptr, &stream, encoding, options)); @@ -321,45 +321,45 @@ namespace NXml { THROW(XmlException, "Failed saving node to stream"); } - void TNode::Save(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { + void TNode::Save(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { SaveInternal(stream, enc, shouldFormat ? XML_SAVE_FORMAT : 0); } - void TNode::SaveAsHtml(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { + void TNode::SaveAsHtml(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const { int options = XML_SAVE_AS_HTML; options |= shouldFormat ? XML_SAVE_FORMAT : 0; SaveInternal(stream, enc, options); } TConstNodes::TConstNodes(const TConstNodes& nodes) - : SizeValue(nodes.Size()) - , Doc(nodes.Doc) - , Obj(nodes.Obj) + : SizeValue(nodes.Size()) + , Doc(nodes.Doc) + , Obj(nodes.Obj) { } TConstNodes& TConstNodes::operator=(const TConstNodes& nodes) { if (this != &nodes) { - SizeValue = nodes.Size(); - Doc = nodes.Doc; - Obj = nodes.Obj; + SizeValue = nodes.Size(); + Doc = nodes.Doc; + Obj = nodes.Obj; } return *this; } TConstNodes::TConstNodes(TConstNodesRef ref) - : SizeValue(ref.r_.Size()) - , Doc(ref.r_.Doc) - , Obj(ref.r_.Obj) + : SizeValue(ref.r_.Size()) + , Doc(ref.r_.Doc) + , Obj(ref.r_.Obj) { } TConstNodes& TConstNodes::operator=(TConstNodesRef ref) { if (this != &ref.r_) { - SizeValue = ref.r_.Size(); - Doc = ref.r_.Doc; - Obj = ref.r_.Obj; + SizeValue = ref.r_.Size(); + Doc = ref.r_.Doc; + Obj = ref.r_.Obj; } return *this; } @@ -376,18 +376,18 @@ namespace NXml { } TConstNode TConstNodes::operator[](size_t number) const { - if (number + 1 > Size()) + if (number + 1 > Size()) THROW(XmlException, "index out of range " << number); - if (!Obj || !Obj->nodesetval) - THROW(XmlException, "Broken TConstNodes object, Obj is null"); + if (!Obj || !Obj->nodesetval) + THROW(XmlException, "Broken TConstNodes object, Obj is null"); - xmlNode* node = Obj->nodesetval->nodeTab[number]; - return TNode(Doc, node); + xmlNode* node = Obj->nodesetval->nodeTab[number]; + return TNode(Doc, node); } TConstNode TConstNodes::TNodeIter::operator*() const { - return Nodes[Index]; - } - + return Nodes[Index]; + } + } diff --git a/library/cpp/xml/document/xml-document.h b/library/cpp/xml/document/xml-document.h index 829ba09cc4..728f76761f 100644 --- a/library/cpp/xml/document/xml-document.h +++ b/library/cpp/xml/document/xml-document.h @@ -1,4 +1,4 @@ #pragma once -#include "xml-document-decl.h" -#include "node-attr.h" +#include "xml-document-decl.h" +#include "node-attr.h" diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index 9f537b75c4..a8cfb45117 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -1,8 +1,8 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/generic/map.h> - -#include "xml-document.h" - + +#include "xml-document.h" + Y_UNIT_TEST_SUITE(TestXmlDocument) { Y_UNIT_TEST(Iteration) { NXml::TDocument xml( @@ -19,40 +19,40 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { } Y_UNIT_TEST(ParseString) { - NXml::TDocument xml( + NXml::TDocument xml( "<?xml version=\"1.0\"?>\n" "<root>\n" "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" "<text>Некоторый текст</text>\n" "</root>", NXml::TDocument::String); - - NXml::TConstNode root = xml.Root(); - NXml::TConstNode b = root.Node("a/b"); - UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15); - UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true); - - NXml::TConstNode text = root.Node("text"); + + NXml::TConstNode root = xml.Root(); + NXml::TConstNode b = root.Node("a/b"); + UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15); + UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true); + + NXml::TConstNode text = root.Node("text"); UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст"); - } + } Y_UNIT_TEST(SerializeString) { - NXml::TDocument xml("frob", NXml::TDocument::RootName); - xml.Root().SetAttr("xyzzy", "Frobozz"); - xml.Root().SetAttr("kulness", 0.3); - xml.Root().SetAttr("timelimit", 3); - - NXml::TNode authors = xml.Root().AddChild("authors"); - authors.AddChild("graham").SetAttr("name", "Nelson"); - authors.AddChild("zarf").SetValue("Andrew Plotkin"); - authors.AddChild("emshort", "Emily Short"); - + NXml::TDocument xml("frob", NXml::TDocument::RootName); + xml.Root().SetAttr("xyzzy", "Frobozz"); + xml.Root().SetAttr("kulness", 0.3); + xml.Root().SetAttr("timelimit", 3); + + NXml::TNode authors = xml.Root().AddChild("authors"); + authors.AddChild("graham").SetAttr("name", "Nelson"); + authors.AddChild("zarf").SetValue("Andrew Plotkin"); + authors.AddChild("emshort", "Emily Short"); + TString data = xml.ToString("utf-8"); - UNIT_ASSERT_EQUAL(data, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" - "<frob xyzzy=\"Frobozz\" kulness=\"0.3\" timelimit=\"3\">\n" + UNIT_ASSERT_EQUAL(data, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<frob xyzzy=\"Frobozz\" kulness=\"0.3\" timelimit=\"3\">\n" " <authors>\n" " <graham name=\"Nelson\"/>\n" " <zarf>Andrew Plotkin</zarf>\n" - " <emshort>Emily Short</emshort>\n" + " <emshort>Emily Short</emshort>\n" " </authors>\n" "</frob>\n"); // check default utf8 output with ru @@ -62,7 +62,7 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_VALUES_EQUAL(xml2.ToString(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" "<frob xyzzy=\"привет =)\"/>\n"); } - } + } Y_UNIT_TEST(XPathNs) { using namespace NXml; TDocument xml( @@ -114,11 +114,11 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EQUAL(node.Value<TString>(), ""); node = node.NextSibling(); UNIT_ASSERT_EQUAL(node.IsNull(), true); - TStringStream iterLog; + TStringStream iterLog; for (const auto& node2 : root.Nodes("/root/*")) { iterLog << node2.Name() << ';'; - } - UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); + } + UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); // get only element nodes, ignore text nodes with empty "name" param node = root.FirstChild(TString()); @@ -152,29 +152,29 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EXCEPTION(node.IsText(), yexception); } Y_UNIT_TEST(DefVal) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" "<root><a></a></root>", NXml::TDocument::String); - UNIT_ASSERT_EQUAL(xml.Root().Node("a", true).Node("b", true).Value<int>(3), 3); - } + UNIT_ASSERT_EQUAL(xml.Root().Node("a", true).Node("b", true).Value<int>(3), 3); + } Y_UNIT_TEST(NodesVsXPath) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" "<root><a x=\"y\"></a></root>", NXml::TDocument::String); - UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("/root/a/@x"), yexception); - UNIT_ASSERT_VALUES_EQUAL(xml.Root().XPath("/root/a/@x").Size(), 1); - } + UNIT_ASSERT_EXCEPTION(xml.Root().Nodes("/root/a/@x"), yexception); + UNIT_ASSERT_VALUES_EQUAL(xml.Root().XPath("/root/a/@x").Size(), 1); + } Y_UNIT_TEST(NodeIsFirst) { - using namespace NXml; - TDocument xml("<?xml version=\"1.0\"?>\n" - "<root><a x=\"y\">first</a>" + using namespace NXml; + TDocument xml("<?xml version=\"1.0\"?>\n" + "<root><a x=\"y\">first</a>" "<a>second</a></root>", NXml::TDocument::String); - UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); + UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); UNIT_ASSERT_STRINGS_EQUAL(xml.Root().Node("/root/a").Value<TString>(), "first"); - } + } Y_UNIT_TEST(CopyNode) { using namespace NXml; // default-construct empty node @@ -304,16 +304,16 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { UNIT_ASSERT_EXCEPTION(xml1.Root(), yexception); UNIT_ASSERT_VALUES_EQUAL(xml2.Root().ToString(), "<foo><bar/></foo>"); } - - Y_UNIT_TEST(StringConversion) { - using namespace NXml; - TDocument xml("foo", TDocument::RootName); - auto root = xml.Root(); - const TStringBuf stringBuf = "bar"; - root.SetAttr("bar", stringBuf); - const TString tString = "baz"; - root.SetAttr("baz", tString); - root.SetAttr("quux", "literal"); - root.SetAttr("frob", 500); - } -} + + Y_UNIT_TEST(StringConversion) { + using namespace NXml; + TDocument xml("foo", TDocument::RootName); + auto root = xml.Root(); + const TStringBuf stringBuf = "bar"; + root.SetAttr("bar", stringBuf); + const TString tString = "baz"; + root.SetAttr("baz", tString); + root.SetAttr("quux", "literal"); + root.SetAttr("frob", 500); + } +} diff --git a/library/cpp/xml/document/xml-textreader.cpp b/library/cpp/xml/document/xml-textreader.cpp index b946f1fbf2..ab5c0436f3 100644 --- a/library/cpp/xml/document/xml-textreader.cpp +++ b/library/cpp/xml/document/xml-textreader.cpp @@ -113,15 +113,15 @@ namespace NXml { return TempStringResult(xmlTextReaderGetAttributeNo(Impl.Get(), number)); } - TString TTextReader::GetAttribute(TZtStringBuf name) const { + TString TTextReader::GetAttribute(TZtStringBuf name) const { return TempStringResult(xmlTextReaderGetAttribute(Impl.Get(), XMLCHAR(name.data()))); } - TString TTextReader::GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const { + TString TTextReader::GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const { return TempStringResult(xmlTextReaderGetAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); } - TString TTextReader::LookupNamespace(TZtStringBuf prefix) const { + TString TTextReader::LookupNamespace(TZtStringBuf prefix) const { return TempStringResult(xmlTextReaderLookupNamespace(Impl.Get(), XMLCHAR(prefix.data()))); } @@ -129,11 +129,11 @@ namespace NXml { return BoolResult(xmlTextReaderMoveToAttributeNo(Impl.Get(), number)); } - bool TTextReader::MoveToAttribute(TZtStringBuf name) { + bool TTextReader::MoveToAttribute(TZtStringBuf name) { return BoolResult(xmlTextReaderMoveToAttribute(Impl.Get(), XMLCHAR(name.data()))); } - bool TTextReader::MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri) { + bool TTextReader::MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri) { return BoolResult(xmlTextReaderMoveToAttributeNs(Impl.Get(), XMLCHAR(localName.data()), XMLCHAR(nsUri.data()))); } diff --git a/library/cpp/xml/document/xml-textreader.h b/library/cpp/xml/document/xml-textreader.h index ab4c329d26..1507bfe22c 100644 --- a/library/cpp/xml/document/xml-textreader.h +++ b/library/cpp/xml/document/xml-textreader.h @@ -5,8 +5,8 @@ #include <contrib/libs/libxml/include/libxml/xmlreader.h> -#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> - +#include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> + #include <util/generic/noncopyable.h> #include <util/generic/ptr.h> #include <util/generic/strbuf.h> @@ -222,21 +222,21 @@ namespace NXml { * Provides the value of the attribute with the specified qualified name. * @param name the qualified name of the attribute */ - TString GetAttribute(TZtStringBuf name) const; + TString GetAttribute(TZtStringBuf name) const; /** * Provides the value of the specified attribute. * @param localName the local name of the attribute * @param nsUri the namespace URI of the attribute */ - TString GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const; + TString GetAttribute(TZtStringBuf localName, TZtStringBuf nsUri) const; /** * Resolves a namespace prefix in the scope of the current element. * @param prefix the prefix whose namespace URI is to be resolved. To return the default namespace, specify empty string. * @return a string containing the namespace URI to which the prefix maps. */ - TString LookupNamespace(TZtStringBuf prefix) const; + TString LookupNamespace(TZtStringBuf prefix) const; /** * Moves the position of the current instance to the attribute with the specified index relative to the containing element. @@ -250,7 +250,7 @@ namespace NXml { * @param name the qualified name of the attribute * @return true in case of success, false if not found */ - bool MoveToAttribute(TZtStringBuf name); + bool MoveToAttribute(TZtStringBuf name); /** * Moves the position of the current instance to the attribute with the specified local name and namespace URI. @@ -258,7 +258,7 @@ namespace NXml { * @param nsUri the namespace URI of the attribute * @return true in case of success, false if not found */ - bool MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri); + bool MoveToAttribute(TZtStringBuf localName, TZtStringBuf nsUri); /** * Moves the position of the current instance to the first attribute associated with the current node. diff --git a/library/cpp/xml/document/ya.make b/library/cpp/xml/document/ya.make index 86bbd639cf..c66f368d28 100644 --- a/library/cpp/xml/document/ya.make +++ b/library/cpp/xml/document/ya.make @@ -11,7 +11,7 @@ SRCS( PEERDIR( library/cpp/xml/init contrib/libs/libxml - library/cpp/string_utils/ztstrbuf + library/cpp/string_utils/ztstrbuf ) END() |