Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/xmloff/source/chart/SchXMLTools.cxx
Line
Count
Source
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
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include "SchXMLTools.hxx"
21
22
#include <rtl/ustrbuf.hxx>
23
#include <xmloff/xmluconv.hxx>
24
#include <xmloff/xmlement.hxx>
25
#include <xmloff/xmlimppr.hxx>
26
#include <xmloff/prstylei.hxx>
27
#include <xmloff/xmlprmap.hxx>
28
#include <xmloff/xmlexp.hxx>
29
#include <xmloff/xmlimp.hxx>
30
#include <xmloff/xmlnamespace.hxx>
31
#include <xmloff/xmlmetai.hxx>
32
#include <xmloff/maptype.hxx>
33
34
#include <com/sun/star/beans/PropertyAttribute.hpp>
35
#include <com/sun/star/uno/XComponentContext.hpp>
36
#include <com/sun/star/chart2/data/LabeledDataSequence.hpp>
37
#include <com/sun/star/chart2/data/XDataProvider.hpp>
38
#include <com/sun/star/chart2/data/XDataReceiver.hpp>
39
#include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
40
#include <com/sun/star/chart2/FormattedString.hpp>
41
#include <com/sun/star/chart2/XChartDocument.hpp>
42
#include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
43
#include <com/sun/star/container/XChild.hpp>
44
#include <com/sun/star/document/XDocumentProperties.hpp>
45
#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
46
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
47
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
48
49
#include <chart2/AbstractPivotTableDataProvider.hxx>
50
#include <comphelper/processfactory.hxx>
51
#include <comphelper/diagnose_ex.hxx>
52
#include <comphelper/sequence.hxx>
53
#include <sal/log.hxx>
54
#include <o3tl/string_view.hxx>
55
#include <algorithm>
56
#include <map>
57
58
using namespace com::sun::star;
59
using namespace ::xmloff::token;
60
61
using ::com::sun::star::uno::Reference;
62
using ::com::sun::star::uno::Sequence;
63
64
namespace
65
{
66
67
OUString lcl_getGeneratorFromModel( const uno::Reference< frame::XModel >& xChartModel )
68
0
{
69
0
    OUString aGenerator;
70
0
    uno::Reference< document::XDocumentPropertiesSupplier> xChartDocumentPropertiesSupplier( xChartModel, uno::UNO_QUERY );
71
0
    if( xChartDocumentPropertiesSupplier.is() )
72
0
    {
73
0
        uno::Reference< document::XDocumentProperties > xChartDocumentProperties(
74
0
            xChartDocumentPropertiesSupplier->getDocumentProperties());
75
0
        if( xChartDocumentProperties.is() )
76
0
            aGenerator =  xChartDocumentProperties->getGenerator();
77
0
    }
78
0
    return aGenerator;
79
0
}
80
81
OUString lcl_getGeneratorFromModelOrItsParent( const uno::Reference< frame::XModel >& xChartModel )
82
0
{
83
0
    OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
84
0
    if( aGenerator.isEmpty() ) //try to get the missing info from the parent document
85
0
    {
86
0
        uno::Reference< container::XChild > xChild( xChartModel, uno::UNO_QUERY );
87
0
        if( xChild.is() )
88
0
            aGenerator = lcl_getGeneratorFromModel( uno::Reference< frame::XModel >( xChild->getParent(), uno::UNO_QUERY) );
89
0
    }
90
0
    return aGenerator;
91
0
}
92
93
sal_Int32 lcl_getBuildIDFromGenerator( std::u16string_view rGenerator )
94
0
{
95
    //returns -1 if nothing found
96
0
    sal_Int32 nBuildId = -1;
97
0
    static constexpr OUString sBuildCompare(  u"$Build-"_ustr  );
98
0
    size_t nBegin = rGenerator.find( sBuildCompare );
99
0
    if( nBegin != std::u16string_view::npos )
100
0
    {
101
0
        std::u16string_view sBuildId = rGenerator.substr( nBegin + sBuildCompare.getLength() );
102
0
        nBuildId = o3tl::toInt32(sBuildId);
103
0
    }
104
0
    return nBuildId;
105
0
}
106
107
OUString lcl_ConvertRange( const OUString & rRange, const Reference< chart2::data::XDataProvider >& xDataProvider )
108
0
{
109
0
    OUString aResult = rRange;
110
0
    Reference< chart2::data::XRangeXMLConversion > xRangeConversion( xDataProvider, uno::UNO_QUERY );
111
0
    if( xRangeConversion.is())
112
0
        aResult = xRangeConversion->convertRangeFromXML( rRange );
113
0
    return aResult;
114
0
}
115
116
Reference< chart2::data::XDataSequence > lcl_createNewSequenceFromCachedXMLRange( const Reference< chart2::data::XDataSequence >& xSeq, const Reference< chart2::data::XDataProvider >& xDataProvider )
117
0
{
118
0
    Reference< chart2::data::XDataSequence > xRet;
119
0
    OUString aRange;
120
0
    if( xSeq.is() && SchXMLTools::getXMLRangePropertyFromDataSequence( xSeq, aRange, /* bClearProp = */ true ) )
121
0
    {
122
0
        xRet.set( xDataProvider->createDataSequenceByRangeRepresentation(
123
0
            lcl_ConvertRange( aRange, xDataProvider )) );
124
0
        SchXMLTools::copyProperties( Reference< beans::XPropertySet >( xSeq, uno::UNO_QUERY ),
125
0
            Reference< beans::XPropertySet >( xRet, uno::UNO_QUERY ));
126
0
    }
127
0
    return xRet;
128
0
}
129
130
} // anonymous namespace
131
132
namespace SchXMLTools
133
{
134
135
const SvXMLEnumMapEntry<SchXMLChartTypeEnum> aXMLChartClassMap[] =
136
{
137
    { XML_LINE,         XML_CHART_CLASS_LINE    },
138
    { XML_AREA,         XML_CHART_CLASS_AREA    },
139
    { XML_CIRCLE,       XML_CHART_CLASS_CIRCLE  },
140
    { XML_RING,         XML_CHART_CLASS_RING    },
141
    { XML_SCATTER,      XML_CHART_CLASS_SCATTER },
142
    { XML_RADAR,        XML_CHART_CLASS_RADAR   },
143
    { XML_FILLED_RADAR, XML_CHART_CLASS_FILLED_RADAR },
144
    { XML_BAR,          XML_CHART_CLASS_BAR     },
145
    { XML_HISTOGRAM,    XML_CHART_CLASS_HISTOGRAM },
146
    { XML_STOCK,        XML_CHART_CLASS_STOCK   },
147
    { XML_BUBBLE,       XML_CHART_CLASS_BUBBLE  },
148
    { XML_CORRELATION_CIRCLE, XML_CHART_CLASS_CORRELATION_CIRCLE },
149
    { XML_SURFACE,      XML_CHART_CLASS_BAR     }, //@todo change this if a surface chart is available
150
    { XML_ADD_IN,       XML_CHART_CLASS_ADDIN   },
151
    { XML_TOKEN_INVALID, XML_CHART_CLASS_UNKNOWN }
152
};
153
154
SchXMLChartTypeEnum GetChartTypeEnum( std::u16string_view rClassName )
155
0
{
156
0
    SchXMLChartTypeEnum nEnumVal = XML_CHART_CLASS_UNKNOWN;
157
0
    SvXMLUnitConverter::convertEnum( nEnumVal, rClassName, aXMLChartClassMap );
158
0
    return nEnumVal;
159
0
}
160
161
typedef std::map< OUString, OUString > tMakeStringStringMap;
162
//static
163
static const tMakeStringStringMap& lcl_getChartTypeNameMap()
164
0
{
165
    //shape property -- chart model object property
166
0
    static const tMakeStringStringMap g_aChartTypeNameMap{
167
0
        {"com.sun.star.chart.LineDiagram",
168
0
         "com.sun.star.chart2.LineChartType"},
169
0
        {"com.sun.star.chart.AreaDiagram",
170
0
         "com.sun.star.chart2.AreaChartType"},
171
0
        {"com.sun.star.chart.BarDiagram",
172
0
         "com.sun.star.chart2.ColumnChartType"},
173
0
        {"com.sun.star.chart.HistogramDiagram",
174
0
         "com.sun.star.chart2.HistogramChartType"},
175
0
        {"com.sun.star.chart.PieDiagram",
176
0
         "com.sun.star.chart2.PieChartType"},
177
0
        {"com.sun.star.chart.BarOfPieDiagram",
178
0
         "com.sun.star.chart2.BarOfPieChartType"},
179
0
        {"com.sun.star.chart.PieOfPieDiagram",
180
0
         "com.sun.star.chart2.PieOfPieChartType"},
181
0
        {"com.sun.star.chart.DonutDiagram",
182
0
         "com.sun.star.chart2.DonutChartType"},
183
0
        {"com.sun.star.chart.XYDiagram",
184
0
         "com.sun.star.chart2.ScatterChartType"},
185
0
        {"com.sun.star.chart.NetDiagram",
186
0
         "com.sun.star.chart2.NetChartType"},
187
0
        {"com.sun.star.chart.FilledNetDiagram",
188
0
         "com.sun.star.chart2.FilledNetChartType"},
189
0
        {"com.sun.star.chart.StockDiagram",
190
0
         "com.sun.star.chart2.CandleStickChartType"},
191
0
        {"com.sun.star.chart.BubbleDiagram",
192
0
         "com.sun.star.chart2.BubbleChartType"},
193
0
        {"com.sun.star.chart.CorrelationCircleDiagram",
194
0
         "com.sun.star.chart2.CorrelationCircleChartType"}};
195
0
    return g_aChartTypeNameMap;
196
0
}
197
198
OUString GetNewChartTypeName( const OUString & rOldChartTypeName )
199
0
{
200
0
    OUString aNew(rOldChartTypeName);
201
202
0
    const tMakeStringStringMap& rMap = lcl_getChartTypeNameMap();
203
0
    tMakeStringStringMap::const_iterator aIt( rMap.find( rOldChartTypeName ));
204
0
    if( aIt != rMap.end())
205
0
    {
206
0
        aNew = aIt->second;
207
0
    }
208
0
    return aNew;
209
0
}
210
211
OUString GetChartTypeByClassName(
212
    std::u16string_view rClassName, bool bUseOldNames )
213
0
{
214
0
    OUStringBuffer aResultBuffer;
215
0
    bool bInternalType = false;
216
217
0
    if( bUseOldNames )
218
0
        aResultBuffer.append( "com.sun.star.chart.");
219
0
    else
220
0
        aResultBuffer.append( "com.sun.star.chart2.");
221
222
0
    bInternalType = true;
223
224
0
    if( IsXMLToken( rClassName, XML_LINE ))
225
0
        aResultBuffer.append("Line");
226
0
    else if( IsXMLToken( rClassName, XML_AREA ))
227
0
        aResultBuffer.append("Area");
228
0
    else if( IsXMLToken( rClassName, XML_BAR ))
229
0
    {
230
0
        if( bUseOldNames )
231
0
            aResultBuffer.append("Bar");
232
0
        else
233
0
        {
234
0
            aResultBuffer.append("Column");
235
            // @todo: might be Bar
236
0
        }
237
0
    }
238
0
    else if (IsXMLToken(rClassName, XML_HISTOGRAM))
239
0
        aResultBuffer.append("Histogram");
240
0
    else if( IsXMLToken( rClassName, XML_CIRCLE ))
241
0
        aResultBuffer.append("Pie");
242
0
    else if( IsXMLToken( rClassName, XML_RING ))
243
0
        aResultBuffer.append("Donut");
244
0
    else if( IsXMLToken( rClassName, XML_SCATTER ))
245
0
    {
246
0
        if( bUseOldNames )
247
0
            aResultBuffer.append("XY");
248
0
        else
249
0
            aResultBuffer.append("Scatter");
250
0
    }
251
252
0
    else if( IsXMLToken( rClassName, XML_BUBBLE ))
253
0
        aResultBuffer.append("Bubble");
254
0
    else if( IsXMLToken( rClassName, XML_CORRELATION_CIRCLE ))
255
0
        aResultBuffer.append("CorrelationCircle");
256
0
    else if( IsXMLToken( rClassName, XML_RADAR ))
257
0
        aResultBuffer.append("Net");
258
0
    else if( IsXMLToken( rClassName, XML_FILLED_RADAR ))
259
0
        aResultBuffer.append("FilledNet");
260
0
    else if( IsXMLToken( rClassName, XML_STOCK ))
261
0
    {
262
0
        if( bUseOldNames )
263
0
            aResultBuffer.append("Stock");
264
0
        else
265
0
            aResultBuffer.append("CandleStick");
266
0
    }
267
0
    else if( IsXMLToken( rClassName, XML_SURFACE ))
268
0
    {
269
        //@todo change this if a surface chart is available
270
0
        if( bUseOldNames )
271
0
            aResultBuffer.append("Bar");
272
0
        else
273
0
            aResultBuffer.append("Column");
274
0
    }
275
0
    else
276
0
        bInternalType = false;
277
278
0
    if( ! bInternalType )
279
0
        return OUString();
280
281
0
    if( bUseOldNames )
282
0
        aResultBuffer.append("Diagram");
283
0
    else
284
0
        aResultBuffer.append("ChartType");
285
286
0
    return aResultBuffer.makeStringAndClear();
287
288
0
}
289
290
XMLTokenEnum getTokenByChartType(
291
    std::u16string_view rChartTypeService, bool bUseOldNames )
292
0
{
293
0
    XMLTokenEnum eResult = XML_TOKEN_INVALID;
294
0
    OUString aPrefix, aPostfix;
295
296
0
    if( bUseOldNames )
297
0
    {
298
0
        aPrefix = "com.sun.star.chart.";
299
0
        aPostfix = "Diagram";
300
0
    }
301
0
    else
302
0
    {
303
0
        aPrefix = "com.sun.star.chart2.";
304
0
        aPostfix = "ChartType";
305
0
    }
306
307
0
    if( o3tl::starts_with(rChartTypeService, aPrefix))
308
0
    {
309
0
        sal_Int32 nSkip = aPrefix.getLength();
310
0
        SAL_WARN_IF( static_cast<sal_Int32>(rChartTypeService.size()) < nSkip, "xmloff.chart", "ChartTypeService.getLength() < nSkip" );
311
0
        sal_Int32 nTypeLength = rChartTypeService.size() - nSkip - aPostfix.getLength();
312
        // if postfix matches and leaves a non-empty type
313
0
        if( nTypeLength > 0 && o3tl::starts_with(rChartTypeService.substr(nSkip + nTypeLength), aPostfix) )
314
0
        {
315
0
            std::u16string_view aServiceName( rChartTypeService.substr( nSkip, nTypeLength ));
316
317
0
            if ( aServiceName == u"Line" )
318
0
                eResult = XML_LINE;
319
0
            else if ( aServiceName == u"Area" )
320
0
                eResult = XML_AREA;
321
0
            else if( aServiceName == u"Bar" ||
322
0
                     (!bUseOldNames && aServiceName == u"Column"))
323
0
                eResult = XML_BAR;
324
0
            else if (aServiceName == u"Histogram")
325
0
                eResult = XML_HISTOGRAM;
326
0
            else if ( aServiceName == u"Pie" || aServiceName == u"BarOfPie" ||
327
0
                    aServiceName == u"PieOfPie" )
328
0
                eResult = XML_CIRCLE;
329
0
            else if ( aServiceName == u"Donut" )
330
0
                eResult = XML_RING;
331
0
            else if( (bUseOldNames && aServiceName == u"XY") ||
332
0
                     (!bUseOldNames && aServiceName == u"Scatter"))
333
0
                eResult = XML_SCATTER;
334
0
            else if ( aServiceName == u"Bubble" )
335
0
                eResult = XML_BUBBLE;
336
0
            else if ( aServiceName == u"CorrelationCircle" )
337
0
                eResult = XML_CORRELATION_CIRCLE;
338
0
            else if ( aServiceName == u"Net" )
339
0
                eResult = XML_RADAR;
340
0
            else if ( aServiceName == u"FilledNet" )
341
0
                eResult = XML_FILLED_RADAR;
342
0
            else if( (bUseOldNames && aServiceName == u"Stock") ||
343
0
                     (!bUseOldNames && aServiceName == u"CandleStick"))
344
0
                eResult = XML_STOCK;
345
0
        }
346
0
    }
347
348
0
    if( eResult == XML_TOKEN_INVALID && !rChartTypeService.empty() )
349
0
        eResult = XML_ADD_IN;
350
351
0
    return eResult;
352
0
}
353
354
Reference< chart2::data::XLabeledDataSequence2 > GetNewLabeledDataSequence()
355
0
{
356
0
    const Reference< uno::XComponentContext >& xContext( comphelper::getProcessComponentContext() );
357
0
    Reference< chart2::data::XLabeledDataSequence2 > xResult = chart2::data::LabeledDataSequence::create(xContext);
358
0
    return xResult;
359
0
}
360
361
Reference< chart2::data::XDataSequence > CreateDataSequence(
362
        const OUString & rRange,
363
        const Reference< chart2::XChartDocument >& xChartDoc )
364
0
{
365
0
    Reference< chart2::data::XDataSequence > xRet;
366
367
0
    if( !xChartDoc.is() )
368
0
    {
369
0
        SAL_WARN("xmloff.chart", "need a chart document" );
370
0
        return xRet;
371
0
    }
372
373
0
    Reference< chart2::data::XDataProvider > xDataProvider( xChartDoc->getDataProvider() );
374
0
    if( !xDataProvider.is() )
375
0
    {
376
0
        SAL_WARN("xmloff.chart", "need a data provider" );
377
0
        return xRet;
378
0
    }
379
380
0
    bool bUseInternal = false;
381
0
    uno::Reference<beans::XPropertySet> xPropSet(xDataProvider, uno::UNO_QUERY);
382
0
    if (xPropSet.is())
383
0
    {
384
0
        try
385
0
        {
386
0
            bool bVal = false;
387
0
            uno::Any any = xPropSet->getPropertyValue(u"UseInternalDataProvider"_ustr);
388
0
            if (any >>= bVal)
389
0
                bUseInternal = bVal;
390
0
        }
391
0
        catch (const beans::UnknownPropertyException&)
392
0
        {
393
            // Do nothing
394
0
        }
395
0
    }
396
397
0
    if (!bUseInternal)
398
0
    {
399
0
        try
400
0
        {
401
0
            xRet.set( xDataProvider->createDataSequenceByRangeRepresentation( lcl_ConvertRange( rRange, xDataProvider )));
402
0
            SchXMLTools::setXMLRangePropertyAtDataSequence( xRet, rRange );
403
0
        }
404
0
        catch( const lang::IllegalArgumentException & )
405
0
        {
406
0
            SAL_WARN("xmloff.chart", "could not create data sequence" );
407
0
        }
408
0
    }
409
410
0
    if( !xRet.is() && !xChartDoc->hasInternalDataProvider() && !rRange.isEmpty() )
411
0
    {
412
        //#i103911# switch to internal data in case the parent cannot provide the requested data
413
0
        xChartDoc->createInternalDataProvider( true /* bCloneExistingData */ );
414
0
        xDataProvider = xChartDoc->getDataProvider();
415
0
        try
416
0
        {
417
0
            xRet.set( xDataProvider->createDataSequenceByRangeRepresentation( lcl_ConvertRange( rRange, xDataProvider )));
418
0
            SchXMLTools::setXMLRangePropertyAtDataSequence( xRet, rRange );
419
0
        }
420
0
        catch( const lang::IllegalArgumentException & )
421
0
        {
422
0
            SAL_WARN("xmloff.chart", "could not create data sequence" );
423
0
        }
424
0
    }
425
0
    return xRet;
426
0
}
427
428
Reference< chart2::data::XDataSequence > CreateDataSequenceWithoutConvert(
429
        const OUString & rRange,
430
        const Reference< chart2::XChartDocument >& xChartDoc )
431
0
{
432
0
    Reference< chart2::data::XDataSequence > xRet;
433
434
0
    if( !xChartDoc.is() )
435
0
    {
436
0
        SAL_WARN("xmloff.chart", "need a chart document" );
437
0
        return xRet;
438
0
    }
439
440
0
    Reference< chart2::data::XDataProvider > xDataProvider( xChartDoc->getDataProvider() );
441
0
    if( !xDataProvider.is() )
442
0
    {
443
0
        SAL_WARN("xmloff.chart", "need a data provider" );
444
0
        return xRet;
445
0
    }
446
447
0
    try
448
0
    {
449
0
        xRet.set( xDataProvider->createDataSequenceByRangeRepresentation( rRange ) );
450
0
        SchXMLTools::setXMLRangePropertyAtDataSequence( xRet, rRange );
451
0
    }
452
0
    catch( const lang::IllegalArgumentException & )
453
0
    {
454
0
        SAL_WARN("xmloff.chart", "could not create data sequence" );
455
0
    }
456
457
0
    return xRet;
458
0
}
459
460
void CreateCategories(
461
    const uno::Reference< chart2::data::XDataProvider > & xDataProvider,
462
    const uno::Reference< chart2::XChartDocument > & xNewDoc,
463
    const OUString & rRangeAddress,
464
    sal_Int32 nCooSysIndex,
465
    sal_Int32 nDimensionIndex,
466
    tSchXMLLSequencesPerIndex * pLSequencesPerIndex )
467
0
{
468
0
    try
469
0
    {
470
0
        if( xNewDoc.is() && !rRangeAddress.isEmpty())
471
0
        {
472
0
            if( xDataProvider.is())
473
0
            {
474
0
                uno::Reference< chart2::XDiagram > xDia( xNewDoc->getFirstDiagram());
475
0
                if( !xDia.is())
476
0
                    return;
477
478
0
                uno::Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDia, uno::UNO_QUERY_THROW );
479
0
                uno::Sequence< uno::Reference< chart2::XCoordinateSystem > >
480
0
                    aCooSysSeq( xCooSysCnt->getCoordinateSystems());
481
0
                if( nCooSysIndex < aCooSysSeq.getLength())
482
0
                {
483
0
                    const uno::Reference< chart2::XCoordinateSystem >& xCooSys( aCooSysSeq[nCooSysIndex] );
484
0
                    SAL_WARN_IF( !xCooSys.is(), "xmloff.chart", "xCooSys is NULL");
485
0
                    if( nDimensionIndex < xCooSys->getDimension() )
486
0
                    {
487
0
                        const sal_Int32 nMaxAxisIndex = xCooSys->getMaximumAxisIndexByDimension(nDimensionIndex);
488
0
                        for(sal_Int32 nI=0; nI<=nMaxAxisIndex; ++nI)
489
0
                        {
490
0
                            uno::Reference< chart2::XAxis > xAxis( xCooSys->getAxisByDimension( nDimensionIndex, nI ));
491
0
                            if( xAxis.is() )
492
0
                            {
493
0
                                chart2::ScaleData aData( xAxis->getScaleData());
494
0
                                uno::Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
495
0
                                    GetNewLabeledDataSequence(), uno::UNO_QUERY_THROW);
496
0
                                try
497
0
                                {
498
0
                                    OUString aConvertedRange( rRangeAddress );
499
0
                                    bool bRangeConverted = false;
500
0
                                    if( ! (xNewDoc->hasInternalDataProvider() && aConvertedRange == "categories"))
501
0
                                    {
502
0
                                        Reference< chart2::data::XRangeXMLConversion > xXMLConv( xDataProvider, uno::UNO_QUERY );
503
0
                                        if( xXMLConv.is())
504
0
                                        {
505
0
                                            aConvertedRange = xXMLConv->convertRangeFromXML( rRangeAddress );
506
0
                                            bRangeConverted = true;
507
0
                                        }
508
0
                                    }
509
510
0
                                    Reference<chart2::data::XDataSequence> xSequence;
511
0
                                    chart2api::AbstractPivotTableDataProvider* pPivotTableDataProvider =
512
0
                                        dynamic_cast<chart2api::AbstractPivotTableDataProvider*>(xDataProvider.get());
513
0
                                    if (pPivotTableDataProvider)
514
0
                                    {
515
0
                                        xSequence.set(pPivotTableDataProvider->createDataSequenceOfCategories());
516
0
                                    }
517
0
                                    else
518
0
                                    {
519
0
                                        xSequence.set(xDataProvider->createDataSequenceByRangeRepresentation(aConvertedRange));
520
0
                                        if (bRangeConverted)
521
0
                                            setXMLRangePropertyAtDataSequence(xSequence, rRangeAddress);
522
0
                                    }
523
0
                                    xLabeledSeq->setValues(xSequence);
524
525
0
                                }
526
0
                                catch( const lang::IllegalArgumentException & )
527
0
                                {
528
0
                                    DBG_UNHANDLED_EXCEPTION("xmloff.chart");
529
0
                                }
530
0
                                aData.Categories.set( xLabeledSeq );
531
0
                                if( pLSequencesPerIndex )
532
0
                                {
533
                                    // register for setting local data if external data provider is not present
534
0
                                    pLSequencesPerIndex->emplace(
535
0
                                            tSchXMLIndexWithPart( SCH_XML_CATEGORIES_INDEX, SCH_XML_PART_VALUES ), xLabeledSeq );
536
0
                                }
537
0
                                xAxis->setScaleData( aData );
538
0
                            }
539
0
                        }
540
0
                    }
541
0
                }
542
0
            }
543
0
        }
544
0
    }
