LibreOffice Module xmloff (master)  1
SchXMLImport.cxx
Go to the documentation of this file.
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>
27 #include <xmloff/namespacemap.hxx>
28 #include <xmloff/prstylei.hxx>
29 #include <xmloff/xmlnamespace.hxx>
30 #include <xmloff/xmltoken.hxx>
31 #include <xmloff/xmluconv.hxx>
32 #include <xmloff/xmlictxt.hxx>
33 #include <xmloff/xmlstyle.hxx>
34 #include <com/sun/star/chart/XChartDocument.hpp>
35 #include <com/sun/star/container/XChild.hpp>
36 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 #include <com/sun/star/uno/XComponentContext.hpp>
38 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
39 #include <com/sun/star/chart2/XChartDocument.hpp>
40 #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
41 #include <com/sun/star/chart2/XChartTypeContainer.hpp>
42 #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
43 
44 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
45 
46 #include <tools/diagnose_ex.h>
47 
48 using namespace com::sun::star;
49 using namespace ::xmloff::token;
50 
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::Sequence;
53 
54 namespace
55 {
56 class lcl_MatchesChartType
57 {
58 public:
59  explicit lcl_MatchesChartType( const OUString & aChartTypeName ) :
60  m_aChartTypeName( aChartTypeName )
61  {}
62 
63  bool operator () ( const Reference< chart2::XChartType > & xChartType ) const
64  {
65  return (xChartType.is() &&
66  xChartType->getChartType() == m_aChartTypeName );
67  }
68 
69 private:
70  OUString m_aChartTypeName;
71 };
72 } // anonymous namespace
73 
74  // TokenMaps for distinguishing different
75  // tokens in different contexts
76 
77 // element maps
78 
79 // attribute maps
80 
82  mpAutoStyles( nullptr )
83 {
84 }
85 
87  SvXMLImport& rImport,
88  const Reference< frame::XModel >& rChartModel )
89 {
90  SvXMLImportContext* pContext = nullptr;
91 
92  Reference< chart::XChartDocument > xDoc( rChartModel, uno::UNO_QUERY );
93  if( xDoc.is())
94  {
95  mxChartDoc = xDoc;
96  pContext = new SchXMLChartContext( *this, rImport );
97  }
98  else
99  {
100  SAL_WARN("xmloff.chart", "No valid XChartDocument given as XModel" );
101  }
102 
103  return pContext;
104 }
105 
106 void SchXMLImportHelper::FillAutoStyle(const OUString& rAutoStyleName, const uno::Reference<beans::XPropertySet>& rProp)
107 {
108  if (!rProp.is())
109  return;
110 
111  const SvXMLStylesContext* pStylesCtxt = GetAutoStylesContext();
112  if (pStylesCtxt)
113  {
114  SvXMLStyleContext* pStyle = const_cast<SvXMLStyleContext*>(pStylesCtxt->FindStyleChildContext(SchXMLImportHelper::GetChartFamilyID(), rAutoStyleName));
115 
116  if (XMLPropStyleContext* pPropStyle = dynamic_cast<XMLPropStyleContext*>(pStyle))
117  pPropStyle->FillPropertySet(rProp);
118  }
119 }
120 
121 // get various token maps
122 
123 
124 //static
126  const Reference< chart2::XDataSeries > & xSeries,
127  const Reference< chart2::XChartDocument > & xDoc )
128 {
129  if( !xDoc.is() )
130  return;
131  try
132  {
133  Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
134  xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW );
135  const Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
136  xCooSysCnt->getCoordinateSystems());
137 
138  for( const auto& rCooSys : aCooSysSeq )
139  {
140  Reference< chart2::XChartTypeContainer > xCTCnt( rCooSys, uno::UNO_QUERY_THROW );
141  const Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes());
142 
143  for( const auto& rChartType : aChartTypes )
144  {
145  Reference< chart2::XDataSeriesContainer > xSeriesCnt( rChartType, uno::UNO_QUERY_THROW );
146  Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries());
147 
148  if (std::find(aSeriesSeq.begin(), aSeriesSeq.end(), xSeries) != aSeriesSeq.end())
149  {
150  xSeriesCnt->removeDataSeries(xSeries);
151  return;
152  }
153  }
154  }
155  }
156  catch( const uno::Exception &)
157  {
158  DBG_UNHANDLED_EXCEPTION("xmloff.chart");
159  }
160 }
161 
162 // static
164  const Reference< chart2::XChartDocument > & xDoc,
165  sal_Int32 nCoordinateSystemIndex,
166  const OUString & rChartTypeName,
167  bool bPushLastChartType /* = false */ )
168 {
170  if(!xDoc.is())
171  return xResult;
172 
173  try
174  {
175  Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
176  xDoc->getFirstDiagram(), uno::UNO_QUERY_THROW );
177  Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
178  xCooSysCnt->getCoordinateSystems());
181 
182  if( nCoordinateSystemIndex < aCooSysSeq.getLength())
183  {
184  Reference< chart2::XChartType > xCurrentType;
185  {
186  Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[ nCoordinateSystemIndex ], uno::UNO_QUERY_THROW );
187  Sequence< Reference< chart2::XChartType > > aChartTypes( xCTCnt->getChartTypes());
188  // find matching chart type group
189  const Reference< chart2::XChartType > * pBegin = aChartTypes.getConstArray();
190  const Reference< chart2::XChartType > * pEnd = pBegin + aChartTypes.getLength();
191  const Reference< chart2::XChartType > * pIt =
192  ::std::find_if( pBegin, pEnd, lcl_MatchesChartType( rChartTypeName ));
193  if( pIt != pEnd )
194  xCurrentType.set( *pIt );
195  // if chart type is set at series and differs from current one,
196  // create a new chart type
197  if( !xCurrentType.is())
198  {
199  xCurrentType.set(
200  xContext->getServiceManager()->createInstanceWithContext( rChartTypeName, xContext ),
201  uno::UNO_QUERY );
202  if( xCurrentType.is())
203  {
204  if( bPushLastChartType && aChartTypes.hasElements())
205  {
206  sal_Int32 nIndex( aChartTypes.getLength() - 1 );
207  aChartTypes.realloc( aChartTypes.getLength() + 1 );
208  aChartTypes[ nIndex + 1 ] = aChartTypes[ nIndex ];
209  aChartTypes[ nIndex ] = xCurrentType;
210  xCTCnt->setChartTypes( aChartTypes );
211  }
212  else
213  xCTCnt->addChartType( xCurrentType );
214  }
215  }
216  }
217 
218  if( xCurrentType.is())
219  {
220  Reference< chart2::XDataSeriesContainer > xSeriesCnt( xCurrentType, uno::UNO_QUERY_THROW );
221 
222  if( xContext.is() )
223  {
224  xResult.set(
225  xContext->getServiceManager()->createInstanceWithContext(
226  "com.sun.star.chart2.DataSeries",
227  xContext ), uno::UNO_QUERY_THROW );
228  }
229  if( xResult.is() )
230  xSeriesCnt->addDataSeries( xResult );
231  }
232  }
233  }
234  catch( const uno::Exception & )
235  {
236  DBG_UNHANDLED_EXCEPTION("xmloff.chart");
237  }
238  return xResult;
239 }
240 
242  const Reference< uno::XComponentContext >& xContext,
243  OUString const & implementationName, SvXMLImportFlags nImportFlags ) :
244  SvXMLImport( xContext, implementationName, nImportFlags ),
245  maImportHelper(new SchXMLImportHelper)
246 {
249 }
250 
252 {
253  uno::Reference< chart2::XChartDocument > xChartDoc( GetModel(), uno::UNO_QUERY );
254  if( xChartDoc.is() && xChartDoc->hasControllersLocked() )
255  xChartDoc->unlockControllers();
256 }
257 
258 // create the main context (subcontexts are created
259 // by the one created here)
261  const uno::Reference< xml::sax::XFastAttributeList >& /*xAttrList*/ )
262 {
263  SvXMLImportContext* pContext = nullptr;
264 
265  switch (nElement)
266  {
269  {
270  uno::Reference<document::XDocumentPropertiesSupplier> xDPS(
271  GetModel(), uno::UNO_QUERY);
272  // mst@: right now, this seems to be not supported, so it is untested
273  if (xDPS.is()) {
274  pContext = (nElement == XML_ELEMENT(OFFICE, XML_DOCUMENT_META))
275  ? new SvXMLMetaDocumentContext(*this, xDPS->getDocumentProperties())
276  // flat OpenDocument file format
277  : new SchXMLFlatDocContext_Impl(*maImportHelper, *this, nElement,
278  xDPS->getDocumentProperties());
279  }
280  }
281  break;
282  // accept <office:document>
285  pContext = new SchXMLDocContext(*maImportHelper, *this, nElement);
286  break;
287  }
288  return pContext;
289 }
290 
292 {
293  //#i103287# make sure that the version information is set before importing all the properties (especially stroke-opacity!)
295 
296  SvXMLStylesContext* pStylesCtxt = new SvXMLStylesContext( *this );
297 
298  // set context at base class, so that all auto-style classes are imported
299  SetAutoStyles( pStylesCtxt );
300  maImportHelper->SetAutoStylesContext( pStylesCtxt );
301 
302  return pStylesCtxt;
303 }
304 
305 void SAL_CALL SchXMLImport::setTargetDocument(const uno::Reference<lang::XComponent>& xDoc)
306 {
307  uno::Reference<chart2::XChartDocument> xOldDoc(GetModel(), uno::UNO_QUERY);
308  if (xOldDoc.is() && xOldDoc->hasControllersLocked())
309  xOldDoc->unlockControllers();
310 
312 
313  uno::Reference<chart2::XChartDocument> xChartDoc(GetModel(), uno::UNO_QUERY);
314 
315  if (!xChartDoc.is())
316  return;
317  try
318  {
319  // prevent rebuild of view during load (necessary especially if loaded not
320  // via load api, which is the case for example if binary files are loaded)
321  xChartDoc->lockControllers();
322 
323  uno::Reference<container::XChild> xChild(xChartDoc, uno::UNO_QUERY);
324  uno::Reference<chart2::data::XDataReceiver> xDataReceiver(xChartDoc, uno::UNO_QUERY);
325  if (xChild.is() && xDataReceiver.is())
326  {
327  Reference<lang::XMultiServiceFactory> xFact(xChild->getParent(), uno::UNO_QUERY);
328  if (xFact.is())
329  {
330  //if the parent has a number formatter we will use the numberformatter of the parent
331  Reference<util::XNumberFormatsSupplier> xNumberFormatsSupplier(xFact, uno::UNO_QUERY);
332  xDataReceiver->attachNumberFormatsSupplier(xNumberFormatsSupplier);
333  }
334  }
335  }
336  catch (const uno::Exception &)
337  {
338  TOOLS_INFO_EXCEPTION("xmloff.chart", "SchXMLChartContext::StartElement(): Exception caught");
339  }
340 }
341 
342 // first version: everything comes from one storage
343 
344 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
346  uno::Sequence<uno::Any> const& /*rSeq*/)
347 {
348  return cppu::acquire(new SchXMLImport(pCtx, "SchXMLImport", SvXMLImportFlags::ALL));
349 }
350 
351 // multiple storage version: one for content / styles / meta
352 
353 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
355  uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
356 {
357  return cppu::acquire(new SchXMLImport(pCtx, "SchXMLImport.Meta", SvXMLImportFlags::META));
358 }
359 
360 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
362  uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
363 {
364  return cppu::acquire(new SchXMLImport(pCtx, "SchXMLImport.Styles", SvXMLImportFlags::STYLES));
365 }
366 
367 extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface*
369  uno::XComponentContext* pCtx, uno::Sequence<uno::Any> const& /*rSeq*/)
370 {
371  return cppu::acquire(new SchXMLImport(pCtx, "SchXMLImport.Content",
374 }
375 
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~SchXMLImport() override
sal_Int32 nIndex
void setBuildIDAtImportInfo(const uno::Reference< frame::XModel > &xModel, const Reference< beans::XPropertySet > &xImportInfo)
SvXMLImportContext * CreateStylesContext()
SvXMLStylesContext * GetAutoStylesContext() const
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Chart_XMLOasisMetaImporter_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
constexpr sal_uInt16 XML_NAMESPACE_XLINK
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:393
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Chart_XMLOasisContentImporter_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
void FillAutoStyle(const OUString &rAutoStyleName, const css::uno::Reference< css::beans::XPropertySet > &rProp)
Fill in the autostyle.
SvXMLImportFlags
Definition: xmlimp.hxx:110
static void DeleteDataSeries(const css::uno::Reference< css::chart2::XDataSeries > &xSeries, const css::uno::Reference< css::chart2::XChartDocument > &xDoc)
SvXMLImportContext * CreateChartContext(SvXMLImport &rImport, const css::uno::Reference< css::frame::XModel > &rChartModel)
get the context for reading the element with subelements.
SchXMLImport(const css::uno::Reference< css::uno::XComponentContext > &xContext, OUString const &implementationName, SvXMLImportFlags nImportFlags)
#define DBG_UNHANDLED_EXCEPTION(...)
virtual SvXMLImportContext * CreateFastContext(sal_Int32 nElement, const ::css::uno::Reference<::css::xml::sax::XFastAttributeList > &xAttrList) override
static css::uno::Reference< css::chart2::XDataSeries > GetNewDataSeries(const css::uno::Reference< css::chart2::XChartDocument > &xDoc, sal_Int32 nCoordinateSystemIndex, const OUString &rChartTypeName, bool bPushLastChartType)
constexpr sal_uInt16 XML_NAMESPACE_CHART_EXT
static XmlStyleFamily GetChartFamilyID()
With this class you can import a element containing its data as element o...
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:45
#define TOOLS_INFO_EXCEPTION(area, stream)
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
rtl::Reference< SchXMLImportHelper > maImportHelper
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3411
Handling of tokens in XML:
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:96
sal_uInt16 Add(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey=XML_NAMESPACE_UNKNOWN)
const css::uno::Reference< css::frame::XModel > & GetModel() const
Definition: xmlimp.hxx:399
Reference< XComponentContext > getProcessComponentContext()
void SetAutoStyles(SvXMLStylesContext *pAutoStyles)
Definition: xmlimp.cxx:1533
const SvXMLStyleContext * FindStyleChildContext(XmlStyleFamily nFamily, const OUString &rName, bool bCreateIndex=false) const
Definition: xmlstyle.cxx:788
const css::uno::Reference< css::beans::XPropertySet > & getImportInfo() const
Definition: xmlimp.hxx:381
handles the top-level office:document-meta element of meta.xml documents
Definition: xmlmetai.hxx:35
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Chart_XMLOasisImporter_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)
#define SAL_WARN(area, stream)
virtual void SAL_CALL setTargetDocument(const css::uno::Reference< css::lang::XComponent > &xDoc) override
Definition: xmlimp.cxx:898
css::uno::Reference< css::chart::XChartDocument > mxChartDoc
SAL_DLLPUBLIC_EXPORT uno::XInterface * com_sun_star_comp_Chart_XMLOasisStylesImporter_get_implementation(uno::XComponentContext *pCtx, uno::Sequence< uno::Any > const &)