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