diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /tools/source/xml/XmlWalker.cxx | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/source/xml/XmlWalker.cxx')
-rw-r--r-- | tools/source/xml/XmlWalker.cxx | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tools/source/xml/XmlWalker.cxx b/tools/source/xml/XmlWalker.cxx new file mode 100644 index 000000000..b21c22335 --- /dev/null +++ b/tools/source/xml/XmlWalker.cxx @@ -0,0 +1,106 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <tools/stream.hxx> +#include <tools/XmlWalker.hxx> + +#include <libxml/tree.h> +#include <libxml/parser.h> +#include <libxml/xmlstring.h> +#include <vector> + +namespace tools +{ +struct XmlWalkerImpl +{ + XmlWalkerImpl() + : mpDocPtr(nullptr) + , mpRoot(nullptr) + , mpCurrent(nullptr) + { + } + + xmlDocPtr mpDocPtr; + xmlNodePtr mpRoot; + xmlNodePtr mpCurrent; + + std::vector<xmlNodePtr> mpStack; +}; + +XmlWalker::XmlWalker() + : mpImpl(std::make_unique<XmlWalkerImpl>()) +{ +} + +XmlWalker::~XmlWalker() +{ + if (mpImpl) + xmlFreeDoc(mpImpl->mpDocPtr); +} + +bool XmlWalker::open(SvStream* pStream) +{ + std::size_t nSize = pStream->remainingSize(); + std::vector<sal_uInt8> aBuffer(nSize + 1); + pStream->ReadBytes(aBuffer.data(), nSize); + aBuffer[nSize] = 0; + mpImpl->mpDocPtr = xmlParseDoc(reinterpret_cast<xmlChar*>(aBuffer.data())); + if (mpImpl->mpDocPtr == nullptr) + return false; + mpImpl->mpRoot = xmlDocGetRootElement(mpImpl->mpDocPtr); + mpImpl->mpCurrent = mpImpl->mpRoot; + mpImpl->mpStack.push_back(mpImpl->mpCurrent); + return true; +} + +OString XmlWalker::name() { return reinterpret_cast<const char*>(mpImpl->mpCurrent->name); } + +OString XmlWalker::content() +{ + OString aContent; + if (mpImpl->mpCurrent->xmlChildrenNode != nullptr) + { + xmlChar* pContent + = xmlNodeListGetString(mpImpl->mpDocPtr, mpImpl->mpCurrent->xmlChildrenNode, 1); + aContent = OString(reinterpret_cast<const char*>(pContent)); + xmlFree(pContent); + } + return aContent; +} + +void XmlWalker::children() +{ + mpImpl->mpStack.push_back(mpImpl->mpCurrent); + mpImpl->mpCurrent = mpImpl->mpCurrent->xmlChildrenNode; +} + +void XmlWalker::parent() +{ + mpImpl->mpCurrent = mpImpl->mpStack.back(); + mpImpl->mpStack.pop_back(); +} + +OString XmlWalker::attribute(const OString& sName) +{ + xmlChar* xmlName = xmlCharStrdup(sName.getStr()); + xmlChar* xmlAttribute = xmlGetProp(mpImpl->mpCurrent, xmlName); + OString aAttributeContent(reinterpret_cast<const char*>(xmlAttribute)); + xmlFree(xmlAttribute); + xmlFree(xmlName); + + return aAttributeContent; +} + +void XmlWalker::next() { mpImpl->mpCurrent = mpImpl->mpCurrent->next; } + +bool XmlWalker::isValid() const { return mpImpl->mpCurrent != nullptr; } + +} // end tools namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |