Coverage Report

Created: 2025-07-07 10:01

/src/libreoffice/tools/source/xml/XmlWalker.cxx
Line
Count
Source (jump to first uncovered line)
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
18
namespace tools
19
{
20
struct XmlWalkerImpl
21
{
22
    XmlWalkerImpl()
23
0
        : mpDocPtr(nullptr)
24
0
        , mpRoot(nullptr)
25
0
        , mpCurrent(nullptr)
26
0
    {
27
0
    }
28
29
    xmlDocPtr mpDocPtr;
30
    xmlNodePtr mpRoot;
31
    xmlNodePtr mpCurrent;
32
33
    std::vector<xmlNodePtr> mpStack;
34
};
35
36
XmlWalker::XmlWalker()
37
0
    : mpImpl(std::make_unique<XmlWalkerImpl>())
38
0
{
39
0
}
40
41
XmlWalker::~XmlWalker()
42
0
{
43
0
    if (mpImpl)
44
0
        xmlFreeDoc(mpImpl->mpDocPtr);
45
0
}
46
47
bool XmlWalker::open(SvStream* pStream)
48
0
{
49
0
    std::size_t nSize = pStream->remainingSize();
50
0
    std::vector<sal_uInt8> aBuffer(nSize + 1);
51
0
    pStream->ReadBytes(aBuffer.data(), nSize);
52
0
    aBuffer[nSize] = 0;
53
0
    mpImpl->mpDocPtr = xmlParseDoc(reinterpret_cast<xmlChar*>(aBuffer.data()));
54
0
    if (mpImpl->mpDocPtr == nullptr)
55
0
        return false;
56
0
    mpImpl->mpRoot = xmlDocGetRootElement(mpImpl->mpDocPtr);
57
0
    mpImpl->mpCurrent = mpImpl->mpRoot;
58
0
    mpImpl->mpStack.push_back(mpImpl->mpCurrent);
59
0
    return true;
60
0
}
61
62
std::string_view XmlWalker::name()
63
0
{
64
0
    return reinterpret_cast<const char*>(mpImpl->mpCurrent->name);
65
0
}
66
67
std::string_view XmlWalker::namespaceHref()
68
0
{
69
0
    return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->href);
70
0
}
71
72
std::string_view XmlWalker::namespacePrefix()
73
0
{
74
0
    return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->prefix);
75
0
}
76
77
OString XmlWalker::content()
78
0
{
79
0
    OString aContent;
80
0
    if (mpImpl->mpCurrent->xmlChildrenNode != nullptr)
81
0
    {
82
0
        xmlChar* pContent
83
0
            = xmlNodeListGetString(mpImpl->mpDocPtr, mpImpl->mpCurrent->xmlChildrenNode, 1);
84
0
        aContent = OString(reinterpret_cast<const char*>(pContent));
85
0
        xmlFree(pContent);
86
0
    }
87
0
    return aContent;
88
0
}
89
90
void XmlWalker::children()
91
0
{
92
0
    mpImpl->mpStack.push_back(mpImpl->mpCurrent);
93
0
    mpImpl->mpCurrent = mpImpl->mpCurrent->xmlChildrenNode;
94
0
}
95
96
void XmlWalker::parent()
97
0
{
98
0
    mpImpl->mpCurrent = mpImpl->mpStack.back();
99
0
    mpImpl->mpStack.pop_back();
100
0
}
101
102
OString XmlWalker::attribute(const OString& sName) const
103
0
{
104
0
    xmlChar* xmlAttribute
105
0
        = xmlGetProp(mpImpl->mpCurrent, reinterpret_cast<const xmlChar*>(sName.getStr()));
106
0
    OString aAttributeContent(
107
0
        xmlAttribute == nullptr ? "" : reinterpret_cast<const char*>(xmlAttribute));
108
0
    xmlFree(xmlAttribute);
109
110
0
    return aAttributeContent;
111
0
}
112
113
0
void XmlWalker::next() { mpImpl->mpCurrent = mpImpl->mpCurrent->next; }
114
115
0
bool XmlWalker::isValid() const { return mpImpl->mpCurrent != nullptr; }
116
117
} // end tools namespace
118
119
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */