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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312 | /* -*- 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 <dataprovider.hxx>
#include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
#include <com/sun/star/ucb/SimpleFileAccess.hpp>
#include <com/sun/star/io/XInputStream.hpp>
#include <rtl/strbuf.hxx>
#include <sal/log.hxx>
#include <unotools/charclass.hxx>
#include <tools/stream.hxx>
#include <comphelper/processfactory.hxx>
#include "htmldataprovider.hxx"
#include "xmldataprovider.hxx"
#include "sqldataprovider.hxx"
#include <datamapper.hxx>
#include <dbdata.hxx>
#include <docsh.hxx>
using namespace com::sun::star;
namespace sc {
std::unique_ptr<SvStream> DataProvider::FetchStreamFromURL(const OUString& rURL, OStringBuffer& rBuffer)
{
try
{
uno::Reference< ucb::XSimpleFileAccess3 > xFileAccess = ucb::SimpleFileAccess::create( comphelper::getProcessComponentContext() );
uno::Reference< io::XInputStream > xStream = xFileAccess->openFileRead( rURL );
const sal_Int32 BUF_LEN = 8000;
uno::Sequence< sal_Int8 > buffer( BUF_LEN );
sal_Int32 nRead = 0;
while ( ( nRead = xStream->readBytes( buffer, BUF_LEN ) ) == BUF_LEN )
{
rBuffer.append( reinterpret_cast< const char* >( buffer.getConstArray() ), nRead );
}
if ( nRead > 0 )
{
rBuffer.append( reinterpret_cast< const char* >( buffer.getConstArray() ), nRead );
}
xStream->closeInput();
SvStream* pStream = new SvMemoryStream(const_cast<char*>(rBuffer.getStr()), rBuffer.getLength(), StreamMode::READ);
return std::unique_ptr<SvStream>(pStream);
}
catch(...)
{
rBuffer.setLength(0);
return nullptr;
}
}
ExternalDataSource::ExternalDataSource(const OUString& rURL,
const OUString& rProvider, ScDocument* pDoc)
: maURL(rURL)
, maProvider(rProvider)
, mpDoc(pDoc)
{
}
void ExternalDataSource::setID(const OUString& rID)
{
maID = rID;
}
void ExternalDataSource::setXMLImportParam(const ScOrcusImportXMLParam& rParam)
{
maParam = rParam;
}
void ExternalDataSource::setURL(const OUString& rURL)
{
maURL = rURL;
}
void ExternalDataSource::setProvider(const OUString& rProvider)
{
maProvider = rProvider;
mpDataProvider.reset();
}
const OUString& ExternalDataSource::getURL() const
{
return maURL;
}
const OUString& ExternalDataSource::getProvider() const
{
return maProvider;
}
const OUString& ExternalDataSource::getID() const
{
return maID;
}
const ScOrcusImportXMLParam& ExternalDataSource::getXMLImportParam() const
{
return maParam;
}
OUString ExternalDataSource::getDBName() const
{
if (mpDBDataManager)
{
ScDBData* pDBData = mpDBDataManager->getDBData();
if (pDBData)
return pDBData->GetName();
}
return OUString();
}
void ExternalDataSource::setDBData(const OUString& rDBName)
{
if (!mpDBDataManager)
{
mpDBDataManager = std::make_shared<ScDBDataManager>(rDBName, mpDoc);
}
else
{
mpDBDataManager->SetDatabase(rDBName);
}
}
double ExternalDataSource::getUpdateFrequency()
{
return 0;
}
ScDBDataManager* ExternalDataSource::getDBManager()
{
return mpDBDataManager.get();
}
void ExternalDataSource::refresh(ScDocument* pDoc, bool bDeterministic)
{
// no DB data available
if (!mpDBDataManager)
return;
// if no data provider exists, try to create one
if (!mpDataProvider)<--- First condition
mpDataProvider = DataProviderFactory::getDataProvider(pDoc, *this);
// if we still have not been able to create one, we can not refresh the data
if (!mpDataProvider)<--- Second condition
return;
if (bDeterministic)
mpDataProvider->setDeterministic();
mpDataProvider->Import();
}
void ExternalDataSource::AddDataTransformation(
const std::shared_ptr<sc::DataTransformation>& mpDataTransformation)
{
maDataTransformations.push_back(mpDataTransformation);
}
const std::vector<std::shared_ptr<sc::DataTransformation>>& ExternalDataSource::getDataTransformation() const
{
return maDataTransformations;
}
ExternalDataMapper::ExternalDataMapper(ScDocument* /*pDoc*/)
//mpDoc(pDoc)
{
}
ExternalDataMapper::~ExternalDataMapper()
{
}
void ExternalDataMapper::insertDataSource(const sc::ExternalDataSource& rSource)
{
maDataSources.push_back(rSource);
}
const std::vector<sc::ExternalDataSource>& ExternalDataMapper::getDataSources() const
{
return maDataSources;
}
std::vector<sc::ExternalDataSource>& ExternalDataMapper::getDataSources()
{
return maDataSources;
}
DataProvider::DataProvider(sc::ExternalDataSource& rDataSource):
mbDeterministic(false),
mrDataSource(rDataSource)
{
}
void DataProvider::setDeterministic()
{
mbDeterministic = true;
}
DataProvider::~DataProvider()
{
}
void ScDBDataManager::WriteToDoc(ScDocument& rDoc)
{
// first apply all data transformations
bool bShrunk = false;
SCCOL nStartCol = 0;
SCROW nStartRow = 0;
SCCOL nEndCol = rDoc.MaxCol();
SCROW nEndRow = rDoc.MaxRow();
rDoc.ShrinkToUsedDataArea(bShrunk, 0, nStartCol, nStartRow, nEndCol, nEndRow, false, true, true);
ScRange aClipRange(nStartCol, nStartRow, 0, nEndCol, nEndRow, 0);
rDoc.SetClipArea(aClipRange);
ScRange aDestRange;
getDBData()->GetArea(aDestRange);
SCCOL nColSize = std::min<SCCOL>(aDestRange.aEnd.Col() - aDestRange.aStart.Col(), nEndCol);
aDestRange.aEnd.SetCol(aDestRange.aStart.Col() + nColSize);
SCROW nRowSize = std::min<SCROW>(aDestRange.aEnd.Row() - aDestRange.aStart.Row(), nEndRow);
aDestRange.aEnd.SetRow(aDestRange.aStart.Row() + nRowSize);
ScMarkData aMark(mpDoc->GetSheetLimits());
aMark.SelectTable(0, true);
mpDoc->CopyFromClip(aDestRange, aMark, InsertDeleteFlags::CONTENTS, nullptr, &rDoc);
ScDocShell* pDocShell = static_cast<ScDocShell*>(mpDoc->GetDocumentShell());
if (pDocShell)
pDocShell->PostPaint(aDestRange, PaintPartFlags::All);
}
ScDBDataManager::ScDBDataManager(const OUString& rDBName, ScDocument* pDoc):
maDBName(rDBName),
mpDoc(pDoc)
{
}
ScDBDataManager::~ScDBDataManager()
{
}
void ScDBDataManager::SetDatabase(const OUString& rDBName)
{
maDBName = rDBName;
}
ScDBData* ScDBDataManager::getDBData()
{
ScDBData* pDBData = mpDoc->GetDBCollection()->getNamedDBs().findByUpperName(ScGlobal::getCharClassPtr()->uppercase(maDBName));
return pDBData;
}
bool DataProviderFactory::isInternalDataProvider(const OUString& rProvider)
{
return rProvider.startsWith("org.libreoffice.calc");
}
std::shared_ptr<DataProvider> DataProviderFactory::getDataProvider(ScDocument* pDoc,
sc::ExternalDataSource& rDataSource)
{
const OUString& rDataProvider = rDataSource.getProvider();
bool bInternal = DataProviderFactory::isInternalDataProvider(rDataProvider);
if (bInternal)
{
if (rDataProvider == "org.libreoffice.calc.csv")
return std::make_shared<CSVDataProvider>(pDoc, rDataSource);
else if (rDataProvider == "org.libreoffice.calc.html")
return std::make_shared<HTMLDataProvider>(pDoc, rDataSource);
else if (rDataProvider == "org.libreoffice.calc.xml")
return std::make_shared<XMLDataProvider>(pDoc, rDataSource);
else if (rDataProvider == "org.libreoffice.calc.sql")
return std::make_shared<SQLDataProvider>(pDoc, rDataSource);
}
else
{
SAL_WARN("sc", "no external data provider supported yet");
return std::shared_ptr<DataProvider>();
}
return std::shared_ptr<DataProvider>();
}
std::vector<OUString> DataProviderFactory::getDataProviders()
{
std::vector<OUString> aDataProviders;
aDataProviders.emplace_back("org.libreoffice.calc.csv");
aDataProviders.emplace_back("org.libreoffice.calc.html");
aDataProviders.emplace_back("org.libreoffice.calc.xml");
aDataProviders.emplace_back("org.libreoffice.calc.sql");
return aDataProviders;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|