545
0
    catch( uno::Exception & )
546
0
    {
547
0
        TOOLS_WARN_EXCEPTION("xmloff.chart", "Exception caught while creating Categories" );
548
0
    }
549
0
}
550
551
uno::Any getPropertyFromContext( std::u16string_view rPropertyName, const XMLPropStyleContext* pPropStyleContext, const SvXMLStylesContext* pStylesCtxt )
552
0
{
553
0
    uno::Any aRet;
554
0
    if( !pPropStyleContext || !pStylesCtxt )
555
0
        return aRet;
556
0
    const ::std::vector< XMLPropertyState >& rProperties = pPropStyleContext->GetProperties();
557
0
    const rtl::Reference< XMLPropertySetMapper >& rMapper = pStylesCtxt->GetImportPropertyMapper( pPropStyleContext->GetFamily()/*XML_STYLE_FAMILY_SCH_CHART_ID*/ )->getPropertySetMapper();
558
0
    for( const auto& rProp : rProperties )
559
0
    {
560
0
        sal_Int32 nIdx = rProp.mnIndex;
561
0
        if( nIdx == -1 )
562
0
            continue;
563
0
        OUString aPropName = rMapper->GetEntryAPIName( nIdx );
564
0
        if(rPropertyName == aPropName)
565
0
            return rProp.maValue;
566
0
    }
567
0
    return aRet;
568
0
}
569
570
void exportText( SvXMLExport& rExport, const OUString& rText, bool bConvertTabsLFs )
571
0
{
572
0
    SvXMLElementExport aPara( rExport, XML_NAMESPACE_TEXT,
573
0
                              ::xmloff::token::GetXMLToken( ::xmloff::token::XML_P ),
574
0
                              true, false );
575
576
0
    if( bConvertTabsLFs )
577
0
    {
578
0
        sal_Int32 nStartPos = 0;
579
0
        sal_Int32 nEndPos = rText.getLength();
580
0
        sal_Unicode cChar;
581
582
0
        for( sal_Int32 nPos = 0; nPos < nEndPos; nPos++ )
583
0
        {
584
0
            cChar = rText[ nPos ];
585
0
            switch( cChar )
586
0
            {
587
0
                case 0x0009:        // tabulator
588
0
                    {
589
0
                        if( nPos > nStartPos )
590
0
                            rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nPos - nStartPos)) );
591
0
                        nStartPos = nPos + 1;
592
593
0
                        SvXMLElementExport aElem( rExport, XML_NAMESPACE_TEXT,
594
0
                                                  ::xmloff::token::GetXMLToken( ::xmloff::token::XML_TAB_STOP ),
595
0
                                                  false, false );
596
0
                    }
597
0
                    break;
598
599
0
                case 0x000A:        // linefeed
600
0
                    {
601
0
                        if( nPos > nStartPos )
602
0
                            rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nPos - nStartPos)) );
