diff options
author | finder <finder@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:24 +0300 |
commit | 2ab6bab688484680d3ebc5c4a7e9cd3ff04fda04 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/xml/document/xml-document.cpp | |
parent | abbbaf4075fbaa0ff4ce9faa1188089466a21dbe (diff) | |
download | ydb-2ab6bab688484680d3ebc5c4a7e9cd3ff04fda04.tar.gz |
Restoring authorship annotation for <finder@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/xml/document/xml-document.cpp')
-rw-r--r-- | library/cpp/xml/document/xml-document.cpp | 246 |
1 files changed, 123 insertions, 123 deletions
diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 3e29147b53..18a554d732 100644 --- a/library/cpp/xml/document/xml-document.cpp +++ b/library/cpp/xml/document/xml-document.cpp @@ -1,8 +1,8 @@ -#include "xml-document.h" - +#include "xml-document.h" + #include <libxml/xinclude.h> #include <libxml/xpathInternals.h> - + #include <library/cpp/xml/init/init.h> #include <util/generic/yexception.h> @@ -16,7 +16,7 @@ namespace { } initer; } -namespace NXml { +namespace NXml { TDocument::TDocument(const TString& xml, Source type) { switch (type) { case File: @@ -27,87 +27,87 @@ namespace NXml { break; case RootName: { TDocHolder doc(xmlNewDoc(XMLCHAR("1.0"))); - if (!doc) - THROW(XmlException, "Can't create xml document."); + if (!doc) + THROW(XmlException, "Can't create xml document."); doc->encoding = xmlStrdup(XMLCHAR("utf-8")); TNodePtr node(xmlNewNode(nullptr, XMLCHAR(xml.c_str()))); if (!node) - THROW(XmlException, "Can't create root node."); - xmlDocSetRootElement(doc.Get(), node.Get()); + THROW(XmlException, "Can't create root node."); + xmlDocSetRootElement(doc.Get(), node.Get()); Y_UNUSED(node.Release()); Doc = std::move(doc); } break; default: THROW(InvalidArgument, "Wrong source type"); - } - } - + } + } + TDocument::TDocument(TDocument&& doc) : Doc(std::move(doc.Doc)) { } - + TDocument& TDocument::operator=(TDocument&& doc) { - if (this != &doc) + if (this != &doc) doc.Swap(*this); - - return *this; - } - + + return *this; + } + void TDocument::ParseFile(const TString& file) { if (!NFs::Exists(file)) - THROW(XmlException, "File " << file << " doesn't exist"); - - TParserCtxtPtr pctx(xmlNewParserCtxt()); - if (!pctx) - THROW(XmlException, "Can't create parser context"); - + THROW(XmlException, "File " << file << " doesn't exist"); + + TParserCtxtPtr pctx(xmlNewParserCtxt()); + if (!pctx) + THROW(XmlException, "Can't create parser context"); + TDocHolder doc(xmlCtxtReadFile(pctx.Get(), file.c_str(), nullptr, XML_PARSE_NOCDATA)); - if (!doc) - THROW(XmlException, "Can't parse file " << file); - - int res = xmlXIncludeProcessFlags(doc.Get(), XML_PARSE_XINCLUDE | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE); - - if (res == -1) - THROW(XmlException, "XIncludes processing failed"); - + if (!doc) + THROW(XmlException, "Can't parse file " << file); + + int res = xmlXIncludeProcessFlags(doc.Get(), XML_PARSE_XINCLUDE | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE); + + if (res == -1) + THROW(XmlException, "XIncludes processing failed"); + Doc = std::move(doc); - } - + } + void TDocument::ParseString(TZtStringBuf xml) { - TParserCtxtPtr pctx(xmlNewParserCtxt()); + TParserCtxtPtr pctx(xmlNewParserCtxt()); if (pctx.Get() == nullptr) - THROW(XmlException, "Can't create parser context"); - + THROW(XmlException, "Can't create parser context"); + TDocHolder doc(xmlCtxtReadMemory(pctx.Get(), xml.c_str(), (int)xml.size(), nullptr, nullptr, XML_PARSE_NOCDATA)); - - if (!doc) - THROW(XmlException, "Can't parse string"); - + + if (!doc) + THROW(XmlException, "Can't parse string"); + Doc = std::move(doc); - } - + } + TNode TDocument::Root() { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) - THROW(XmlException, "TDocument hasn't root element"); - + THROW(XmlException, "TDocument hasn't root element"); + return TNode(Doc.Get(), r); - } - + } + TConstNode TDocument::Root() const { xmlNode* r = xmlDocGetRootElement(Doc.Get()); if (r == nullptr) - THROW(XmlException, "TDocument hasn't root element"); - + THROW(XmlException, "TDocument hasn't root element"); + return TConstNode(TNode(Doc.Get(), r)); - } - + } + bool TNode::IsNull() const { return NodePointer == nullptr; - } - + } + bool TNode::IsElementNode() const { return !IsNull() && (NodePointer->type == XML_ELEMENT_NODE); } @@ -130,7 +130,7 @@ namespace NXml { TXPathContextPtr ctxt = CreateXPathContext(ns); return XPath(xpath, quiet, *ctxt); } - + 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"); @@ -142,11 +142,11 @@ namespace NXml { TConstNodes nodes(DocPointer, obj); if (nodes.Size() == 0 && !quiet) - THROW(NodeNotFound, xpath); - - return nodes; - } - + THROW(NodeNotFound, xpath); + + return nodes; + } + TConstNodes TNode::Nodes(TZtStringBuf xpath, bool quiet, const TNamespacesForXPath& ns) const { TXPathContextPtr ctxt = CreateXPathContext(ns); return Nodes(xpath, quiet, *ctxt); @@ -158,12 +158,12 @@ namespace NXml { THROW(XmlException, "xpath points to non-element nodes: " << xpath); return nodes; } - + 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 { TXPathContextPtr ctxt = CreateXPathContext(ns); return Node(xpath, quiet, *ctxt); @@ -173,29 +173,29 @@ namespace NXml { TConstNodes n = Nodes(xpath, quiet, ctxt); if (n.Size() == 0 && !quiet) - THROW(NodeNotFound, xpath); - + THROW(NodeNotFound, xpath); + if (n.Size() == 0) return TNode(); - else + else return n[0].ConstCast(); - } - + } + 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()) - THROW(XmlException, "Node is null"); - + THROW(XmlException, "Node is null"); + return Find(NodePointer->children, name); - } - + } + TConstNode TNode::FirstChild(TZtStringBuf name) const { return const_cast<TNode*>(this)->FirstChild(name); - } - + } + TNode TNode::FirstChild() { if (IsNull()) THROW(XmlException, "Node is null"); @@ -220,15 +220,15 @@ namespace NXml { TNode TNode::NextSibling(TZtStringBuf name) { if (IsNull()) - THROW(XmlException, "Node is null"); - + THROW(XmlException, "Node is null"); + return Find(NodePointer->next, name); - } - + } + TConstNode TNode::NextSibling(TZtStringBuf name) const { return const_cast<TNode*>(this)->NextSibling(name); - } - + } + TNode TNode::NextSibling() { if (IsNull()) THROW(XmlException, "Node is null"); @@ -240,12 +240,12 @@ namespace NXml { return const_cast<TNode*>(this)->NextSibling(); } - /* NOTE: by default child will inherit it's parent ns */ - + /* NOTE: by default child will inherit it's parent ns */ + TNode TNode::AddChild(TZtStringBuf name) { - return AddChild(name, ""); - } - + return AddChild(name, ""); + } + /* NOTE: source node will be copied, as otherwise it will be double-freed from this and its own document */ TNode TNode::AddChild(const TConstNode& node) { @@ -257,34 +257,34 @@ namespace NXml { void TNode::SetPrivate(void* priv) { NodePointer->_private = priv; } - + void* TNode::GetPrivate() const { return NodePointer->_private; } TNode TNode::Find(xmlNode* start, TZtStringBuf name) { - for (; start; start = start->next) + 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(); - } - + } + TString TNode::Name() const { if (IsNull()) THROW(XmlException, "Node is null"); return CAST2CHAR(NodePointer->name); } - + TString TNode::Path() const { TCharPtr path(xmlGetNodePath(NodePointer)); - if (!!path) - return CAST2CHAR(path.Get()); - else - return ""; - } - + if (!!path) + return CAST2CHAR(path.Get()); + else + return ""; + } + xmlNode* TNode::GetPtr() { return NodePointer; } @@ -335,57 +335,57 @@ namespace NXml { : 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; - } - - return *this; - } - - TConstNodes::TConstNodes(TConstNodesRef ref) + } + + return *this; + } + + TConstNodes::TConstNodes(TConstNodesRef ref) : 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; - } - return *this; - } - + } + return *this; + } + TConstNodes::operator TConstNodesRef() { - return TConstNodesRef(*this); - } - + return TConstNodesRef(*this); + } + TConstNodes::TConstNodes(xmlDoc* doc, TXPathObjectPtr obj) : SizeValue(obj && obj->nodesetval ? obj->nodesetval->nodeNr : 0) , Doc(doc) , Obj(obj) - { - } - + { + } + TConstNode TConstNodes::operator[](size_t number) const { if (number + 1 > Size()) - THROW(XmlException, "index out of range " << number); - + THROW(XmlException, "index out of range " << number); + if (!Obj || !Obj->nodesetval) THROW(XmlException, "Broken TConstNodes object, Obj is null"); - + xmlNode* node = Obj->nodesetval->nodeTab[number]; return TNode(Doc, node); - } - + } + TConstNode TConstNodes::TNodeIter::operator*() const { return Nodes[Index]; } |