1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/xls/excelchartconverter.hxx"
25 
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/chart2/data/XDataProvider.hpp>
28 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
29 #include "oox/core/filterbase.hxx"
30 #include "oox/drawingml/chart/datasourcemodel.hxx"
31 #include "oox/helper/containerhelper.hxx"
32 #include "oox/xls/formulaparser.hxx"
33 
34 namespace oox {
35 namespace xls {
36 
37 // ============================================================================
38 
39 using namespace ::com::sun::star::chart2;
40 using namespace ::com::sun::star::chart2::data;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::table;
43 using namespace ::com::sun::star::uno;
44 
45 using ::oox::drawingml::chart::DataSequenceModel;
46 using ::rtl::OUString;
47 
48 // ============================================================================
49 
ExcelChartConverter(const WorkbookHelper & rHelper)50 ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
51     WorkbookHelper( rHelper )
52 {
53 }
54 
~ExcelChartConverter()55 ExcelChartConverter::~ExcelChartConverter()
56 {
57 }
58 
createDataProvider(const Reference<XChartDocument> & rxChartDoc)59 void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
60 {
61     try
62     {
63         Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
64         Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
65             CREATE_OUSTRING( "com.sun.star.chart2.data.DataProvider" ) ), UNO_QUERY_THROW );
66         xDataRec->attachDataProvider( xDataProv );
67     }
68     catch( Exception& )
69     {
70     }
71 }
72 
createDataSequence(const Reference<XDataProvider> & rxDataProvider,const DataSequenceModel & rDataSeq)73 Reference< XDataSequence > ExcelChartConverter::createDataSequence(
74         const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
75 {
76     Reference< XDataSequence > xDataSeq;
77     if( rxDataProvider.is() )
78     {
79         OUString aRangeRep;
80         if( rDataSeq.maFormula.getLength() > 0 )
81         {
82             // parse the formula string, create a token sequence
83             FormulaParser& rParser = getFormulaParser();
84             CellAddress aBaseAddr( getCurrentSheetIndex(), 0, 0 );
85             ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
86 
87             // create a range list from the token sequence
88             ApiCellRangeList aRanges;
89             rParser.extractCellRangeList( aRanges, aTokens, false );
90             aRangeRep = rParser.generateApiRangeListString( aRanges );
91         }
92         else if( !rDataSeq.maData.empty() )
93         {
94             // create a single-row array from constant source data
95             Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
96             Matrix< Any >::iterator aMIt = aMatrix.begin();
97             // TODO: how to handle missing values in the map?
98             for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
99                 *aMIt = aDIt->second;
100             aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
101         }
102 
103         if( aRangeRep.getLength() > 0 ) try
104         {
105             // create the data sequence
106             xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
107         }
108         catch( Exception& )
109         {
110             OSL_ENSURE( false, "ExcelChartConverter::createDataSequence - cannot create data sequence" );
111         }
112     }
113     return xDataSeq;
114 }
115 
116 // ============================================================================
117 
118 } // namespace xls
119 } // namespace oox
120