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 | abbbaf4075fbaa0ff4ce9faa1188089466a21dbe (patch) | |
tree | 4beaffe75727862ab08110c7ce520dc7aa49ff30 /library/cpp/xml | |
parent | 46f4bc6ab513a0ed1407f9095284a00e20f05adc (diff) | |
download | ydb-abbbaf4075fbaa0ff4ce9faa1188089466a21dbe.tar.gz |
Restoring authorship annotation for <finder@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/xml')
-rw-r--r-- | library/cpp/xml/document/libxml-guards.h | 58 | ||||
-rw-r--r-- | library/cpp/xml/document/node-attr.h | 174 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document-decl.h | 520 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.cpp | 246 | ||||
-rw-r--r-- | library/cpp/xml/document/xml-document.h | 4 | ||||
-rw-r--r-- | library/cpp/xml/document/ya.make | 18 |
6 files changed, 510 insertions, 510 deletions
diff --git a/library/cpp/xml/document/libxml-guards.h b/library/cpp/xml/document/libxml-guards.h index 4188cecff1..4028c53e9f 100644 --- a/library/cpp/xml/document/libxml-guards.h +++ b/library/cpp/xml/document/libxml-guards.h @@ -1,50 +1,50 @@ -#pragma once +#pragma once #include <library/cpp/xml/init/ptr.h> -#include <util/generic/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/xmlsave.h> - -namespace NXml { - namespace NDetail { - struct TSignedCharPtrTraits { - static void Destroy(char* handle) { - xmlFree(handle); - } - }; - - struct TCharPtrTraits { - static void Destroy(xmlChar* handle) { - xmlFree(handle); - } - }; - - struct TOutputBufferPtrTraits { - static void Destroy(xmlOutputBufferPtr handle) { - xmlOutputBufferClose(handle); - } - }; - + +namespace NXml { + namespace NDetail { + struct TSignedCharPtrTraits { + static void Destroy(char* handle) { + xmlFree(handle); + } + }; + + struct TCharPtrTraits { + static void Destroy(xmlChar* handle) { + xmlFree(handle); + } + }; + + struct TOutputBufferPtrTraits { + static void Destroy(xmlOutputBufferPtr handle) { + xmlOutputBufferClose(handle); + } + }; + struct TSaveCtxtPtrTraits { static void Destroy(xmlSaveCtxtPtr handle) { xmlSaveClose(handle); } }; - } - + } + typedef TxmlXPathContextPtr TXPathContextPtr; typedef TxmlXPathObjectPtr TXPathObjectPtr; - typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; - typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; + typedef TAutoPtr<char, NDetail::TSignedCharPtrTraits> TSignedCharPtr; + typedef TAutoPtr<xmlChar, NDetail::TCharPtrTraits> TCharPtr; typedef TxmlDocHolder TDocHolder; typedef TxmlURIPtr TURIPtr; typedef TxmlNodePtr TNodePtr; - typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; + typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr; 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..1c4082e6fe 100644 --- a/library/cpp/xml/document/node-attr.h +++ b/library/cpp/xml/document/node-attr.h @@ -1,13 +1,13 @@ -#pragma once - +#pragma once + #include "xml-document-decl.h" #include "libxml-guards.h" -#include <util/stream/str.h> +#include <util/stream/str.h> #include <util/string/cast.h> - -namespace NXml { + +namespace NXml { #define THROW(x, y) ythrow yexception() << #x << ": " << y - + // libxml defines unsigned char -> xmlChar, // and all functions use xmlChar. inline static const char* CAST2CHAR(const xmlChar* x) { @@ -17,123 +17,123 @@ namespace NXml { return reinterpret_cast<const xmlChar*>(x); } - template <class T> + template <class T> void TNode::AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const { - try { - res = FromString<T>(CAST2CHAR(value.Get())); + try { + res = FromString<T>(CAST2CHAR(value.Get())); } catch (TFromStringException&) { THROW(XmlException, "Failed to convert string " << TString{TStringBuf(CAST2CHAR(value.Get())).substr(0, 50)}.Quote() << " from '" << errContext << "' to requested type"); - } - } - - template <> + } + } + + template <> inline void TNode::AttrInternal(TCharPtr& value, TString& res, TStringBuf /*errContext*/) const { TString tmp(CAST2CHAR(value.Get())); - res.swap(tmp); - } - - template <class T> + res.swap(tmp); + } + + template <class T> T TNode::Attr(TZtStringBuf name) const { TCharPtr value(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!value) { THROW(AttributeNotFound, Path() << "@" << name); - } - - T t; + } + + T t; AttrInternal(value, t, name); - return t; - } - - template <class T> + return t; + } + + template <class T> T TNode::Attr(TZtStringBuf name, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { - return defvalue; - } - - T t; + return defvalue; + } + + T t; AttrInternal(attr, t, name); - return t; - } - - template <class T> + return t; + } + + template <class T> void TNode::Attr(TZtStringBuf name, T& value) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); if (!attr) { THROW(AttributeNotFound, Path() << name); - } - + } + AttrInternal(attr, value, name); - } - - template <class T> + } + + template <class T> void TNode::Attr(TZtStringBuf name, T& value, const T& defvalue) const { TCharPtr attr(xmlGetProp(NodePointer, XMLCHAR(name.c_str()))); - + if (!attr) { - value = defvalue; + value = defvalue; } else { AttrInternal(attr, value, name); - } - } - - template <class T> + } + } + + template <class T> T TNode::Value() const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { THROW(NodeIsBlank, Path()); - } - + } + TCharPtr val(xmlNodeGetContent(NodePointer)); - T t; + T t; AttrInternal(val, t, this->Name()); - return t; - } - - template <class T> + return t; + } + + template <class T> T TNode::Value(const T& defvalue) const { if (!NodePointer || xmlIsBlankNode(NodePointer)) { - return defvalue; - } - + return defvalue; + } + TCharPtr val(xmlNodeGetContent(NodePointer)); - T t; + T t; AttrInternal(val, t, this->Name()); - return t; - } - - template <class T> + return t; + } + + template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, void>::type TNode::SetValue(const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; SetValue(ss.Str()); } inline void TNode::SetValue(TStringBuf value) { - xmlNodeSetContent(NodePointer, XMLCHAR("")); + xmlNodeSetContent(NodePointer, XMLCHAR("")); xmlNodeAddContentLen(NodePointer, XMLCHAR(value.data()), value.Size()); - } - + } + 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 << ">"); - } - } - - template <class T> + } + } + + template <class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, void>::type TNode::SetAttr(TZtStringBuf name, const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; SetAttr(name, TZtStringBuf(ss.Str())); - } - + } + inline void TNode::SetAttr(TZtStringBuf name) { xmlAttr* attr = xmlSetProp(NodePointer, XMLCHAR(name.c_str()), nullptr); @@ -151,37 +151,37 @@ namespace NXml { << ">"); } - template <class T> + template <class T> typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type TNode::AddChild(TZtStringBuf name, const T& value) { - TStringStream ss; - ss << value; + TStringStream ss; + ss << value; return AddChild(name, TZtStringBuf(ss.Str())); - } - + } + inline TNode TNode::AddChild(TZtStringBuf name, TZtStringBuf value) { if (IsNull()) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: can't add child to null node"); - } - + } + xmlNode* child = nullptr; - + if (value.empty()) { child = xmlNewTextChild(NodePointer, nullptr, XMLCHAR(name.c_str()), nullptr); } else { - child = xmlNewTextChild( + child = xmlNewTextChild( NodePointer, nullptr, XMLCHAR(name.c_str()), XMLCHAR(value.c_str())); - } - + } + if (!child) { THROW(XmlException, "addChild [name=" << name << ", value=" << value << "]: xmlNewTextChild returned NULL"); - } - + } + return TNode(DocPointer, child); - } - + } + template <class T> typename std::enable_if<!std::is_convertible_v<T, TStringBuf>, TNode>::type TNode::AddText(const T& value) { diff --git a/library/cpp/xml/document/xml-document-decl.h b/library/cpp/xml/document/xml-document-decl.h index bfda1fb7e6..dbc289ee04 100644 --- a/library/cpp/xml/document/xml-document-decl.h +++ b/library/cpp/xml/document/xml-document-decl.h @@ -1,52 +1,52 @@ -#pragma once +#pragma once #include <library/cpp/string_utils/ztstrbuf/ztstrbuf.h> #include <util/generic/string.h> -#include <util/generic/vector.h> +#include <util/generic/vector.h> #include <util/stream/output.h> #include <util/stream/str.h> -#include <algorithm> +#include <algorithm> #include "libxml-guards.h" - -namespace NXml { + +namespace NXml { class TNode; - - class TConstNodes; + + class TConstNodes; class TConstNode; - + using TXPathContext = xmlXPathContext; class TDocument { - public: + public: enum Source { - File, - String, - RootName, - }; + File, + String, + RootName, + }; - public: - /** + public: + /** * 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 - */ + * @param src: source type: File | String | RootName + * throws if file not found or cannot be parsed + */ TDocument(const TString& source, Source type = File); - - public: + + public: TDocument(const TDocument& that) = delete; TDocument& operator=(const TDocument& that) = delete; - + TDocument(TDocument&& that); TDocument& operator=(TDocument&& that); - - /** - * get root element - */ + + /** + * get root element + */ TNode Root(); TConstNode Root() const; - + void Save(IOutputStream& stream, TZtStringBuf enc = "", bool shouldFormat = true) const { int bufferSize = 0; xmlChar* xmlBuff = nullptr; @@ -55,7 +55,7 @@ namespace NXml { TCharPtr xmlCharBuffPtr(xmlBuff); stream.Write(xmlBuff, bufferSize); } - + TString ToString(TZtStringBuf enc = "", bool shouldFormat = true) const { TStringStream s; Save(s, enc, shouldFormat); @@ -64,65 +64,65 @@ namespace NXml { void Swap(TDocument& that) { std::swap(this->Doc, that.Doc); - } - + } + xmlDocPtr GetImpl() { return Doc.Get(); } - private: + private: void ParseFile(const TString& file); void ParseString(TZtStringBuf xml); - + TDocument(TDocHolder doc) : Doc(std::move(doc)) { } - + TDocHolder Doc; - }; - - struct TNamespaceForXPath { + }; + + struct TNamespaceForXPath { TString Prefix; TString Url; - }; + }; typedef TVector<TNamespaceForXPath> TNamespacesForXPath; - + class TConstNodes { - private: + private: struct TConstNodesRef { explicit TConstNodesRef(TConstNodes& n) : r_(n) { } - TConstNodes& r_; - }; + TConstNodes& r_; + }; - public: + public: TConstNodes(const TConstNodes& nodes); TConstNodes& operator=(const TConstNodes& nodes); - - TConstNodes(TConstNodesRef ref); + + TConstNodes(TConstNodesRef ref); TConstNodes& operator=(TConstNodesRef ref); - - operator TConstNodesRef(); - - /** - * get node by id - * @param number: node id - */ + + operator TConstNodesRef(); + + /** + * get node by id + * @param number: node id + */ TConstNode operator[](size_t number) const; - - /** - * get number of nodes - */ + + /** + * get number of nodes + */ size_t Size() const { return SizeValue; } size_t size() const { return SizeValue; } - + struct TNodeIter { const TConstNodes& Nodes; size_t Index; @@ -145,30 +145,30 @@ namespace NXml { return TNodeIter{*this, size()}; } - private: - friend class TDocument; + private: + friend class TDocument; friend class TConstNode; friend class TNode; - + TConstNodes(xmlDoc* doc, TXPathObjectPtr obj); - + size_t SizeValue; xmlDoc* Doc; TXPathObjectPtr Obj; - }; - + }; + class TNode { - public: - friend class TDocument; + public: + friend class TDocument; friend class TConstNode; friend class TTextReader; - - /** - * check if node is null - */ + + /** + * check if node is null + */ bool IsNull() const; - - /** + + /** * check if node is element node */ bool IsElementNode() const; @@ -187,16 +187,16 @@ namespace NXml { /** * 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 + * @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. - */ + */ TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const; - - /** + + /** * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found @@ -225,19 +225,19 @@ namespace NXml { /** * 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, + * @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 + * @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. - */ + */ /// @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; - - /** + + /** * 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, @@ -251,18 +251,18 @@ namespace NXml { TConstNode Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const; /** - * get node first child - * @param name: child name + * get node first child + * @param name: child name * @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(); TConstNode FirstChild() const; - /** + /** * get parent node * throws exception if has no parent */ @@ -270,36 +270,36 @@ namespace NXml { TConstNode Parent() const; /** - * get node neighbour - * @param name: neighbour name + * get node neighbour + * @param name: neighbour name * @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(); TConstNode NextSibling() const; - /** - * create child node - * @param name: child name - * returns new empty node - */ + /** + * create child node + * @param name: child name + * returns new empty node + */ TNode AddChild(TZtStringBuf name); - - /** - * create child node with given value - * @param name: child name - * @param value: node value - */ - template <class T> + + /** + * create child node with given value + * @param name: child name + * @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); - /** + /** * add child node, making recursive copy of original * @param node: node to copy from * returns added node @@ -318,80 +318,80 @@ namespace NXml { TNode AddText(TStringBuf value); /** - * get node attribute - * @param name: attribute name - * throws exception if attribute not found - */ - template <class T> + * get node attribute + * @param name: attribute name + * throws exception if attribute not found + */ + template <class T> T Attr(TZtStringBuf name) const; - - /** - * get node attribute - * @param name: attribute name - * returns default value if attribute not found - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * returns default value if attribute not found + */ + template <class T> T Attr(TZtStringBuf name, const T& defvalue) const; - - /** - * get node attribute - * @param name: attribute name - * @param value: return-value - * throws exception if attribute not found - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * @param value: return-value + * throws exception if attribute not found + */ + template <class T> void Attr(TZtStringBuf name, T& value) const; - - /** - * get node attribute - * @param name: attribute name - * @param defvalue: default value - * @param value: return-value - * returns default value if attribute not found, attr value otherwise - */ - template <class T> + + /** + * get node attribute + * @param name: attribute name + * @param defvalue: default value + * @param value: return-value + * returns default value if attribute not found, attr value otherwise + */ + template <class T> void Attr(TZtStringBuf name, T& value, const T& defvalue) const; - - /** - * get node value (text) - * @throws exception if node is blank - */ - template <class T> + + /** + * get node value (text) + * @throws exception if node is blank + */ + template <class T> T Value() const; - - /** - * get node value - * @param defvalue: default value - * returns default value if node is blank - */ - template <class T> + + /** + * get node value + * @param defvalue: default value + * returns default value if node is blank + */ + template <class T> T Value(const T& defvalue) const; - - /** - * set node value - * @param value: new text value - */ + + /** + * 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); - /** - * set/reset node attribute value, - * if attribute does not exist, it'll be created - * @param name: attribute name - * @param value: attribute 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); void SetAttr(TZtStringBuf name); - /** + /** * delete node attribute * @param name: attribute name */ @@ -414,10 +414,10 @@ namespace NXml { TString Name() const; /** - * get node xpath - */ + * get node xpath + */ TString Path() const; - + /** * get node xml representation */ @@ -445,15 +445,15 @@ namespace NXml { */ void Remove(); - /** - * constructs null node - */ + /** + * constructs null node + */ TNode() : NodePointer(nullptr) , DocPointer(nullptr) { } - + private: friend class TConstNodes; @@ -462,30 +462,30 @@ namespace NXml { , DocPointer(doc) { } - + TNode Find(xmlNode* start, TZtStringBuf name); - + template <class T> void AttrInternal(TCharPtr& value, T& res, TStringBuf errContext) const; - + void SaveInternal(IOutputStream& stream, TZtStringBuf enc, int options) const; xmlNode* NodePointer; xmlDoc* DocPointer; - }; - + }; + class TConstNode { - public: - friend class TDocument; - friend class TConstNodes; + public: + friend class TDocument; + friend class TConstNodes; friend class TNode; - /** - * check if node is null - */ + /** + * check if node is null + */ bool IsNull() const { return ActualNode.IsNull(); - } - + } + bool IsElementNode() const { return ActualNode.IsElementNode(); } @@ -494,7 +494,7 @@ namespace NXml { 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 */ @@ -504,18 +504,18 @@ namespace NXml { /** * get all element nodes matching given xpath expression - * @param xpath: xpath expression - * @param quiet: don't throw exception if zero nodes found + * @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. - */ + */ TConstNodes Nodes(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { return ActualNode.Nodes(xpath, quiet, ns); - } - - /** + } + + /** * get all element nodes matching given xpath expression * @param xpath: xpath expression * @param quiet: don't throw exception if zero nodes found @@ -550,18 +550,18 @@ namespace NXml { /** * 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, + * @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. - */ + */ TConstNode Node(TZtStringBuf xpath, bool quiet = false, const TNamespacesForXPath& ns = TNamespacesForXPath()) const { return ActualNode.Node(xpath, quiet, ns); - } - + } + /** * get the first element node matching given xpath expression * @param xpath: path to node (from current node) @@ -578,88 +578,88 @@ namespace NXml { TConstNode FirstChild(TZtStringBuf name) const { return ActualNode.FirstChild(name); - } - + } + TConstNode FirstChild() const { return ActualNode.FirstChild(); } - /** - * get node neighbour - * @param name: neighbour name - * throws exception if no neighbour found - */ + /** + * get node neighbour + * @param name: neighbour name + * throws exception if no neighbour found + */ TConstNode NextSibling(TZtStringBuf name) const { return ActualNode.NextSibling(name); - } - + } + TConstNode NextSibling() const { return ActualNode.NextSibling(); } - /** - * get node attribute - * @param name: attribute name - * throws exception if attribute not found - */ - template <class T> + /** + * get node attribute + * @param name: attribute name + * throws exception if attribute not found + */ + template <class T> T Attr(TZtStringBuf name) const { return ActualNode.Attr<T>(name); - } - - /** - * get node attribute - * @param name: attribute name - * returns default value if attribute not found - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * returns default value if attribute not found + */ + template <class T> T Attr(TZtStringBuf name, const T& defvalue) const { return ActualNode.Attr(name, defvalue); - } - - /** - * get node attribute - * @param name: attribute name - * @param value: return-value - * throws exception if attribute not found - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * @param value: return-value + * throws exception if attribute not found + */ + template <class T> void Attr(TZtStringBuf name, T& value) const { return ActualNode.Attr(name, value); - } - - /** - * get node attribute - * @param name: attribute name - * @param defvalue: default value - * @param value: return-value - * returns default value if attribute not found, attr value otherwise - */ - template <class T> + } + + /** + * get node attribute + * @param name: attribute name + * @param defvalue: default value + * @param value: return-value + * 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); - } - - /** - * get node value (text) - * @throws exception if node is blank - */ - template <class T> + } + + /** + * get node value (text) + * @throws exception if node is blank + */ + template <class T> T Value() const { return ActualNode.Value<T>(); - } - - /** - * get node value - * @param defvalue: default value - * returns default value if node is blank - */ - template <class T> + } + + /** + * get node value + * @param defvalue: default value + * returns default value if node is blank + */ + template <class T> T Value(const T& defvalue) const { return ActualNode.Value(defvalue); - } - - /** + } + + /** * get node name */ TString Name() const { @@ -688,12 +688,12 @@ namespace NXml { } /** - * get node xpath - */ + * get node xpath + */ TString Path() const { return ActualNode.Path(); - } - + } + /** * get node xml representation */ @@ -706,13 +706,13 @@ namespace NXml { : ActualNode(node) { } - + TNode ConstCast() const { return ActualNode; } - private: + private: TNode ActualNode; - }; - + }; + } diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp index 18a554d732..3e29147b53 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]; } diff --git a/library/cpp/xml/document/xml-document.h b/library/cpp/xml/document/xml-document.h index 829ba09cc4..ff496bb138 100644 --- a/library/cpp/xml/document/xml-document.h +++ b/library/cpp/xml/document/xml-document.h @@ -1,4 +1,4 @@ -#pragma once - +#pragma once + #include "xml-document-decl.h" #include "node-attr.h" diff --git a/library/cpp/xml/document/ya.make b/library/cpp/xml/document/ya.make index 86bbd639cf..1f284951e8 100644 --- a/library/cpp/xml/document/ya.make +++ b/library/cpp/xml/document/ya.make @@ -1,17 +1,17 @@ LIBRARY() - + OWNER(finder) SRCS( - xml-document.cpp + xml-document.cpp xml-textreader.cpp xml-options.cpp -) - -PEERDIR( +) + +PEERDIR( library/cpp/xml/init - contrib/libs/libxml + contrib/libs/libxml library/cpp/string_utils/ztstrbuf -) - -END() +) + +END() |