summaryrefslogtreecommitdiff
path: root/sc/source/filter/xml/xmlexternaltabi.cxx
blob: fc90e5d28f2cca62c69d1233189751dcc15d38c7 (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* -*- 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 "token.hxx"
#include "document.hxx"
#include <documentimport.hxx>

#include <svl/sharedstringpool.hxx>
#include <xmloff/nmspmap.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 ::com::sun::star::uno::Reference;
using ::com::sun::star::xml::sax::XAttributeList;

ScXMLExternalRefTabSourceContext::ScXMLExternalRefTabSourceContext(
    ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
    const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
    SvXMLImportContext( rImport, nPrefix, rLName ),
    mrScImport(rImport),
    mrExternalRefInfo(rRefInfo)
{
    using namespace ::xmloff::token;

    sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
    for (sal_Int16 i = 0; i < nAttrCount; ++i)
    {
        const OUString& sAttrName = xAttrList->getNameByIndex(i);
        OUString aLocalName;
        sal_uInt16 nAttrPrefix = mrScImport.GetNamespaceMap().GetKeyByAttrName(sAttrName, &aLocalName);
        const OUString& sValue = xAttrList->getValueByIndex(i);
        if (nAttrPrefix == XML_NAMESPACE_XLINK)
        {
            if (IsXMLToken(aLocalName, XML_HREF))
                maRelativeUrl = sValue;
        }
        else if (nAttrPrefix == XML_NAMESPACE_TABLE)
        {
            if (IsXMLToken(aLocalName, XML_TABLE_NAME))
                maTableName = sValue;
            else if (IsXMLToken(aLocalName, XML_FILTER_NAME))
                maFilterName = sValue;
            else if (IsXMLToken(aLocalName, XML_FILTER_OPTIONS))
                maFilterOptions = sValue;
        }
    }
}

ScXMLExternalRefTabSourceContext::~ScXMLExternalRefTabSourceContext()
{
}

SvXMLImportContext* ScXMLExternalRefTabSourceContext::CreateChildContext(
    sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& /*xAttrList*/ )
{
    return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
}

/**
 * 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 ScXMLExternalRefTabSourceContext::EndElement()
{
    ScDocument* pDoc = mrScImport.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, sal_uInt16 nPrefix, const OUString& rLName,
    const Reference<XAttributeList>& /* xAttrList */, ScXMLExternalTabData& rRefInfo ) :
    SvXMLImportContext( rImport, nPrefix, rLName ),
    mrScImport(rImport),
    mrExternalRefInfo(rRefInfo)
{
}

ScXMLExternalRefRowsContext::~ScXMLExternalRefRowsContext()
{
}

SvXMLImportContext* ScXMLExternalRefRowsContext::CreateChildContext(
    sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
{
    // #i101319# row elements inside group, rows or header-rows
    // are treated like row elements directly in the table element

    const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowsElemTokenMap();
    sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
    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(
                mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);
        case XML_TOK_TABLE_ROWS_ROW:
            return new ScXMLExternalRefRowContext(
                mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);
        default:
            ;
    }
    return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
}

void ScXMLExternalRefRowsContext::EndElement()
{
}

ScXMLExternalRefRowContext::ScXMLExternalRefRowContext(
    ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
    const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
    SvXMLImportContext( rImport, nPrefix, rLName ),
    mrScImport(rImport),
    mrExternalRefInfo(rRefInfo),
    mnRepeatRowCount(1)
{
    mrExternalRefInfo.mnCol = 0;

    sal_Int16 nAttrCount(xAttrList.is() ? xAttrList->getLength() : 0);
    const SvXMLTokenMap& rAttrTokenMap = mrScImport.GetTableRowAttrTokenMap();
    for( sal_Int16 i=0; i < nAttrCount; ++i )
    {
        const OUString& sAttrName = xAttrList->getNameByIndex(i);
        OUString aLocalName;
        sal_uInt16 nAttrPrefix = mrScImport.GetNamespaceMap().GetKeyByAttrName(sAttrName, &aLocalName);
        const OUString& sValue = xAttrList->getValueByIndex(i);

        switch (rAttrTokenMap.Get(nAttrPrefix, aLocalName))
        {
            case XML_TOK_TABLE_ROW_ATTR_REPEATED:
            {
                mnRepeatRowCount = std::max(sValue.toInt32(), static_cast<sal_Int32>(1));
            }
            break;
        }
    }
}

ScXMLExternalRefRowContext::~ScXMLExternalRefRowContext()
{
}

SvXMLImportContext* ScXMLExternalRefRowContext::CreateChildContext(
    sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
{
    const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowElemTokenMap();
    sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
    if (nToken == XML_TOK_TABLE_ROW_CELL || nToken == XML_TOK_TABLE_ROW_COVERED_CELL)
        return new ScXMLExternalRefCellContext(mrScImport, nPrefix, rLocalName, xAttrList, mrExternalRefInfo);

    return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
}

void ScXMLExternalRefRowContext::EndElement()
{
    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.get())
            {
                pTab->setCell(static_cast<SCCOL>(j),
                              static_cast<SCROW>(mrExternalRefInfo.mnRow+i), pToken);
            }
        }
    }
    mrExternalRefInfo.mnRow += mnRepeatRowCount;
}

