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 
10 #include "SchXMLPropertyMappingContext.hxx"
11 #include "SchXMLTools.hxx"
12 #include <xmloff/xmlnamespace.hxx>
13 #include <xmloff/xmlimp.hxx>
14 #include <xmloff/namespacemap.hxx>
15 #include <SchXMLImport.hxx>
16 #include <sal/log.hxx>
17 
18 #include <com/sun/star/chart2/data/XLabeledDataSequence2.hpp>
19 #include <com/sun/star/chart2/data/XDataSource.hpp>
20 #include <com/sun/star/chart2/data/XDataSink.hpp>
21 #include <com/sun/star/chart2/XChartDocument.hpp>
22 
23 using namespace com::sun::star;
24 using namespace com::sun::star::uno;
25 using namespace ::xmloff::token;
26 
27 namespace {
28 
createAndAddSequenceToSeries(const OUString & rRole,const OUString & rRange,const Reference<chart2::XChartDocument> & xChartDoc,const Reference<chart2::XDataSeries> & xSeries)29 Reference< chart2::data::XLabeledDataSequence2 > createAndAddSequenceToSeries( const OUString& rRole
30         , const OUString& rRange
31         , const Reference< chart2::XChartDocument >& xChartDoc
32         , const Reference< chart2::XDataSeries >& xSeries )
33 {
34     Reference< chart2::data::XLabeledDataSequence2 > xLabeledSeq;
35 
36     Reference< chart2::data::XDataSource > xSeriesSource( xSeries,uno::UNO_QUERY );
37 
38     if( !(!rRange.isEmpty() && xChartDoc.is() && xSeriesSource.is()) )
39         return xLabeledSeq;
40 
41     // create a new sequence
42     xLabeledSeq = SchXMLTools::GetNewLabeledDataSequence();
43 
44     // set values at the new sequence
45     Reference< chart2::data::XDataSequence > xSeq = SchXMLTools::CreateDataSequence( rRange, xChartDoc );
46     Reference< beans::XPropertySet > xSeqProp( xSeq, uno::UNO_QUERY );
47     if( xSeqProp.is())
48         xSeqProp->setPropertyValue("Role", uno::makeAny( rRole));
49     xLabeledSeq->setValues( xSeq );
50 
51     Reference< chart2::data::XDataSink > xSink( xSeriesSource, uno::UNO_QUERY );
52     if( xSink.is())
53     {
54         Sequence< Reference< chart2::data::XLabeledDataSequence > > aData( xSeriesSource->getDataSequences());
55         aData.realloc( aData.getLength() + 1 );
56         aData[ aData.getLength() - 1 ] = xLabeledSeq;
57         xSink->setData( aData );
58     }
59 
60     return xLabeledSeq;
61 }
62 
63 }
64 
SchXMLPropertyMappingContext(SvXMLImport & rImport,tSchXMLLSequencesPerIndex & rLSequencesPerIndex,uno::Reference<chart2::XDataSeries> const & xSeries)65 SchXMLPropertyMappingContext::SchXMLPropertyMappingContext(
66         SvXMLImport& rImport,
67         tSchXMLLSequencesPerIndex & rLSequencesPerIndex,
68         uno::Reference<
69         chart2::XDataSeries > const & xSeries ):
70     SvXMLImportContext( rImport ),
71     mxDataSeries(xSeries),
72     mrLSequencesPerIndex(rLSequencesPerIndex)
73 {
74 
75 }
76 
~SchXMLPropertyMappingContext()77 SchXMLPropertyMappingContext::~SchXMLPropertyMappingContext()
78 {
79 }
80 
startFastElement(sal_Int32,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)81 void SchXMLPropertyMappingContext::startFastElement (sal_Int32 /*nElement*/,
82         const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList)
83 {
84     OUString aRange;
85     OUString aRole;
86     // parse attributes
87     for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
88     {
89         OUString aValue = aIter.toString();
90         switch( aIter.getToken() )
91         {
92             case XML_ELEMENT(LO_EXT, XML_PROPERTY):
93                 aRole = aValue;
94                 break;
95             case XML_ELEMENT(LO_EXT, XML_CELL_RANGE_ADDRESS):
96                 aRange = aValue;
97                 break;
98             default:
99                 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
100         }
101     }
102 
103     if( !aRange.isEmpty() && !aRole.isEmpty() )
104     {
105         Reference< chart2::XChartDocument > xChartDoc( GetImport().GetModel(), uno::UNO_QUERY );
106         Reference< chart2::data::XLabeledDataSequence2 > xSeq =
107             createAndAddSequenceToSeries(aRole, aRange, xChartDoc, mxDataSeries);
108         mrLSequencesPerIndex.emplace(
109                     tSchXMLIndexWithPart( 0, SCH_XML_PART_VALUES),
110                     xSeq);
111     }
112 }
113 
114 
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
116