1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/xls/excelchartconverter.hxx"
29 
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/chart2/data/XDataProvider.hpp>
32 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
33 #include "oox/core/filterbase.hxx"
34 #include "oox/drawingml/chart/datasourcemodel.hxx"
35 #include "oox/helper/containerhelper.hxx"
36 #include "oox/xls/formulaparser.hxx"
37 
38 namespace oox {
39 namespace xls {
40 
41 // ============================================================================
42 
43 using namespace ::com::sun::star::chart2;
44 using namespace ::com::sun::star::chart2::data;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::table;
47 using namespace ::com::sun::star::uno;
48 
49 using ::oox::drawingml::chart::DataSequenceModel;
50 using ::rtl::OUString;
51 
52 // ============================================================================
53 
54 ExcelChartConverter::ExcelChartConverter( const WorkbookHelper& rHelper ) :
55     WorkbookHelper( rHelper )
56 {
57 }
58 
59 ExcelChartConverter::~ExcelChartConverter()
60 {
61 }
62 
63 void ExcelChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
64 {
65     try
66     {
67         Reference< XDataReceiver > xDataRec( rxChartDoc, UNO_QUERY_THROW );
68         Reference< XDataProvider > xDataProv( getBaseFilter().getModelFactory()->createInstance(
69             CREATE_OUSTRING( "com.sun.star.chart2.data.DataProvider" ) ), UNO_QUERY_THROW );
70         xDataRec->attachDataProvider( xDataProv );
71     }
72     catch( Exception& )
73     {
74     }
75 }
76 
77 Reference< XDataSequence > ExcelChartConverter::createDataSequence(
78         const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq )
79 {
80     Reference< XDataSequence > xDataSeq;
81     if( rxDataProvider.is() )
82     {
83         OUString aRangeRep;
84         if( rDataSeq.maFormula.getLength() > 0 )
85         {
86             // parse the formula string, create a token sequence
87             FormulaParser& rParser = getFormulaParser();
88             CellAddress aBaseAddr( getCurrentSheetIndex(), 0, 0 );
89             ApiTokenSequence aTokens = rParser.importFormula( aBaseAddr, rDataSeq.maFormula );
90 
91             // create a range list from the token sequence
92             ApiCellRangeList aRanges;
93             rParser.extractCellRangeList( aRanges, aTokens, false );
94             aRangeRep = rParser.generateApiRangeListString( aRanges );
95         }
96         else if( !rDataSeq.maData.empty() )
97         {
98             // create a single-row array from constant source data
99             Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
100             Matrix< Any >::iterator aMIt = aMatrix.begin();
101             // TODO: how to handle missing values in the map?
102             for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
103                 *aMIt = aDIt->second;
104             aRangeRep = FormulaProcessorBase::generateApiArray( aMatrix );
105         }
106 
107         if( aRangeRep.getLength() > 0 ) try
108         {
109             // create the data sequence
110             xDataSeq = rxDataProvider->createDataSequenceByRangeRepresentation( aRangeRep );
111         }
112         catch( Exception& )
113         {
114             OSL_ENSURE( false, "ExcelChartConverter::createDataSequence - cannot create data sequence" );
115         }
116     }
117     return xDataSeq;
118 }
119 
120 // ============================================================================
121 
122 } // namespace xls
123 } // namespace oox
124