LCOV - code coverage report
Current view: top level - oox/source/drawingml/chart - chartconverter.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 57 61 93.4 %
Date: 2014-11-03 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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 "oox/drawingml/chart/chartconverter.hxx"
      21             : 
      22             : #include <com/sun/star/chart2/XChartDocument.hpp>
      23             : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
      24             : #include <com/sun/star/util/XNumberFormatsSupplier.hpp>
      25             : #include "drawingml/chart/chartspaceconverter.hxx"
      26             : #include "drawingml/chart/chartspacemodel.hxx"
      27             : #include "oox/helper/containerhelper.hxx"
      28             : #include "oox/core/xmlfilterbase.hxx"
      29             : 
      30             : using ::oox::drawingml::chart::DataSequenceModel;
      31             : using ::com::sun::star::uno::Any;
      32             : namespace oox {
      33             : namespace drawingml {
      34             : namespace chart {
      35             : 
      36             : using namespace ::com::sun::star;
      37             : using namespace ::com::sun::star::chart2;
      38             : using namespace ::com::sun::star::chart2::data;
      39             : using namespace ::com::sun::star::drawing;
      40             : using namespace ::com::sun::star::uno;
      41             : 
      42             : using ::oox::core::XmlFilterBase;
      43             : 
      44             : static const sal_Unicode API_TOKEN_ARRAY_OPEN      = '{';
      45             : static const sal_Unicode API_TOKEN_ARRAY_CLOSE     = '}';
      46             : static const sal_Unicode API_TOKEN_ARRAY_ROWSEP    = '|';
      47             : static const sal_Unicode API_TOKEN_ARRAY_COLSEP    = ';';
      48             : 
      49             : // Code similar to oox/source/xls/formulabase.cxx
      50        1482 : static OUString lclGenerateApiString( const OUString& rString )
      51             : {
      52        1482 :     OUString aRetString = rString;
      53        1482 :     sal_Int32 nQuotePos = aRetString.getLength();
      54        2980 :     while( (nQuotePos = aRetString.lastIndexOf( '"', nQuotePos )) >= 0 )
      55          16 :         aRetString = aRetString.replaceAt( nQuotePos, 1, "\"\"" );
      56        1482 :     return OUStringBuffer().append( '"' ).append( aRetString ).append( '"' ).makeStringAndClear();
      57             : }
      58             : 
      59        1086 : static OUString lclGenerateApiArray( const Matrix< Any >& rMatrix )
      60             : {
      61             :     OSL_ENSURE( !rMatrix.empty(), "ChartConverter::lclGenerateApiArray - missing matrix values" );
      62        1086 :     OUStringBuffer aBuffer;
      63        1086 :     aBuffer.append( API_TOKEN_ARRAY_OPEN );
      64        2172 :     for( size_t nRow = 0, nHeight = rMatrix.height(); nRow < nHeight; ++nRow )
      65             :     {
      66        1086 :         if( nRow > 0 )
      67           0 :             aBuffer.append( API_TOKEN_ARRAY_ROWSEP );
      68        4718 :         for( Matrix< Any >::const_iterator aBeg = rMatrix.row_begin( nRow ), aIt = aBeg, aEnd = rMatrix.row_end( nRow ); aIt != aEnd; ++aIt )
      69             :         {
      70        3632 :             double fValue = 0.0;
      71        3632 :             OUString aString;
      72        3632 :             if( aIt != aBeg )
      73        2546 :                 aBuffer.append( API_TOKEN_ARRAY_COLSEP );
      74        3632 :             if( *aIt >>= fValue )
      75        2150 :                 aBuffer.append( fValue );
      76        1482 :             else if( *aIt >>= aString )
      77        1482 :                 aBuffer.append( lclGenerateApiString( aString ) );
      78             :             else
      79           0 :                 aBuffer.appendAscii( "\"\"" );
      80        3632 :         }
      81             :     }
      82        1086 :     aBuffer.append( API_TOKEN_ARRAY_CLOSE );
      83        1086 :     return aBuffer.makeStringAndClear();
      84             : }
      85             : 
      86        1756 : ChartConverter::ChartConverter()
      87             : {
      88        1756 : }
      89             : 
      90        3384 : ChartConverter::~ChartConverter()
      91             : {
      92        3384 : }
      93             : 
      94         224 : void ChartConverter::convertFromModel( XmlFilterBase& rFilter,
      95             :         ChartSpaceModel& rChartModel, const Reference< XChartDocument >& rxChartDoc,
      96             :         const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos, const awt::Size& rChartSize )
      97             : {
      98             :     OSL_ENSURE( rxChartDoc.is(), "ChartConverter::convertFromModel - missing chart document" );
      99         224 :     if( rxChartDoc.is() )
     100             :     {
     101         224 :         Reference< data::XDataReceiver > xDataReceiver( rxChartDoc, uno::UNO_QUERY_THROW );
     102         448 :         Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( rFilter.getModel(), uno::UNO_QUERY );
     103         224 :         if (xNumberFormatsSupplier.is())
     104         206 :             xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
     105             : 
     106         448 :         ConverterRoot aConvBase( rFilter, *this, rChartModel, rxChartDoc, rChartSize );
     107         448 :         ChartSpaceConverter aSpaceConv( aConvBase, rChartModel );
     108         448 :         aSpaceConv.convertFromModel( rxExternalPage, rChartPos );
     109             :     }
     110         224 : }
     111             : 
     112         190 : void ChartConverter::createDataProvider( const Reference< XChartDocument >& rxChartDoc )
     113             : {
     114             :     try
     115             :     {
     116         190 :         if( !rxChartDoc->hasInternalDataProvider() )
     117         190 :             rxChartDoc->createInternalDataProvider( sal_False );
     118             :     }
     119           0 :     catch( Exception& )
     120             :     {
     121             :     }
     122         190 : }
     123             : 
     124        1092 : Reference< XDataSequence > ChartConverter::createDataSequence(
     125             :     const Reference< XDataProvider >& rxDataProvider, const DataSequenceModel& rDataSeq,
     126             :     const OUString& rRole )
     127             : {
     128        1092 :     Reference< XDataSequence > xDataSeq;
     129        1092 :     if( rxDataProvider.is() )
     130             :     {
     131        1092 :         OUString aRangeRep;
     132        1092 :         if( !rDataSeq.maData.empty() )
     133             :         {
     134             :             // create a single-row array from constant source data
     135        1086 :             Matrix< Any > aMatrix( rDataSeq.maData.size(), 1 );
     136        1086 :             Matrix< Any >::iterator aMIt = aMatrix.begin();
     137             :             // TODO: how to handle missing values in the map?
     138        4718 :             for( DataSequenceModel::AnyMap::const_iterator aDIt = rDataSeq.maData.begin(), aDEnd = rDataSeq.maData.end(); aDIt != aDEnd; ++aDIt, ++aMIt )
     139        3632 :                 *aMIt = aDIt->second;
     140        1086 :             aRangeRep = lclGenerateApiArray( aMatrix );
     141             :         }
     142             : 
     143        1092 :         if( !aRangeRep.isEmpty() ) try
     144             :         {
     145             :             // create the data sequence
     146        1086 :             xDataSeq = rxDataProvider->createDataSequenceByValueArray(rRole, aRangeRep);
     147        1086 :             return xDataSeq;
     148             :         }
     149           0 :         catch( Exception& )
     150             :         {
     151             :             OSL_FAIL( "ChartConverter::createDataSequence - cannot create data sequence" );
     152           6 :         }
     153             :     }
     154             : 
     155           6 :     return 0;
     156             : }
     157             : 
     158             : } // namespace chart
     159             : } // namespace drawingml
     160         408 : } // namespace oox
     161             : 
     162             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10