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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_xmloff.hxx"
26 
27 #include "SchXMLImport.hxx"
28 #include "SchXMLChartContext.hxx"
29 #include "contexts.hxx"
30 #include "XMLChartPropertySetMapper.hxx"
31 #include "SchXMLTools.hxx"
32 
33 #include <tools/debug.hxx>
34 #include <rtl/ustrbuf.hxx>
35 // header for class ByteString
36 #include <tools/string.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include "xmloff/xmlnmspe.hxx"
39 #include <xmloff/xmltoken.hxx>
40 #include <xmloff/xmluconv.hxx>
41 #include <xmloff/nmspmap.hxx>
42 #include <xmloff/xmlictxt.hxx>
43 #include <xmloff/xmlstyle.hxx>
44 #include <com/sun/star/task/XStatusIndicatorSupplier.hpp>
45 #include <com/sun/star/chart/XChartDocument.hpp>
46 #include <com/sun/star/chart/ChartDataRowSource.hpp>
47 #include <com/sun/star/container/XChild.hpp>
48 #include <com/sun/star/uno/XComponentContext.hpp>
49 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
50 #include <com/sun/star/chart2/data/XDataProvider.hpp>
51 #include <com/sun/star/chart2/XChartDocument.hpp>
52 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
53 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
54 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
55 
56 #include <com/sun/star/document/XDocumentProperties.hpp>
57 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
58 
59 #include <typeinfo>
60 
61 using namespace com::sun::star;
62 using namespace ::xmloff::token;
63 
64 using ::rtl::OUString;
65 using ::rtl::OUStringBuffer;
66 using ::rtl::OUStringToOString;
67 using ::com::sun::star::uno::Reference;
68 using ::com::sun::star::uno::Sequence;
69 
70 namespace
71 {
lcl_getComponentContext()72 Reference< uno::XComponentContext > lcl_getComponentContext()
73 {
74     Reference< uno::XComponentContext > xContext;
75     try
76     {
77         Reference< beans::XPropertySet > xFactProp( comphelper::getProcessServiceFactory(), uno::UNO_QUERY );
78         if( xFactProp.is())
79             xFactProp->getPropertyValue(OUString::createFromAscii("DefaultContext")) >>= xContext;
80     }
81     catch( uno::Exception& )
82     {}
83 
84     return xContext;
85 }
86 
87 class lcl_MatchesChartType : public ::std::unary_function< Reference< chart2::XChartType >, bool >
88 {
89 public:
lcl_MatchesChartType(const OUString & aChartTypeName)90     explicit lcl_MatchesChartType( const OUString & aChartTypeName ) :
91             m_aChartTypeName( aChartTypeName )
92     {}
93 
operator ()(const Reference<chart2::XChartType> & xChartType) const94     bool operator () ( const Reference< chart2::XChartType > & xChartType ) const
95     {
96         return (xChartType.is() &&
97                 xChartType->getChartType().equals( m_aChartTypeName ));
98     }
99 
100 private:
101     OUString m_aChartTypeName;
102 };
103 } // anonymous namespace
104 
105 /* ----------------------------------------
106    TokenMaps for distinguishing different
107    tokens in different contexts
108    ----------------------------------------*/
109 
110 // ----------------------------------------
111 // element maps
112 // ----------------------------------------
113 
114 
115 
116 
117 
118 
119 
120 // ----------------------------------------
121 // attribute maps
122 // ----------------------------------------
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 // ========================================
133 
SchXMLImportHelper()134 SchXMLImportHelper::SchXMLImportHelper() :
135 		mpAutoStyles( 0 ),
136 
137 		mpChartDocElemTokenMap( 0 ),
138 		mpTableElemTokenMap( 0 ),
139 		mpChartElemTokenMap( 0 ),
140 		mpPlotAreaElemTokenMap( 0 ),
141 		mpSeriesElemTokenMap( 0 ),
142 
143 		mpChartAttrTokenMap( 0 ),
144 		mpPlotAreaAttrTokenMap( 0 ),
145 		mpAutoStyleAttrTokenMap( 0 ),
146 		mpCellAttrTokenMap( 0 ),
147 		mpSeriesAttrTokenMap( 0 ),
148         mpRegEquationAttrTokenMap( 0 )
149 {
150 }
151 
~SchXMLImportHelper()152 SchXMLImportHelper::~SchXMLImportHelper()
153 {
154 		// delete token maps
155 	if( mpChartDocElemTokenMap )
156 		delete mpChartDocElemTokenMap;
157 	if( mpTableElemTokenMap )
158 		delete mpTableElemTokenMap;
159 	if( mpChartElemTokenMap )
160 		delete mpChartElemTokenMap;
161 	if( mpPlotAreaElemTokenMap )
162 		delete mpPlotAreaElemTokenMap;
163 	if( mpSeriesElemTokenMap )
164 		delete mpSeriesElemTokenMap;
165 
166 	if( mpChartAttrTokenMap )
167 		delete mpChartAttrTokenMap;
168 	if( mpPlotAreaAttrTokenMap )
169 		delete mpPlotAreaAttrTokenMap;
170 	if( mpAutoStyleAttrTokenMap )
171 		delete mpAutoStyleAttrTokenMap;
172 	if( mpCellAttrTokenMap )
173 		delete mpCellAttrTokenMap;
174 	if( mpSeriesAttrTokenMap )
175 		delete mpSeriesAttrTokenMap;
176 }
177 
CreateChartContext(SvXMLImport & rImport,sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<frame::XModel> xChartModel,const Reference<xml::sax::XAttributeList> &)178 SvXMLImportContext* SchXMLImportHelper::CreateChartContext(
179 	SvXMLImport& rImport,
180 	sal_uInt16 nPrefix, const OUString& rLocalName,
181 	const Reference< frame::XModel > xChartModel,
182 	const Reference< xml::sax::XAttributeList >& )
183 {
184 	SvXMLImportContext* pContext = 0;
185 
186 	Reference< chart::XChartDocument > xDoc( xChartModel, uno::UNO_QUERY );
187 	if( xDoc.is())
188 	{
189 		mxChartDoc = xDoc;
190 		pContext = new SchXMLChartContext( *this, rImport, rLocalName );
191 	}
192 	else
193 	{
194 		DBG_ERROR( "No valid XChartDocument given as XModel" );
195 		pContext = new SvXMLImportContext( rImport, nPrefix, rLocalName );
196 	}
197 
198 	return pContext;
199 }
200 
201 /* ----------------------------------------
202    get various token maps
203    ----------------------------------------*/
204 
GetDocElemTokenMap()205 const SvXMLTokenMap& SchXMLImportHelper::GetDocElemTokenMap()
206 {
207 	if( ! mpChartDocElemTokenMap )
208     {
209         static __FAR_DATA SvXMLTokenMapEntry aDocElemTokenMap[] =
210         {
211 	        { XML_NAMESPACE_OFFICE, XML_AUTOMATIC_STYLES,	XML_TOK_DOC_AUTOSTYLES	},
212 	        { XML_NAMESPACE_OFFICE, XML_STYLES,			    XML_TOK_DOC_STYLES	},
213 	        { XML_NAMESPACE_OFFICE, XML_META, 				XML_TOK_DOC_META	},
214 	        { XML_NAMESPACE_OFFICE, XML_BODY, 				XML_TOK_DOC_BODY	},
215 	        XML_TOKEN_MAP_END
216         };
217 
218 		mpChartDocElemTokenMap = new SvXMLTokenMap( aDocElemTokenMap );
219     } // if( ! mpChartDocElemTokenMap )
220 
221 	return *mpChartDocElemTokenMap;
222 }
223 
GetTableElemTokenMap()224 const SvXMLTokenMap& SchXMLImportHelper::GetTableElemTokenMap()
225 {
226 	if( ! mpTableElemTokenMap )
227     {
228         static __FAR_DATA SvXMLTokenMapEntry aTableElemTokenMap[] =
229     {
230 	    { XML_NAMESPACE_TABLE,	XML_TABLE_HEADER_COLUMNS,	XML_TOK_TABLE_HEADER_COLS	},
231 	    { XML_NAMESPACE_TABLE,	XML_TABLE_COLUMNS,			XML_TOK_TABLE_COLUMNS		},
232 	    { XML_NAMESPACE_TABLE,	XML_TABLE_COLUMN,			XML_TOK_TABLE_COLUMN		},
233 	    { XML_NAMESPACE_TABLE,	XML_TABLE_HEADER_ROWS,		XML_TOK_TABLE_HEADER_ROWS	},
234 	    { XML_NAMESPACE_TABLE,	XML_TABLE_ROWS,			    XML_TOK_TABLE_ROWS 			},
235 	    { XML_NAMESPACE_TABLE,	XML_TABLE_ROW,				XML_TOK_TABLE_ROW 			},
236 	    XML_TOKEN_MAP_END
237     };
238 
239 		mpTableElemTokenMap = new SvXMLTokenMap( aTableElemTokenMap );
240     } // if( ! mpTableElemTokenMap )
241 
242 	return *mpTableElemTokenMap;
243 }
244 
GetChartElemTokenMap()245 const SvXMLTokenMap& SchXMLImportHelper::GetChartElemTokenMap()
246 {
247 	if( ! mpChartElemTokenMap )
248     {
249         static __FAR_DATA SvXMLTokenMapEntry aChartElemTokenMap[] =
250         {
251 	        { XML_NAMESPACE_CHART,	XML_PLOT_AREA,				XML_TOK_CHART_PLOT_AREA		},
252 	        { XML_NAMESPACE_CHART,	XML_TITLE,					XML_TOK_CHART_TITLE			},
253 	        { XML_NAMESPACE_CHART,	XML_SUBTITLE,				XML_TOK_CHART_SUBTITLE		},
254 	        { XML_NAMESPACE_CHART,	XML_LEGEND,				XML_TOK_CHART_LEGEND		},
255 	        { XML_NAMESPACE_TABLE,	XML_TABLE,					XML_TOK_CHART_TABLE			},
256 	        XML_TOKEN_MAP_END
257         };
258 
259 		mpChartElemTokenMap = new SvXMLTokenMap( aChartElemTokenMap );
260     } // if( ! mpChartElemTokenMap )
261 
262 	return *mpChartElemTokenMap;
263 }
264 
GetPlotAreaElemTokenMap()265 const SvXMLTokenMap& SchXMLImportHelper::GetPlotAreaElemTokenMap()
266 {
267 	if( ! mpPlotAreaElemTokenMap )
268     {
269         static __FAR_DATA SvXMLTokenMapEntry aPlotAreaElemTokenMap[] =
270 {
271     { XML_NAMESPACE_CHART_EXT,	XML_COORDINATE_REGION,		XML_TOK_PA_COORDINATE_REGION_EXT },
272     { XML_NAMESPACE_CHART,	XML_COORDINATE_REGION,		XML_TOK_PA_COORDINATE_REGION },
273 	{ XML_NAMESPACE_CHART,	XML_AXIS,					XML_TOK_PA_AXIS				},
274 	{ XML_NAMESPACE_CHART,	XML_SERIES,				    XML_TOK_PA_SERIES			},
275 	{ XML_NAMESPACE_CHART,	XML_WALL,					XML_TOK_PA_WALL				},
276 	{ XML_NAMESPACE_CHART,	XML_FLOOR,					XML_TOK_PA_FLOOR			},
277 	{ XML_NAMESPACE_DR3D,	XML_LIGHT,					XML_TOK_PA_LIGHT_SOURCE		},
278 	{ XML_NAMESPACE_CHART,  XML_STOCK_GAIN_MARKER,      XML_TOK_PA_STOCK_GAIN       },
279 	{ XML_NAMESPACE_CHART,  XML_STOCK_LOSS_MARKER,      XML_TOK_PA_STOCK_LOSS       },
280 	{ XML_NAMESPACE_CHART,  XML_STOCK_RANGE_LINE,       XML_TOK_PA_STOCK_RANGE      },
281 	XML_TOKEN_MAP_END
282 };
283 
284 		mpPlotAreaElemTokenMap = new SvXMLTokenMap( aPlotAreaElemTokenMap );
285     } // if( ! mpPlotAreaElemTokenMap )
286 
287 	return *mpPlotAreaElemTokenMap;
288 }
289 
GetSeriesElemTokenMap()290 const SvXMLTokenMap& SchXMLImportHelper::GetSeriesElemTokenMap()
291 {
292 	if( ! mpSeriesElemTokenMap )
293     {
294         static __FAR_DATA SvXMLTokenMapEntry aSeriesElemTokenMap[] =
295 {
296 	{ XML_NAMESPACE_CHART,	XML_DATA_POINT,	      XML_TOK_SERIES_DATA_POINT       },
297 	{ XML_NAMESPACE_CHART,	XML_DOMAIN,		      XML_TOK_SERIES_DOMAIN	          },
298     { XML_NAMESPACE_CHART,	XML_MEAN_VALUE,       XML_TOK_SERIES_MEAN_VALUE_LINE  },
299     { XML_NAMESPACE_CHART,	XML_REGRESSION_CURVE, XML_TOK_SERIES_REGRESSION_CURVE },
300     { XML_NAMESPACE_CHART,	XML_ERROR_INDICATOR,  XML_TOK_SERIES_ERROR_INDICATOR  },
301 	XML_TOKEN_MAP_END
302 };
303 
304 		mpSeriesElemTokenMap = new SvXMLTokenMap( aSeriesElemTokenMap );
305     } // if( ! mpSeriesElemTokenMap )
306 
307 	return *mpSeriesElemTokenMap;
308 }
309 
310 // ----------------------------------------
311 
GetChartAttrTokenMap()312 const SvXMLTokenMap& SchXMLImportHelper::GetChartAttrTokenMap()
313 {
314 	if( ! mpChartAttrTokenMap )
315     {
316         static __FAR_DATA SvXMLTokenMapEntry aChartAttrTokenMap[] =
317 {
318     { XML_NAMESPACE_XLINK,  XML_HREF,                   XML_TOK_CHART_HREF          },
319 	{ XML_NAMESPACE_CHART,	XML_CLASS,					XML_TOK_CHART_CLASS			},
320 	{ XML_NAMESPACE_SVG,	XML_WIDTH,					XML_TOK_CHART_WIDTH			},
321 	{ XML_NAMESPACE_SVG,	XML_HEIGHT,				    XML_TOK_CHART_HEIGHT		},
322 	{ XML_NAMESPACE_CHART,	XML_STYLE_NAME,			    XML_TOK_CHART_STYLE_NAME	},
323     { XML_NAMESPACE_CHART,  XML_COLUMN_MAPPING,         XML_TOK_CHART_COL_MAPPING   },
324     { XML_NAMESPACE_CHART,  XML_ROW_MAPPING,            XML_TOK_CHART_ROW_MAPPING   },
325 	XML_TOKEN_MAP_END
326 };
327 
328 		mpChartAttrTokenMap = new SvXMLTokenMap( aChartAttrTokenMap );
329     } // if( ! mpChartAttrTokenMap )
330 
331 	return *mpChartAttrTokenMap;
332 }
333 
GetPlotAreaAttrTokenMap()334 const SvXMLTokenMap& SchXMLImportHelper::GetPlotAreaAttrTokenMap()
335 {
336 	if( ! mpPlotAreaAttrTokenMap )
337     {
338         static __FAR_DATA SvXMLTokenMapEntry aPlotAreaAttrTokenMap[] =
339 {
340     { XML_NAMESPACE_SVG,    XML_X,                      XML_TOK_PA_X                 },
341     { XML_NAMESPACE_SVG,    XML_Y,                      XML_TOK_PA_Y                 },
342     { XML_NAMESPACE_SVG,    XML_WIDTH,                  XML_TOK_PA_WIDTH             },
343     { XML_NAMESPACE_SVG,    XML_HEIGHT,                 XML_TOK_PA_HEIGHT            },
344     { XML_NAMESPACE_CHART,  XML_STYLE_NAME,             XML_TOK_PA_STYLE_NAME        },
345     { XML_NAMESPACE_TABLE,  XML_CELL_RANGE_ADDRESS,     XML_TOK_PA_CHART_ADDRESS     },
346     { XML_NAMESPACE_CHART,  XML_DATA_SOURCE_HAS_LABELS, XML_TOK_PA_DS_HAS_LABELS     },
347     { XML_NAMESPACE_DR3D,   XML_TRANSFORM,              XML_TOK_PA_TRANSFORM         },
348     { XML_NAMESPACE_DR3D,   XML_VRP,                    XML_TOK_PA_VRP               },
349     { XML_NAMESPACE_DR3D,   XML_VPN,                    XML_TOK_PA_VPN               },
350     { XML_NAMESPACE_DR3D,   XML_VUP,                    XML_TOK_PA_VUP               },
351     { XML_NAMESPACE_DR3D,   XML_PROJECTION,             XML_TOK_PA_PROJECTION        },
352     { XML_NAMESPACE_DR3D,   XML_DISTANCE,               XML_TOK_PA_DISTANCE          },
353     { XML_NAMESPACE_DR3D,   XML_FOCAL_LENGTH,           XML_TOK_PA_FOCAL_LENGTH      },
354     { XML_NAMESPACE_DR3D,   XML_SHADOW_SLANT,           XML_TOK_PA_SHADOW_SLANT      },
355     { XML_NAMESPACE_DR3D,   XML_SHADE_MODE,             XML_TOK_PA_SHADE_MODE        },
356     { XML_NAMESPACE_DR3D,   XML_AMBIENT_COLOR,          XML_TOK_PA_AMBIENT_COLOR     },
357     { XML_NAMESPACE_DR3D,   XML_LIGHTING_MODE,          XML_TOK_PA_LIGHTING_MODE     },
358     XML_TOKEN_MAP_END
359 };
360 
361 		mpPlotAreaAttrTokenMap = new SvXMLTokenMap( aPlotAreaAttrTokenMap );
362     } // if( ! mpPlotAreaAttrTokenMap )
363 
364 	return *mpPlotAreaAttrTokenMap;
365 }
366 
GetAutoStyleAttrTokenMap()367 const SvXMLTokenMap& SchXMLImportHelper::GetAutoStyleAttrTokenMap()
368 {
369 	if( ! mpAutoStyleAttrTokenMap )
370     {
371         static __FAR_DATA SvXMLTokenMapEntry aAutoStyleAttrTokenMap[] =
372 {
373 	{ XML_NAMESPACE_STYLE,	XML_FAMILY,				    XML_TOK_AS_FAMILY			},
374 	{ XML_NAMESPACE_STYLE,	XML_NAME,					XML_TOK_AS_NAME				},
375 	XML_TOKEN_MAP_END
376 };
377 
378 		mpAutoStyleAttrTokenMap = new SvXMLTokenMap( aAutoStyleAttrTokenMap );
379     } // if( ! mpAutoStyleAttrTokenMap )
380 
381 	return *mpAutoStyleAttrTokenMap;
382 }
383 
GetCellAttrTokenMap()384 const SvXMLTokenMap& SchXMLImportHelper::GetCellAttrTokenMap()
385 {
386 	if( ! mpCellAttrTokenMap )
387     {
388         static __FAR_DATA SvXMLTokenMapEntry aCellAttrTokenMap[] =
389 {
390 	{ XML_NAMESPACE_OFFICE,	XML_VALUE_TYPE,			    XML_TOK_CELL_VAL_TYPE		},
391 	{ XML_NAMESPACE_OFFICE,	XML_VALUE,					XML_TOK_CELL_VALUE			},
392 	XML_TOKEN_MAP_END
393 };
394 
395 		mpCellAttrTokenMap = new SvXMLTokenMap( aCellAttrTokenMap );
396     } // if( ! mpCellAttrTokenMap )
397 
398 	return *mpCellAttrTokenMap;
399 }
400 
GetSeriesAttrTokenMap()401 const SvXMLTokenMap& SchXMLImportHelper::GetSeriesAttrTokenMap()
402 {
403 	if( ! mpSeriesAttrTokenMap )
404     {
405         static __FAR_DATA SvXMLTokenMapEntry aSeriesAttrTokenMap[] =
406 {
407 	{ XML_NAMESPACE_CHART,	XML_VALUES_CELL_RANGE_ADDRESS,	XML_TOK_SERIES_CELL_RANGE	 },
408 	{ XML_NAMESPACE_CHART,	XML_LABEL_CELL_ADDRESS,         XML_TOK_SERIES_LABEL_ADDRESS },
409 	{ XML_NAMESPACE_CHART,	XML_ATTACHED_AXIS,				XML_TOK_SERIES_ATTACHED_AXIS },
410 	{ XML_NAMESPACE_CHART,	XML_STYLE_NAME, 				XML_TOK_SERIES_STYLE_NAME	 },
411 	{ XML_NAMESPACE_CHART,	XML_CLASS, 					    XML_TOK_SERIES_CHART_CLASS	 },
412 	XML_TOKEN_MAP_END
413 };
414 
415 		mpSeriesAttrTokenMap = new SvXMLTokenMap( aSeriesAttrTokenMap );
416     } // if( ! mpSeriesAttrTokenMap )
417 
418 	return *mpSeriesAttrTokenMap;
419 }
420 
GetRegEquationAttrTokenMap()421 const SvXMLTokenMap& SchXMLImportHelper::GetRegEquationAttrTokenMap()
422 {
423 	if( ! mpRegEquationAttrTokenMap )
424     {
425         static __FAR_DATA SvXMLTokenMapEntry aRegressionEquationAttrTokenMap[] =
426 {
427     { XML_NAMESPACE_CHART,  XML_STYLE_NAME,             XML_TOK_REGEQ_STYLE_NAME         },
428     { XML_NAMESPACE_CHART,  XML_DISPLAY_EQUATION,       XML_TOK_REGEQ_DISPLAY_EQUATION   },
429     { XML_NAMESPACE_CHART,  XML_DISPLAY_R_SQUARE,       XML_TOK_REGEQ_DISPLAY_R_SQUARE   },
430     { XML_NAMESPACE_SVG,    XML_X,                      XML_TOK_REGEQ_POS_X              },
431     { XML_NAMESPACE_SVG,    XML_Y,                      XML_TOK_REGEQ_POS_Y              },
432     XML_TOKEN_MAP_END
433 };
434 
435 		mpRegEquationAttrTokenMap = new SvXMLTokenMap( aRegressionEquationAttrTokenMap );
436     } // if( ! mpRegEquationAttrTokenMap )
437 
438 	return *mpRegEquationAttrTokenMap;
439 }
440 
441 // ----------------------------------------
442 
443 //static
DeleteDataSeries(const Reference<chart2::XDataSeries> & xSeries,const Reference<chart2::XChartDocument> & xDoc)444 void SchXMLImportHelper::DeleteDataSeries(
445                     const Reference< chart2::XDataSeries > & xSeries,
446                     const Reference< chart2::XChartDocument > & xDoc )
447 {
448     if( xDoc.is() )
449     try
450     {
451         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
452             xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW );
453         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
454             xCooSysCnt->getCoordinateSystems());
455 
456         sal_Int32 nCooSysIndex = 0;
457         for( nCooSysIndex=0; nCooSysIndex<aCooSysSeq.getLength(); nCooSysIndex++ )
458         {
459             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[ nCooSysIndex ], uno::UNO_QUERY_THROW );
460             Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes());
461 
462             sal_Int32 nChartTypeIndex = 0;
463             for( nChartTypeIndex=0; nChartTypeIndex<aChartTypes.getLength(); nChartTypeIndex++ )
464             {
465                 Reference< chart2::XDataSeriesContainer > xSeriesCnt( aChartTypes[nChartTypeIndex], uno::UNO_QUERY_THROW );
466                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries());
467 
468                 sal_Int32 nSeriesIndex = 0;
469                 for( nSeriesIndex=0; nSeriesIndex<aSeriesSeq.getLength(); nSeriesIndex++ )
470                 {
471                     if( xSeries==aSeriesSeq[nSeriesIndex] )
472                     {
473                         xSeriesCnt->removeDataSeries(xSeries);
474                         return;
475                     }
476                 }
477             }
478         }
479     }
480     catch( uno::Exception & ex )
481     {
482         (void)ex; // avoid warning for pro build
483         OSL_ENSURE( false, OUStringToOString(
484                         OUString( RTL_CONSTASCII_USTRINGPARAM( "Exception caught. Type: " )) +
485                         OUString::createFromAscii( typeid( ex ).name()) +
486                         OUString( RTL_CONSTASCII_USTRINGPARAM( ", Message: " )) +
487                         ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
488     }
489 }
490 
491 // static
GetNewDataSeries(const Reference<chart2::XChartDocument> & xDoc,sal_Int32 nCoordinateSystemIndex,const OUString & rChartTypeName,bool bPushLastChartType)492 Reference< chart2::XDataSeries > SchXMLImportHelper::GetNewDataSeries(
493     const Reference< chart2::XChartDocument > & xDoc,
494     sal_Int32 nCoordinateSystemIndex,
495     const OUString & rChartTypeName,
496     bool bPushLastChartType /* = false */ )
497 {
498     Reference< chart2::XDataSeries > xResult;
499     if(!xDoc.is())
500         return xResult;
501 
502     try
503     {
504         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
505             xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW );
506         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
507             xCooSysCnt->getCoordinateSystems());
508         Reference< uno::XComponentContext > xContext( lcl_getComponentContext());
509 
510         if( nCoordinateSystemIndex < aCooSysSeq.getLength())
511         {
512             Reference< chart2::XChartType > xCurrentType;
513             {
514                 Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[ nCoordinateSystemIndex ], uno::UNO_QUERY_THROW );
515                 Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes());
516                 // find matching chart type group
517                 const Reference< chart2::XChartType > * pBegin = aChartTypes.getConstArray();
518                 const Reference< chart2::XChartType > * pEnd = pBegin + aChartTypes.getLength();
519                 const Reference< chart2::XChartType > * pIt =
520                     ::std::find_if( pBegin, pEnd, lcl_MatchesChartType( rChartTypeName ));
521                 if( pIt != pEnd )
522                     xCurrentType.set( *pIt );
523                 // if chart type is set at series and differs from current one,
524                 // create a new chart type
525                 if( !xCurrentType.is())
526                 {
527                     xCurrentType.set(
528                         xContext->getServiceManager()->createInstanceWithContext( rChartTypeName, xContext ),
529                         uno::UNO_QUERY );
530                     if( xCurrentType.is())
531                     {
532                         if( bPushLastChartType && aChartTypes.getLength())
533                         {
534                             sal_Int32 nIndex( aChartTypes.getLength() - 1 );
535                             aChartTypes.realloc( aChartTypes.getLength() + 1 );
536                             aChartTypes[ nIndex + 1 ] = aChartTypes[ nIndex ];
537                             aChartTypes[ nIndex ] = xCurrentType;
538                             xCTCnt->setChartTypes( aChartTypes );
539                         }
540                         else
541                             xCTCnt->addChartType( xCurrentType );
542                     }
543                 }
544             }
545 
546             if( xCurrentType.is())
547             {
548                 Reference< chart2::XDataSeriesContainer > xSeriesCnt( xCurrentType, uno::UNO_QUERY_THROW );
549                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries());
550 
551                 if( xContext.is() )
552                 {
553                     xResult.set(
554                         xContext->getServiceManager()->createInstanceWithContext(
555                             OUString::createFromAscii("com.sun.star.chart2.DataSeries"),
556                             xContext ), uno::UNO_QUERY_THROW );
557                 }
558                 if( xResult.is() )
559                     xSeriesCnt->addDataSeries( xResult );
560             }
561         }
562     }
563     catch( uno::Exception & ex )
564     {
565         (void)ex; // avoid warning for pro build
566         OSL_ENSURE( false, OUStringToOString(
567                         OUString( RTL_CONSTASCII_USTRINGPARAM( "Exception caught. Type: " )) +
568                         OUString::createFromAscii( typeid( ex ).name()) +
569                         OUString( RTL_CONSTASCII_USTRINGPARAM( ", Message: " )) +
570                         ex.Message, RTL_TEXTENCODING_ASCII_US ).getStr());
571     }
572     return xResult;
573 }
574 
575 // static
GetNewLabeledDataSequence()576 Reference< chart2::data::XLabeledDataSequence > SchXMLImportHelper::GetNewLabeledDataSequence()
577 {
578     // @todo: remove this asap
579     OSL_ENSURE( false, "Do not call this method" );
580     Reference< chart2::data::XLabeledDataSequence >  xResult;
581     // DO NOT USED -- DEPRECATED. Use SchXMLTools::GetNewLabeledDataSequence() instead
582     return xResult;
583 }
584 
585 // ========================================
586 
587 // #110680#
SchXMLImport(const Reference<lang::XMultiServiceFactory> & xServiceFactory,sal_uInt16 nImportFlags)588 SchXMLImport::SchXMLImport(
589 	const Reference< lang::XMultiServiceFactory >& xServiceFactory,
590 	sal_uInt16 nImportFlags ) :
591         SvXMLImport( xServiceFactory, nImportFlags )
592 {
593     GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
594     GetNamespaceMap().Add( GetXMLToken(XML_NP_CHART_EXT), GetXMLToken(XML_N_CHART_EXT), XML_NAMESPACE_CHART_EXT);
595 
596     mbIsGraphicLoadOnDemandSupported = false;
597 }
598 
599 // #110680#
SchXMLImport(const Reference<lang::XMultiServiceFactory> & xServiceFactory,Reference<frame::XModel> xModel,Reference<document::XGraphicObjectResolver> & rGrfContainer,sal_Bool,sal_Bool bShowProgress)600 SchXMLImport::SchXMLImport(
601 	const Reference< lang::XMultiServiceFactory >& xServiceFactory,
602 	Reference< frame::XModel > xModel,
603 	Reference< document::XGraphicObjectResolver >& rGrfContainer,
604 	sal_Bool /*bLoadDoc*/, sal_Bool bShowProgress )
605 :	SvXMLImport( xServiceFactory, xModel, rGrfContainer )
606 {
607     GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK );
608     GetNamespaceMap().Add( GetXMLToken(XML_NP_CHART_EXT), GetXMLToken(XML_N_CHART_EXT), XML_NAMESPACE_CHART_EXT);
609 
610 	// get status indicator (if requested)
611 	if( bShowProgress )
612 	{
613 		Reference< frame::XController > xController( xModel->getCurrentController());
614 		if( xController.is())
615 		{
616 			Reference< frame::XFrame > xFrame( xController->getFrame());
617 			if( xFrame.is())
618 			{
619 				Reference< task::XStatusIndicatorSupplier > xFactory( xFrame, uno::UNO_QUERY );
620 				if( xFactory.is())
621 				{
622 					mxStatusIndicator = xFactory->getStatusIndicator();
623 				}
624 			}
625 		}
626 	}
627 
628 	// add progress view
629 	if( mxStatusIndicator.is())
630 	{
631 		const OUString aText( RTL_CONSTASCII_USTRINGPARAM( "XML Import" ));
632 		mxStatusIndicator->start( aText, 100 );		// use percentage as values
633 	}
634 }
635 
~SchXMLImport()636 SchXMLImport::~SchXMLImport() throw ()
637 {
638 	// stop progress view
639 	if( mxStatusIndicator.is())
640 	{
641 		mxStatusIndicator->end();
642 		mxStatusIndicator->reset();
643 	}
644 
645     uno::Reference< chart2::XChartDocument > xChartDoc( GetModel(), uno::UNO_QUERY );
646     if( xChartDoc.is() && xChartDoc->hasControllersLocked() )
647         xChartDoc->unlockControllers();
648 }
649 
650 // create the main context (subcontexts are created
651 // by the one created here)
CreateContext(sal_uInt16 nPrefix,const OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList)652 SvXMLImportContext *SchXMLImport::CreateContext( sal_uInt16 nPrefix, const OUString& rLocalName,
653 	const Reference< xml::sax::XAttributeList >& xAttrList )
654 {
655 	SvXMLImportContext* pContext = 0;
656 
657 	// accept <office:document>
658 	if( XML_NAMESPACE_OFFICE == nPrefix &&
659 		( IsXMLToken( rLocalName, XML_DOCUMENT_STYLES) ||
660           IsXMLToken( rLocalName, XML_DOCUMENT_CONTENT) ))
661 	{
662 		pContext = new SchXMLDocContext( maImportHelper, *this, nPrefix, rLocalName );
663 	} else if ( (XML_NAMESPACE_OFFICE == nPrefix) &&
664 		        ( IsXMLToken(rLocalName, XML_DOCUMENT) ||
665 		          (IsXMLToken(rLocalName, XML_DOCUMENT_META)
666                    && (getImportFlags() & IMPORT_META) )) )
667     {
668         uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
669             GetModel(), uno::UNO_QUERY);
670         // mst@: right now, this seems to be not supported, so it is untested
671         if (xDPS.is()) {
672             uno::Reference<xml::sax::XDocumentHandler> xDocBuilder(
673                 mxServiceFactory->createInstance(
674                     ::rtl::OUString::createFromAscii(
675                         "com.sun.star.xml.dom.SAXDocumentBuilder")),
676                     uno::UNO_QUERY_THROW);
677             pContext = (IsXMLToken(rLocalName, XML_DOCUMENT_META))
678                 ? new SvXMLMetaDocumentContext(*this,
679                             XML_NAMESPACE_OFFICE, rLocalName,
680                             xDPS->getDocumentProperties(), xDocBuilder)
681                 // flat OpenDocument file format
682                 : new SchXMLFlatDocContext_Impl(
683                             maImportHelper, *this, nPrefix, rLocalName,
684                             xDPS->getDocumentProperties(), xDocBuilder);
685         } else {
686             pContext = (IsXMLToken(rLocalName, XML_DOCUMENT_META))
687 		        ? SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList )
688                 : new SchXMLDocContext( maImportHelper, *this,
689                                         nPrefix, rLocalName );
690         }
691 	} else {
692 		pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList );
693 	}
694 
695 	return pContext;
696 }
697 
CreateStylesContext(const OUString & rLocalName,const Reference<xml::sax::XAttributeList> & xAttrList)698 SvXMLImportContext* SchXMLImport::CreateStylesContext(
699     const OUString& rLocalName,
700     const Reference<xml::sax::XAttributeList>& xAttrList )
701 {
702     //#i103287# make sure that the version information is set before importing all the properties (especially stroke-opacity!)
703     SchXMLTools::setBuildIDAtImportInfo( GetModel(), getImportInfo() );
704 
705     SvXMLStylesContext* pStylesCtxt =
706         new SvXMLStylesContext( *(this), XML_NAMESPACE_OFFICE, rLocalName, xAttrList );
707 
708     // set context at base class, so that all auto-style classes are imported
709     SetAutoStyles( pStylesCtxt );
710     maImportHelper.SetAutoStylesContext( pStylesCtxt );
711 
712     return pStylesCtxt;
713 }
714 
setTargetDocument(const uno::Reference<lang::XComponent> & xDoc)715 void SAL_CALL SchXMLImport::setTargetDocument( const uno::Reference< lang::XComponent >& xDoc )
716 	throw(lang::IllegalArgumentException, uno::RuntimeException)
717 {
718     uno::Reference< chart2::XChartDocument > xOldDoc( GetModel(), uno::UNO_QUERY );
719     if( xOldDoc.is() && xOldDoc->hasControllersLocked() )
720         xOldDoc->unlockControllers();
721 
722     SvXMLImport::setTargetDocument( xDoc );
723 
724     //set data provider and number formatter
725     // try to get an XDataProvider and set it
726     // @todo: if we have our own data, we must not use the parent as data provider
727     uno::Reference< chart2::XChartDocument > xChartDoc( GetModel(), uno::UNO_QUERY );
728 
729     if( xChartDoc.is() )
730     try
731     {
732         //prevent rebuild of view during load ( necesarry especially if loaded not via load api, which is the case for example if binary files are loaded )
733         xChartDoc->lockControllers();
734 
735         uno::Reference< container::XChild > xChild( xChartDoc, uno::UNO_QUERY );
736         uno::Reference< chart2::data::XDataReceiver > xDataReceiver( xChartDoc, uno::UNO_QUERY );
737         bool bHasOwnData = true;
738         if( xChild.is() && xDataReceiver.is())
739         {
740             Reference< lang::XMultiServiceFactory > xFact( xChild->getParent(), uno::UNO_QUERY );
741             if( xFact.is() )
742             {
743                 //if the parent has a number formatter we will use the numberformatter of the parent
744                 Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( xFact, uno::UNO_QUERY );
745                 xDataReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
746 
747                 if ( !xChartDoc->getDataProvider().is() )
748                 {
749                     const OUString aDataProviderServiceName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.chart2.data.DataProvider"));
750                     const uno::Sequence< OUString > aServiceNames( xFact->getAvailableServiceNames());
751                     const OUString * pBegin = aServiceNames.getConstArray();
752                     const OUString * pEnd = pBegin + aServiceNames.getLength();
753                     if( ::std::find( pBegin, pEnd, aDataProviderServiceName ) != pEnd )
754                     {
755                         Reference< chart2::data::XDataProvider > xProvider(
756                             xFact->createInstance( aDataProviderServiceName ), uno::UNO_QUERY );
757                         if( xProvider.is())
758                         {
759                             xDataReceiver->attachDataProvider( xProvider );
760                             bHasOwnData = false;
761                         }
762                     }
763                 }
764                 else
765                     bHasOwnData = false;
766             }
767 //             else we have no parent => we have our own data
768 
769             if( bHasOwnData && ! xChartDoc->hasInternalDataProvider() )
770                 xChartDoc->createInternalDataProvider( sal_False );
771         }
772     }
773     catch( uno::Exception & rEx )
774     {
775 #ifdef DBG_UTIL
776 		String aStr( rEx.Message );
777 		ByteString aBStr( aStr, RTL_TEXTENCODING_ASCII_US );
778 		DBG_ERROR1( "SchXMLChartContext::StartElement(): Exception caught: %s", aBStr.GetBuffer());
779 #else
780         (void)rEx; // avoid warning for pro build
781 #endif
782     }
783 }
784 
785 // export components ========================================
786 
787 // first version: everything comes from one storage
788 
SchXMLImport_getSupportedServiceNames()789 Sequence< OUString > SAL_CALL SchXMLImport_getSupportedServiceNames() throw()
790 {
791 	const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Chart.XMLOasisImporter" ) );
792 	const Sequence< OUString > aSeq( &aServiceName, 1 );
793 	return aSeq;
794 }
795 
SchXMLImport_getImplementationName()796 OUString SAL_CALL SchXMLImport_getImplementationName() throw()
797 {
798 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "SchXMLImport" ) );
799 }
800 
SchXMLImport_createInstance(const Reference<lang::XMultiServiceFactory> & rSMgr)801 Reference< uno::XInterface > SAL_CALL SchXMLImport_createInstance(const Reference< lang::XMultiServiceFactory > & rSMgr) throw( uno::Exception )
802 {
803 	// #110680#
804 	// return (cppu::OWeakObject*)new SchXMLImport();
805 	return (cppu::OWeakObject*)new SchXMLImport(rSMgr);
806 }
807 
808 // ============================================================
809 
810 // multiple storage version: one for content / styles / meta
811 
SchXMLImport_Styles_getSupportedServiceNames()812 Sequence< OUString > SAL_CALL SchXMLImport_Styles_getSupportedServiceNames() throw()
813 {
814 	const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Chart.XMLOasisStylesImporter" ) );
815 	const Sequence< OUString > aSeq( &aServiceName, 1 );
816 	return aSeq;
817 }
818 
SchXMLImport_Styles_getImplementationName()819 OUString SAL_CALL SchXMLImport_Styles_getImplementationName() throw()
820 {
821 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "SchXMLImport.Styles" ) );
822 }
823 
SchXMLImport_Styles_createInstance(const Reference<lang::XMultiServiceFactory> & rSMgr)824 Reference< uno::XInterface > SAL_CALL SchXMLImport_Styles_createInstance(const Reference< lang::XMultiServiceFactory > & rSMgr) throw( uno::Exception )
825 {
826 	// #110680#
827 	// return (cppu::OWeakObject*)new SchXMLImport( IMPORT_STYLES );
828 	return (cppu::OWeakObject*)new SchXMLImport( rSMgr, IMPORT_STYLES );
829 }
830 
831 // ------------------------------------------------------------
832 
SchXMLImport_Content_getSupportedServiceNames()833 Sequence< OUString > SAL_CALL SchXMLImport_Content_getSupportedServiceNames() throw()
834 {
835 	const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Chart.XMLOasisContentImporter" ) );
836 	const Sequence< OUString > aSeq( &aServiceName, 1 );
837 	return aSeq;
838 }
839 
SchXMLImport_Content_getImplementationName()840 OUString SAL_CALL SchXMLImport_Content_getImplementationName() throw()
841 {
842 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "SchXMLImport.Content" ) );
843 }
844 
SchXMLImport_Content_createInstance(const Reference<lang::XMultiServiceFactory> & rSMgr)845 Reference< uno::XInterface > SAL_CALL SchXMLImport_Content_createInstance(const Reference< lang::XMultiServiceFactory > & rSMgr) throw( uno::Exception )
846 {
847 	// #110680#
848 	// return (cppu::OWeakObject*)new SchXMLImport( IMPORT_CONTENT | IMPORT_AUTOSTYLES | IMPORT_FONTDECLS );
849 	return (cppu::OWeakObject*)new SchXMLImport( rSMgr, IMPORT_CONTENT | IMPORT_AUTOSTYLES | IMPORT_FONTDECLS );
850 }
851 
852 // ------------------------------------------------------------
853 
SchXMLImport_Meta_getSupportedServiceNames()854 Sequence< OUString > SAL_CALL SchXMLImport_Meta_getSupportedServiceNames() throw()
855 {
856 	const OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.Chart.XMLOasisMetaImporter" ) );
857 	const Sequence< OUString > aSeq( &aServiceName, 1 );
858 	return aSeq;
859 }
860 
SchXMLImport_Meta_getImplementationName()861 OUString SAL_CALL SchXMLImport_Meta_getImplementationName() throw()
862 {
863 	return OUString( RTL_CONSTASCII_USTRINGPARAM( "SchXMLImport.Meta" ) );
864 }
865 
SchXMLImport_Meta_createInstance(const Reference<lang::XMultiServiceFactory> & rSMgr)866 Reference< uno::XInterface > SAL_CALL SchXMLImport_Meta_createInstance(const Reference< lang::XMultiServiceFactory > & rSMgr) throw( uno::Exception )
867 {
868 	// #110680#
869 	// return (cppu::OWeakObject*)new SchXMLImport( IMPORT_META );
870 	return (cppu::OWeakObject*)new SchXMLImport( rSMgr, IMPORT_META );
871 }
872 
873 // XServiceInfo
getImplementationName()874 OUString SAL_CALL SchXMLImport::getImplementationName() throw( uno::RuntimeException )
875 {
876     switch( getImportFlags())
877     {
878         case IMPORT_ALL:
879             return SchXMLImport_getImplementationName();
880         case IMPORT_STYLES:
881             return SchXMLImport_Styles_getImplementationName();
882         case ( IMPORT_CONTENT | IMPORT_AUTOSTYLES | IMPORT_FONTDECLS ):
883             return SchXMLImport_Content_getImplementationName();
884         case IMPORT_META:
885             return SchXMLImport_Meta_getImplementationName();
886 
887         case IMPORT_SETTINGS:
888         // there is no settings component in chart
889         default:
890             return OUString::createFromAscii( "SchXMLImport" );
891     }
892 }
893