603
0
                        nStartPos = nPos + 1;
604
605
0
                        SvXMLElementExport aElem( rExport, XML_NAMESPACE_TEXT,
606
0
                                                  ::xmloff::token::GetXMLToken( ::xmloff::token::XML_LINE_BREAK ),
607
0
                                                  false, false );
608
0
                    }
609
0
                    break;
610
0
            }
611
0
        }
612
0
        if( nEndPos > nStartPos )
613
0
        {
614
0
            if( nStartPos == 0 )
615
0
                rExport.GetDocHandler()->characters( rText );
616
0
            else
617
0
                rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nEndPos - nStartPos)) );
618
0
        }
619
0
    }
620
0
    else // do not convert tabs and linefeeds (eg for numbers coming from unit converter)
621
0
    {
622
0
        rExport.GetDocHandler()->characters( rText );
623
0
    }
624
0
}
625
626
void exportFormattedText( SvXMLExport& rExport, const uno::Reference< beans::XPropertySet >& xTitleProps )
627
0
{
628
0
    Sequence< uno::Reference< chart2::XFormattedString > > xFormattedTitle;
629
0
    if (xTitleProps.is())
630
0
    {
631
0
        OUString aTitle;
632
0
        if ((xTitleProps->getPropertyValue(u"String"_ustr) >>= aTitle) && !aTitle.isEmpty())
633
0
            xTitleProps->getPropertyValue(u"FormattedStrings"_ustr) >>= xFormattedTitle;
634
0
    }
635
636
0
    if (xFormattedTitle.hasElements())
637
0
    {
638
0
        SvXMLElementExport aElemP(rExport, XML_NAMESPACE_TEXT, XML_P, true, false);
639
0
        for (const uno::Reference<chart2::XFormattedString>& rxFS : xFormattedTitle)
640
0
        {
641
0
            Reference< beans::XPropertySet > xRunPropSet(rxFS, uno::UNO_QUERY);
642
0
            bool bIsUICharStyle, bHasAutoStyle = false;
643
0
            OUString sStyle = rExport.GetTextParagraphExport()->FindTextStyle(xRunPropSet, bIsUICharStyle, bHasAutoStyle);
644
0
            if (!sStyle.isEmpty())
645
0
            {
646
0
                rExport.AddAttribute(XML_NAMESPACE_TEXT, XML_STYLE_NAME,
647
0
                    rExport.EncodeStyleName(sStyle));
648
0
            }
649
650
0
            SvXMLElementExport aSpan(rExport, !sStyle.isEmpty(), XML_NAMESPACE_TEXT, XML_SPAN, true, false);
651
0
            rExport.GetDocHandler()->characters(rxFS->getString());
652
0
        }
653
0
    }
654
0
}
655
656
void importFormattedText( SvXMLImport& rImport, const std::vector<std::pair<OUString, OUString>>& rTitle,
657
    const uno::Reference< beans::XPropertySet >& xTitleProp )
658
0
{
659
0
    if (!xTitleProp.is())
660
0
        return;
661
662
0
    std::vector< Reference< chart2::XFormattedString > > aStringVec;
663
0
    Sequence< uno::Reference< chart2::XFormattedString > > xFullTextTitle;
664
0
    Reference < beans::XPropertySet > xFullTextTitleProps;
665
666
0
    try
667
0
    {
668
0
        if ((xTitleProp->getPropertyValue(u"FormattedStrings"_ustr) >>= xFullTextTitle) &&
669
0
            xFullTextTitle.hasElements())
670
0
        {
671
            // these are the properties from the textshape object - needs to apply them
672
            // to all the string parts firstly - (necessary for backward compatibility)
673
0
            xFullTextTitleProps.set(xFullTextTitle.getArray()[0], uno::UNO_QUERY);
674
0
        }
675
676
0
        for (auto aRIt = std::begin(rTitle); aRIt != std::end(rTitle); ++aRIt)
677
0
        {
678
0
            Reference< chart2::XFormattedString2 > xNewFmtStr;
679
0
            xNewFmtStr = chart2::FormattedString::create(rImport.GetComponentContext());
680
681
0
            if (xFullTextTitleProps.is())
682
0
            {
683
                // these are the properties from the textshape object - needs to apply them
684
                // to all the string parts firstly
685
0
                uno::Reference< beans::XPropertySetInfo > xInfo = xNewFmtStr->getPropertySetInfo();
686
0
                for (const beans::Property& rProp : xFullTextTitleProps->getPropertySetInfo()->getProperties())
687
0
                {
688
0
                    if (xInfo.is() && xInfo->hasPropertyByName(rProp.Name))
689
0
                    {
690
0
                        const uno::Any aValue = xFullTextTitleProps->getPropertyValue(rProp.Name);
691
0
                        xNewFmtStr->setPropertyValue(rProp.Name, aValue);
692
0
                    }
693
0
                }
694
0
            }
695
696
0
            if (auto pStyle = rImport.GetTextImport()->FindAutoCharStyle(aRIt->first))
697
0
            {
698
0
                pStyle->FillPropertySet(xNewFmtStr);
699
0
            }
700
701
0
            xNewFmtStr->setString(aRIt->second);
702
0
            aStringVec.emplace_back(xNewFmtStr);
703
0
        }
704
705
0
        uno::Sequence< Reference< chart2::XFormattedString > > aStringSeq =
706
0
            comphelper::containerToSequence(aStringVec);
707
0
        xTitleProp->setPropertyValue(u"FormattedStrings"_ustr, uno::Any(aStringSeq));
708
0
    }
709
0
    catch (const beans::UnknownPropertyException&)
710
0
    {
711
0
        SAL_WARN("xmloff.chart", "Property FormattedStrings for Title not available");
712
0
    }
713
0
}
714
715
void exportRangeToSomewhere( SvXMLExport& rExport, const OUString& rValue )
716
0
{
717
    //with issue #i366# and CWS chart20 ranges for error bars were introduced
718
    //to keep them during copy paste from calc to impress for example it
719
    //was necessary to introduce a mapping between the used ranges within calc and the data written to the local table
720
    //this is why we write these ranges here
721
722
    //#i113950# first the range was exported to attribute text:id, but that attribute does not allow arbitrary strings anymore within ODF 1.2
723
    //as an alternative the range info is now saved into the description at an empty group element (not very nice, but ODF conform)
724
725
0
    const SvtSaveOptions::ODFSaneDefaultVersion nCurrentODFVersion(
726
0
            rExport.getSaneDefaultVersion());
727
0
    if (nCurrentODFVersion == SvtSaveOptions::ODFSVER_010 || nCurrentODFVersion == SvtSaveOptions::ODFSVER_011)
728
0
        return;//svg:desc is not allowed at draw:g in ODF1.0; but as the ranges for error bars are anyhow not allowed within ODF1.0 nor ODF1.1 we do not need the information
729
730
0
    SvXMLElementExport aEmptyShapeGroup( rExport, XML_NAMESPACE_DRAW,
731
0
                              ::xmloff::token::GetXMLToken( ::xmloff::token::XML_G ),
732
0
                              true, false );
733
0
    SvXMLElementExport aDescription( rExport, XML_NAMESPACE_SVG,
734
0
                              ::xmloff::token::GetXMLToken( ::xmloff::token::XML_DESC ),
735
0
                              true, false );
736
0
    rExport.GetDocHandler()->characters( rValue );
737
0
}
738
739
void setXMLRangePropertyAtDataSequence(
740
    const Reference< chart2::data::XDataSequence > & xDataSequence,
741
    const OUString & rXMLRange )