ScXMLExternalRefCellContext::ScXMLExternalRefCellContext(
    ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName,
    const Reference<XAttributeList>& xAttrList, ScXMLExternalTabData& rRefInfo ) :
    SvXMLImportContext( rImport, nPrefix, rLName ),
    mrScImport(rImport),
    mrExternalRefInfo(rRefInfo),
    mfCellValue(0.0),
    mnRepeatCount(1),
    mnNumberFormat(-1),
    mnCellType(::com::sun::star::util::NumberFormat::UNDEFINED),
    mbIsNumeric(false),
    mbIsEmpty(true)
{
    using namespace ::xmloff::token;

    sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
    const SvXMLTokenMap& rTokenMap = rImport.GetTableRowCellAttrTokenMap();
    for (sal_Int16 i = 0; i < nAttrCount; ++i)
    {
        OUString aLocalName;
        sal_uInt16 nAttrPrefix = rImport.GetNamespaceMap().GetKeyByAttrName(
            xAttrList->getNameByIndex(i), &aLocalName);

        const OUString& sValue = xAttrList->getValueByIndex(i);
        sal_uInt16 nToken = rTokenMap.Get(nAttrPrefix, aLocalName);

        switch (nToken)
        {
            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(XML_STYLE_FAMILY_TABLE_CELL, sValue, true));
                if (pStyle)
                    mnNumberFormat = const_cast<XMLTableStyleContext*>(pStyle)->GetNumberFormat();
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_REPEATED:
            {
                mnRepeatCount = ::std::max(sValue.toInt32(), static_cast<sal_Int32>(1));
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE_TYPE:
            {
                mnCellType = mrScImport.GetCellType(sValue);
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_VALUE:
            {
                if (!sValue.isEmpty())
                {
                    ::sax::Converter::convertDouble(mfCellValue, sValue);
                    mbIsNumeric = true;
                    mbIsEmpty = false;
                }
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_DATE_VALUE:
            {
                if (!sValue.isEmpty() && mrScImport.SetNullDateOnUnitConverter())
                {
                    mrScImport.GetMM100UnitConverter().convertDateTime(mfCellValue, sValue);
                    mbIsNumeric = true;
                    mbIsEmpty = false;
                }
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_TIME_VALUE:
            {
                if (!sValue.isEmpty())
                {
                    ::sax::Converter::convertDuration(mfCellValue, sValue);
                    mbIsNumeric = true;
                    mbIsEmpty = false;
                }
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_STRING_VALUE:
            {
                if (!sValue.isEmpty())
                {
                    maCellString = sValue;
                    mbIsNumeric = false;
                    mbIsEmpty = false;
                }
            }
            break;
            case XML_TOK_TABLE_ROW_CELL_ATTR_BOOLEAN_VALUE:
            {
                if (!sValue.isEmpty())
                {
                    mfCellValue = IsXMLToken(sValue, XML_TRUE) ? 1.0 : 0.0;
                    mbIsNumeric = true;
                    mbIsEmpty = false;
                }
            }
            break;
            default:
                ;
        }
    }
}

ScXMLExternalRefCellContext::~ScXMLExternalRefCellContext()
{
}

SvXMLImportContext* ScXMLExternalRefCellContext::CreateChildContext(
    sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& xAttrList )
{
    const SvXMLTokenMap& rTokenMap = mrScImport.GetTableRowCellElemTokenMap();
    sal_uInt16 nToken = rTokenMap.Get(nPrefix, rLocalName);
    if (nToken == XML_TOK_TABLE_ROW_CELL_P)
        return new ScXMLExternalRefCellTextContext(mrScImport, nPrefix, rLocalName, xAttrList, *this);

    return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
}

void ScXMLExternalRefCellContext::EndElement()
{
    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, sal_uInt16 nPrefix, const OUString& rLName,
    const Reference<XAttributeList>& /*xAttrList*/,
    ScXMLExternalRefCellContext& rParent ) :
    SvXMLImportContext( rImport, nPrefix, rLName ),
    mrParent(rParent)
{
}

ScXMLExternalRefCellTextContext::~ScXMLExternalRefCellTextContext()
{
}

SvXMLImportContext* ScXMLExternalRefCellTextContext::CreateChildContext(
    sal_uInt16 nPrefix, const OUString& rLocalName, const Reference<XAttributeList>& /*xAttrList*/ )
{
    return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
}

void ScXMLExternalRefCellTextContext::Characters(const OUString& rChar)
{
    maCellStrBuf.append(rChar);
}

void ScXMLExternalRefCellTextContext::EndElement()
{
    mrParent.SetCellString(maCellStrBuf.makeStringAndClear());
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */