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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "xmlexternaltabi.hxx"
#include "xmlimprt.hxx"
#include "xmltabi.hxx"
#include "xmlstyli.hxx"
#include <document.hxx>
#include <documentimport.hxx>
#include <svl/sharedstringpool.hxx>
#include <xmloff/xmlnmspe.hxx>
#include <xmloff/xmltoken.hxx>
#include <xmloff/xmluconv.hxx>
#include <sax/tools/converter.hxx>
#include <com/sun/star/util/NumberFormat.hpp>
using namespace ::com::sun::star;
using namespace ::com::sun::star::xml::sax;
using ::com::sun::star::uno::Reference;
ScXMLExternalRefTabSourceContext::ScXMLExternalRefTabSourceContext(
ScXMLImport& rImport,
const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList, ScXMLExternalTabData& rRefInfo ) :
ScXMLImportContext( rImport ),
mrExternalRefInfo(rRefInfo)
{
using namespace ::xmloff::token;
if ( !rAttrList.is() )
return;
for (auto &it : *rAttrList)
{
sal_Int32 nAttrToken = it.getToken();
if ( nAttrToken == XML_ELEMENT( XLINK, XML_HREF ) )
maRelativeUrl = it.toString();
else if ( nAttrToken == XML_ELEMENT( TABLE, XML_TABLE_NAME ) )
// todo
;
else if ( nAttrToken == XML_ELEMENT( TABLE, XML_FILTER_NAME ) )
maFilterName = it.toString();
else if ( nAttrToken == XML_ELEMENT( TABLE, XML_FILTER_OPTIONS ) )
maFilterOptions = it.toString();
}
}
ScXMLExternalRefTabSourceContext::~ScXMLExternalRefTabSourceContext()
{
}
/**
* Make sure the URL is a valid relative URL, mainly to avoid storing
* absolute URL as relative URL by accident. For now, we only check the first
* three characters which are assumed to be always '../', because the relative
* URL for an external document is always in reference to the content.xml
* fragment of the original document.
*/
static bool lcl_isValidRelativeURL(const OUString& rUrl)
{
sal_Int32 n = ::std::min( rUrl.getLength(), static_cast<sal_Int32>(3));
if (n < 3)
return false;
const sal_Unicode* p = rUrl.getStr();
for (sal_Int32 i = 0; i < n; ++i)
{
sal_Unicode c = p[i];
if (i < 2 && c != '.')
// the path must begin with '..'
return false;
else if (i == 2 && c != '/')
// a '/' path separator must follow
return false;
}
return true;
}
void SAL_CALL ScXMLExternalRefTabSourceContext::endFastElement( sal_Int32 /*nElement*/ )
{
ScDocument* pDoc = GetScImport().GetDocument();
if (!pDoc)
return;
ScExternalRefManager* pRefMgr = pDoc->GetExternalRefManager();
if (lcl_isValidRelativeURL(maRelativeUrl))
pRefMgr->setRelativeFileName(mrExternalRefInfo.mnFileId, maRelativeUrl);
pRefMgr->setFilterData(mrExternalRefInfo.mnFileId, maFilterName, maFilterOptions);
}
ScXMLExternalRefRowsContext::ScXMLExternalRefRowsContext(
ScXMLImport& rImport, ScXMLExternalTabData& rRefInfo ) :
ScXMLImportContext( rImport ),
mrExternalRefInfo(rRefInfo)
{
}
ScXMLExternalRefRowsContext::~ScXMLExternalRefRowsContext()
{
}
Reference< XFastContextHandler > SAL_CALL ScXMLExternalRefRowsContext::createFastChildContext(
sal_Int32 nElement, const Reference< XFastAttributeList >& xAttrList )
{
// #i101319# row elements inside group, rows or header-rows
// are treated like row elements directly in the table element
const SvXMLTokenMap& rTokenMap = GetScImport().GetTableRowsElemTokenMap();
sal_uInt16 nToken = rTokenMap.Get( nElement );
sax_fastparser::FastAttributeList *pAttribList =
&sax_fastparser::castToFastAttributeList( xAttrList );
switch (nToken)
{
case XML_TOK_TABLE_ROWS_ROW_GROUP:
case XML_TOK_TABLE_ROWS_HEADER_ROWS:
case XML_TOK_TABLE_ROWS_ROWS:
return new ScXMLExternalRefRowsContext(
GetScImport(), mrExternalRefInfo);
case XML_TOK_TABLE_ROWS_ROW:
return new ScXMLExternalRefRowContext(
GetScImport(), pAttribList, mrExternalRefInfo);
default:
;
}
return nullptr;
}
ScXMLExternalRefRowContext::ScXMLExternalRefRowContext(
ScXMLImport& rImport,
const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList, ScXMLExternalTabData& rRefInfo ) :
ScXMLImportContext( rImport ),
mrScImport(rImport),
mrExternalRefInfo(rRefInfo),
mnRepeatRowCount(1)
{
mrExternalRefInfo.mnCol = 0;
const SvXMLTokenMap& rAttrTokenMap = mrScImport.GetTableRowAttrTokenMap();
if ( !rAttrList.is() )
return;
for (auto &it : *rAttrList)
{
switch ( rAttrTokenMap.Get( it.getToken() ) )
{
case XML_TOK_TABLE_ROW_ATTR_REPEATED:
{
mnRepeatRowCount = std::max(it.toInt32(), static_cast<sal_Int32>(1));
}
break;
}
}
}
ScXMLExternalRefRowContext::~ScXMLExternalRefRowContext()
{
}
Reference< XFastContextHandler > SAL_CALL ScXMLExternalRefRowContext::createFastChildContext(
sal_Int32 nElement, const Reference< XFastAttributeList >& xAttrList )
{
const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowElemTokenMap();
sal_uInt16 nToken = rTokenMap.Get( nElement );
sax_fastparser::FastAttributeList *pAttribList =
&sax_fastparser::castToFastAttributeList( xAttrList );
if (nToken == XML_TOK_TABLE_ROW_CELL || nToken == XML_TOK_TABLE_ROW_COVERED_CELL)
return new ScXMLExternalRefCellContext(mrScImport, pAttribList, mrExternalRefInfo);
return nullptr;
}
void SAL_CALL ScXMLExternalRefRowContext::endFastElement( sal_Int32 /* nElement */ )
{
ScExternalRefCache::TableTypeRef pTab = mrExternalRefInfo.mpCacheTable;
for (sal_Int32 i = 1; i < mnRepeatRowCount; ++i)
{
// Performance: duplicates of a non-existent row will still not exist.
// Don't find that out for every cell.
// External references often are a sparse matrix.
if (i == 1 && !pTab->hasRow( mrExternalRefInfo.mnRow))
{
mrExternalRefInfo.mnRow += mnRepeatRowCount;
return;
}
for (sal_Int32 j = 0; j < mrExternalRefInfo.mnCol; ++j)
{
ScExternalRefCache::TokenRef pToken = pTab->getCell(
static_cast<SCCOL>(j), static_cast<SCROW>(mrExternalRefInfo.mnRow));
if (pToken)
{
pTab->setCell(static_cast<SCCOL>(j),
static_cast<SCROW>(mrExternalRefInfo.mnRow+i), pToken);
}
}
}
mrExternalRefInfo.mnRow += mnRepeatRowCount;
}
ScXMLExternalRefCellContext::ScXMLExternalRefCellContext(
ScXMLImport& rImport,
const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList, ScXMLExternalTabData& rRefInfo ) :
ScXMLImportContext( rImport ),
mrScImport(rImport),
mrExternalRefInfo(rRefInfo),
mfCellValue(0.0),
mnRepeatCount(1),
mnNumberFormat(-1),
mnCellType(css::util::NumberFormat::UNDEFINED),
mbIsNumeric(false),
mbIsEmpty(true)
{
using namespace ::xmloff::token;
const SvXMLTokenMap& rTokenMap = rImport.GetTableRowCellAttrTokenMap();
if ( !rAttrList.is() )
return;
for (auto &it : *rAttrList)
{
switch ( rTokenMap.Get( it.getToken() ) )
{
case XML_TOK_TABLE_ROW_CELL_ATTR_STYLE_NAME:
{
XMLTableStylesContext* pStyles = static_cast<XMLTableStylesContext*>(mrScImport.GetAutoStyles());
const XMLTableStyleContext* pStyle = static_cast<const XMLTableStyleContext*>(
pStyles->FindStyleChildContext(XmlStyleFamily::TABLE_CELL, it.toString(), true));
if (pStyle)
mnNumberFormat = const_cast<XMLTableStyleContext*>(pStyle)->GetNumberFormat();
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_REPEATED:
{
mnRepeatCount = ::std::max( it.toInt32(), static_cast<sal_Int32>(1) );
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE_TYPE:
{
mnCellType = ScXMLImport::GetCellType( it.toCString(), it.getLength() );
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE:
{
if ( !it.isEmpty() )
{
mfCellValue = it.toDouble();
mbIsNumeric = true;
mbIsEmpty = false;
}
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_DATE_VALUE:
{
if ( !it.isEmpty() && mrScImport.SetNullDateOnUnitConverter() )
{
mrScImport.GetMM100UnitConverter().convertDateTime( mfCellValue, it.toString() );
mbIsNumeric = true;
mbIsEmpty = false;
}
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_TIME_VALUE:
{
if ( !it.isEmpty() )
{
::sax::Converter::convertDuration( mfCellValue, it.toString() );
mbIsNumeric = true;
mbIsEmpty = false;
}
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_STRING_VALUE:
{
if ( !it.isEmpty() )
{
maCellString = it.toString();
mbIsNumeric = false;
mbIsEmpty = false;
}
}
break;
case XML_TOK_TABLE_ROW_CELL_ATTR_BOOLEAN_VALUE:
{
if ( !it.isEmpty() )
{
mfCellValue = IsXMLToken( it, XML_TRUE ) ? 1.0 : 0.0;
mbIsNumeric = true;
mbIsEmpty = false;
}
}
break;
default:
;
}
}
}
ScXMLExternalRefCellContext::~ScXMLExternalRefCellContext()
{
}
Reference< XFastContextHandler > SAL_CALL ScXMLExternalRefCellContext::createFastChildContext(
sal_Int32 nElement, const Reference< XFastAttributeList >& /*xAttrList*/ )
{
const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowCellElemTokenMap();
sal_uInt16 nToken = rTokenMap.Get( nElement );
if (nToken == XML_TOK_TABLE_ROW_CELL_P)
return new ScXMLExternalRefCellTextContext(mrScImport, *this);
return nullptr;
}
void SAL_CALL ScXMLExternalRefCellContext::endFastElement( sal_Int32 /*nElement*/ )
{
if (!maCellString.isEmpty())
mbIsEmpty = false;
for (sal_Int32 i = 0; i < mnRepeatCount; ++i, ++mrExternalRefInfo.mnCol)
{
if (mbIsEmpty)
continue;
ScExternalRefCache::TokenRef aToken;
if (mbIsNumeric)
aToken.reset(new formula::FormulaDoubleToken(mfCellValue));
else
{
ScDocument& rDoc = mrScImport.GetDoc().getDoc();
svl::SharedString aSS = rDoc.GetSharedStringPool().intern(maCellString);
aToken.reset(new formula::FormulaStringToken(aSS));
}
sal_uInt32 nNumFmt = mnNumberFormat >= 0 ? static_cast<sal_uInt32>(mnNumberFormat) : 0;
mrExternalRefInfo.mpCacheTable->setCell(
static_cast<SCCOL>(mrExternalRefInfo.mnCol),
static_cast<SCROW>(mrExternalRefInfo.mnRow),
aToken, nNumFmt);
}
}
void ScXMLExternalRefCellContext::SetCellString(const OUString& rStr)
{
maCellString = rStr;
}
ScXMLExternalRefCellTextContext::ScXMLExternalRefCellTextContext(
ScXMLImport& rImport,
ScXMLExternalRefCellContext& rParent ) :
ScXMLImportContext( rImport ),
mrParent(rParent)
{
}
ScXMLExternalRefCellTextContext::~ScXMLExternalRefCellTextContext()
{
}
void SAL_CALL ScXMLExternalRefCellTextContext::characters( const OUString& rChars )
{
maCellStrBuf.append( rChars );
}
void SAL_CALL ScXMLExternalRefCellTextContext::endFastElement( sal_Int32 /*nElement*/ )
{
mrParent.SetCellString( maCellStrBuf.makeStringAndClear() );
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|