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 :
21 : #include "SchXMLTools.hxx"
22 :
23 : #include <rtl/ustrbuf.hxx>
24 : #include <comphelper/InlineContainer.hxx>
25 : // header for class SvXMLUnitConverter
26 : #include <xmloff/xmluconv.hxx>
27 : // header for struct SvXMLEnumMapEntry
28 : #include <xmloff/xmlement.hxx>
29 : #include <tools/solar.h>
30 :
31 : // header for class SvXMLImportPropertyMapper
32 : #include <xmloff/xmlimppr.hxx>
33 : // header for class XMLPropStyleContext
34 : #include <xmloff/prstylei.hxx>
35 : // header for class XMLPropertySetMapper
36 : #include <xmloff/xmlprmap.hxx>
37 : #include <xmloff/xmlexp.hxx>
38 : #include "xmloff/xmlnmspe.hxx"
39 : #include <xmloff/xmlmetai.hxx>
40 :
41 : #include <com/sun/star/beans/PropertyAttribute.hpp>
42 : #include <com/sun/star/uno/XComponentContext.hpp>
43 : #include <com/sun/star/chart2/data/XDataProvider.hpp>
44 : #include <com/sun/star/chart2/data/XDataReceiver.hpp>
45 : #include <com/sun/star/chart2/data/XRangeXMLConversion.hpp>
46 : #include <com/sun/star/chart2/XChartDocument.hpp>
47 : #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
48 : #include <com/sun/star/chart2/XRegressionCurveContainer.hpp>
49 : #include <com/sun/star/container/XChild.hpp>
50 : #include <com/sun/star/document/XDocumentProperties.hpp>
51 : #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
52 : #include <com/sun/star/lang/XServiceName.hpp>
53 :
54 : #include <comphelper/processfactory.hxx>
55 :
56 : using namespace com::sun::star;
57 : using namespace ::xmloff::token;
58 :
59 : using ::rtl::OUString;
60 : using ::rtl::OUStringBuffer;
61 : using ::com::sun::star::uno::Reference;
62 : using ::com::sun::star::uno::Sequence;
63 :
64 : namespace
65 : {
66 :
67 0 : rtl::OUString lcl_getGeneratorFromModel( const uno::Reference< frame::XModel >& xChartModel )
68 : {
69 0 : ::rtl::OUString aGenerator;
70 0 : uno::Reference< document::XDocumentPropertiesSupplier> xChartDocumentPropertiesSupplier( xChartModel, uno::UNO_QUERY );
71 0 : if( xChartDocumentPropertiesSupplier.is() )
72 : {
73 : uno::Reference< document::XDocumentProperties > xChartDocumentProperties(
74 0 : xChartDocumentPropertiesSupplier->getDocumentProperties());
75 0 : if( xChartDocumentProperties.is() )
76 0 : aGenerator = xChartDocumentProperties->getGenerator();
77 : }
78 0 : return aGenerator;
79 : }
80 :
81 0 : rtl::OUString lcl_getGeneratorFromModelOrItsParent( const uno::Reference< frame::XModel >& xChartModel )
82 : {
83 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
84 0 : if( aGenerator.isEmpty() ) //try to get the missing info from the parent document
85 : {
86 0 : uno::Reference< container::XChild > xChild( xChartModel, uno::UNO_QUERY );
87 0 : if( xChild.is() )
88 0 : aGenerator = lcl_getGeneratorFromModel( uno::Reference< frame::XModel >( xChild->getParent(), uno::UNO_QUERY) );
89 : }
90 0 : return aGenerator;
91 : }
92 :
93 0 : sal_Int32 lcl_getBuildIDFromGenerator( const ::rtl::OUString& rGenerator )
94 : {
95 : //returns -1 if nothing found
96 0 : sal_Int32 nBuildId = -1;
97 0 : const OUString sBuildCompare( "$Build-" );
98 0 : sal_Int32 nEnd = -1;
99 0 : sal_Int32 nBegin = rGenerator.indexOf( sBuildCompare, nEnd );
100 0 : if( nBegin != -1 )
101 : {
102 0 : OUString sBuildId( rGenerator.copy( nBegin + sBuildCompare.getLength() ) );
103 0 : nBuildId = sBuildId.toInt32();
104 : }
105 0 : return nBuildId;
106 : }
107 :
108 0 : OUString lcl_ConvertRange( const ::rtl::OUString & rRange, const Reference< chart2::data::XDataProvider >& xDataProvider )
109 : {
110 0 : OUString aResult = rRange;
111 0 : Reference< chart2::data::XRangeXMLConversion > xRangeConversion( xDataProvider, uno::UNO_QUERY );
112 0 : if( xRangeConversion.is())
113 0 : aResult = xRangeConversion->convertRangeFromXML( rRange );
114 0 : return aResult;
115 : }
116 :
117 0 : Reference< chart2::data::XDataSequence > lcl_createNewSequenceFromCachedXMLRange( const Reference< chart2::data::XDataSequence >& xSeq, const Reference< chart2::data::XDataProvider >& xDataProvider )
118 : {
119 0 : Reference< chart2::data::XDataSequence > xRet;
120 0 : OUString aRange;
121 0 : if( xSeq.is() && SchXMLTools::getXMLRangePropertyFromDataSequence( xSeq, aRange, /* bClearProp = */ true ) )
122 : {
123 0 : xRet.set( xDataProvider->createDataSequenceByRangeRepresentation(
124 0 : lcl_ConvertRange( aRange, xDataProvider )) );
125 : SchXMLTools::copyProperties( Reference< beans::XPropertySet >( xSeq, uno::UNO_QUERY ),
126 0 : Reference< beans::XPropertySet >( xRet, uno::UNO_QUERY ));
127 : }
128 0 : return xRet;
129 : }
130 :
131 : } // anonymous namespace
132 :
133 : // ----------------------------------------
134 :
135 : namespace SchXMLTools
136 : {
137 :
138 : static SvXMLEnumMapEntry aXMLChartClassMap[] =
139 : {
140 : { XML_LINE, XML_CHART_CLASS_LINE },
141 : { XML_AREA, XML_CHART_CLASS_AREA },
142 : { XML_CIRCLE, XML_CHART_CLASS_CIRCLE },
143 : { XML_RING, XML_CHART_CLASS_RING },
144 : { XML_SCATTER, XML_CHART_CLASS_SCATTER },
145 : { XML_RADAR, XML_CHART_CLASS_RADAR },
146 : { XML_FILLED_RADAR, XML_CHART_CLASS_FILLED_RADAR },
147 : { XML_BAR, XML_CHART_CLASS_BAR },
148 : { XML_STOCK, XML_CHART_CLASS_STOCK },
149 : { XML_BUBBLE, XML_CHART_CLASS_BUBBLE },
150 : { XML_SURFACE, XML_CHART_CLASS_BAR }, //@todo change this if a surface chart is available
151 : { XML_ADD_IN, XML_CHART_CLASS_ADDIN },
152 : { XML_TOKEN_INVALID, XML_CHART_CLASS_UNKNOWN }
153 : };
154 :
155 0 : SchXMLChartTypeEnum GetChartTypeEnum( const OUString& rClassName )
156 : {
157 0 : sal_uInt16 nEnumVal = XML_CHART_CLASS_UNKNOWN;
158 0 : if( !SvXMLUnitConverter::convertEnum(
159 0 : nEnumVal, rClassName, aXMLChartClassMap ) )
160 0 : nEnumVal = XML_CHART_CLASS_UNKNOWN;
161 0 : return SchXMLChartTypeEnum(nEnumVal);
162 : }
163 :
164 : typedef ::comphelper::MakeMap< ::rtl::OUString, ::rtl::OUString > tMakeStringStringMap;
165 : //static
166 0 : const tMakeStringStringMap& lcl_getChartTypeNameMap()
167 : {
168 : //shape property -- chart model object property
169 : static tMakeStringStringMap g_aChartTypeNameMap =
170 : tMakeStringStringMap
171 : ( ::rtl::OUString( "com.sun.star.chart.LineDiagram" )
172 : , ::rtl::OUString( "com.sun.star.chart2.LineChartType" ) )
173 :
174 : ( ::rtl::OUString( "com.sun.star.chart.AreaDiagram" )
175 0 : , ::rtl::OUString( "com.sun.star.chart2.AreaChartType" ) )
176 :
177 : ( ::rtl::OUString( "com.sun.star.chart.BarDiagram" )
178 0 : , ::rtl::OUString( "com.sun.star.chart2.ColumnChartType" ) )
179 :
180 : ( ::rtl::OUString( "com.sun.star.chart.PieDiagram" )
181 0 : , ::rtl::OUString( "com.sun.star.chart2.PieChartType" ) )
182 :
183 : ( ::rtl::OUString( "com.sun.star.chart.DonutDiagram" )
184 0 : , ::rtl::OUString( "com.sun.star.chart2.DonutChartType" ) )
185 :
186 : ( ::rtl::OUString( "com.sun.star.chart.XYDiagram" )
187 0 : , ::rtl::OUString( "com.sun.star.chart2.ScatterChartType" ) )
188 :
189 : ( ::rtl::OUString( "com.sun.star.chart.NetDiagram" )
190 0 : , ::rtl::OUString( "com.sun.star.chart2.NetChartType" ) )
191 :
192 : ( ::rtl::OUString( "com.sun.star.chart.FilledNetDiagram" )
193 0 : , ::rtl::OUString( "com.sun.star.chart2.FilledNetChartType" ) )
194 :
195 : ( ::rtl::OUString( "com.sun.star.chart.StockDiagram" )
196 0 : , ::rtl::OUString( "com.sun.star.chart2.CandleStickChartType" ) )
197 :
198 : ( ::rtl::OUString( "com.sun.star.chart.BubbleDiagram" )
199 0 : , ::rtl::OUString( "com.sun.star.chart2.BubbleChartType" ) )
200 :
201 : ;
202 0 : return g_aChartTypeNameMap;
203 : }
204 :
205 :
206 0 : OUString GetNewChartTypeName( const OUString & rOldChartTypeName )
207 : {
208 0 : OUString aNew(rOldChartTypeName);
209 :
210 0 : const tMakeStringStringMap& rMap = lcl_getChartTypeNameMap();
211 0 : tMakeStringStringMap::const_iterator aIt( rMap.find( rOldChartTypeName ));
212 0 : if( aIt != rMap.end())
213 : {
214 0 : aNew = aIt->second;
215 : }
216 0 : return aNew;
217 : }
218 :
219 0 : OUString GetChartTypeByClassName(
220 : const OUString & rClassName, bool bUseOldNames )
221 : {
222 0 : OUStringBuffer aResultBuffer;
223 0 : bool bInternalType = false;
224 :
225 0 : if( bUseOldNames )
226 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("com.sun.star.chart."));
227 : else
228 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("com.sun.star.chart2."));
229 :
230 0 : bInternalType = true;
231 :
232 0 : if( IsXMLToken( rClassName, XML_LINE ))
233 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Line"));
234 0 : else if( IsXMLToken( rClassName, XML_AREA ))
235 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Area"));
236 0 : else if( IsXMLToken( rClassName, XML_BAR ))
237 : {
238 0 : if( bUseOldNames )
239 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Bar"));
240 : else
241 : {
242 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Column"));
243 : // @todo: might be Bar
244 : }
245 : }
246 0 : else if( IsXMLToken( rClassName, XML_CIRCLE ))
247 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Pie"));
248 0 : else if( IsXMLToken( rClassName, XML_RING ))
249 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Donut"));
250 0 : else if( IsXMLToken( rClassName, XML_SCATTER ))
251 : {
252 0 : if( bUseOldNames )
253 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("XY"));
254 : else
255 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Scatter"));
256 : }
257 :
258 0 : else if( IsXMLToken( rClassName, XML_BUBBLE ))
259 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Bubble"));
260 0 : else if( IsXMLToken( rClassName, XML_RADAR ))
261 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Net"));
262 0 : else if( IsXMLToken( rClassName, XML_FILLED_RADAR ))
263 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("FilledNet"));
264 0 : else if( IsXMLToken( rClassName, XML_STOCK ))
265 : {
266 0 : if( bUseOldNames )
267 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Stock"));
268 : else
269 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("CandleStick"));
270 : }
271 0 : else if( IsXMLToken( rClassName, XML_SURFACE ))
272 : {
273 : //@todo change this if a surface chart is available
274 0 : if( bUseOldNames )
275 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Bar"));
276 : else
277 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Column"));
278 : }
279 : else
280 0 : bInternalType = false;
281 :
282 0 : if( ! bInternalType )
283 0 : return OUString();
284 :
285 0 : if( bUseOldNames )
286 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("Diagram"));
287 : else
288 0 : aResultBuffer.appendAscii( RTL_CONSTASCII_STRINGPARAM("ChartType"));
289 :
290 0 : return aResultBuffer.makeStringAndClear();
291 :
292 : }
293 :
294 0 : XMLTokenEnum getTokenByChartType(
295 : const OUString & rChartTypeService, bool bUseOldNames )
296 : {
297 0 : XMLTokenEnum eResult = XML_TOKEN_INVALID;
298 0 : OUString aPrefix, aPostfix;
299 :
300 0 : if( bUseOldNames )
301 : {
302 0 : aPrefix = OUString( "com.sun.star.chart.");
303 0 : aPostfix = OUString( "Diagram");
304 : }
305 : else
306 : {
307 0 : aPrefix = OUString( "com.sun.star.chart2.");
308 0 : aPostfix = OUString( "ChartType");
309 : }
310 :
311 0 : if( rChartTypeService.match( aPrefix ))
312 : {
313 0 : sal_Int32 nSkip = aPrefix.getLength();
314 : OSL_ASSERT( rChartTypeService.getLength() >= nSkip );
315 0 : sal_Int32 nTypeLength = rChartTypeService.getLength() - nSkip - aPostfix.getLength();
316 : // if postfix matches and leaves a non-empty type
317 0 : if( nTypeLength > 0 && rChartTypeService.match( aPostfix, nSkip + nTypeLength ))
318 : {
319 0 : OUString aServiceName( rChartTypeService.copy( nSkip, nTypeLength ));
320 :
321 0 : if ( aServiceName == "Line" )
322 0 : eResult = XML_LINE;
323 0 : else if ( aServiceName == "Area" )
324 0 : eResult = XML_AREA;
325 0 : else if( aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Bar")) ||
326 0 : (!bUseOldNames && aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Column"))))
327 0 : eResult = XML_BAR;
328 0 : else if ( aServiceName == "Pie" )
329 0 : eResult = XML_CIRCLE;
330 0 : else if ( aServiceName == "Donut" )
331 0 : eResult = XML_RING;
332 0 : else if( (bUseOldNames && aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("XY"))) ||
333 0 : (!bUseOldNames && aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Scatter"))))
334 0 : eResult = XML_SCATTER;
335 0 : else if ( aServiceName == "Bubble" )
336 0 : eResult = XML_BUBBLE;
337 0 : else if ( aServiceName == "Net" )
338 0 : eResult = XML_RADAR;
339 0 : else if ( aServiceName == "FilledNet" )
340 0 : eResult = XML_FILLED_RADAR;
341 0 : else if( (bUseOldNames && aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("Stock"))) ||
342 0 : (!bUseOldNames && aServiceName.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("CandleStick"))))
343 0 : eResult = XML_STOCK;
344 : }
345 : }
346 :
347 0 : if( eResult == XML_TOKEN_INVALID && !rChartTypeService.isEmpty() )
348 0 : eResult = XML_ADD_IN;
349 :
350 0 : return eResult;
351 : }
352 :
353 0 : Reference< chart2::data::XLabeledDataSequence > GetNewLabeledDataSequence()
354 : {
355 : Reference< uno::XComponentContext > xContext(
356 0 : comphelper::getProcessComponentContext() );
357 : Reference< chart2::data::XLabeledDataSequence > xResult(
358 0 : xContext->getServiceManager()->createInstanceWithContext(
359 0 : "com.sun.star.chart2.data.LabeledDataSequence", xContext ),
360 0 : uno::UNO_QUERY_THROW );
361 0 : return xResult;
362 : }
363 :
364 0 : Reference< chart2::data::XDataSequence > CreateDataSequence(
365 : const OUString & rRange,
366 : const Reference< chart2::XChartDocument >& xChartDoc )
367 : {
368 0 : Reference< chart2::data::XDataSequence > xRet;
369 :
370 0 : if( !xChartDoc.is() )
371 : {
372 : OSL_FAIL( "need a chart document" );
373 : return xRet;
374 : }
375 :
376 0 : Reference< chart2::data::XDataProvider > xDataProvider( xChartDoc->getDataProvider() );
377 0 : if( !xDataProvider.is() )
378 : {
379 : OSL_FAIL( "need a data provider" );
380 : return xRet;
381 : }
382 :
383 : try
384 : {
385 0 : xRet.set( xDataProvider->createDataSequenceByRangeRepresentation( lcl_ConvertRange( rRange, xDataProvider )));
386 0 : SchXMLTools::setXMLRangePropertyAtDataSequence( xRet, rRange );
387 : }
388 0 : catch( const lang::IllegalArgumentException & )
389 : {
390 : OSL_FAIL( "could not create data sequence" );
391 : }
392 :
393 0 : if( !xRet.is() && !xChartDoc->hasInternalDataProvider() && !rRange.isEmpty() )
394 : {
395 : //#i103911# switch to internal data in case the parent cannot provide the requested data
396 0 : xChartDoc->createInternalDataProvider( sal_True /* bCloneExistingData */ );
397 0 : xDataProvider = xChartDoc->getDataProvider();
398 : try
399 : {
400 0 : xRet.set( xDataProvider->createDataSequenceByRangeRepresentation( lcl_ConvertRange( rRange, xDataProvider )));
401 0 : SchXMLTools::setXMLRangePropertyAtDataSequence( xRet, rRange );
402 : }
403 0 : catch( const lang::IllegalArgumentException & )
404 : {
405 : OSL_FAIL( "could not create data sequence" );
406 : }
407 : }
408 0 : return xRet;
409 : }
410 :
411 0 : void CreateCategories(
412 : const uno::Reference< chart2::data::XDataProvider > & xDataProvider,
413 : const uno::Reference< chart2::XChartDocument > & xNewDoc,
414 : const OUString & rRangeAddress,
415 : sal_Int32 nCooSysIndex,
416 : sal_Int32 nDimensionIndex,
417 : tSchXMLLSequencesPerIndex * pLSequencesPerIndex )
418 : {
419 : try
420 : {
421 0 : if( xNewDoc.is() && !rRangeAddress.isEmpty())
422 : {
423 0 : if( xDataProvider.is())
424 : {
425 0 : uno::Reference< chart2::XDiagram > xDia( xNewDoc->getFirstDiagram());
426 0 : if( !xDia.is())
427 0 : return;
428 :
429 0 : uno::Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDia, uno::UNO_QUERY_THROW );
430 : uno::Sequence< uno::Reference< chart2::XCoordinateSystem > >
431 0 : aCooSysSeq( xCooSysCnt->getCoordinateSystems());
432 0 : if( nCooSysIndex < aCooSysSeq.getLength())
433 : {
434 0 : uno::Reference< chart2::XCoordinateSystem > xCooSys( aCooSysSeq[nCooSysIndex] );
435 : OSL_ASSERT( xCooSys.is());
436 0 : if( nDimensionIndex < xCooSys->getDimension() )
437 : {
438 0 : const sal_Int32 nMaxAxisIndex = xCooSys->getMaximumAxisIndexByDimension(nDimensionIndex);
439 0 : for(sal_Int32 nI=0; nI<=nMaxAxisIndex; ++nI)
440 : {
441 0 : uno::Reference< chart2::XAxis > xAxis( xCooSys->getAxisByDimension( nDimensionIndex, nI ));
442 0 : if( xAxis.is() )
443 : {
444 0 : chart2::ScaleData aData( xAxis->getScaleData());
445 : uno::Reference< chart2::data::XLabeledDataSequence > xLabeledSeq(
446 0 : GetNewLabeledDataSequence());
447 : try
448 : {
449 0 : OUString aConvertedRange( rRangeAddress );
450 0 : bool bRangeConverted = false;
451 0 : if( ! (xNewDoc->hasInternalDataProvider() && aConvertedRange == "categories"))
452 : {
453 0 : Reference< chart2::data::XRangeXMLConversion > xXMLConv( xDataProvider, uno::UNO_QUERY );
454 0 : if( xXMLConv.is())
455 : {
456 0 : aConvertedRange = xXMLConv->convertRangeFromXML( rRangeAddress );
457 0 : bRangeConverted = true;
458 0 : }
459 : }
460 : Reference< chart2::data::XDataSequence > xSeq(
461 0 : xDataProvider->createDataSequenceByRangeRepresentation( aConvertedRange ));
462 0 : xLabeledSeq->setValues( xSeq );
463 0 : if( bRangeConverted )
464 0 : setXMLRangePropertyAtDataSequence( xSeq, rRangeAddress );
465 : }
466 0 : catch( const lang::IllegalArgumentException & ex )
467 : {
468 : (void)ex; // avoid warning for pro build
469 : OSL_FAIL( ::rtl::OUStringToOString(
470 : ::rtl::OUString( "IllegalArgumentException caught, Message: " ) +
471 : ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
472 : }
473 0 : aData.Categories.set( xLabeledSeq );
474 0 : if( pLSequencesPerIndex )
475 : {
476 : // register for setting local data if external data provider is not present
477 : pLSequencesPerIndex->insert(
478 : tSchXMLLSequencesPerIndex::value_type(
479 0 : tSchXMLIndexWithPart( SCH_XML_CATEGORIES_INDEX, SCH_XML_PART_VALUES ), xLabeledSeq ));
480 : }
481 0 : xAxis->setScaleData( aData );
482 : }
483 0 : }
484 0 : }
485 0 : }
486 : }
487 : }
488 : }
489 0 : catch( uno::Exception & )
490 : {
491 : OSL_FAIL( "Exception caught while creating Categories" );
492 : }
493 : }
494 :
495 :
496 0 : uno::Any getPropertyFromContext( const rtl::OUString& rPropertyName, const XMLPropStyleContext* pPropStyleContext, const SvXMLStylesContext* pStylesCtxt )
497 : {
498 0 : uno::Any aRet;
499 0 : if( !pPropStyleContext || !pStylesCtxt )
500 0 : return aRet;
501 0 : const ::std::vector< XMLPropertyState >& rProperties = pPropStyleContext->GetProperties();
502 0 : const UniReference< XMLPropertySetMapper >& rMapper = pStylesCtxt->GetImportPropertyMapper( pPropStyleContext->GetFamily()/*XML_STYLE_FAMILY_SCH_CHART_ID*/ )->getPropertySetMapper();
503 0 : ::std::vector< XMLPropertyState >::const_iterator aEnd( rProperties.end() );
504 0 : ::std::vector< XMLPropertyState >::const_iterator aPropIter( rProperties.begin() );
505 0 : for( aPropIter = rProperties.begin(); aPropIter != aEnd; ++aPropIter )
506 : {
507 0 : sal_Int32 nIdx = aPropIter->mnIndex;
508 0 : if( nIdx == -1 )
509 0 : continue;
510 0 : OUString aPropName = rMapper->GetEntryAPIName( nIdx );
511 0 : if(rPropertyName.equals(aPropName))
512 0 : return aPropIter->maValue;
513 0 : }
514 0 : return aRet;
515 : }
516 :
517 0 : void exportText( SvXMLExport& rExport, const OUString& rText, bool bConvertTabsLFs )
518 : {
519 : SvXMLElementExport aPara( rExport, XML_NAMESPACE_TEXT,
520 0 : ::xmloff::token::GetXMLToken( ::xmloff::token::XML_P ),
521 0 : sal_True, sal_False );
522 :
523 0 : if( bConvertTabsLFs )
524 : {
525 0 : sal_Int32 nStartPos = 0;
526 0 : sal_Int32 nEndPos = rText.getLength();
527 : sal_Unicode cChar;
528 :
529 0 : for( sal_Int32 nPos = 0; nPos < nEndPos; nPos++ )
530 : {
531 0 : cChar = rText[ nPos ];
532 0 : switch( cChar )
533 : {
534 : case 0x0009: // tabulator
535 : {
536 0 : if( nPos > nStartPos )
537 0 : rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nPos - nStartPos)) );
538 0 : nStartPos = nPos + 1;
539 :
540 : SvXMLElementExport aElem( rExport, XML_NAMESPACE_TEXT,
541 0 : ::xmloff::token::GetXMLToken( ::xmloff::token::XML_TAB_STOP ),
542 0 : sal_False, sal_False );
543 : }
544 0 : break;
545 :
546 : case 0x000A: // linefeed
547 : {
548 0 : if( nPos > nStartPos )
549 0 : rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nPos - nStartPos)) );
550 0 : nStartPos = nPos + 1;
551 :
552 : SvXMLElementExport aElem( rExport, XML_NAMESPACE_TEXT,
553 0 : ::xmloff::token::GetXMLToken( ::xmloff::token::XML_LINE_BREAK ),
554 0 : sal_False, sal_False );
555 : }
556 0 : break;
557 : }
558 : }
559 0 : if( nEndPos > nStartPos )
560 : {
561 0 : if( nStartPos == 0 )
562 0 : rExport.GetDocHandler()->characters( rText );
563 : else
564 0 : rExport.GetDocHandler()->characters( rText.copy( nStartPos, (nEndPos - nStartPos)) );
565 : }
566 : }
567 : else // do not convert tabs and linefeeds (eg for numbers coming from unit converter)
568 : {
569 0 : rExport.GetDocHandler()->characters( rText );
570 0 : }
571 0 : }
572 :
573 0 : void exportRangeToSomewhere( SvXMLExport& rExport, const ::rtl::OUString& rValue )
574 : {
575 : //with issue #i366# and CWS chart20 ranges for error bars were introduced
576 : //to keep them during copy paste from calc to impress for example it
577 : //was necessary to introduce a mapping between the used ranges within calc and the data written to the local table
578 : //this is why we write this ranges here
579 :
580 : //#i113950# first the range was exported to attribute text:id, but that attribute does not allow arbitrary strings anymore within ODF 1.2
581 : //as an alternative the range info is now saved into the description at an empty group element (not very nice, but ODF conform)
582 :
583 0 : const SvtSaveOptions::ODFDefaultVersion nCurrentODFVersion( SvtSaveOptions().GetODFDefaultVersion() );
584 0 : if( nCurrentODFVersion == SvtSaveOptions::ODFVER_010 || nCurrentODFVersion == SvtSaveOptions::ODFVER_011 )
585 0 : return;//svg:desc is not allowed at draw:g in ODF1.0; but as the ranges for error bars are anyhow not allowed within ODF1.0 nor ODF1.1 we do not need the information
586 :
587 : SvXMLElementExport aEmptyShapeGroup( rExport, XML_NAMESPACE_DRAW,
588 0 : ::xmloff::token::GetXMLToken( ::xmloff::token::XML_G ),
589 0 : sal_True, sal_False );
590 : SvXMLElementExport aDescription( rExport, XML_NAMESPACE_SVG,
591 0 : ::xmloff::token::GetXMLToken( ::xmloff::token::XML_DESC ),
592 0 : sal_True, sal_False );
593 0 : rExport.GetDocHandler()->characters( rValue );
594 : }
595 :
596 0 : Reference< chart2::XRegressionCurve > getRegressionCurve(
597 : const Reference< chart2::XDataSeries > & xDataSeries )
598 : {
599 0 : Reference< chart2::XRegressionCurve > xResult;
600 :
601 0 : Reference< chart2::XRegressionCurveContainer > xRegCurveCnt( xDataSeries, uno::UNO_QUERY );
602 0 : if( xRegCurveCnt.is())
603 : {
604 : // find equation properties of first regression curve
605 : Sequence< Reference< chart2::XRegressionCurve > > aCurveSeq(
606 0 : xRegCurveCnt->getRegressionCurves() );
607 0 : for( sal_Int32 nI=0; nI<aCurveSeq.getLength(); ++nI )
608 : {
609 : // skip mean-value line
610 0 : Reference< lang::XServiceName > xServiceName( aCurveSeq[nI], uno::UNO_QUERY );
611 0 : if( xServiceName.is())
612 : {
613 0 : OUString aServiceName( xServiceName->getServiceName());
614 0 : if( aServiceName == "com.sun.star.chart2.MeanValueRegressionCurve" )
615 0 : continue;
616 : }
617 : // take first non-empty curve
618 0 : if( aCurveSeq[nI].is())
619 : {
620 0 : xResult.set( aCurveSeq[nI] );
621 : break;
622 : }
623 0 : }
624 : }
625 0 : return xResult;
626 : }
627 :
628 0 : void setXMLRangePropertyAtDataSequence(
629 : const Reference< chart2::data::XDataSequence > & xDataSequence,
630 : const OUString & rXMLRange )
631 : {
632 0 : if( !xDataSequence.is())
633 0 : return;
634 : try
635 : {
636 0 : const OUString aXMLRangePropName( "CachedXMLRange" );
637 0 : Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY_THROW );
638 0 : Reference< beans::XPropertySetInfo > xInfo( xProp->getPropertySetInfo());
639 0 : if( xInfo.is() && xInfo->hasPropertyByName( aXMLRangePropName ))
640 0 : xProp->setPropertyValue( aXMLRangePropName, uno::makeAny( rXMLRange ));
641 : }
642 0 : catch( const uno::Exception & ex )
643 : {
644 : (void)ex; // avoid warning for pro build
645 : OSL_FAIL( ::rtl::OUStringToOString(
646 : ::rtl::OUString( "Exception caught, Message: " ) +
647 : ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
648 : }
649 : }
650 :
651 0 : bool getXMLRangePropertyFromDataSequence(
652 : const Reference< chart2::data::XDataSequence > & xDataSequence,
653 : OUString & rOutXMLRange,
654 : bool bClearProp /* = false */)
655 : {
656 0 : bool bResult = false;
657 0 : if( xDataSequence.is())
658 : {
659 : try
660 : {
661 0 : const OUString aXMLRangePropName( "CachedXMLRange" );
662 0 : Reference< beans::XPropertySet > xProp( xDataSequence, uno::UNO_QUERY_THROW );
663 0 : Reference< beans::XPropertySetInfo > xInfo( xProp->getPropertySetInfo());
664 : bResult =
665 0 : ( xInfo.is() && xInfo->hasPropertyByName( aXMLRangePropName ) &&
666 0 : ( xProp->getPropertyValue( aXMLRangePropName ) >>= rOutXMLRange ) &&
667 0 : !rOutXMLRange.isEmpty());
668 : // clear the property after usage
669 0 : if( bClearProp && bResult )
670 0 : xProp->setPropertyValue( aXMLRangePropName, uno::Any( OUString()));
671 : }
672 0 : catch( const uno::Exception & ex )
673 : {
674 : (void)ex; // avoid warning for pro build
675 : OSL_FAIL( ::rtl::OUStringToOString(
676 : ::rtl::OUString( "Exception caught, Message: " ) +
677 : ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
678 : }
679 : }
680 0 : return bResult;
681 : }
682 :
683 0 : void copyProperties(
684 : const Reference< beans::XPropertySet > & xSource,
685 : const Reference< beans::XPropertySet > & xDestination )
686 : {
687 0 : if( ! (xSource.is() && xDestination.is()) )
688 0 : return;
689 :
690 : try
691 : {
692 0 : Reference< beans::XPropertySetInfo > xSrcInfo( xSource->getPropertySetInfo(), uno::UNO_QUERY_THROW );
693 0 : Reference< beans::XPropertySetInfo > xDestInfo( xDestination->getPropertySetInfo(), uno::UNO_QUERY_THROW );
694 0 : Sequence< beans::Property > aProperties( xSrcInfo->getProperties());
695 0 : const sal_Int32 nLength = aProperties.getLength();
696 0 : for( sal_Int32 i = 0; i < nLength; ++i )
697 : {
698 0 : OUString aName( aProperties[i].Name);
699 0 : if( xDestInfo->hasPropertyByName( aName ))
700 : {
701 0 : beans::Property aProp( xDestInfo->getPropertyByName( aName ));
702 0 : if( (aProp.Attributes & beans::PropertyAttribute::READONLY) == 0 )
703 0 : xDestination->setPropertyValue(
704 0 : aName, xSource->getPropertyValue( aName ));
705 : }
706 0 : }
707 : }
708 0 : catch( const uno::Exception & )
709 : {
710 : OSL_FAIL( "Copying property sets failed!" );
711 : }
712 : }
713 :
714 0 : bool switchBackToDataProviderFromParent( const Reference< chart2::XChartDocument >& xChartDoc, const tSchXMLLSequencesPerIndex & rLSequencesPerIndex )
715 : {
716 : //return whether the switch is successful
717 0 : if( !xChartDoc.is() || !xChartDoc->hasInternalDataProvider() )
718 0 : return false;
719 0 : Reference< chart2::data::XDataProvider > xDataProviderFromParent( SchXMLTools::getDataProviderFromParent( xChartDoc ) );
720 0 : if( !xDataProviderFromParent.is() )
721 0 : return false;
722 0 : uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartDoc, uno::UNO_QUERY );
723 0 : if( !xDataReceiver.is() )
724 0 : return false;
725 :
726 0 : xDataReceiver->attachDataProvider( xDataProviderFromParent );
727 :
728 0 : for( tSchXMLLSequencesPerIndex::const_iterator aLSeqIt( rLSequencesPerIndex.begin() );
729 0 : aLSeqIt != rLSequencesPerIndex.end(); ++aLSeqIt )
730 : {
731 0 : Reference< chart2::data::XLabeledDataSequence > xLabeledSeq( aLSeqIt->second );
732 0 : if( !xLabeledSeq.is() )
733 0 : continue;
734 0 : Reference< chart2::data::XDataSequence > xNewSeq;
735 0 : xNewSeq = lcl_createNewSequenceFromCachedXMLRange( xLabeledSeq->getValues(), xDataProviderFromParent );
736 0 : if( xNewSeq.is() )
737 0 : xLabeledSeq->setValues( xNewSeq );
738 0 : xNewSeq = lcl_createNewSequenceFromCachedXMLRange( xLabeledSeq->getLabel(), xDataProviderFromParent );
739 0 : if( xNewSeq.is() )
740 0 : xLabeledSeq->setLabel( xNewSeq );
741 0 : }
742 0 : return true;
743 : }
744 :
745 0 : void setBuildIDAtImportInfo( uno::Reference< frame::XModel > xModel, Reference< beans::XPropertySet > xImportInfo )
746 : {
747 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModelOrItsParent(xModel) );
748 0 : if( !aGenerator.isEmpty() )
749 0 : SvXMLMetaDocumentContext::setBuildId( aGenerator, xImportInfo );
750 0 : }
751 :
752 0 : bool isDocumentGeneratedWithOpenOfficeOlderThan3_3( const uno::Reference< frame::XModel >& xChartModel )
753 : {
754 0 : bool bResult = isDocumentGeneratedWithOpenOfficeOlderThan3_0( xChartModel );
755 0 : if( !bResult )
756 : {
757 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
758 0 : if( aGenerator.indexOf( "OpenOffice.org_project/3" ) != -1 )
759 : {
760 0 : if( aGenerator.indexOf( "OpenOffice.org_project/300m" ) != -1 )
761 : {
762 0 : sal_Int32 nBuilId = lcl_getBuildIDFromGenerator( lcl_getGeneratorFromModel(xChartModel) );
763 0 : if( nBuilId>0 && nBuilId<9491 ) //9491 is build id of dev300m76
764 0 : bResult= true;
765 : }
766 0 : else if( aGenerator.indexOf( "OpenOffice.org_project/310m" ) != -1 )
767 0 : bResult= true;
768 0 : else if( aGenerator.indexOf( "OpenOffice.org_project/320m" ) != -1 )
769 0 : bResult= true;
770 0 : }
771 : }
772 0 : return bResult;
773 : }
774 :
775 0 : bool isDocumentGeneratedWithOpenOfficeOlderThan3_0( const uno::Reference< frame::XModel >& xChartModel )
776 : {
777 0 : bool bResult = isDocumentGeneratedWithOpenOfficeOlderThan2_3( xChartModel );
778 0 : if( !bResult )
779 : {
780 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
781 0 : if( aGenerator.indexOf( "OpenOffice.org_project/680m" ) != -1 )
782 0 : bResult= true;
783 : }
784 0 : return bResult;
785 : }
786 :
787 0 : bool isDocumentGeneratedWithOpenOfficeOlderThan2_4( const uno::Reference< frame::XModel >& xChartModel )
788 : {
789 0 : if( isDocumentGeneratedWithOpenOfficeOlderThan2_3( xChartModel ) )
790 0 : return true;
791 :
792 0 : if( isDocumentGeneratedWithOpenOfficeOlderThan3_0( xChartModel ) )
793 : {
794 0 : sal_Int32 nBuilId = lcl_getBuildIDFromGenerator( lcl_getGeneratorFromModel(xChartModel) );
795 0 : if( nBuilId>0 && nBuilId<=9238 ) //9238 is build id of OpenOffice.org 2.3.1
796 0 : return true;
797 : }
798 0 : return false;
799 : }
800 :
801 0 : bool isDocumentGeneratedWithOpenOfficeOlderThan2_3( const uno::Reference< frame::XModel >& xChartModel )
802 : {
803 0 : bool bResult = false;
804 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModel(xChartModel) );
805 : //if there is a meta stream at the chart object it was not written with an older OpenOffice version < 2.3
806 0 : if( aGenerator.isEmpty() )
807 : {
808 : //if there is no meta stream at the chart object we need to check whether the parent document is OpenOffice at all
809 0 : uno::Reference< container::XChild > xChild( xChartModel, uno::UNO_QUERY );
810 0 : if( xChild.is() )
811 : {
812 0 : aGenerator = lcl_getGeneratorFromModel( uno::Reference< frame::XModel >( xChild->getParent(), uno::UNO_QUERY) );
813 0 : if( aGenerator.indexOf( "OpenOffice.org_project" ) != -1 )
814 : {
815 : //the chart application has not created files without a meta stream since OOo 2.3 (OOo 2.3 has written a metastream already)
816 : //only the report builder extension has created some files with OOo 3.1 that do not have a meta stream
817 0 : if( aGenerator.indexOf( "OpenOffice.org_project/31" ) != -1 )
818 0 : bResult = false;//#i100102# probably generated with OOo 3.1 by the report designer
819 : else
820 0 : bResult= true; //in this case the OLE chart was created by an older version, as OLE objects are sometimes stream copied the version can differ from the parents version, so the parents version is not a reliable indicator
821 : }
822 0 : else if( isDocumentGeneratedWithOpenOfficeOlderThan2_0(xChartModel) )
823 0 : bResult= true;
824 0 : }
825 : }
826 0 : return bResult;
827 : }
828 :
829 0 : bool isDocumentGeneratedWithOpenOfficeOlderThan2_0( const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& xChartModel)
830 : {
831 0 : bool bResult = false;
832 0 : ::rtl::OUString aGenerator( lcl_getGeneratorFromModelOrItsParent(xChartModel) );
833 0 : if( ( aGenerator.indexOf( "OpenOffice.org 1" ) == 0 )
834 0 : || ( aGenerator.indexOf( "StarOffice 6" ) == 0 )
835 0 : || ( aGenerator.indexOf( "StarOffice 7" ) == 0 )
836 0 : || ( aGenerator.indexOf( "StarSuite 6" ) == 0 )
837 0 : || ( aGenerator.indexOf( "StarSuite 7" ) == 0 )
838 : )
839 0 : bResult= true;
840 0 : return bResult;
841 : }
842 :
843 0 : Reference< chart2::data::XDataProvider > getDataProviderFromParent( const Reference< chart2::XChartDocument >& xChartDoc )
844 : {
845 0 : Reference< chart2::data::XDataProvider > xRet;
846 0 : uno::Reference< container::XChild > xChild( xChartDoc, uno::UNO_QUERY );
847 0 : if( xChild.is() )
848 : {
849 0 : Reference< lang::XMultiServiceFactory > xFact( xChild->getParent(), uno::UNO_QUERY );
850 0 : if( xFact.is() )
851 : {
852 0 : const OUString aDataProviderServiceName( "com.sun.star.chart2.data.DataProvider");
853 0 : const uno::Sequence< OUString > aServiceNames( xFact->getAvailableServiceNames());
854 0 : const OUString * pBegin = aServiceNames.getConstArray();
855 0 : const OUString * pEnd = pBegin + aServiceNames.getLength();
856 0 : if( ::std::find( pBegin, pEnd, aDataProviderServiceName ) != pEnd )
857 : {
858 : xRet = Reference< chart2::data::XDataProvider >(
859 0 : xFact->createInstance( aDataProviderServiceName ), uno::UNO_QUERY );
860 0 : }
861 0 : }
862 : }
863 0 : return xRet;
864 : }
865 :
866 : } // namespace SchXMLTools
867 :
868 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|