aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/xml/document
diff options
context:
space:
mode:
authorkerzum <kerzum@yandex-team.ru>2022-02-10 16:49:33 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:33 +0300
commit9a7232babfd763ccfe827bc70e82e0f50cfd8276 (patch)
treea39808b7482c4711a80f799a7281adb36d76a13a /library/cpp/xml/document
parent0e68ae909d3b76a5a001a07880eb0010dec6b2ea (diff)
downloadydb-9a7232babfd763ccfe827bc70e82e0f50cfd8276.tar.gz
Restoring authorship annotation for <kerzum@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/xml/document')
-rw-r--r--library/cpp/xml/document/README36
-rw-r--r--library/cpp/xml/document/libxml-guards.h16
-rw-r--r--library/cpp/xml/document/node-attr.h6
-rw-r--r--library/cpp/xml/document/xml-document-decl.h82
-rw-r--r--library/cpp/xml/document/xml-document.cpp46
-rw-r--r--library/cpp/xml/document/xml-document_ut.cpp130
6 files changed, 158 insertions, 158 deletions
diff --git a/library/cpp/xml/document/README b/library/cpp/xml/document/README
index b2649523d8..69f83c55e6 100644
--- a/library/cpp/xml/document/README
+++ b/library/cpp/xml/document/README
@@ -13,30 +13,30 @@ The standard way to use it is as follows:
// iterate over the nodeset
for (size_t i = 0; i < nodes.size(); ++i) {
- using namespace NXml;
+ using namespace NXml;
TConstNode& node = nodes[i];
- // query node
+ // query node
TString name = node.Name();
TString lang = node.Attr<TString>("lang");
TString text = node.Value<TString>();
- TConstNode child = node.GetFirstChild("");
- // edit node
- TNode node = child.ConstCast();
- node.DelAttr("id");
- node.SetAttr("x", 2);
- node.SetValue(5);
- node.AddText(" apples");
+ TConstNode child = node.GetFirstChild("");
+ // edit node
+ TNode node = child.ConstCast();
+ node.DelAttr("id");
+ 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");
- // copy node's subtree from one document to another
- place.AddChild(xml.Root());
- // save (render) single element
+ // edit documents with copy-paste
+ NXml::TDocument xml2("<xpath><node/></xpath>", NXml::TDocument::String);
+ NXml::TNode place = xml2.Root().Node("xpath/node");
+ // copy node's subtree from one document to another
+ place.AddChild(xml.Root());
+ // save (render) single element
TString modifiedNode = place.ToString();
- // save whole document with optional encoding
+ // save whole document with optional encoding
TString modifiedDoc = xml2.ToString("ISO-8559-1");
-
-
+
+
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..9a7c7a0fbb 100644
--- a/library/cpp/xml/document/libxml-guards.h
+++ b/library/cpp/xml/document/libxml-guards.h
@@ -6,7 +6,7 @@
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <libxml/uri.h>
-#include <libxml/xmlsave.h>
+#include <libxml/xmlsave.h>
namespace NXml {
namespace NDetail {
@@ -28,12 +28,12 @@ namespace NXml {
}
};
- struct TSaveCtxtPtrTraits {
- static void Destroy(xmlSaveCtxtPtr handle) {
- xmlSaveClose(handle);
- }
- };
-
+ struct TSaveCtxtPtrTraits {
+ static void Destroy(xmlSaveCtxtPtr handle) {
+ xmlSaveClose(handle);
+ }
+ };
+
}
typedef TxmlXPathContextPtr TXPathContextPtr;
@@ -45,6 +45,6 @@ namespace NXml {
typedef TxmlNodePtr TNodePtr;
typedef TAutoPtr<xmlOutputBuffer, NDetail::TOutputBufferPtrTraits> TOutputBufferPtr;
typedef TxmlParserCtxtPtr TParserCtxtPtr;
- typedef TAutoPtr<xmlSaveCtxt, NDetail::TSaveCtxtPtrTraits> TSaveCtxtPtr;
+ 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..50641cac55 100644
--- a/library/cpp/xml/document/node-attr.h
+++ b/library/cpp/xml/document/node-attr.h
@@ -145,12 +145,12 @@ namespace NXml {
}
inline void TNode::DelAttr(TZtStringBuf name) {
- if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0)
+ if (xmlUnsetProp(NodePointer, XMLCHAR(name.c_str())) < 0)
THROW(XmlException, "Can't delete node attribute <"
<< name
<< ">");
- }
-
+ }
+
template <class T>
typename std::enable_if<!std::is_convertible_v<T, TZtStringBuf>, TNode>::type
TNode::AddChild(TZtStringBuf name, 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..5ab17f65bd 100644
--- a/library/cpp/xml/document/xml-document-decl.h
+++ b/library/cpp/xml/document/xml-document-decl.h
@@ -51,7 +51,7 @@ namespace NXml {
int bufferSize = 0;
xmlChar* xmlBuff = nullptr;
const char* encoding = enc.size() ? enc.data() : Doc->encoding ? nullptr : "UTF-8";
- xmlDocDumpFormatMemoryEnc(Doc.Get(), &xmlBuff, &bufferSize, encoding, shouldFormat);
+ xmlDocDumpFormatMemoryEnc(Doc.Get(), &xmlBuff, &bufferSize, encoding, shouldFormat);
TCharPtr xmlCharBuffPtr(xmlBuff);
stream.Write(xmlBuff, bufferSize);
}
@@ -233,7 +233,7 @@ namespace NXml {
* 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
+ /// @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;
@@ -300,13 +300,13 @@ namespace NXml {
TNode AddChild(TZtStringBuf name, TZtStringBuf value);
/**
- * add child node, making recursive copy of original
- * @param node: node to copy from
- * returns added node
- */
- TNode AddChild(const TConstNode& node);
-
- /**
+ * add child node, making recursive copy of original
+ * @param node: node to copy from
+ * returns added node
+ */
+ TNode AddChild(const TConstNode& node);
+
+ /**
* create text child node
* @param name: child name
* @param value: node value
@@ -392,12 +392,12 @@ namespace NXml {
void SetAttr(TZtStringBuf name);
/**
- * delete node attribute
- * @param name: attribute name
- */
+ * delete node attribute
+ * @param name: attribute name
+ */
void DelAttr(TZtStringBuf name);
-
- /**
+
+ /**
* set node application data
* @param priv: new application data pointer
*/
@@ -409,27 +409,27 @@ namespace NXml {
void* GetPrivate() const;
/**
- * get node name
- */
+ * get node name
+ */
TString Name() const;
-
- /**
+
+ /**
* get node xpath
*/
TString Path() const;
/**
- * get node xml representation
- */
+ * get node xml representation
+ */
TString ToString(TZtStringBuf enc = "") const {
- TStringStream s;
- Save(s, enc);
+ 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;
-
- /**
+
+ /**
* get pointer to internal node
*/
xmlNode* GetPtr();
@@ -454,9 +454,9 @@ namespace NXml {
{
}
- private:
- friend class TConstNodes;
-
+ private:
+ friend class TConstNodes;
+
TNode(xmlDoc* doc, xmlNode* node)
: NodePointer(node)
, DocPointer(doc)
@@ -660,13 +660,13 @@ namespace NXml {
}
/**
- * get node name
- */
+ * get node name
+ */
TString Name() const {
- return ActualNode.Name();
- }
-
- /**
+ return ActualNode.Name();
+ }
+
+ /**
* @return application data pointer, passed by SetPrivate
*/
void* GetPrivate() const {
@@ -694,14 +694,14 @@ namespace NXml {
return ActualNode.Path();
}
- /**
- * get node xml representation
- */
+ /**
+ * get node xml representation
+ */
TString ToString(TZtStringBuf enc = "") const {
- return ActualNode.ToString(enc);
- }
-
- TConstNode() = default;
+ return ActualNode.ToString(enc);
+ }
+
+ TConstNode() = default;
TConstNode(TNode node)
: ActualNode(node)
{
diff --git a/library/cpp/xml/document/xml-document.cpp b/library/cpp/xml/document/xml-document.cpp
index 18a554d732..5b195f6151 100644
--- a/library/cpp/xml/document/xml-document.cpp
+++ b/library/cpp/xml/document/xml-document.cpp
@@ -29,10 +29,10 @@ namespace NXml {
TDocHolder doc(xmlNewDoc(XMLCHAR("1.0")));
if (!doc)
THROW(XmlException, "Can't create xml document.");
- doc->encoding = xmlStrdup(XMLCHAR("utf-8"));
-
+ doc->encoding = xmlStrdup(XMLCHAR("utf-8"));
+
TNodePtr node(xmlNewNode(nullptr, XMLCHAR(xml.c_str())));
- if (!node)
+ if (!node)
THROW(XmlException, "Can't create root node.");
xmlDocSetRootElement(doc.Get(), node.Get());
Y_UNUSED(node.Release());
@@ -178,7 +178,7 @@ namespace NXml {
if (n.Size() == 0)
return TNode();
else
- return n[0].ConstCast();
+ return n[0].ConstCast();
}
TConstNode TNode::Node(TZtStringBuf xpath, bool quiet, TXPathContext& ctxt) const {
@@ -246,14 +246,14 @@ namespace NXml {
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) {
- xmlNodePtr copy = xmlDocCopyNode(node.ConstCast().NodePointer, DocPointer, 1 /* recursive */);
- copy = xmlAddChild(NodePointer, copy);
- return TNode(DocPointer, copy);
- }
-
+ /* 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) {
+ xmlNodePtr copy = xmlDocCopyNode(node.ConstCast().NodePointer, DocPointer, 1 /* recursive */);
+ copy = xmlAddChild(NodePointer, copy);
+ return TNode(DocPointer, copy);
+ }
+
void TNode::SetPrivate(void* priv) {
NodePointer->_private = priv;
}
@@ -274,8 +274,8 @@ namespace NXml {
if (IsNull())
THROW(XmlException, "Node is null");
- return CAST2CHAR(NodePointer->name);
- }
+ return CAST2CHAR(NodePointer->name);
+ }
TString TNode::Path() const {
TCharPtr path(xmlGetNodePath(NodePointer));
@@ -307,20 +307,20 @@ namespace NXml {
}
static int XmlWriteToOstream(void* context, const char* buffer, int len) {
- // possibly use to save doc as well
+ // possibly use to save doc as well
IOutputStream* out = (IOutputStream*)context;
- out->Write(buffer, len);
- return len;
- }
-
+ out->Write(buffer, len);
+ return len;
+ }
+
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));
- if (xmlSaveTree(ctx.Get(), (xmlNode*)GetPtr()) < 0)
- THROW(XmlException, "Failed saving node to stream");
- }
-
+ if (xmlSaveTree(ctx.Get(), (xmlNode*)GetPtr()) < 0)
+ THROW(XmlException, "Failed saving node to stream");
+ }
+
void TNode::Save(IOutputStream& stream, TZtStringBuf enc, bool shouldFormat) const {
SaveInternal(stream, enc, shouldFormat ? XML_SAVE_FORMAT : 0);
}
diff --git a/library/cpp/xml/document/xml-document_ut.cpp b/library/cpp/xml/document/xml-document_ut.cpp
index 9f537b75c4..f19a7dbaea 100644
--- a/library/cpp/xml/document/xml-document_ut.cpp
+++ b/library/cpp/xml/document/xml-document_ut.cpp
@@ -1,5 +1,5 @@
#include <library/cpp/testing/unittest/registar.h>
-#include <util/generic/map.h>
+#include <util/generic/map.h>
#include "xml-document.h"
@@ -55,34 +55,34 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) {
" <emshort>Emily Short</emshort>\n"
" </authors>\n"
"</frob>\n");
- // check default utf8 output with ru
- {
+ // 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");
- }
+ }
}
Y_UNIT_TEST(XPathNs) {
- using namespace NXml;
- TDocument xml(
+ 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);
-
- TNamespacesForXPath nss;
- TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
- nss.push_back(ns);
-
- TConstNode root = xml.Root();
- TConstNode b = root.Node("h:a/h:b", false, nss);
- UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15);
- UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true);
-
- TConstNode text = root.Node("h:text", false, nss);
+
+ TNamespacesForXPath nss;
+ TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
+ nss.push_back(ns);
+
+ TConstNode root = xml.Root();
+ TConstNode b = root.Node("h:a/h:b", false, nss);
+ UNIT_ASSERT_EQUAL(b.Attr<int>("len"), 15);
+ UNIT_ASSERT_EQUAL(b.Attr<bool>("correct"), true);
+
+ TConstNode text = root.Node("h:text", false, nss);
UNIT_ASSERT_EQUAL(text.Value<TString>(), "Некоторый текст");
// For performance you can create xpath context once using nss and pass it.
@@ -90,7 +90,7 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) {
UNIT_ASSERT(root.Node("text", true, *ctxt).IsNull());
UNIT_ASSERT_EXCEPTION(root.Node("text", false, *ctxt), yexception);
UNIT_ASSERT_EQUAL(root.Node("h:text", false, *ctxt).Value<TString>(), "Некоторый текст");
- }
+ }
Y_UNIT_TEST(XmlNodes) {
using namespace NXml;
TDocument xml("<?xml version=\"1.0\"?>\n"
@@ -176,65 +176,65 @@ Y_UNIT_TEST_SUITE(TestXmlDocument) {
UNIT_ASSERT_STRINGS_EQUAL(xml.Root().Node("/root/a").Value<TString>(), "first");
}
Y_UNIT_TEST(CopyNode) {
- using namespace NXml;
- // default-construct empty node
- TNode empty;
- // put to container
+ using namespace NXml;
+ // default-construct empty node
+ TNode empty;
+ // put to container
TMap<int, TNode> nmap;
- nmap[2];
-
- // do copy
- TDocument xml("<?xml version=\"1.0\"?>\n"
+ nmap[2];
+
+ // do copy
+ TDocument xml("<?xml version=\"1.0\"?>\n"
"<root><a></a></root>",
TDocument::String);
-
- TDocument xml2("<?xml version=\"1.0\"?>\n"
+
+ TDocument xml2("<?xml version=\"1.0\"?>\n"
"<root><node><b>bold</b><i>ita</i></node></root>",
TDocument::String);
-
- TNode node = xml2.Root().Node("//node");
- TNode place = xml.Root().Node("//a");
-
- place.AddChild(node);
-
- TStringStream s;
- xml.Save(s, "", false);
- UNIT_ASSERT_VALUES_EQUAL(s.Str(),
+
+ TNode node = xml2.Root().Node("//node");
+ TNode place = xml.Root().Node("//a");
+
+ place.AddChild(node);
+
+ 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");
- }
-
+ }
+
Y_UNIT_TEST(RenderNode) {
- using namespace NXml;
- {
- // no namespaces
- TDocument xml(
- "<?xml version=\"1.0\"?>\n"
- "<root>\n"
- "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
- "<text>Некоторый текст</text>\n"
+ using namespace NXml;
+ {
+ // no namespaces
+ TDocument xml(
+ "<?xml version=\"1.0\"?>\n"
+ "<root>\n"
+ "<a><b len=\"15\" correct=\"1\">hello world</b></a>\n"
+ "<text>Некоторый текст</text>\n"
"</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>");
- }
- {
- // namespaces
- 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"
+ TNode n = xml.Root().Node("//a");
+ UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>");
+ }
+ {
+ // namespaces
+ 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);
- TNamespacesForXPath nss;
- TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
- nss.push_back(ns);
-
- TNode n = xml.Root().Node("//h:a", false, nss);
- UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>");
- }
- }
+ TNamespacesForXPath nss;
+ TNamespaceForXPath ns = {"h", "http://hello.com/hello"};
+ nss.push_back(ns);
+
+ TNode n = xml.Root().Node("//h:a", false, nss);
+ UNIT_ASSERT_VALUES_EQUAL(n.ToString(), "<a><b len=\"15\" correct=\"1\">hello world</b></a>");
+ }
+ }
Y_UNIT_TEST(ReuseXPathContext) {
using namespace NXml;