/src/libreoffice/xmloff/source/chart/SchXMLImport.cxx
Line | Count | Source |
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 <SchXMLImport.hxx> |
21 | | #include "SchXMLChartContext.hxx" |
22 | | #include "contexts.hxx" |
23 | | #include "SchXMLTools.hxx" |
24 | | |
25 | | #include <sal/log.hxx> |
26 | | #include <comphelper/processfactory.hxx> |
27 | | #include <utility> |
28 | | #include <xmloff/namespacemap.hxx> |
29 | | #include <xmloff/prstylei.hxx> |
30 | | #include <xmloff/xmlnamespace.hxx> |
31 | | #include <xmloff/xmltoken.hxx> |
32 | | #include <xmloff/xmluconv.hxx> |
33 | | #include <xmloff/xmlictxt.hxx> |
34 | | #include <xmloff/xmlstyle.hxx> |
35 | | #include <com/sun/star/chart/XChartDocument.hpp> |
36 | | #include <com/sun/star/container/XChild.hpp> |
37 | | #include <com/sun/star/lang/XMultiServiceFactory.hpp> |
38 | | #include <com/sun/star/uno/XComponentContext.hpp> |
39 | | #include <com/sun/star/chart2/data/XDataReceiver.hpp> |
40 | | #include <com/sun/star/chart2/XChartDocument.hpp> |
41 | | #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp> |
42 | | #include <com/sun/star/chart2/XChartTypeContainer.hpp> |
43 | | #include <com/sun/star/chart2/XDataSeriesContainer.hpp> |
44 | | |
45 | | #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp> |
46 | | |
47 | | #include <comphelper/diagnose_ex.hxx> |
48 | | |
49 | | using namespace com::sun::star; |
50 | | using namespace ::xmloff::token; |
51 | | |
52 | | using ::com::sun::star::uno::Reference; |
53 | | using ::com::sun::star::uno::Sequence; |
54 | | |
55 | | namespace |
56 | | { |
57 | | class lcl_MatchesChartType |
58 | | { |
59 | | public: |
60 | | explicit lcl_MatchesChartType( OUString aChartTypeName ) : |
61 | 0 | m_aChartTypeName(std::move( aChartTypeName )) |
62 | 0 | {} |
63 | | |
64 | | bool operator () ( const Reference< chart2::XChartType > & xChartType ) const |
65 | 0 | { |
66 | 0 | return (xChartType.is() && |
67 | 0 | xChartType->getChartType() == m_aChartTypeName ); |
68 | 0 | } |
69 | | |
70 | | private: |
71 | | OUString m_aChartTypeName; |
72 | | }; |
73 | | } // anonymous namespace |
74 | | |
75 | | // TokenMaps for distinguishing different |
76 | | // tokens in different contexts |
77 | | |
78 | | // element maps |
79 | | |
80 | | // attribute maps |
81 | | |
82 | | SchXMLImportHelper::SchXMLImportHelper() : |
83 | 8.56k | mpAutoStyles( nullptr ) |
84 | 8.56k | { |
85 | 8.56k | } |
86 | | |
87 | | SvXMLImportContext* SchXMLImportHelper::CreateChartContext( |
88 | | SvXMLImport& rImport, |
89 | | const Reference< frame::XModel >& rChartModel ) |
90 | 0 | { |
91 | 0 | SvXMLImportContext* pContext = nullptr; |
92 | |
|
93 | 0 | Reference< chart::XChartDocument > xDoc( rChartModel, uno::UNO_QUERY ); |
94 | 0 | if( xDoc.is()) |
95 | 0 | { |
96 | 0 | mxChartDoc = std::move(xDoc); |
97 | 0 | pContext = new SchXMLChartContext( *this, rImport ); |
98 | 0 | } |
99 | 0 | else |
100 | 0 | { |
101 | 0 | SAL_WARN("xmloff.chart", "No valid XChartDocument given as XModel" ); |
102 | 0 | } |
103 | | |
104 | 0 | return pContext; |
105 | 0 | } |
106 | | |
107 | | void SchXMLImportHelper::FillAutoStyle(const OUString& rAutoStyleName, const uno::Reference<beans::XPropertySet>& rProp) |
108 | 0 | { |
109 | 0 | if (!rProp.is()) |
110 | 0 | return; |
111 | | |
112 | 0 | const SvXMLStylesContext* pStylesCtxt = GetAutoStylesContext(); |
113 | 0 | if (pStylesCtxt) |
114 | 0 | { |
115 | 0 | SvXMLStyleContext* pStyle = const_cast<SvXMLStyleContext*>(pStylesCtxt->FindStyleChildContext(SchXMLImportHelper::GetChartFamilyID(), rAutoStyleName)); |
116 | |
|
117 | 0 | if (XMLPropStyleContext* pPropStyle = dynamic_cast<XMLPropStyleContext*>(pStyle)) |
118 | 0 | pPropStyle->FillPropertySet(rProp); |
119 | 0 | } |
120 | 0 | } |
121 | | |
122 | | // get various token maps |
123 | | |
124 | | |
125 | | //static |
126 | | void SchXMLImportHelper::DeleteDataSeries( |
127 | | const Reference< chart2::XDataSeries > & xSeries, |
128 | | const Reference< chart2::XChartDocument > & xDoc ) |
129 | 0 | { |
130 | 0 | if( !xDoc.is() ) |
131 | 0 | return; |
132 | 0 | try |
133 | 0 | { |
134 | 0 | Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( |
135 | 0 | xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW ); |
136 | 0 | const Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( |
137 | 0 | xCooSysCnt->getCoordinateSystems()); |
138 | |
|
139 | 0 | for( const auto& rCooSys : aCooSysSeq ) |
140 | 0 | { |
141 | 0 | Reference< chart2::XChartTypeContainer > xCTCnt( rCooSys, uno::UNO_QUERY_THROW ); |
142 | 0 | const Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes()); |
143 | |
|
144 | 0 | for( const auto& rChartType : aChartTypes ) |
145 | 0 | { |
146 | 0 | Reference< chart2::XDataSeriesContainer > xSeriesCnt( rChartType, uno::UNO_QUERY_THROW ); |
147 | 0 | const Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries()); |
148 | |
|
149 | 0 | if (std::find(aSeriesSeq.begin(), aSeriesSeq.end(), xSeries) != aSeriesSeq.end()) |
150 | 0 | { |
151 | 0 | xSeriesCnt->removeDataSeries(xSeries); |
152 | 0 | return; |
153 | 0 | } |
154 | 0 | } |
155 | 0 | } |
156 | 0 | } |
157 | 0 | catch( const uno::Exception &) |
158 | 0 | { |
159 | 0 | DBG_UNHANDLED_EXCEPTION("xmloff.chart"); |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | | // static |
164 | | Reference< chart2::XDataSeries > SchXMLImportHelper::GetNewDataSeries( |
165 | | const Reference< chart2::XChartDocument > & xDoc, |
166 | | sal_Int32 nCoordinateSystemIndex, |
167 | | const OUString & rChartTypeName, |
168 | | bool bPushLastChartType /* = false */ ) |
169 | 0 | { |
170 | 0 | Reference< chart2::XDataSeries > xResult; |
171 | 0 | if(!xDoc.is()) |
172 | 0 | return xResult; |
173 | | |
174 | 0 | try |
175 | 0 | { |
176 | 0 | Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( |
177 | 0 | xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW ); |
178 | 0 | Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( |
179 | 0 | xCooSysCnt->getCoordinateSystems()); |
180 | 0 | const Reference< uno::XComponentContext >& xContext( |
181 | 0 | comphelper::getProcessComponentContext() ); |
182 | |
|
183 | 0 | if( nCoordinateSystemIndex < aCooSysSeq.getLength()) |
184 | 0 | { |
185 | 0 | Reference< chart2::XChartType > xCurrentType; |
186 | 0 | { |
187 | 0 | Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[ nCoordinateSystemIndex ], uno::UNO_QUERY_THROW ); |
188 | 0 | Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes()); |
189 | | // find matching chart type group |
190 | 0 | const Reference< chart2::XChartType > * pBegin = aChartTypes.getConstArray(); |
191 | 0 | const Reference< chart2::XChartType > * pEnd = pBegin + aChartTypes.getLength(); |
192 | 0 | const Reference< chart2::XChartType > * pIt = |
193 | 0 | ::std::find_if( pBegin, pEnd, lcl_MatchesChartType( rChartTypeName )); |
194 | 0 | if( pIt != pEnd ) |
195 | 0 | xCurrentType.set( *pIt ); |
196 | | // if chart type is set at series and differs from current one, |
197 | | // create a new chart type |
198 | 0 | if( !xCurrentType.is()) |
199 | 0 | { |
200 | 0 | xCurrentType.set( |
201 | 0 | xContext->getServiceManager()->createInstanceWithContext( rChartTypeName, xContext ), |
202 | 0 | uno::UNO_QUERY ); |
203 | 0 | if( xCurrentType.is()) |
204 | 0 | { |
205 | 0 | if( bPushLastChartType && aChartTypes.hasElements()) |
206 | 0 | { |
207 | 0 | sal_Int32 nIndex( aChartTypes.getLength() - 1 ); |
208 | 0 | aChartTypes.realloc( aChartTypes.getLength() + 1 ); |
209 | 0 | auto pChartTypes = aChartTypes.getArray(); |
210 | 0 | pChartTypes[ nIndex + 1 ] = aChartTypes[ nIndex ]; |
211 | 0 | pChartTypes[ nIndex ] = xCurrentType; |
212 | 0 | xCTCnt->setChartTypes( aChartTypes ); |
213 | 0 | } |
214 | 0 | else |
215 | 0 | xCTCnt->addChartType( xCurrentType ); |
216 | 0 | } |
217 | 0 | } |
218 | 0 | } |
219 | |
|
220 | 0 | if( xCurrentType.is()) |
221 | 0 | { |
222 | 0 | Reference< chart2::XDataSeriesContainer > xSeriesCnt( xCurrentType, uno::UNO_QUERY_THROW ); |
223 | |
|
224 | 0 | if( xContext.is() ) |
225 | 0 | { |
226 | 0 | xResult.set( |
227 | 0 | xContext->getServiceManager()->createInstanceWithContext( |
228 | 0 | u"com.sun.star.chart2.DataSeries"_ustr, |
229 | 0 | xContext ), uno::UNO_QUERY_THROW ); |
230 | 0 | } |
231 | 0 | if( xResult.is() ) |
232 | 0 | xSeriesCnt->addDataSeries( xResult ); |
233 | 0 | } |
234 | 0 | } |
235 | 0 | } |
236 | 0 | catch( const uno::Exception & ) |
237 | 0 | { |
238 | 0 | DBG_UNHANDLED_EXCEPTION("xmloff.chart"); |
239 | 0 | } |
240 | 0 | return xResult; |
241 | 0 | } |
242 | | |
243 | | SchXMLImport::SchXMLImport( |
244 | | const Reference< uno::XComponentContext >& xContext, |
245 | | OUString const & implementationName, SvXMLImportFlags nImportFlags ) : |
246 | 0 | SvXMLImport( xContext, implementationName, nImportFlags ), |
247 | 0 | maImportHelper(new SchXMLImportHelper) |
248 | 0 | { |
249 | 0 | GetNamespaceMap().Add( GetXMLToken(XML_NP_XLINK), GetXMLToken(XML_N_XLINK), XML_NAMESPACE_XLINK ); |
250 | 0 | GetNamespaceMap().Add( GetXMLToken(XML_NP_CHART_EXT), GetXMLToken(XML_N_CHART_EXT), XML_NAMESPACE_CHART_EXT); |
251 | 0 | } |
252 | | |
253 | | SchXMLImport::~SchXMLImport() noexcept |
254 | 0 | { |
255 | 0 | uno::Reference< chart2::XChartDocument > xChartDoc( GetModel(), uno::UNO_QUERY ); |
256 | 0 | if (xChartDoc.is()) |
257 | 0 | { |
258 | 0 | if (xChartDoc->hasControllersLocked()) |
259 | 0 | xChartDoc->unlockControllers(); |
260 | 0 | if (auto xPropSet = xChartDoc.query<beans::XPropertySet>()) |
261 | 0 | { |
262 | 0 | try |
263 | 0 | { |
264 | | // The view of the chart might not received a notification about the updates, |
265 | | // which is only sent when the chart model is set modified; during the load, |
266 | | // setting modified is disabled. So, the view needs an explicit notification. |
267 | | // See ChartDocumentWrapper::setPropertyValue |
268 | 0 | xPropSet->setPropertyValue(u"ODFImport_UpdateView"_ustr, css::uno::Any(xChartDoc)); |
269 | 0 | } |
270 | 0 | catch (css::beans::UnknownPropertyException&) |
271 | 0 | { |
272 | | // That's absolutely fine! |
273 | 0 | } |
274 | 0 | } |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | | // create the main context (subcontexts are created |
279 | | // by the one created here) |
280 | | SvXMLImportContext *SchXMLImport::CreateFastContext( sal_Int32 nElement, |
281 | | const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/ ) |
282 | 0 | { |
283 | 0 | SvXMLImportContext* pContext = nullptr; |
284 | |
|
285 | 0 | switch (nElement) |
286 | 0 | { |
287 | 0 | case XML_ELEMENT( OFFICE, XML_DOCUMENT ): |
288 | 0 | case XML_ELEMENT( OFFICE, XML_DOCUMENT_META ): |
289 | 0 | { |
290 | 0 | uno::Reference<document::XDocumentPropertiesSupplier> xDPS( |
291 | 0 | GetModel(), uno::UNO_QUERY); |
292 | | // mst@: right now, this seems to be not supported, so it is untested |
293 | 0 | if (xDPS.is()) { |
294 | 0 | pContext = (nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT_META)) |
295 | 0 | ? new SvXMLMetaDocumentContext(*this, xDPS->getDocumentProperties()) |
296 | | // flat OpenDocument file format |
297 | 0 | : new SchXMLFlatDocContext_Impl(*maImportHelper, *this, nElement, |
298 | 0 | xDPS->getDocumentProperties()); |
299 | 0 | } |
300 | 0 | } |
301 | 0 | break; |
302 | | // accept <office:document> |
303 | 0 | case XML_ELEMENT(OFFICE, XML_DOCUMENT_STYLES): |
304 | 0 | case XML_ELEMENT(OFFICE, XML_DOCUMENT_CONTENT): |
305 | 0 | pContext = new SchXMLDocContext(*maImportHelper, *this, nElement); |
306 | 0 | break; |
307 | 0 | } |
308 | 0 | return pContext; |
309 | 0 | } |
310 | | |
311 | | SvXMLImportContext* SchXMLImport::CreateStylesContext() |
312 | 0 | { |
313 | | //#i103287# make sure that the version information is set before importing all the properties (especially stroke-opacity!) |
314 | 0 | SchXMLTools::setBuildIDAtImportInfo( GetModel(), getImportInfo() ); |
315 | |
|
316 | 0 | SvXMLStylesContext* pStylesCtxt = new SvXMLStylesContext( *this ); |
317 | | |
318 | | // set context at base class, so that all auto-style classes are imported |
319 | 0 | SetAutoStyles( pStylesCtxt ); |
320 | 0 | maImportHelper->SetAutoStylesContext( pStylesCtxt ); |
321 | |
|
322 | 0 | return pStylesCtxt; |
323 | 0 | } |
324 | | |
325 | | void SAL_CALL SchXMLImport::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc) |
326 | 0 | { |
327 | 0 | uno::Reference<chart2::XChartDocument> xOldDoc(GetModel(), uno::UNO_QUERY); |
328 | 0 | if (xOldDoc.is() && xOldDoc->hasControllersLocked()) |
329 | 0 | xOldDoc->unlockControllers(); |
330 | |
|
331 | 0 | SvXMLImport::setTargetDocument(xDoc); |
332 | |
|
333 | 0 | uno::Reference<chart2::XChartDocument> xChartDoc(GetModel(), uno::UNO_QUERY); |
334 | |
|
335 | 0 | if (!xChartDoc.is()) |
336 | 0 | return; |
337 | 0 | try |
338 | 0 | { |
339 | | // prevent rebuild of view during load (necessary especially if loaded not |
340 | | // via load api, which is the case for example if binary files are loaded) |
341 | 0 | xChartDoc->lockControllers(); |
342 | |
|
343 | 0 | uno::Reference<container::XChild> xChild(xChartDoc, uno::UNO_QUERY); |
344 | 0 | uno::Reference<chart2::data::XDataReceiver> xDataReceiver(xChartDoc, uno::UNO_QUERY); |
345 | 0 | if (xChild.is() && xDataReceiver.is()) |
346 | 0 | { |
347 | 0 | Reference<lang::XMultiServiceFactory> xFact(xChild->getParent(), uno::UNO_QUERY); |
348 | 0 | if (xFact.is()) |
349 | 0 | { |
350 | | //if the parent has a number formatter we will use the numberformatter of the parent |
351 | 0 | Reference<util::XNumberFormatsSupplier> xNumberFormatsSupplier(xFact, uno::UNO_QUERY); |
352 | 0 | xDataReceiver->attachNumberFormatsSupplier(xNumberFormatsSupplier); |
353 | 0 | } |
354 | 0 | } |
355 | 0 | } |
356 | 0 | catch (const uno::Exception &) |
357 | 0 | { |
358 | 0 | TOOLS_INFO_EXCEPTION("xmloff.chart", "SchXMLChartContext::StartElement(): Exception caught"); |
359 | 0 | } |
360 | 0 | } |
361 | | |
362 | | // first version: everything comes from one storage |
363 | | |
364 | | extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
365 | | com_sun_star_comp_Chart_XMLOasisImporter_get_implementation(uno::XComponentContext* pCtx, |
366 | | uno::Sequence<uno::Any> const& /*rSeq*/) |
367 | 0 | { |
368 | 0 | return cppu::acquire(new SchXMLImport(pCtx, u"SchXMLImport"_ustr, SvXMLImportFlags::ALL)); |
369 | 0 | } |
370 | | |
371 | | // multiple storage version: one for content / styles / meta |
372 | | |
373 | | extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
374 | | com_sun_star_comp_Chart_XMLOasisMetaImporter_get_implementation( |
375 | | uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |
376 | 0 | { |
377 | 0 | return cppu::acquire(new SchXMLImport(pCtx, u"SchXMLImport.Meta"_ustr, SvXMLImportFlags::META)); |
378 | 0 | } |
379 | | |
380 | | extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
381 | | com_sun_star_comp_Chart_XMLOasisStylesImporter_get_implementation( |
382 | | uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |
383 | 0 | { |
384 | 0 | return cppu::acquire(new SchXMLImport(pCtx, u"SchXMLImport.Styles"_ustr, SvXMLImportFlags::STYLES)); |
385 | 0 | } |
386 | | |
387 | | extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* |
388 | | com_sun_star_comp_Chart_XMLOasisContentImporter_get_implementation( |
389 | | uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/) |
390 | 0 | { |
391 | 0 | return cppu::acquire(new SchXMLImport(pCtx, u"SchXMLImport.Content"_ustr, |
392 | 0 | SvXMLImportFlags::CONTENT | SvXMLImportFlags::AUTOSTYLES |
393 | 0 | | SvXMLImportFlags::FONTDECLS)); |
394 | 0 | } |
395 | | |
396 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |