summaryrefslogtreecommitdiffstats
path: root/xmloff/source/chart/SchXMLPropertyMappingContext.cxx
blob: 688cfd752a1e6a054c71e0a7f848c095d9d54982 (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
/* -*- 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 "SchXMLPropertyMappingContext.hxx"
#include "SchXMLTools.hxx"
#include <xmloff/xmlnmspe.hxx>
#include <xmloff/xmlimp.hxx>
#include <xmloff/nmspmap.hxx>
#include <SchXMLImport.hxx>

#include <com/sun/star/chart2/data/XLabeledDataSequence2.hpp>
#include <com/sun/star/chart2/data/XDataSource.hpp>
#include <com/sun/star/chart2/data/XDataSink.hpp>
#include <com/sun/star/chart2/XChartDocument.hpp>

using namespace com::sun::star;
using namespace com::sun::star::uno;

namespace {

Reference< chart2::data::XLabeledDataSequence2 > createAndAddSequenceToSeries( const OUString& rRole
        , const OUString& rRange
        , const Reference< chart2::XChartDocument >& xChartDoc
        , const Reference< chart2::XDataSeries >& xSeries )
{
    Reference< chart2::data::XLabeledDataSequence2 > xLabeledSeq;

    Reference< chart2::data::XDataSource > xSeriesSource( xSeries,uno::UNO_QUERY );

    if( !(!rRange.isEmpty() && xChartDoc.is() && xSeriesSource.is()) )
        return xLabeledSeq;

    // create a new sequence
    xLabeledSeq = SchXMLTools::GetNewLabeledDataSequence();

    // set values at the new sequence
    Reference< chart2::data::XDataSequence > xSeq = SchXMLTools::CreateDataSequence( rRange, xChartDoc );
    Reference< beans::XPropertySet > xSeqProp( xSeq, uno::UNO_QUERY );
    if( xSeqProp.is())
        xSeqProp->setPropertyValue("Role", uno::makeAny( rRole));
    xLabeledSeq->setValues( xSeq );

    Reference< chart2::data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY );
    if( xSink.is())
    {
        Sequence< Reference< chart2::data::XLabeledDataSequence > > aData( xSeriesSource->getDataSequences());
        aData.realloc( aData.getLength() + 1 );
        aData[ aData.getLength() - 1 ] = xLabeledSeq;
        xSink->setData( aData );
    }

    return xLabeledSeq;
}

}

SchXMLPropertyMappingContext::SchXMLPropertyMappingContext( SchXMLImportHelper& rImpHelper,
        SvXMLImport& rImport, const OUString& rLocalName,
        tSchXMLLSequencesPerIndex & rLSequencesPerIndex,
        uno::Reference<
        chart2::XDataSeries > const & xSeries ):
    SvXMLImportContext( rImport, XML_NAMESPACE_LO_EXT, rLocalName ),
    mrImportHelper( rImpHelper ),
    mxDataSeries(xSeries),
    mrLSequencesPerIndex(rLSequencesPerIndex)
{

}

SchXMLPropertyMappingContext::~SchXMLPropertyMappingContext()
{
}

void SchXMLPropertyMappingContext::StartElement(const uno::Reference< xml::sax::XAttributeList>& xAttrList )
{
    OUString aRange;
    OUString aRole;
    // parse attributes
    sal_Int16 nAttrCount = xAttrList.is()? xAttrList->getLength(): 0;
    const SvXMLTokenMap& rAttrTokenMap = mrImportHelper.GetPropMappingAttrTokenMap();

    for( sal_Int16 i = 0; i < nAttrCount; i++ )
    {
        OUString sAttrName = xAttrList->getNameByIndex( i );
        OUString aLocalName;
        OUString aValue = xAttrList->getValueByIndex( i );
        sal_uInt16 nPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( sAttrName, &aLocalName );

        switch( rAttrTokenMap.Get( nPrefix, aLocalName ))
        {
            case XML_TOK_PROPERTY_MAPPING_PROPERTY:
                aRole = aValue;
                break;
            case XML_TOK_PROPERTY_MAPPING_RANGE:
                aRange = aValue;
                break;
        }
    }

    if( !aRange.isEmpty() && !aRole.isEmpty() )
    {
        Reference< chart2::XChartDocument > xChartDoc( GetImport().GetModel(), uno::UNO_QUERY );
        Reference< chart2::data::XLabeledDataSequence2 > xSeq =
            createAndAddSequenceToSeries(aRole, aRange, xChartDoc, mxDataSeries);
        mrLSequencesPerIndex.emplace(
                    tSchXMLIndexWithPart( 0, SCH_XML_PART_VALUES),
                    xSeq);
    }
}


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