742
0
{
743
0
    if( !xDataSequence.is())
744
0
        return;
745
0
    try
746
0
    {
747
0
        static constexpr OUString aXMLRangePropName(  u"CachedXMLRange"_ustr );
748
0
        Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY_THROW );
749
0
        Reference< beans::XPropertySetInfo > xInfo( xProp->getPropertySetInfo());
750
0
        if( xInfo.is() && xInfo->hasPropertyByName( aXMLRangePropName ))
751
0
            xProp->setPropertyValue( aXMLRangePropName, uno::Any( rXMLRange ));
752
0
    }
753
0
    catch( const uno::Exception & )
754
0
    {
755
0
        DBG_UNHANDLED_EXCEPTION("xmloff.chart");
756
0
    }
757
0
}
758
759
bool getXMLRangePropertyFromDataSequence(
760
    const Reference< chart2::data::XDataSequence > & xDataSequence,
761
    OUString & rOutXMLRange,
762
    bool bClearProp /* = false */)
763
0
{
764
0
    bool bResult = false;
765
0
    if( xDataSequence.is())
766
0
    {
767
0
        try
768
0
        {
769
0
            static constexpr OUString aXMLRangePropName(  u"CachedXMLRange"_ustr );
770
0
            Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY_THROW );
771
0
            Reference< beans::XPropertySetInfo > xInfo( xProp->getPropertySetInfo());
772
0
            bResult =
773
0
                ( xInfo.is() && xInfo->hasPropertyByName( aXMLRangePropName ) &&
774
0
                  ( xProp->getPropertyValue( aXMLRangePropName ) >>= rOutXMLRange ) &&
775
0
                  !rOutXMLRange.isEmpty());
776
            // clear the property after usage
777
0
            if( bClearProp && bResult )
778
0
                xProp->setPropertyValue( aXMLRangePropName, uno::Any( OUString()));
779
0
        }
780
0
        catch( const uno::Exception & )
781
0
        {
782
0
            DBG_UNHANDLED_EXCEPTION("xmloff.chart");
783
0
        }
784
0
    }
