summaryrefslogtreecommitdiffstats
path: root/tools/source/xml/XmlWalker.cxx
blob: c0e8a2abef48b0c50351be3c7a2b70b00f5e0f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* -*- 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::namespaceHref()
{
    return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->href);
}

OString XmlWalker::namespacePrefix()
{
    return reinterpret_cast<const char*>(mpImpl->mpCurrent->ns->prefix);
}

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) const
{
    xmlChar* xmlAttribute
        = xmlGetProp(mpImpl->mpCurrent, reinterpret_cast<const xmlChar*>(sName.getStr()));
    OString aAttributeContent(
        xmlAttribute == nullptr ? "" : reinterpret_cast<const char*>(xmlAttribute));
    xmlFree(xmlAttribute);

    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: */