diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /library/cpp/xml/document | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/xml/document')
-rw-r--r-- | library/cpp/xml/document/libxml-guards.h | 4 | ||||
-rw-r--r-- | library/cpp/xml/document/node-attr.h | 96 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 144 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.cpp | 148 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document_ut.cpp | 96 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-options.h | 2 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-textreader.cpp | 2 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-textreader.h | 2 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-textreader_ut.cpp | 78 | ||||
-rw-r--r-- | library/cpp/xml/document/ya.make | 6 |
10 files changed, 289 insertions, 289 deletions
diff --git a/library/cpp/xml/document/libxml-guards.h b/library/cpp/xml/document/libxml-guards.h index 4188cecff1..7e8a0bfece 100644 --- a/library/cpp/xml/document/libxml-guards.h +++ b/library/cpp/xml/document/libxml-guards.h @@ -1,5 +1,5 @@ #pragma once - + #include <library/cpp/xml/init/ptr.h> #include <util/generic/ptr.h> #include <libxml/xmlstring.h> @@ -47,4 +47,4 @@ namespace NXml { 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..8cdcacfed2 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -10,18 +10,18 @@ namespace NXml { // 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); - } + 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 { try { res = FromString<T>(CAST2CHAR(value.Get())); - } catch (TFromStringException&) { + } catch (TFromStringException&) { THROW(XmlException, "Failed to convert string " << TString{TStringBuf(CAST2CHAR(value.Get())).substr(0, 50)}.Quote() << " from '" << errContext << "' to requested type"); } } @@ -35,7 +35,7 @@ namespace NXml { template <class T> T TNode::Attr(TZtStringBuf name) const { TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - if (!value) { + if (!value) { THROW(AttributeNotFound, Path() << "@" << name); } @@ -47,7 +47,7 @@ namespace NXml { template <class T> T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - if (!attr) { + if (!attr) { return defvalue; } @@ -59,7 +59,7 @@ namespace NXml { template <class T> void TNode::Attr(TZtStringBuf name, T& value) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - if (!attr) { + if (!attr) { THROW(AttributeNotFound, Path() << name); } @@ -70,16 +70,16 @@ namespace NXml { void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - if (!attr) { + if (!attr) { value = defvalue; - } else { + } else { AttrInternal(attr, value, name); } } template <class T> - T TNode::Value() const { - if (!NodePointer || xmlIsBlankNode(NodePointer)) { + T TNode::Value() const { + if (!NodePointer || xmlIsBlankNode(NodePointer)) { THROW(NodeIsBlank, Path()); } @@ -90,8 +90,8 @@ namespace NXml { } template <class T> - T TNode::Value(const T& defvalue) const { - if (!NodePointer || xmlIsBlankNode(NodePointer)) { + T TNode::Value(const T& defvalue) const { + if (!NodePointer || xmlIsBlankNode(NodePointer)) { return defvalue; } @@ -115,14 +115,14 @@ namespace NXml { } inline void TNode::SetAttr(TZtStringBuf name, TZtStringBuf value) { - xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); - - if (!attr) { - THROW(XmlException, "Can't set node attribute <" - << name - << "> to <" - << value - << ">"); + xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); + + if (!attr) { + THROW(XmlException, "Can't set node attribute <" + << name + << "> to <" + << value + << ">"); } } @@ -135,20 +135,20 @@ namespace NXml { } inline void TNode::SetAttr(TZtStringBuf name) { - xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); + xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); - if (!attr) { - THROW(XmlException, "Can't set node empty attribute <" - << name - << ">"); + if (!attr) { + THROW(XmlException, "Can't set node empty attribute <" + << name + << ">"); } } inline void TNode::DelAttr(TZtStringBuf name) { if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0) - THROW(XmlException, "Can't delete node attribute <" - << name - << ">"); + THROW(XmlException, "Can't delete node attribute <" + << name + << ">"); } template <class T> @@ -160,23 +160,23 @@ namespace NXml { } inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { - if (IsNull()) { - THROW(XmlException, "addChild [name=" << name << ", value=" << value - << "]: can't add child to null node"); + if (IsNull()) { + THROW(XmlException, "addChild [name=" << name << ", value=" << value + << "]: can't add child to null node"); } - xmlNode* child = nullptr; + xmlNode* child = nullptr; - if (value.empty()) { + if (value.empty()) { child = xmlNewTextChild(NodePointer, nullptr, XMLCHAR(name.c_str()), nullptr); - } else { + } else { child = xmlNewTextChild( NodePointer, nullptr, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); } - if (!child) { - THROW(XmlException, "addChild [name=" << name << ", value=" << value - << "]: xmlNewTextChild returned NULL"); + if (!child) { + THROW(XmlException, "addChild [name=" << name << ", value=" << value + << "]: xmlNewTextChild returned NULL"); } return TNode(DocPointer, child); @@ -191,19 +191,19 @@ namespace NXml { } inline TNode TNode::AddText(TStringBuf value) { - if (IsNull()) { - THROW(XmlException, "addChild [value=" << value - << "]: can't add child to null node"); + if (IsNull()) { + THROW(XmlException, "addChild [value=" << value + << "]: can't add child to null node"); } xmlNode* child = xmlNewTextLen((xmlChar*)value.data(), value.size()); child = xmlAddChild(NodePointer, child); - if (!child) { - THROW(XmlException, "addChild [value=" << value - << "]: xmlNewTextChild returned NULL"); + if (!child) { + THROW(XmlException, "addChild [value=" << value + << "]: xmlNewTextChild returned NULL"); } return TNode(DocPointer, child); } -} +} diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6..5461738d8c 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -1,5 +1,5 @@ #pragma once - + #include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> #include <util/generic/string.h> @@ -17,9 +17,9 @@ namespace NXml { using TXPathContext = xmlXPathContext; - class TDocument { + class TDocument { public: - enum Source { + enum Source { File, String, RootName, @@ -62,11 +62,11 @@ namespace NXml { return s.Str(); } - void Swap(TDocument& that) { + void Swap(TDocument& that) { std::swap(this->Doc, that.Doc); } - xmlDocPtr GetImpl() { + xmlDocPtr GetImpl() { return Doc.Get(); } @@ -76,8 +76,8 @@ namespace NXml { TDocument(TDocHolder doc) : Doc(std::move(doc)) - { - } + { + } TDocHolder Doc; }; @@ -88,22 +88,22 @@ namespace NXml { }; typedef TVector<TNamespaceForXPath> TNamespacesForXPath; - class TConstNodes { + class TConstNodes { private: - struct TConstNodesRef { - explicit TConstNodesRef(TConstNodes& n) - : r_(n) - { - } + struct TConstNodesRef { + explicit TConstNodesRef(TConstNodes& n) + : r_(n) + { + } TConstNodes& r_; }; - + public: TConstNodes(const TConstNodes& nodes); TConstNodes& operator=(const TConstNodes& nodes); TConstNodes(TConstNodesRef ref); - TConstNodes& operator=(TConstNodesRef ref); + TConstNodes& operator=(TConstNodesRef ref); operator TConstNodesRef(); @@ -116,17 +116,17 @@ namespace NXml { /** * get number of nodes */ - size_t Size() const { - return SizeValue; - } - size_t size() const { - return SizeValue; - } + size_t Size() const { + return SizeValue; + } + size_t size() const { + return SizeValue; + } struct TNodeIter { const TConstNodes& Nodes; size_t Index; - TConstNode operator*() const; + TConstNode operator*() const; bool operator==(const TNodeIter& other) const { return Index == other.Index; } @@ -138,12 +138,12 @@ namespace NXml { return *this; } }; - TNodeIter begin() const { - return TNodeIter{*this, 0}; - } - TNodeIter end() const { - return TNodeIter{*this, size()}; - } + TNodeIter begin() const { + return TNodeIter{*this, 0}; + } + TNodeIter end() const { + return TNodeIter{*this, size()}; + } private: friend class TDocument; @@ -157,7 +157,7 @@ namespace NXml { TXPathObjectPtr Obj; }; - class TNode { + class TNode { public: friend class TDocument; friend class TConstNode; @@ -169,11 +169,11 @@ namespace NXml { bool IsNull() const; /** - * check if node is element node - */ - bool IsElementNode() const; - - /** + * check if node is element node + */ + bool IsElementNode() const; + + /** * Create xpath context to be used later for fast xpath evaluation. * @param nss: explicitly specify XML namespaces to use and their prefixes * @@ -259,9 +259,9 @@ namespace NXml { TNode FirstChild(TZtStringBuf name); TConstNode FirstChild(TZtStringBuf name) const; - TNode FirstChild(); - TConstNode FirstChild() const; - + TNode FirstChild(); + TConstNode FirstChild() const; + /** * get parent node * throws exception if has no parent @@ -278,9 +278,9 @@ namespace NXml { TNode NextSibling(TZtStringBuf name); TConstNode NextSibling(TZtStringBuf name) const; - TNode NextSibling(); - TConstNode NextSibling() const; - + TNode NextSibling(); + TConstNode NextSibling() const; + /** * create child node * @param name: child name @@ -371,7 +371,7 @@ namespace NXml { * set node value * @param value: new text value */ - template <class T> + template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type SetValue(const T& value); @@ -451,8 +451,8 @@ namespace NXml { TNode() : NodePointer(nullptr) , DocPointer(nullptr) - { - } + { + } private: friend class TConstNodes; @@ -460,12 +460,12 @@ namespace NXml { TNode(xmlDoc* doc, xmlNode* node) : NodePointer(node) , DocPointer(doc) - { - } + { + } TNode Find(xmlNode* start, TZtStringBuf name); - template <class T> + template <class T> void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; @@ -474,7 +474,7 @@ namespace NXml { xmlDoc* DocPointer; }; - class TConstNode { + class TConstNode { public: friend class TDocument; friend class TConstNodes; @@ -482,15 +482,15 @@ namespace NXml { /** * check if node is null */ - bool IsNull() const { + bool IsNull() const { return ActualNode.IsNull(); } - bool IsElementNode() const { - return ActualNode.IsElementNode(); - } - - TConstNode Parent() const { + bool IsElementNode() const { + return ActualNode.IsElementNode(); + } + + TConstNode Parent() const { return ActualNode.Parent(); } @@ -498,7 +498,7 @@ namespace NXml { * Create xpath context to be used later for fast xpath evaluation. * @param nss: explicitly specify XML namespaces to use and their prefixes */ - TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const { + TXPathContextPtr CreateXPathContext(const TNamespacesForXPath& nss = TNamespacesForXPath()) const { return ActualNode.CreateXPathContext(nss); } @@ -580,10 +580,10 @@ namespace NXml { return ActualNode.FirstChild(name); } - TConstNode FirstChild() const { - return ActualNode.FirstChild(); - } - + TConstNode FirstChild() const { + return ActualNode.FirstChild(); + } + /** * get node neighbour * @param name: neighbour name @@ -593,10 +593,10 @@ namespace NXml { return ActualNode.NextSibling(name); } - TConstNode NextSibling() const { - return ActualNode.NextSibling(); - } - + TConstNode NextSibling() const { + return ActualNode.NextSibling(); + } + /** * get node attribute * @param name: attribute name @@ -645,7 +645,7 @@ namespace NXml { * @throws exception if node is blank */ template <class T> - T Value() const { + T Value() const { return ActualNode.Value<T>(); } @@ -655,7 +655,7 @@ namespace NXml { * returns default value if node is blank */ template <class T> - T Value(const T& defvalue) const { + T Value(const T& defvalue) const { return ActualNode.Value(defvalue); } @@ -669,28 +669,28 @@ namespace NXml { /** * @return application data pointer, passed by SetPrivate */ - void* GetPrivate() const { + void* GetPrivate() const { return ActualNode.GetPrivate(); } /** * get pointer to internal node */ - const xmlNode* GetPtr() const { + const xmlNode* GetPtr() const { return ActualNode.GetPtr(); } /** * check if node is text-only node */ - bool IsText() const { + bool IsText() const { return ActualNode.IsText(); } /** * get node xpath */ - TString Path() const { + TString Path() const { return ActualNode.Path(); } @@ -704,10 +704,10 @@ namespace NXml { TConstNode() = default; TConstNode(TNode node) : ActualNode(node) - { - } + { + } - TNode ConstCast() const { + TNode ConstCast() const { return ActualNode; } @@ -715,4 +715,4 @@ namespace NXml { TNode ActualNode; }; -} +} diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 18a554d732..679f4814af 100644 --- a/library/cpp/xml/document/xml-document.cpp +++ b/library/cpp/xml/document/xml-document.cpp @@ -4,28 +4,28 @@ #include <libxml/xpathInternals.h> #include <library/cpp/xml/init/init.h> - -#include <util/generic/yexception.h> -#include <util/folder/dirut.h> - -namespace { + +#include <util/generic/yexception.h> +#include <util/folder/dirut.h> + +namespace { struct TInit { - inline TInit() { - NXml::InitEngine(); - } - } initer; -} - + inline TInit() { + NXml::InitEngine(); + } + } initer; +} + namespace NXml { - TDocument::TDocument(const TString& xml, Source type) { - switch (type) { - case File: - ParseFile(xml); - break; - case String: - ParseString(xml); - break; - case RootName: { + TDocument::TDocument(const TString& xml, Source type) { + switch (type) { + case File: + ParseFile(xml); + break; + case String: + ParseString(xml); + break; + case RootName: { TDocHolder doc(xmlNewDoc(XMLCHAR("1.0"))); if (!doc) THROW(XmlException, "Can't create xml document."); @@ -37,16 +37,16 @@ namespace NXml { xmlDocSetRootElement(doc.Get(), node.Get()); Y_UNUSED(node.Release()); Doc = std::move(doc); - } break; - default: - THROW(InvalidArgument, "Wrong source type"); + } break; + default: + THROW(InvalidArgument, "Wrong source type"); } } TDocument::TDocument(TDocument&& doc) : Doc(std::move(doc.Doc)) - { - } + { + } TDocument& TDocument::operator=(TDocument&& doc) { if (this != &doc) @@ -55,7 +55,7 @@ namespace NXml { return *this; } - void TDocument::ParseFile(const TString& file) { + void TDocument::ParseFile(const TString& file) { if (!NFs::Exists(file)) THROW(XmlException, "File " << file << " doesn't exist"); @@ -88,7 +88,7 @@ namespace NXml { Doc = std::move(doc); } - TNode TDocument::Root() { + TNode TDocument::Root() { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); @@ -96,7 +96,7 @@ namespace NXml { return TNode(Doc.Get(), r); } - TConstNode TDocument::Root() const { + TConstNode TDocument::Root() const { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) THROW(XmlException, "TDocument hasn't root element"); @@ -104,15 +104,15 @@ namespace NXml { return TConstNode(TNode(Doc.Get(), r)); } - bool TNode::IsNull() const { + bool TNode::IsNull() const { return NodePointer == nullptr; } - bool TNode::IsElementNode() const { - return !IsNull() && (NodePointer->type == XML_ELEMENT_NODE); - } - - TXPathContextPtr TNode::CreateXPathContext(const TNamespacesForXPath& nss) const { + bool TNode::IsElementNode() const { + return !IsNull() && (NodePointer->type == XML_ELEMENT_NODE); + } + + TXPathContextPtr TNode::CreateXPathContext(const TNamespacesForXPath& nss) const { TXPathContextPtr ctx = xmlXPathNewContext(DocPointer); if (!ctx) THROW(XmlException, "Can't create empty xpath context"); @@ -196,25 +196,25 @@ namespace NXml { return const_cast<TNode*>(this)->FirstChild(name); } - TNode TNode::FirstChild() { - if (IsNull()) - THROW(XmlException, "Node is null"); - - return TNode(DocPointer, NodePointer->children); - } - - TConstNode TNode::FirstChild() const { - return const_cast<TNode*>(this)->FirstChild(); - } - - TNode TNode::Parent() { + TNode TNode::FirstChild() { + if (IsNull()) + THROW(XmlException, "Node is null"); + + return TNode(DocPointer, NodePointer->children); + } + + TConstNode TNode::FirstChild() const { + return const_cast<TNode*>(this)->FirstChild(); + } + + TNode TNode::Parent() { if (nullptr == NodePointer->parent) THROW(XmlException, "Parent node not exists"); - + return TNode(DocPointer, NodePointer->parent); } - TConstNode TNode::Parent() const { + TConstNode TNode::Parent() const { return const_cast<TNode*>(this)->Parent(); } @@ -229,17 +229,17 @@ namespace NXml { return const_cast<TNode*>(this)->NextSibling(name); } - TNode TNode::NextSibling() { - if (IsNull()) - THROW(XmlException, "Node is null"); - - return TNode(DocPointer, NodePointer->next); - } - - TConstNode TNode::NextSibling() const { - return const_cast<TNode*>(this)->NextSibling(); - } - + TNode TNode::NextSibling() { + if (IsNull()) + THROW(XmlException, "Node is null"); + + return TNode(DocPointer, NodePointer->next); + } + + TConstNode TNode::NextSibling() const { + return const_cast<TNode*>(this)->NextSibling(); + } + /* NOTE: by default child will inherit it's parent ns */ TNode TNode::AddChild(TZtStringBuf name) { @@ -254,17 +254,17 @@ namespace NXml { return TNode(DocPointer, copy); } - void TNode::SetPrivate(void* priv) { + void TNode::SetPrivate(void* priv) { NodePointer->_private = priv; } - void* TNode::GetPrivate() const { + void* TNode::GetPrivate() const { return NodePointer->_private; } 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())))) + if (start->type == XML_ELEMENT_NODE && (name.empty() || !xmlStrcmp(start->name, XMLCHAR(name.c_str())))) return TNode(DocPointer, start); return TNode(); @@ -277,7 +277,7 @@ namespace NXml { return CAST2CHAR(NodePointer->name); } - TString TNode::Path() const { + TString TNode::Path() const { TCharPtr path(xmlGetNodePath(NodePointer)); if (!!path) return CAST2CHAR(path.Get()); @@ -285,15 +285,15 @@ namespace NXml { return ""; } - xmlNode* TNode::GetPtr() { + xmlNode* TNode::GetPtr() { return NodePointer; } - const xmlNode* TNode::GetPtr() const { + const xmlNode* TNode::GetPtr() const { return NodePointer; } - bool TNode::IsText() const { + bool TNode::IsText() const { if (IsNull()) THROW(XmlException, "Node is null"); @@ -306,7 +306,7 @@ namespace NXml { xmlFreeNode(nodePtr); } - static int XmlWriteToOstream(void* context, const char* buffer, int len) { + static int XmlWriteToOstream(void* context, const char* buffer, int len) { // possibly use to save doc as well IOutputStream* out = (IOutputStream*)context; out->Write(buffer, len); @@ -339,7 +339,7 @@ namespace NXml { } TConstNodes& TConstNodes::operator=(const TConstNodes& nodes) { - if (this != &nodes) { + if (this != &nodes) { SizeValue = nodes.Size(); Doc = nodes.Doc; Obj = nodes.Obj; @@ -355,8 +355,8 @@ namespace NXml { { } - TConstNodes& TConstNodes::operator=(TConstNodesRef ref) { - if (this != &ref.r_) { + TConstNodes& TConstNodes::operator=(TConstNodesRef ref) { + if (this != &ref.r_) { SizeValue = ref.r_.Size(); Doc = ref.r_.Doc; Obj = ref.r_.Obj; @@ -364,7 +364,7 @@ namespace NXml { return *this; } - TConstNodes::operator TConstNodesRef() { + TConstNodes::operator TConstNodesRef() { return TConstNodesRef(*this); } @@ -375,7 +375,7 @@ namespace NXml { { } - TConstNode TConstNodes::operator[](size_t number) const { + TConstNode TConstNodes::operator[](size_t number) const { if (number + 1 > Size()) THROW(XmlException, "index out of range " << number); @@ -386,8 +386,8 @@ namespace NXml { return TNode(Doc, node); } - TConstNode TConstNodes::TNodeIter::operator*() const { + TConstNode TConstNodes::TNodeIter::operator*() const { return Nodes[Index]; } -} +} diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp index 9f537b75c4..86c995b021 100644 --- a/library/cpp/xml/document/xml-document_ut.cpp +++ b/library/cpp/xml/document/xml-document_ut.cpp @@ -5,27 +5,27 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { Y_UNIT_TEST(Iteration) { - NXml::TDocument xml( - "<?xml version=\"1.0\"?>\n" - "<root>qq<a><b></b></a>ww<c></c></root>", - NXml::TDocument::String); - - NXml::TConstNode root = xml.Root(); - UNIT_ASSERT_EQUAL(root.Name(), "root"); - NXml::TConstNode n = root.FirstChild().NextSibling(); - UNIT_ASSERT_EQUAL(n.Name(), "a"); - n = n.NextSibling().NextSibling(); - UNIT_ASSERT_EQUAL(n.Name(), "c"); - } - + NXml::TDocument xml( + "<?xml version=\"1.0\"?>\n" + "<root>qq<a><b></b></a>ww<c></c></root>", + NXml::TDocument::String); + + NXml::TConstNode root = xml.Root(); + UNIT_ASSERT_EQUAL(root.Name(), "root"); + NXml::TConstNode n = root.FirstChild().NextSibling(); + UNIT_ASSERT_EQUAL(n.Name(), "a"); + n = n.NextSibling().NextSibling(); + UNIT_ASSERT_EQUAL(n.Name(), "c"); + } + Y_UNIT_TEST(ParseString) { 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); + "<?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"); @@ -49,29 +49,29 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { 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" - " <authors>\n" - " <graham name=\"Nelson\"/>\n" - " <zarf>Andrew Plotkin</zarf>\n" + " <authors>\n" + " <graham name=\"Nelson\"/>\n" + " <zarf>Andrew Plotkin</zarf>\n" " <emshort>Emily Short</emshort>\n" - " </authors>\n" - "</frob>\n"); + " </authors>\n" + "</frob>\n"); // check default utf8 output with ru { NXml::TDocument xml2("frob", NXml::TDocument::RootName); xml2.Root().SetAttr("xyzzy", "привет =)"); UNIT_ASSERT_VALUES_EQUAL(xml2.ToString(), "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" - "<frob xyzzy=\"привет =)\"/>\n"); + "<frob xyzzy=\"привет =)\"/>\n"); } } Y_UNIT_TEST(XPathNs) { using namespace NXml; TDocument xml( - "<?xml version=\"1.0\"?>\n" - "<root xmlns='http://hello.com/hello'>\n" - "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" - "<text>Некоторый текст</text>\n" - "</root>", - TDocument::String); + "<?xml version=\"1.0\"?>\n" + "<root xmlns='http://hello.com/hello'>\n" + "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" + "<text>Некоторый текст</text>\n" + "</root>", + TDocument::String); TNamespacesForXPath nss; TNamespaceForXPath ns = {"h", "http://hello.com/hello"}; @@ -115,7 +115,7 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { node = node.NextSibling(); UNIT_ASSERT_EQUAL(node.IsNull(), true); TStringStream iterLog; - for (const auto& node2 : root.Nodes("/root/*")) { + for (const auto& node2 : root.Nodes("/root/*")) { iterLog << node2.Name() << ';'; } UNIT_ASSERT_STRINGS_EQUAL(iterLog.Str(), "a;c;"); @@ -154,15 +154,15 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { Y_UNIT_TEST(DefVal) { using namespace NXml; TDocument xml("<?xml version=\"1.0\"?>\n" - "<root><a></a></root>", - NXml::TDocument::String); + "<root><a></a></root>", + NXml::TDocument::String); 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" - "<root><a x=\"y\"></a></root>", - NXml::TDocument::String); + "<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); } @@ -170,8 +170,8 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { using namespace NXml; TDocument xml("<?xml version=\"1.0\"?>\n" "<root><a x=\"y\">first</a>" - "<a>second</a></root>", - NXml::TDocument::String); + "<a>second</a></root>", + NXml::TDocument::String); UNIT_ASSERT_EXCEPTION(xml.Root().Node("/root/a/@x"), yexception); UNIT_ASSERT_STRINGS_EQUAL(xml.Root().Node("/root/a").Value<TString>(), "first"); } @@ -185,12 +185,12 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { // do copy TDocument xml("<?xml version=\"1.0\"?>\n" - "<root><a></a></root>", - TDocument::String); + "<root><a></a></root>", + TDocument::String); TDocument xml2("<?xml version=\"1.0\"?>\n" - "<root><node><b>bold</b><i>ita</i></node></root>", - TDocument::String); + "<root><node><b>bold</b><i>ita</i></node></root>", + TDocument::String); TNode node = xml2.Root().Node("//node"); TNode place = xml.Root().Node("//a"); @@ -200,8 +200,8 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { TStringStream s; xml.Save(s, "", false); UNIT_ASSERT_VALUES_EQUAL(s.Str(), - "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<root><a><node><b>bold</b><i>ita</i></node></a></root>\n"); + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<root><a><node><b>bold</b><i>ita</i></node></a></root>\n"); } Y_UNIT_TEST(RenderNode) { @@ -213,8 +213,8 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { "<root>\n" "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" "<text>Некоторый текст</text>\n" - "</root>", - TDocument::String); + "</root>", + TDocument::String); TNode n = xml.Root().Node("//a"); UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>"); } @@ -225,8 +225,8 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) { "<root xmlns='http://hello.com/hello'>\n" "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n" "<text>Некоторый текст</text>\n" - "</root>", - TDocument::String); + "</root>", + TDocument::String); TNamespacesForXPath nss; TNamespaceForXPath ns = {"h", "http://hello.com/hello"}; nss.push_back(ns); diff --git a/library/cpp/xml/document/xml-options.h b/library/cpp/xml/document/xml-options.h index bb07da0cfb..5aad891d68 100644 --- a/library/cpp/xml/document/xml-options.h +++ b/library/cpp/xml/document/xml-options.h @@ -64,4 +64,4 @@ namespace NXml { int Mask; }; -} +} diff --git a/library/cpp/xml/document/xml-textreader.cpp b/library/cpp/xml/document/xml-textreader.cpp index b946f1fbf2..944d61928d 100644 --- a/library/cpp/xml/document/xml-textreader.cpp +++ b/library/cpp/xml/document/xml-textreader.cpp @@ -315,4 +315,4 @@ namespace NXml { xmlFreeTextReader(handle); } }; -} +} diff --git a/library/cpp/xml/document/xml-textreader.h b/library/cpp/xml/document/xml-textreader.h index ab4c329d26..9230456479 100644 --- a/library/cpp/xml/document/xml-textreader.h +++ b/library/cpp/xml/document/xml-textreader.h @@ -322,4 +322,4 @@ namespace NXml { THolder<xmlTextReader, TDeleter> Impl; }; -} +} diff --git a/library/cpp/xml/document/xml-textreader_ut.cpp b/library/cpp/xml/document/xml-textreader_ut.cpp index 6232dfe47e..2cd0be96bc 100644 --- a/library/cpp/xml/document/xml-textreader_ut.cpp +++ b/library/cpp/xml/document/xml-textreader_ut.cpp @@ -20,26 +20,26 @@ namespace { while (reader.Read()) { if (reader.GetNodeType() == NXml::TTextReader::ENodeType::Element && reader.GetLocalName() == localName && - reader.GetNamespaceUri() == namespaceUri) - { + reader.GetNamespaceUri() == namespaceUri) + { const NXml::TConstNode node = reader.Expand(); nodeHandlerFunc(node); } } } -} +} Y_UNIT_TEST_SUITE(TestXmlTextReader) { Y_UNIT_TEST(BasicExample) { const TString xml = "<?xml version=\"1.0\"?>\n" - "<example toto=\"1\">\n" - " <examplechild id=\"1\">\n" - " <child_of_child/>\n" - " </examplechild>\n" - " <examplechild id=\"2\" toto=\"3\">\n" - " <child_of_child>Some content : -)</child_of_child>\n" - " </examplechild>\n" - "</example>\n"; + "<example toto=\"1\">\n" + " <examplechild id=\"1\">\n" + " <child_of_child/>\n" + " </examplechild>\n" + " <examplechild id=\"2\" toto=\"3\">\n" + " <child_of_child>Some content : -)</child_of_child>\n" + " </examplechild>\n" + "</example>\n"; TStringInput input(xml); NXml::TTextReader reader(input); @@ -94,7 +94,7 @@ Y_UNIT_TEST_SUITE(TestXmlTextReader) { TItem{2, ENT::SignificantWhitespace, "#text", "", "\n "}, TItem{1, ENT::EndElement, "examplechild", "id: 2, toto: 3", ""}, TItem{1, ENT::SignificantWhitespace, "#text", "", "\n"}, - TItem{0, ENT::EndElement, "example", "toto: 1", ""}}; + TItem{0, ENT::EndElement, "example", "toto: 1", ""}}; UNIT_ASSERT_VALUES_EQUAL(found.size(), expected.size()); @@ -108,31 +108,31 @@ Y_UNIT_TEST_SUITE(TestXmlTextReader) { } const TString GEODATA = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" - "<root>" - "" - " <country id=\"225\">" - " <name>Россия</name>" - " <cities>" - " <city>Москва</city>" - " <city>Санкт-Петербург</city>" - " </cities>" - " </country>" - "" - " <country id=\"149\">" - " <name>Беларусь</name>" - " <cities>" - " <city>Минск</city>" - " </cities>" - " </country>" - "" - " <country id=\"187\">" - " <name>Украина</name>" - " <cities>" - " <city>Киев</city>" - " </cities>" - " </country>" - "" - "</root>"; + "<root>" + "" + " <country id=\"225\">" + " <name>Россия</name>" + " <cities>" + " <city>Москва</city>" + " <city>Санкт-Петербург</city>" + " </cities>" + " </country>" + "" + " <country id=\"149\">" + " <name>Беларусь</name>" + " <cities>" + " <city>Минск</city>" + " </cities>" + " </country>" + "" + " <country id=\"187\">" + " <name>Украина</name>" + " <cities>" + " <city>Киев</city>" + " </cities>" + " </country>" + "" + "</root>"; Y_UNIT_TEST(ParseXmlSimple) { struct TCountry { @@ -253,10 +253,10 @@ Y_UNIT_TEST_SUITE(TestXmlTextReader) { Y_UNIT_TEST(NamespaceHell) { using TNS = NXml::TNamespaceForXPath; - const NXml::TNamespacesForXPath ns = { + const NXml::TNamespacesForXPath ns = { TNS{"b", "http://maps.yandex.ru/backa/1.x"}, TNS{"gml", "http://www.opengis.net/gml"}, - TNS{"xal", "urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"}}; + TNS{"xal", "urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"}}; int count = 0; THashMap<TString, TString> positions; diff --git a/library/cpp/xml/document/ya.make b/library/cpp/xml/document/ya.make index 86bbd639cf..9fed445b29 100644 --- a/library/cpp/xml/document/ya.make +++ b/library/cpp/xml/document/ya.make @@ -1,7 +1,7 @@ -LIBRARY() - -OWNER(finder) +LIBRARY() +OWNER(finder) + SRCS( xml-document.cpp xml-textreader.cpp |