785
0
    return bResult;
786
0
}
787
788
void copyProperties(
789
    const Reference< beans::XPropertySet > & xSource,
790
    const Reference< beans::XPropertySet > & xDestination )
791
0
{
792
0
    if( ! (xSource.is() && xDestination.is()) )
793
0
        return;
794
795
0
    try
796
0
    {
797
0
        Reference< beans::XPropertySetInfo > xSrcInfo( xSource->getPropertySetInfo(), uno::UNO_SET_THROW );
798
0
        Reference< beans::XPropertySetInfo > xDestInfo( xDestination->getPropertySetInfo(), uno::UNO_SET_THROW );
799
0
        const Sequence< beans::Property > aProperties( xSrcInfo->getProperties());
800
0
        for( const auto& rProperty : aProperties )
801
0
        {
802
0
            OUString aName( rProperty.Name);
803
0
            if( xDestInfo->hasPropertyByName( aName ))
804
0
            {
805
0
                beans::Property aProp( xDestInfo->getPropertyByName( aName ));
806
0
                if( (aProp.Attributes & beans::PropertyAttribute::READONLY) == 0 )
807
0
                    xDestination->setPropertyValue(
808
0
                        aName, xSource->getPropertyValue( aName ));
809
0
            }
810
0
        }
811
0
    }
812
0
    catch( const uno::Exception & )
813
0
    {
814
0
        SAL_WARN("xmloff.chart", "Copying property sets failed!" );
815
0
    }
816
0
}
817
818
bool switchBackToDataProviderFromParent( const Reference< chart2::XChartDocument >& xChartDoc, const tSchXMLLSequencesPerIndex & rLSequencesPerIndex )
819
0
{
820
    //return whether the switch is successful
821
0
    if( !xChartDoc.is() || !xChartDoc->hasInternalDataProvider() )
822
0
        return false;
823
0
    Reference< chart2::data::XDataProvider > xDataProviderFromParent( SchXMLTools::getDataProviderFromParent( xChartDoc ) );
824
0
    if( !xDataProviderFromParent.is() )
825
0
        return false;
826
0
    uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartDoc, uno::UNO_QUERY );
