LibreOffice Module sc (master)  1
xmldataprovider.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 "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 {
25 {
27  OUString maURL;
28  OUString maID;
30  std::unique_ptr<ScOrcusXMLContext> mpXMLContext;
31  const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
32  std::function<void()> 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 
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 
56 {
58  if (!pOrcus)
59  return;
60 
61  mpXMLContext = pOrcus->createXMLContext(mrDocument, maURL);
62  if (!mpXMLContext)
63  return;
64 
65  if (!maID.isEmpty())
66  {
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;
83 }
84 
86  : DataProvider(rDataSource)
87  , mpDocument(pDoc)
88 {
89 }
90 
92 {
93  if (mxXMLFetchThread.is())
94  {
95  SolarMutexReleaser aReleaser;
96  mxXMLFetchThread->join();
97  }
98 }
99 
101 {
102  // already importing data
103  if (mpDoc)
104  return;
105 
106  mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
107  mpDoc->ResetClip(mpDocument, SCTAB(0));
111  mxXMLFetchThread->launch();
112 
113  if (mbDeterministic)
114  {
115  SolarMutexReleaser aReleaser;
116  mxXMLFetchThread->join();
117  }
118 }
119 
121 {
123  mxXMLFetchThread.clear();
124  mpDoc.reset();
125 }
126 
127 const OUString& XMLDataProvider::GetURL() const { return mrDataSource.getURL(); }
128 }
129 
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const std::vector< std::shared_ptr< sc::DataTransformation > > maDataTransformations
std::unique_ptr< ScOrcusXMLContext > mpXMLContext
Collection of orcus filter wrappers.
rtl::Reference< XMLFetchThread > mxXMLFetchThread
sc::ExternalDataSource & mrDataSource
virtual void execute() override
virtual void Import() override
Abstract class for all data provider.
static SC_DLLPUBLIC ScFormatFilterPlugin & Get()
Definition: impex.cxx:2347
bool mbDeterministic
If true make the threaded import deterministic for the tests.
virtual ScOrcusFilters * GetOrcusFilters()=0
std::vector< RangeLink > maRangeLinks
Definition: orcusxml.hxx:77
void WriteToDoc(ScDocument &rDoc)
virtual std::unique_ptr< ScOrcusXMLContext > createXMLContext(ScDocument &rDoc, const OUString &rPath) const =0
Create a context for XML file.
ScDocumentUniquePtr mpDoc
const OUString & getURL() const
virtual const OUString & GetURL() const override
ScOrcusImportXMLParam maParam
XMLDataProvider(ScDocument *pDoc, sc::ExternalDataSource &rDataSource)
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
ScDBDataManager * getDBManager()
std::function< void()> maImportFinishedHdl
const ScOrcusImportXMLParam & getXMLImportParam() const
ScDocument & mrDocument
virtual ~XMLDataProvider() override
const std::vector< std::shared_ptr< sc::DataTransformation > > & getDataTransformation() const
const OUString & getID() const
sal_Int16 SCTAB
Definition: types.hxx:23