Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/dataprovider/xmldataprovider.cxx
Line
Count
Source
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 "xmldataprovider.hxx"
11
#include <datatransformation.hxx>
12
#include <salhelper/thread.hxx>
13
#include <filter.hxx>
14
#include <document.hxx>
15
#include <datamapper.hxx>
16
#include <utility>
17
#include <vcl/svapp.hxx>
18
#include <orcusfilters.hxx>
19
20
namespace sc
21
{
22
class XMLFetchThread : public salhelper::Thread
23
{
24
    ScDocument& mrDocument;
25
    OUString maURL;
26
    OUString maID;
27
    ScOrcusImportXMLParam maParam;
28
    std::unique_ptr<ScOrcusXMLContext> mpXMLContext;
29
    const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
30
    std::function<void()> maImportFinishedHdl;
31
32
public:
33
    XMLFetchThread(ScDocument& rDoc, const OUString&, const ScOrcusImportXMLParam& rParam,
34
                   const OUString& rID, std::function<void()> aImportFinishedHdl,
35
                   std::vector<std::shared_ptr<sc::DataTransformation>>&& rTransformations);
36
    virtual void execute() override;
37
};
38
39
XMLFetchThread::XMLFetchThread(
40
    ScDocument& rDoc, const OUString& rURL, const ScOrcusImportXMLParam& rParam,
41
    const OUString& rID, std::function<void()> aImportFinishedHdl,
42
    std::vector<std::shared_ptr<sc::DataTransformation>>&& rTransformations)
43
0
    : salhelper::Thread("XML Fetch Thread")
44
0
    , mrDocument(rDoc)
45
0
    , maURL(rURL)
46
0
    , maID(rID)
47
0
    , maParam(rParam)
48
0
    , maDataTransformations(std::move(rTransformations))
49
0
    , maImportFinishedHdl(std::move(aImportFinishedHdl))
50
0
{
51
0
}
52
53
void XMLFetchThread::execute()
54
0
{
55
0
    ScOrcusFilters* pOrcus = ScFormatFilter::Get().GetOrcusFilters();
56
0
    if (!pOrcus)
57
0
        return;
58
59
0
    mpXMLContext = pOrcus->createXMLContext(mrDocument, maURL);
60
0
    if (!mpXMLContext)
61
0
        return;
62
63
0
    if (!maID.isEmpty())
64
0
    {
65
0
        ScOrcusImportXMLParam::RangeLink aRangeLink;
66
0
        aRangeLink.maPos = ScAddress(0, 0, 0);
67
0
        aRangeLink.maFieldPaths.push_back(OUStringToOString(maID, RTL_TEXTENCODING_UTF8));
68
0
        maParam.maRangeLinks.clear();
69
0
        maParam.maRangeLinks.push_back(std::move(aRangeLink));
70
0
    }
71
    // Do the import.
72
0
    SolarMutexGuard aGuard;
73
0
    mpXMLContext->importXML(maParam);
74
75
0
    for (auto& itr : maDataTransformations)
76
0
    {
77
0
        itr->Transform(mrDocument);
78
0
    }
79
80
0
    maImportFinishedHdl();
81
0
}
82
83
XMLDataProvider::XMLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource)
84
0
    : DataProvider(rDataSource)
85
0
    , mpDocument(pDoc)
86
0
{
87
0
}
88
89
XMLDataProvider::~XMLDataProvider()
90
0
{
91
0
    if (mxXMLFetchThread.is())
92
0
    {
93
0
        SolarMutexReleaser aReleaser;
94
0
        mxXMLFetchThread->join();
95
0
    }
96
0
}
97
98
void XMLDataProvider::Import()
99
0
{
100
    // already importing data
101
0
    if (mpDoc)
102
0
        return;
103
104
0
    mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
105
0
    mpDoc->ResetClip(mpDocument, SCTAB(0));
106
0
    mxXMLFetchThread = new XMLFetchThread(
107
0
        *mpDoc, mrDataSource.getURL(), mrDataSource.getXMLImportParam(), mrDataSource.getID(),
108
0
        [this]() { this->ImportFinished(); }, std::vector(mrDataSource.getDataTransformation()));
109
110
0
    if (mbDeterministic)
111
0
    {
112
0
        SolarMutexReleaser aReleaser;
113
0
        mxXMLFetchThread->execute();
114
0
    }
115
0
    else
116
0
    {
117
0
        mxXMLFetchThread->launch();
118
0
    }
119
0
}
120
121
0
void XMLDataProvider::ImportFinished() { mrDataSource.getDBManager()->WriteToDoc(*mpDoc); }
122
123
0
const OUString& XMLDataProvider::GetURL() const { return mrDataSource.getURL(); }
124
}
125
126
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */