LibreOffice Module tools (master) 1
XmlWalker.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <tools/stream.hxx>
11#include <tools/XmlWalker.hxx>
12
13#include <libxml/tree.h>
14#include <libxml/parser.h>
15#include <libxml/xmlstring.h>
16#include <vector>
17
18namespace tools
19{
21{
23 : mpDocPtr(nullptr)
24 , mpRoot(nullptr)
25 , mpCurrent(nullptr)
26 {
27 }
28
29 xmlDocPtr mpDocPtr;
30 xmlNodePtr mpRoot;
31 xmlNodePtr mpCurrent;
32
33 std::vector<xmlNodePtr> mpStack;
34};
35
37 : mpImpl(std::make_unique<XmlWalkerImpl>())
38{
39}
40
42{
43 if (mpImpl)
44 xmlFreeDoc(mpImpl->mpDocPtr);
45}
46
48{
49 std::size_t nSize = pStream->remainingSize();
50 std::vector<sal_uInt8> aBuffer(nSize + 1);
51 pStream->ReadBytes(aBuffer.data(), nSize);
52 aBuffer[nSize] = 0;
53 mpImpl->mpDocPtr = xmlParseDoc(reinterpret_cast<xmlChar*>(aBuffer.data()));
54 if (mpImpl->mpDocPtr == nullptr)
55 return false;
56 mpImpl->mpRoot = xmlDocGetRootElement(mpImpl->mpDocPtr);
57 mpImpl->mpCurrent = mpImpl->mpRoot;
58 mpImpl->mpStack.push_back(mpImpl->mpCurrent);
59 return true;
60}
61
62OString XmlWalker::name() { return reinterpret_cast<const char*>(mpImpl->mpCurrent->name); }
63
65{
66 return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->href);
67}
68
70{
71 return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->prefix);
72}
73
75{
76 OString aContent;
77 if (mpImpl->mpCurrent->xmlChildrenNode != nullptr)
78 {
79 xmlChar* pContent
80 = xmlNodeListGetString(mpImpl->mpDocPtr, mpImpl->mpCurrent->xmlChildrenNode, 1);
81 aContent = OString(reinterpret_cast<const char*>(pContent));
82 xmlFree(pContent);
83 }
84 return aContent;
85}
86
88{
89 mpImpl->mpStack.push_back(mpImpl->mpCurrent);
90 mpImpl->mpCurrent = mpImpl->mpCurrent->xmlChildrenNode;
91}
92
94{
95 mpImpl->mpCurrent = mpImpl->mpStack.back();
96 mpImpl->mpStack.pop_back();
97}
98
99OString XmlWalker::attribute(const OString& sName) const
100{
101 xmlChar* xmlAttribute
102 = xmlGetProp(mpImpl->mpCurrent, reinterpret_cast<const xmlChar*>(sName.getStr()));
103 OString aAttributeContent(
104 xmlAttribute == nullptr ? "" : reinterpret_cast<const char*>(xmlAttribute));
105 xmlFree(xmlAttribute);
106
107 return aAttributeContent;
108}
109
110void XmlWalker::next() { mpImpl->mpCurrent = mpImpl->mpCurrent->next; }
111
112bool XmlWalker::isValid() const { return mpImpl->mpCurrent != nullptr; }
113
114} // end tools namespace
115
116/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::size_t ReadBytes(void *pData, std::size_t nSize)
Definition: stream.cxx:1114
sal_uInt64 remainingSize()
Definition: stream.cxx:1320
std::unique_ptr< XmlWalkerImpl > mpImpl
Definition: XmlWalker.hxx:34
OString content()
Definition: XmlWalker.cxx:74
OString attribute(const OString &sName) const
Definition: XmlWalker.cxx:99
OString namespacePrefix()
Definition: XmlWalker.cxx:69
OString name()
Definition: XmlWalker.cxx:62
OString namespaceHref()
Definition: XmlWalker.cxx:64
bool isValid() const
Definition: XmlWalker.cxx:112
bool open(SvStream *pStream)
Definition: XmlWalker.cxx:47
OUString sName
Note: this class is a true marvel of engineering: because the author could not decide whether it's be...
xmlNodePtr mpCurrent
Definition: XmlWalker.cxx:31
std::vector< xmlNodePtr > mpStack
Definition: XmlWalker.cxx:33
std::unique_ptr< char[]> aBuffer