827
0
    if( !xDataReceiver.is() )
828
0
        return false;
829
830
0
    xDataReceiver->attachDataProvider( xDataProviderFromParent );
831
832
0
    for( const auto& rLSeq : rLSequencesPerIndex )
833
0
    {
834
0
        Reference< chart2::data::XLabeledDataSequence > xLabeledSeq( rLSeq.second );
835
0
        if( !xLabeledSeq.is() )
836
0
            continue;
837
0
        Reference< chart2::data::XDataSequence > xNewSeq;
838
0
        xNewSeq = lcl_createNewSequenceFromCachedXMLRange( xLabeledSeq->getValues(), xDataProviderFromParent );
839
0
        if( xNewSeq.is() )
840
0
            xLabeledSeq->setValues( xNewSeq );
841
0
        xNewSeq = lcl_createNewSequenceFromCachedXMLRange( xLabeledSeq->getLabel(), xDataProviderFromParent );
842
0
        if( xNewSeq.is() )
843
0
            xLabeledSeq->setLabel( xNewSeq );
844
0
    }
845
0
    return true;
846
0
}
847
848
void setBuildIDAtImportInfo( const uno::Reference< frame::XModel >& xModel, const Reference< beans::XPropertySet >& xImportInfo )
849
0
{
850
0
    OUString aGenerator( lcl_getGeneratorFromModelOrItsParent(xModel) );
851
0
    if( !aGenerator.isEmpty() )
852
0
        SvXMLMetaDocumentContext::setBuildId( aGenerator, xImportInfo );
853
0
}
854
855
bool isDocumentGeneratedWithOpenOfficeOlderThan3_3( const uno::Reference< frame::XModel >& xChartModel )
856
0
{
857
0
    bool bResult = isDocumentGeneratedWithOpenOfficeOlderThan3_0( xChartModel );
858
0
    if( !bResult )
859
0
    {
860
0
        OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
861
0
        if( aGenerator.indexOf( "OpenOffice.org_project/3" ) != -1 )
862
0
        {
863
0
            if( aGenerator.indexOf( "OpenOffice.org_project/300m" ) != -1 )
864
0
            {
865
0
                sal_Int32 nBuilId = lcl_getBuildIDFromGenerator( lcl_getGeneratorFromModel(xChartModel) );
866
0
                if( nBuilId>0 && nBuilId<9491 ) //9491 is build id of dev300m76
867
0
                    bResult= true;
868
0
            }
869
0
            else if( aGenerator.indexOf( "OpenOffice.org_project/310m" ) != -1 )
870
0
                bResult= true;
871
0
            else if( aGenerator.indexOf( "OpenOffice.org_project/320m" ) != -1 )
872
0
                bResult= true;
873
0
        }
874
0
    }
875
0
    return bResult;
876
0
}
877
878
bool isDocumentGeneratedWithOpenOfficeOlderThan3_0( const uno::Reference< frame::XModel >& xChartModel )
879
0
{
880
0
    bool bResult = isDocumentGeneratedWithOpenOfficeOlderThan2_3( xChartModel );
881
0
    if( !bResult )
882
0
    {
883
0
        OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
884
0
        if( aGenerator.indexOf( "OpenOffice.org_project/680m" ) != -1 )
885
0
            bResult= true;
886
0
    }
887
0
    return bResult;
888
0
}
889
890
bool isDocumentGeneratedWithOpenOfficeOlderThan2_4( const uno::Reference< frame::XModel >& xChartModel )
891
0
{
892
0
    if( isDocumentGeneratedWithOpenOfficeOlderThan2_3( xChartModel ) )
893
0
        return true;
894
895
0
    if( isDocumentGeneratedWithOpenOfficeOlderThan3_0( xChartModel ) )
896
0
    {
897
0
        sal_Int32 nBuilId = lcl_getBuildIDFromGenerator( lcl_getGeneratorFromModel(xChartModel) );
898
0
        if( nBuilId>0 && nBuilId<=9238 ) //9238 is build id of OpenOffice.org 2.3.1
899
0
            return true;
900
0
    }
901
0
    return false;
902
0
}
903
904
bool isDocumentGeneratedWithOpenOfficeOlderThan2_3( const uno::Reference< frame::XModel >& xChartModel )
905
0
{
906
0
    bool bResult = false;
907
0
    OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
908
    //if there is a meta stream at the chart object it was not written with an older OpenOffice version < 2.3
909
0
    if( aGenerator.isEmpty() )
910
0
    {
911
        //if there is no meta stream at the chart object we need to check whether the parent document is OpenOffice at all
912
0
        uno::Reference< container::XChild > xChild( xChartModel, uno::UNO_QUERY );
913
0
        if( xChild.is() )
914
0
        {
915
0
            aGenerator = lcl_getGeneratorFromModel( uno::Reference< frame::XModel >( xChild->getParent(), uno::UNO_QUERY) );
916
0
            if( aGenerator.indexOf( "OpenOffice.org_project" ) != -1 )
917
0
            {
918
                //the chart application has not created files without a meta stream since OOo 2.3 (OOo 2.3 has written a metastream already)
919
                //only the report builder extension has created some files with OOo 3.1 that do not have a meta stream
920
0
                if( aGenerator.indexOf( "OpenOffice.org_project/31" ) != -1 )
921
0
                    bResult = false;//#i100102# probably generated with OOo 3.1 by the report designer
922
0
                else
923
0
                    bResult= true; //in this case the OLE chart was created by an older version, as OLE objects are sometimes stream copied the version can differ from the parents version, so the parents version is not a reliable indicator
924
0
            }
925
0
            else if( isDocumentGeneratedWithOpenOfficeOlderThan2_0(xChartModel) )
926
0
                bResult= true;
927
0
        }
928
0
    }
929
0
    return bResult;
930
0
}
931
932
bool isDocumentGeneratedWithOpenOfficeOlderThan2_0( const css::uno::Reference< css::frame::XModel >& xChartModel)
933
0
{
934
0
    bool bResult = false;
935
0
    OUString aGenerator( lcl_getGeneratorFromModelOrItsParent(xChartModel) );
936
0
    if(    aGenerator.startsWith( "OpenOffice.org 1" )
937
0
        || aGenerator.startsWith( "StarOffice 6" )
938
0
        || aGenerator.startsWith( "StarOffice 7" )
939
0
        || aGenerator.startsWith( "StarSuite 6" )
940
0
        || aGenerator.startsWith( "StarSuite 7" )
941
0
        )
942
0
        bResult= true;
943
0
    return bResult;
944
0
}
945
946
Reference< chart2::data::XDataProvider > getDataProviderFromParent( const Reference< chart2::XChartDocument >& xChartDoc )
947
0
{
948
0
    Reference< chart2::data::XDataProvider > xRet;
949
0
    uno::Reference< container::XChild > xChild( xChartDoc, uno::UNO_QUERY );
950
0
    if( xChild.is() )
951
0
    {
952
0
        Reference< lang::XMultiServiceFactory > xFact( xChild->getParent(), uno::UNO_QUERY );
953
0
        if( xFact.is() )
954
0
        {
955
0
            static constexpr OUString aDataProviderServiceName( u"com.sun.star.chart2.data.DataProvider"_ustr);
956
0
            const uno::Sequence< OUString > aServiceNames( xFact->getAvailableServiceNames());
957
0
            const OUString * pBegin = aServiceNames.getConstArray();
958
0
            const OUString * pEnd = pBegin + aServiceNames.getLength();
959
0
            if( ::std::find( pBegin, pEnd, aDataProviderServiceName ) != pEnd )
960
0
            {
961
0
                xRet.set( xFact->createInstance( aDataProviderServiceName ), uno::UNO_QUERY );
962
0
            }
963
0
        }
964
0
    }
965
0
    return xRet;
966
0
}
967
968
} // namespace SchXMLTools
969
970
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */