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
|
/* -*- 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 "htmldataprovider.hxx"
#include <datamapper.hxx>
#include <datatransformation.hxx>
#include <salhelper/thread.hxx>
#include <vcl/svapp.hxx>
#include <tools/stream.hxx>
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
#include <comphelper/string.hxx>
#include <utility>
namespace sc {
class HTMLFetchThread : public salhelper::Thread
{
ScDocument& mrDocument;
OUString const maURL;
OUString const maID;
const std::vector<std::shared_ptr<sc::DataTransformation>> maDataTransformations;
std::function<void()> const maImportFinishedHdl;
void handleTable(xmlNodePtr pTable);
void handleRow(xmlNodePtr pRow, SCROW nRow);
void skipHeadBody(xmlNodePtr pSkip, SCROW& rRow);
void handleCell(xmlNodePtr pCell, SCROW nRow, SCCOL nCol);
public:
HTMLFetchThread(ScDocument& rDoc, const OUString&, const OUString& rID, std::function<void()> aImportFinishedHdl,
const std::vector<std::shared_ptr<sc::DataTransformation>>& rTransformations);
virtual void execute() override;
};
HTMLFetchThread::HTMLFetchThread(
ScDocument& rDoc, const OUString& rURL, const OUString& rID,
std::function<void()> aImportFinishedHdl,
const std::vector<std::shared_ptr<sc::DataTransformation>>& rTransformations)
: salhelper::Thread("HTML Fetch Thread")
, mrDocument(rDoc)
, maURL(rURL)
, maID(rID)
, maDataTransformations(rTransformations)
, maImportFinishedHdl(std::move(aImportFinishedHdl))
{
}
namespace {
OString toString(const xmlChar* pStr)
{
return OString(reinterpret_cast<const char*>(pStr), xmlStrlen(pStr));
}
OUString trim_string(const OUString& aStr)
{
OUString aOldString;
OUString aString = aStr;
do
{
aOldString = aString;
aString = comphelper::string::strip(aString, ' ');
aString = comphelper::string::strip(aString, '\n');
aString = comphelper::string::strip(aString, '\r');
aString = comphelper::string::strip(aString, '\t');
}
while (aOldString != aString);
return aString;
}
OUString get_node_str(xmlNodePtr pNode)
{
OUStringBuffer aStr;
for (xmlNodePtr cur_node = pNode->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_TEXT_NODE)
{
OUString aString = OStringToOUString(toString(cur_node->content), RTL_TEXTENCODING_UTF8);
aStr.append(trim_string(aString));
}
else if (cur_node->type == XML_ELEMENT_NODE)
{
aStr.append(get_node_str(cur_node));
}
}
return aStr.makeStringAndClear();
}
}
void HTMLFetchThread::handleCell(xmlNodePtr pCellNode, SCROW nRow, SCCOL nCol)
{
OUStringBuffer aStr;
for (xmlNodePtr cur_node = pCellNode->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_TEXT_NODE)
{
OUString aString = OStringToOUString(toString(cur_node->content), RTL_TEXTENCODING_UTF8);
aStr.append(trim_string(aString));
}
else if (cur_node->type == XML_ELEMENT_NODE)
{
aStr.append(get_node_str(cur_node));
}
}
if (!aStr.isEmpty())
{
OUString aCellStr = aStr.makeStringAndClear();
mrDocument.SetString(nCol, nRow, 0, aCellStr);
}
}
void HTMLFetchThread::handleRow(xmlNodePtr pRowNode, SCROW nRow)
{
sal_Int32 nCol = 0;
for (xmlNodePtr cur_node = pRowNode->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
OString aNodeName = toString(cur_node->name);
if (aNodeName == "td" || aNodeName == "th")
{
handleCell(cur_node, nRow, nCol);
++nCol;
}
}
}
}
void HTMLFetchThread::skipHeadBody(xmlNodePtr pSkipElement, SCROW& rRow)
{
for (xmlNodePtr cur_node = pSkipElement->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
OString aNodeName = toString(cur_node->name);
if (aNodeName == "tr")
{
handleRow(cur_node, rRow);
++rRow;
}
}
}
}
void HTMLFetchThread::handleTable(xmlNodePtr pTable)
{
sal_Int32 nRow = 0;
for (xmlNodePtr cur_node = pTable->children; cur_node; cur_node = cur_node->next)
{
if (cur_node->type == XML_ELEMENT_NODE)
{
OString aNodeName = toString(cur_node->name);
if (aNodeName == "tr")
{
handleRow(cur_node, nRow);
++nRow;
}
else if (aNodeName == "thead" || aNodeName == "tbody")
{
skipHeadBody(cur_node, nRow);
}
}
}
}
void HTMLFetchThread::execute()
{
OStringBuffer aBuffer(64000);
std::unique_ptr<SvStream> pStream = DataProvider::FetchStreamFromURL(maURL, aBuffer);
if (aBuffer.isEmpty())
return;
htmlDocPtr pHtmlPtr = htmlParseDoc(reinterpret_cast<xmlChar*>(const_cast<char*>(aBuffer.getStr())), nullptr);
OString aID = OUStringToOString(maID, RTL_TEXTENCODING_UTF8);
xmlXPathContextPtr pXmlXpathCtx = xmlXPathNewContext(pHtmlPtr);
xmlXPathObjectPtr pXmlXpathObj = xmlXPathEvalExpression(BAD_CAST(aID.getStr()), pXmlXpathCtx);
xmlNodeSetPtr pXmlNodes = pXmlXpathObj->nodesetval;
if (pXmlNodes->nodeNr == 0)
return;
xmlNodePtr pNode = pXmlNodes->nodeTab[0];
handleTable(pNode);
xmlXPathFreeNodeSet(pXmlNodes);
xmlXPathFreeNodeSetList(pXmlXpathObj);
xmlXPathFreeContext(pXmlXpathCtx);
for (auto& itr : maDataTransformations)
{
itr->Transform(mrDocument);
}
SolarMutexGuard aGuard;
maImportFinishedHdl();
}
HTMLDataProvider::HTMLDataProvider(ScDocument* pDoc, sc::ExternalDataSource& rDataSource):
DataProvider(rDataSource),
mpDocument(pDoc)
{
}
HTMLDataProvider::~HTMLDataProvider()
{
if (mxHTMLFetchThread.is())
{
SolarMutexReleaser aReleaser;
mxHTMLFetchThread->join();
}
}
void HTMLDataProvider::Import()
{
// already importing data
if (mpDoc)
return;
mpDoc.reset(new ScDocument(SCDOCMODE_CLIP));
mpDoc->ResetClip(mpDocument, SCTAB(0));
mxHTMLFetchThread = new HTMLFetchThread(*mpDoc, mrDataSource.getURL(), mrDataSource.getID(),
std::bind(&HTMLDataProvider::ImportFinished, this), mrDataSource.getDataTransformation());
mxHTMLFetchThread->launch();
if (mbDeterministic)
{
SolarMutexReleaser aReleaser;
mxHTMLFetchThread->join();
}
}
void HTMLDataProvider::ImportFinished()
{
mrDataSource.getDBManager()->WriteToDoc(*mpDoc);
mxHTMLFetchThread.clear();
mpDoc.reset();
}
const OUString& HTMLDataProvider::GetURL() const
{
return mrDataSource.getURL();
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|