bump product version to 6.4.0.3
[LibreOffice.git] / sd / source / filter / xml / sdxmlwrp.cxx
blobf23eabd0f6c857e0533add9875cd728fb224fc86
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/errinf.hxx>
21 #include <sal/log.hxx>
22 #include <com/sun/star/container/XChild.hpp>
23 #include <com/sun/star/beans/XPropertySetInfo.hpp>
24 #include <com/sun/star/embed/ElementModes.hpp>
25 #include <com/sun/star/xml/sax/SAXParseException.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertysequence.hxx>
28 #include <editeng/outlobj.hxx>
29 #include <sfx2/docfile.hxx>
30 #include <sfx2/docfilt.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <sot/storage.hxx>
33 #include <drawdoc.hxx>
34 #include <sdpage.hxx>
35 #include <Outliner.hxx>
36 #include <unotools/streamwrap.hxx>
37 #include <svx/dialmgr.hxx>
38 #include <svx/strings.hrc>
39 #include <svx/xmlgrhlp.hxx>
41 #include <DrawDocShell.hxx>
43 #include <sdxmlwrp.hxx>
44 #include <svx/xmleohlp.hxx>
45 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
46 #include <com/sun/star/xml/sax/XFastParser.hpp>
47 #include <com/sun/star/document/XFilter.hpp>
48 #include <com/sun/star/document/XImporter.hpp>
49 #include <com/sun/star/document/XExporter.hpp>
50 #include <com/sun/star/lang/XInitialization.hpp>
51 #include <com/sun/star/lang/XServiceInfo.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/beans/PropertyAttribute.hpp>
54 #include <com/sun/star/packages/WrongPasswordException.hpp>
55 #include <com/sun/star/packages/zip/ZipIOException.hpp>
57 #include <com/sun/star/xml/sax/InputSource.hpp>
58 #include <com/sun/star/xml/sax/Parser.hpp>
59 #include <com/sun/star/xml/sax/Writer.hpp>
60 #include <comphelper/genericpropertyset.hxx>
61 #include <comphelper/propertysetinfo.hxx>
62 #include <editeng/eeitem.hxx>
63 #include <unotools/saveopt.hxx>
65 // include necessary for XML progress bar at load time
66 #include <svl/itemset.hxx>
67 #include <svl/stritem.hxx>
68 #include <svtools/sfxecode.hxx>
70 #include <sddll.hxx>
71 #include <sderror.hxx>
72 #include <sdresid.hxx>
73 #include "sdtransform.hxx"
74 #include <strings.hrc>
76 #include <sfx2/frame.hxx>
77 #include <tools/debug.hxx>
78 #include <tools/diagnose_ex.h>
80 using namespace com::sun::star;
81 using namespace com::sun::star::uno;
82 using namespace com::sun::star::lang;
83 using namespace com::sun::star::document;
84 using namespace comphelper;
86 #define SD_XML_READERROR ErrCode(1234)
88 char const sXML_export_impress_meta_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisMetaExporter";
89 char const sXML_export_impress_styles_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisStylesExporter";
90 char const sXML_export_impress_content_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisContentExporter";
91 char const sXML_export_impress_settings_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisSettingsExporter";
93 char const sXML_export_draw_meta_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisMetaExporter";
94 char const sXML_export_draw_styles_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisStylesExporter";
95 char const sXML_export_draw_content_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisContentExporter";
96 char const sXML_export_draw_settings_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisSettingsExporter";
98 char const sXML_import_impress_meta_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisMetaImporter";
99 char const sXML_import_impress_styles_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisStylesImporter";
100 char const sXML_import_impress_content_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisContentImporter";
101 char const sXML_import_impress_settings_oasis_service[] = "com.sun.star.comp.Impress.XMLOasisSettingsImporter";
103 char const sXML_import_draw_meta_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisMetaImporter";
104 char const sXML_import_draw_styles_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisStylesImporter";
105 char const sXML_import_draw_content_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisContentImporter";
106 char const sXML_import_draw_settings_oasis_service[] = "com.sun.star.comp.Draw.XMLOasisSettingsImporter";
108 // OOo
109 char const sXML_export_impress_meta_ooo_service[] = "com.sun.star.comp.Impress.XMLMetaExporter";
110 char const sXML_export_impress_styles_ooo_service[] = "com.sun.star.comp.Impress.XMLStylesExporter";
111 char const sXML_export_impress_content_ooo_service[] = "com.sun.star.comp.Impress.XMLContentExporter";
112 char const sXML_export_impress_settings_ooo_service[] = "com.sun.star.comp.Impress.XMLSettingsExporter";
114 char const sXML_export_draw_meta_ooo_service[] = "com.sun.star.comp.Draw.XMLMetaExporter";
115 char const sXML_export_draw_styles_ooo_service[] = "com.sun.star.comp.Draw.XMLStylesExporter";
116 char const sXML_export_draw_content_ooo_service[] = "com.sun.star.comp.Draw.XMLContentExporter";
117 char const sXML_export_draw_settings_ooo_service[] = "com.sun.star.comp.Draw.XMLSettingsExporter";
119 char const sXML_import_impress_meta_ooo_service[] = "com.sun.star.comp.Impress.XMLMetaImporter";
120 char const sXML_import_impress_styles_ooo_service[] = "com.sun.star.comp.Impress.XMLStylesImporter";
121 char const sXML_import_impress_content_ooo_service[] = "com.sun.star.comp.Impress.XMLContentImporter";
122 char const sXML_import_impress_settings_ooo_service[] = "com.sun.star.comp.Impress.XMLSettingsImporter";
124 char const sXML_import_draw_meta_ooo_service[] = "com.sun.star.comp.Draw.XMLMetaImporter";
125 char const sXML_import_draw_styles_ooo_service[] = "com.sun.star.comp.Draw.XMLStylesImporter";
126 char const sXML_import_draw_content_ooo_service[] = "com.sun.star.comp.Draw.XMLContentImporter";
127 char const sXML_import_draw_settings_ooo_service[] = "com.sun.star.comp.Draw.XMLSettingsImporter";
129 struct XML_SERVICEMAP
131 const sal_Char* mpService;
132 const sal_Char* mpStream;
135 struct XML_SERVICES
137 const sal_Char* mpMeta;
138 const sal_Char* mpStyles;
139 const sal_Char* mpContent;
140 const sal_Char* mpSettings;
143 static XML_SERVICES const * getServices( bool bImport, bool bDraw, sal_uLong nStoreVer )
145 static XML_SERVICES const gServices[] =
147 { sXML_export_impress_meta_oasis_service, sXML_export_impress_styles_oasis_service, sXML_export_impress_content_oasis_service, sXML_export_impress_settings_oasis_service },
148 { sXML_export_draw_meta_oasis_service, sXML_export_draw_styles_oasis_service, sXML_export_draw_content_oasis_service, sXML_export_draw_settings_oasis_service },
149 { sXML_import_impress_meta_oasis_service, sXML_import_impress_styles_oasis_service, sXML_import_impress_content_oasis_service, sXML_import_impress_settings_oasis_service },
150 { sXML_import_draw_meta_oasis_service, sXML_import_draw_styles_oasis_service, sXML_import_draw_content_oasis_service, sXML_import_draw_settings_oasis_service },
152 { sXML_export_impress_meta_ooo_service, sXML_export_impress_styles_ooo_service, sXML_export_impress_content_ooo_service, sXML_export_impress_settings_ooo_service },
153 { sXML_export_draw_meta_ooo_service, sXML_export_draw_styles_ooo_service, sXML_export_draw_content_ooo_service, sXML_export_draw_settings_ooo_service },
154 { sXML_import_impress_meta_ooo_service, sXML_import_impress_styles_ooo_service, sXML_import_impress_content_ooo_service, sXML_import_impress_settings_ooo_service },
155 { sXML_import_draw_meta_ooo_service, sXML_import_draw_styles_ooo_service, sXML_import_draw_content_ooo_service, sXML_import_draw_settings_ooo_service },
158 return &gServices[ (bImport ? 2 : 0) + ((nStoreVer == SOFFICE_FILEFORMAT_60) ? 4 : 0) + (bDraw ? 1 : 0 ) ];
162 SdXMLFilter::SdXMLFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell, SdXMLFilterMode eFilterMode, sal_uLong nStoreVer ) :
163 SdFilter( rMedium, rDocShell ), meFilterMode( eFilterMode ), mnStoreVer( nStoreVer )
167 SdXMLFilter::~SdXMLFilter()
171 namespace
174 ErrCode ReadThroughComponent(
175 const Reference<io::XInputStream>& xInputStream,
176 const Reference<XComponent>& xModelComponent,
177 const OUString& rStreamName,
178 Reference<uno::XComponentContext> const & rxContext,
179 const sal_Char* pFilterName,
180 const Sequence<Any>& rFilterArguments,
181 const OUString& rName,
182 bool bMustBeSuccessfull,
183 bool bEncrypted )
185 DBG_ASSERT(xInputStream.is(), "input stream missing");
186 DBG_ASSERT(xModelComponent.is(), "document missing");
187 DBG_ASSERT(rxContext.is(), "factory missing");
188 DBG_ASSERT(nullptr != pFilterName,"I need a service name for the component!");
190 SAL_INFO( "sd.filter", "ReadThroughComponent" );
192 // prepare ParserInputSrouce
193 xml::sax::InputSource aParserInput;
194 aParserInput.sSystemId = rName;
195 aParserInput.aInputStream = xInputStream;
197 // get parser
198 Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(rxContext);
199 SAL_INFO( "sd.filter", "parser created" );
201 // get filter
202 OUString aFilterName(OUString::createFromAscii(pFilterName));
203 Reference< xml::sax::XDocumentHandler > xFilter(
204 rxContext->getServiceManager()->createInstanceWithArgumentsAndContext(aFilterName, rFilterArguments, rxContext),
205 UNO_QUERY );
206 SAL_WARN_IF(!xFilter.is(), "sd.filter", "Can't instantiate filter component: " << aFilterName);
207 if( !xFilter.is() )
208 return SD_XML_READERROR;
209 SAL_INFO( "sd.filter", "" << pFilterName << " created" );
211 // connect parser and filter
212 xParser->setDocumentHandler( xFilter );
214 // connect model and filter
215 Reference < XImporter > xImporter( xFilter, UNO_QUERY );
216 xImporter->setTargetDocument( xModelComponent );
218 uno::Reference< xml::sax::XFastParser > xFastParser = dynamic_cast<
219 xml::sax::XFastParser* >( xFilter.get() );
221 // finally, parser the stream
222 SAL_INFO( "sd.filter", "parsing stream" );
225 if( xFastParser.is() )
226 xFastParser->parseStream( aParserInput );
227 else
228 xParser->parseStream( aParserInput );
230 catch (const xml::sax::SAXParseException& r)
232 css::uno::Any ex( cppu::getCaughtException() );
233 // sax parser sends wrapped exceptions,
234 // try to find the original one
235 xml::sax::SAXException aSaxEx = *static_cast<xml::sax::SAXException const *>(&r);
236 bool bTryChild = true;
238 while( bTryChild )
240 xml::sax::SAXException aTmp;
241 if ( aSaxEx.WrappedException >>= aTmp )
242 aSaxEx = aTmp;
243 else
244 bTryChild = false;
247 packages::zip::ZipIOException aBrokenPackage;
248 if ( aSaxEx.WrappedException >>= aBrokenPackage )
249 return ERRCODE_IO_BROKENPACKAGE;
251 if( bEncrypted )
252 return ERRCODE_SFX_WRONGPASSWORD;
254 SAL_WARN( "sd.filter", "SAX parse exception caught while importing: " << exceptionToString(ex));
256 OUString sErr = OUString::number( r.LineNumber ) +
257 "," + OUString::number( r.ColumnNumber );
259 if (!rStreamName.isEmpty())
261 return *new TwoStringErrorInfo(
262 (bMustBeSuccessfull ? ERR_FORMAT_FILE_ROWCOL
263 : WARN_FORMAT_FILE_ROWCOL),
264 rStreamName, sErr,
265 DialogMask::ButtonsOk | DialogMask::MessageError );
267 else
269 DBG_ASSERT( bMustBeSuccessfull, "Warnings are not supported" );
270 return *new StringErrorInfo( ERR_FORMAT_ROWCOL, sErr,
271 DialogMask::ButtonsOk | DialogMask::MessageError );
274 catch (const xml::sax::SAXException& r)
276 css::uno::Any ex( cppu::getCaughtException() );
277 packages::zip::ZipIOException aBrokenPackage;
278 if ( r.WrappedException >>= aBrokenPackage )
279 return ERRCODE_IO_BROKENPACKAGE;
281 if( bEncrypted )
282 return ERRCODE_SFX_WRONGPASSWORD;
284 SAL_WARN( "sd.filter", "SAX exception caught while importing: " << exceptionToString(ex));
285 return SD_XML_READERROR;
287 catch (const packages::zip::ZipIOException&)
289 TOOLS_WARN_EXCEPTION( "sd.filter", "Zip exception caught while importing");
290 return ERRCODE_IO_BROKENPACKAGE;
292 catch (const io::IOException&)
294 TOOLS_WARN_EXCEPTION( "sd.filter", "IO exception caught while importing");
295 return SD_XML_READERROR;
297 catch (const uno::Exception&)
299 TOOLS_WARN_EXCEPTION( "sd.filter", "uno exception caught while importing");
300 return SD_XML_READERROR;
303 // success!
304 return ERRCODE_NONE;
307 ErrCode ReadThroughComponent(
308 const uno::Reference < embed::XStorage >& xStorage,
309 const Reference<XComponent>& xModelComponent,
310 const sal_Char* pStreamName,
311 const sal_Char* pCompatibilityStreamName,
312 Reference<uno::XComponentContext> const & rxContext,
313 const sal_Char* pFilterName,
314 const Sequence<Any>& rFilterArguments,
315 const OUString& rName,
316 bool bMustBeSuccessfull )
318 DBG_ASSERT(xStorage.is(), "Need storage!");
319 DBG_ASSERT(nullptr != pStreamName, "Please, please, give me a name!");
321 // open stream (and set parser input)
322 OUString sStreamName = OUString::createFromAscii(pStreamName);
323 bool bContainsStream = false;
326 bContainsStream = xStorage->isStreamElement(sStreamName);
328 catch (const container::NoSuchElementException&)
332 if (!bContainsStream )
334 // stream name not found! Then try the compatibility name.
335 // if no stream can be opened, return immediately with OK signal
337 // do we even have an alternative name?
338 if ( nullptr == pCompatibilityStreamName )
339 return ERRCODE_NONE;
341 // if so, does the stream exist?
342 sStreamName = OUString::createFromAscii(pCompatibilityStreamName);
345 bContainsStream = xStorage->isStreamElement(sStreamName);
347 catch (const container::NoSuchElementException&)
351 if (! bContainsStream )
352 return ERRCODE_NONE;
355 // set Base URL
356 uno::Reference< beans::XPropertySet > xInfoSet;
357 if( rFilterArguments.hasElements() )
358 rFilterArguments.getConstArray()[0] >>= xInfoSet;
359 DBG_ASSERT( xInfoSet.is(), "missing property set" );
360 if( xInfoSet.is() )
362 xInfoSet->setPropertyValue( "StreamName", makeAny( sStreamName ) );
367 // get input stream
368 Reference <io::XStream> xStream =
369 xStorage->openStreamElement( sStreamName, embed::ElementModes::READ );
370 Reference <beans::XPropertySet > xProps( xStream, uno::UNO_QUERY );
371 if ( !xStream.is() || ! xProps.is() )
372 return SD_XML_READERROR;
374 Any aAny = xProps->getPropertyValue( "Encrypted" );
376 bool bEncrypted = false;
377 aAny >>= bEncrypted;
379 Reference <io::XInputStream> xInputStream = xStream->getInputStream();
381 // read from the stream
382 return ReadThroughComponent(
383 xInputStream, xModelComponent, sStreamName, rxContext,
384 pFilterName, rFilterArguments,
385 rName, bMustBeSuccessfull, bEncrypted );
387 catch (const packages::WrongPasswordException&)
389 return ERRCODE_SFX_WRONGPASSWORD;
391 catch (const packages::zip::ZipIOException&)
393 return ERRCODE_IO_BROKENPACKAGE;
395 catch (const uno::Exception&)
398 return SD_XML_READERROR;
403 //PRESOBJ_OUTLINEs in master pages are the preview of the outline styles
404 //numbering format. Since fdo#78151 toggling bullets on and off changes
405 //the style they are a preview of, previously toggling bullets on and off
406 //would only affect the preview paragraph itself without an effect on the
407 //style. i.e. previews of numbering which don't match the real numbering
408 //they are supposed to be a preview of.
410 //But there exist documents which were saved previous to that modification
411 //so here we detect such cases and fix them up to ensure the previews
412 //numbering level matches that of the outline level it previews
413 static void fixupOutlinePlaceholderNumberingDepths(SdDrawDocument* pDoc)
415 for (sal_uInt16 i = 0; i < pDoc->GetMasterSdPageCount(PageKind::Standard); ++i)
417 SdPage *pMasterPage = pDoc->GetMasterSdPage(i, PageKind::Standard);
418 SdrObject* pMasterOutline = pMasterPage->GetPresObj(PRESOBJ_OUTLINE);
419 if (!pMasterOutline)
420 continue;
421 OutlinerParaObject* pOutlParaObj = pMasterOutline->GetOutlinerParaObject();
422 if (!pOutlParaObj)
423 continue;
424 SdOutliner* pOutliner = pDoc->GetInternalOutliner();
425 pOutliner->Clear();
426 pOutliner->SetText(*pOutlParaObj);
427 bool bInconsistent = false;
428 const sal_Int32 nParaCount = pOutliner->GetParagraphCount();
429 for (sal_Int32 j = 0; j < nParaCount; ++j)
431 //Make sure the depth of the paragraph matches that of the outline style it previews
432 const sal_Int16 nExpectedDepth = j;
433 if (nExpectedDepth != pOutliner->GetDepth(j))
435 Paragraph* p = pOutliner->GetParagraph(j);
436 pOutliner->SetDepth(p, nExpectedDepth);
437 bInconsistent = true;
440 //If the preview has hard-coded bullets/numbering then they must
441 //be stripped to reveal the true underlying styles attributes
442 SfxItemSet aAttrs(pOutliner->GetParaAttribs(j));
443 if (aAttrs.GetItemState(EE_PARA_NUMBULLET) == SfxItemState::SET)
445 aAttrs.ClearItem(EE_PARA_NUMBULLET);
446 pOutliner->SetParaAttribs(j, aAttrs);
447 bInconsistent = true;
451 if (bInconsistent)
453 SAL_WARN("sd.filter", "Fixing inconsistent outline numbering placeholder preview");
454 pMasterOutline->SetOutlinerParaObject(pOutliner->CreateParaObject(0, nParaCount));
456 pOutliner->Clear();
460 bool SdXMLFilter::Import( ErrCode& nError )
462 ErrCode nRet = ERRCODE_NONE;
464 // Get service factory
465 Reference< uno::XComponentContext > rxContext =
466 comphelper::getProcessComponentContext();
468 SdDrawDocument* pDoc = mrDocShell.GetDoc();
469 bool const bWasUndo(pDoc->IsUndoEnabled());
470 pDoc->EnableUndo(false);
471 pDoc->NewOrLoadCompleted( NEW_DOC );
472 pDoc->CreateFirstPages();
473 pDoc->StopWorkStartupDelay();
475 mxModel->lockControllers();
477 /** property map for import info set */
478 PropertyMapEntry const aImportInfoMap[] =
480 // necessary properties for XML progress bar at load time
481 { OUString("ProgressRange"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
482 { OUString("ProgressMax"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
483 { OUString("ProgressCurrent"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
484 { OUString("Preview"), 0, cppu::UnoType<sal_Bool>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
485 { OUString("PageLayouts"), 0, cppu::UnoType<container::XNameAccess>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
486 { OUString("PrivateData"), 0, cppu::UnoType<XInterface>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
487 { OUString("BaseURI"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
488 { OUString("StreamRelPath"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
489 { OUString("StreamName"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
490 { OUString("BuildId"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
491 { OUString("OrganizerMode"), 0, cppu::UnoType<bool>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
492 { OUString("SourceStorage"), 0, cppu::UnoType<embed::XStorage>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
493 { OUString(), 0, css::uno::Type(), 0, 0 }
496 uno::Reference< beans::XPropertySet > xInfoSet( GenericPropertySet_CreateInstance( new PropertySetInfo( aImportInfoMap ) ) );
497 xInfoSet->setPropertyValue( "Preview" , uno::makeAny( mrDocShell.GetDoc()->IsStarDrawPreviewMode() ) );
499 // ---- get BuildId from parent container if available
501 uno::Reference< container::XChild > xChild( mxModel, uno::UNO_QUERY );
502 if( xChild.is() )
504 uno::Reference< beans::XPropertySet > xParentSet( xChild->getParent(), uno::UNO_QUERY );
505 if( xParentSet.is() )
507 uno::Reference< beans::XPropertySetInfo > xPropSetInfo( xParentSet->getPropertySetInfo() );
508 OUString sPropName( "BuildId" );
509 if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName(sPropName) )
511 xInfoSet->setPropertyValue( sPropName, xParentSet->getPropertyValue(sPropName) );
516 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
517 rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
518 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
519 rtl::Reference<SvXMLEmbeddedObjectHelper> xObjectHelper;
521 Reference< lang::XComponent > xModelComp = mxModel;
523 // try to get an XStatusIndicator from the Medium
525 SfxItemSet* pSet = mrMedium.GetItemSet();
526 if(pSet)
528 const SfxUnoAnyItem* pItem = static_cast<const SfxUnoAnyItem*>(
529 pSet->GetItem(SID_PROGRESS_STATUSBAR_CONTROL) );
530 if (pItem)
532 pItem->GetValue() >>= mxStatusIndicator;
536 if(mxStatusIndicator.is())
538 sal_Int32 nProgressRange(1000000);
539 OUString aMsg(SvxResId(RID_SVXSTR_DOC_LOAD));
540 mxStatusIndicator->start(aMsg, nProgressRange);
542 // set ProgressRange
543 uno::Any aProgRange;
544 aProgRange <<= nProgressRange;
545 xInfoSet->setPropertyValue( "ProgressRange" , aProgRange);
547 // set ProgressCurrent
548 uno::Any aProgCurrent;
549 aProgCurrent <<= sal_Int32(0);
550 xInfoSet->setPropertyValue( "ProgressCurrent" , aProgCurrent);
554 // get the input stream (storage or stream)
556 tools::SvRef<SotStorageStream> xDocStream;
557 uno::Reference < embed::XStorage > xStorage = mrMedium.GetStorage();
559 xInfoSet->setPropertyValue( "SourceStorage", Any( xStorage ) );
561 if( !xStorage.is() )
562 nRet = SD_XML_READERROR;
564 if( ERRCODE_NONE == nRet )
566 xGraphicHelper = SvXMLGraphicHelper::Create( xStorage,
567 SvXMLGraphicHelperMode::Read );
568 xGraphicStorageHandler = xGraphicHelper.get();
569 xObjectHelper = SvXMLEmbeddedObjectHelper::Create(
570 xStorage, *pDoc->GetPersist(),
571 SvXMLEmbeddedObjectHelperMode::Read );
572 xObjectResolver = xObjectHelper.get();
575 // Set base URI
576 OUString const baseURI(mrMedium.GetBaseURL());
577 // needed for relative URLs, but in clipboard copy/paste there may be none
578 SAL_INFO_IF(baseURI.isEmpty(), "sd.filter", "SdXMLFilter: no base URL");
579 xInfoSet->setPropertyValue("BaseURI", makeAny(baseURI));
581 if( ERRCODE_NONE == nRet && SfxObjectCreateMode::EMBEDDED == mrDocShell.GetCreateMode() )
583 OUString aName;
584 if ( mrMedium.GetItemSet() )
586 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
587 mrMedium.GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
588 if ( pDocHierarchItem )
589 aName = pDocHierarchItem->GetValue();
591 else
592 aName = "dummyObjectName" ;
594 if( !aName.isEmpty() )
595 xInfoSet->setPropertyValue( "StreamRelPath", Any( aName ) );
598 if (SDXMLMODE_Organizer == meFilterMode)
599 xInfoSet->setPropertyValue("OrganizerMode", uno::makeAny(true));
601 if( ERRCODE_NONE == nRet )
604 // prepare filter arguments
605 Sequence<Any> aFilterArgs( 4 );
606 Any *pArgs = aFilterArgs.getArray();
607 *pArgs++ <<= xInfoSet;
608 *pArgs++ <<= xGraphicStorageHandler;
609 *pArgs++ <<= xObjectResolver;
610 *pArgs++ <<= mxStatusIndicator;
612 Sequence<Any> aEmptyArgs( 2 );
613 pArgs = aEmptyArgs.getArray();
614 *pArgs++ <<= xInfoSet;
615 *pArgs++ <<= mxStatusIndicator;
617 const OUString aName( mrMedium.GetName() );
619 XML_SERVICES const * pServices = getServices( true, IsDraw(), mnStoreVer );
621 ErrCode nWarn = ERRCODE_NONE;
622 ErrCode nWarn2 = ERRCODE_NONE;
623 // read storage streams
624 // #i103539#: always read meta.xml for generator
625 nWarn = ReadThroughComponent(
626 xStorage, xModelComp, "meta.xml", "Meta.xml", rxContext,
627 pServices->mpMeta,
628 aEmptyArgs, aName, false );
630 if( meFilterMode != SDXMLMODE_Organizer )
632 nWarn2 = ReadThroughComponent(
633 xStorage, xModelComp, "settings.xml", nullptr, rxContext,
634 pServices->mpSettings,
635 aFilterArgs, aName, false );
638 nRet = ReadThroughComponent(
639 xStorage, xModelComp, "styles.xml", nullptr, rxContext,
640 pServices->mpStyles,
641 aFilterArgs, aName, true );
643 if( !nRet && (meFilterMode != SDXMLMODE_Organizer) )
644 nRet = ReadThroughComponent(
645 xStorage, xModelComp, "content.xml", "Content.xml", rxContext,
646 pServices->mpContent,
647 aFilterArgs, aName, true );
649 if( !nRet )
651 if( nWarn )
652 nRet = nWarn;
653 else if( nWarn2 )
654 nRet = nWarn2;
658 if( xGraphicHelper )
659 xGraphicHelper->dispose();
660 xGraphicHelper.clear();
661 xGraphicStorageHandler = nullptr;
662 if( xObjectHelper.is() )
663 xObjectHelper->dispose();
664 xObjectHelper.clear();
665 xObjectResolver = nullptr;
667 if( mxStatusIndicator.is() )
668 mxStatusIndicator->end();
670 if( mxModel.is() )
671 mxModel->unlockControllers();
673 if( nRet == ERRCODE_NONE )
674 pDoc->UpdateAllLinks();
676 if( nRet.anyOf( ERRCODE_NONE, SD_XML_READERROR ) )
678 else if( nRet == ERRCODE_IO_BROKENPACKAGE && xStorage.is() )
679 nError = ERRCODE_IO_BROKENPACKAGE;
680 else
682 // TODO/LATER: this is completely wrong! Filter code should never call ErrorHandler directly!
683 ErrorHandler::HandleError( nRet );
684 if( nRet.IsWarning() )
685 nRet = ERRCODE_NONE;
688 // clear unused named items from item pool
690 uno::Reference< lang::XMultiServiceFactory> xModelFactory( mxModel, uno::UNO_QUERY );
691 if( xModelFactory.is() )
695 const OUString aName("~clear~" );
696 uno::Reference< container::XNameContainer > xGradient( xModelFactory->createInstance( "com.sun.star.drawing.GradientTable" ), uno::UNO_QUERY );
697 if( xGradient.is() )
698 xGradient->removeByName( aName );
700 uno::Reference< container::XNameContainer > xHatch( xModelFactory->createInstance( "com.sun.star.drawing.HatchTable" ), uno::UNO_QUERY );
701 if( xHatch.is() )
702 xHatch->removeByName( aName );
704 uno::Reference< container::XNameContainer > xBitmap( xModelFactory->createInstance( "com.sun.star.drawing.BitmapTable" ), uno::UNO_QUERY );
705 if( xBitmap.is() )
706 xBitmap->removeByName( aName );
708 uno::Reference< container::XNameContainer > xTransGradient( xModelFactory->createInstance( "com.sun.star.drawing.TransparencyGradientTable" ), uno::UNO_QUERY );
709 if( xTransGradient.is() )
710 xTransGradient->removeByName( aName );
712 uno::Reference< container::XNameContainer > xMarker( xModelFactory->createInstance( "com.sun.star.drawing.MarkerTable" ), uno::UNO_QUERY );
713 if( xMarker.is() )
714 xMarker->removeByName( aName );
716 uno::Reference< container::XNameContainer > xDashes( xModelFactory->createInstance( "com.sun.star.drawing.DashTable" ), uno::UNO_QUERY );
717 if( xDashes.is() )
718 xDashes->removeByName( aName );
720 catch (const Exception&)
722 TOOLS_WARN_EXCEPTION( "sd.filter","sd::SdXMLFilter::Import(), exception during clearing of unused named items");
726 // set BuildId on XModel for later OLE object loading
727 if( xInfoSet.is() )
729 uno::Reference< beans::XPropertySet > xModelSet( mxModel, uno::UNO_QUERY );
730 if( xModelSet.is() )
732 uno::Reference< beans::XPropertySetInfo > xModelSetInfo( xModelSet->getPropertySetInfo() );
733 const OUString sPropName( "BuildId" );
735 OUString sBuildId;
736 xInfoSet->getPropertyValue(sPropName) >>= sBuildId;
738 if( xModelSetInfo.is() && xModelSetInfo->hasPropertyByName(sPropName) )
740 xModelSet->setPropertyValue( sPropName, Any( sBuildId ) );
743 bool bTransform = false;
745 if( nRet == ERRCODE_NONE )
747 if( !sBuildId.isEmpty() )
749 sal_Int32 nIndex = sBuildId.indexOf('$');
750 if( nIndex != -1 )
752 sal_Int32 nUPD = sBuildId.copy( 0, nIndex ).toInt32();
754 if( nUPD == 300 )
756 sal_Int32 nBuildId = sBuildId.copy( nIndex+1 ).toInt32();
757 if( (nBuildId > 0) && (nBuildId < 9316) )
758 bTransform = true; // treat OOo 3.0 beta1 as OOo 2.x
760 else if( (nUPD == 680) || ( nUPD >= 640 && nUPD <= 645 ) )
761 bTransform = true;
764 else
766 // check for binary formats
767 std::shared_ptr<const SfxFilter> pFilter = mrMedium.GetFilter();
768 if( pFilter )
770 OUString typeName(pFilter->GetRealTypeName());
771 if( typeName.startsWith( "impress_StarImpress" ) ||
772 typeName.startsWith( "draw_StarDraw" ) )
774 bTransform = true;
780 if( bTransform )
781 TransformOOo2xDocument( pDoc );
785 fixupOutlinePlaceholderNumberingDepths(pDoc);
787 pDoc->EnableUndo(bWasUndo);
788 mrDocShell.ClearUndoBuffer();
789 return nRet == ERRCODE_NONE;
792 bool SdXMLFilter::Export()
794 rtl::Reference<SvXMLEmbeddedObjectHelper> xObjectHelper;
795 rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
796 bool bDocRet = false;
798 if( !mxModel.is() )
800 SAL_WARN( "sd.filter","Got NO Model in XMLExport");
801 return false;
804 bool bLocked = mxModel->hasControllersLocked();
808 mxModel->lockControllers();
810 uno::Reference< lang::XServiceInfo > xServiceInfo( mxModel, uno::UNO_QUERY );
812 if( !xServiceInfo.is() || !xServiceInfo->supportsService( "com.sun.star.drawing.GenericDrawingDocument" ) )
814 SAL_WARN( "sd.filter", "Model is no DrawingDocument in XMLExport" );
815 return false;
818 uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
820 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
822 /** property map for export info set */
823 PropertyMapEntry const aExportInfoMap[] =
825 { OUString("ProgressRange"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
826 { OUString("ProgressMax"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
827 { OUString("ProgressCurrent"), 0, cppu::UnoType<sal_Int32>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
828 { OUString("UsePrettyPrinting"),0, cppu::UnoType<bool>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
829 { OUString("PageLayoutNames"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0},
830 { OUString("BaseURI"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
831 { OUString("StreamRelPath"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
832 { OUString("StreamName"), 0, cppu::UnoType<OUString>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
833 { OUString("StyleNames"), 0, cppu::UnoType<Sequence<OUString>>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
834 { OUString("StyleFamilies"), 0, cppu::UnoType<Sequence<sal_Int32>>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
835 { OUString("TargetStorage"), 0, cppu::UnoType<embed::XStorage>::get(), css::beans::PropertyAttribute::MAYBEVOID, 0 },
836 { OUString(), 0, css::uno::Type(), 0, 0 }
839 uno::Reference< beans::XPropertySet > xInfoSet( GenericPropertySet_CreateInstance( new PropertySetInfo( aExportInfoMap ) ) );
841 SvtSaveOptions aSaveOpt;
842 bool bUsePrettyPrinting( aSaveOpt.IsPrettyPrinting() );
843 xInfoSet->setPropertyValue( "UsePrettyPrinting", makeAny( bUsePrettyPrinting ) );
845 const uno::Reference < embed::XStorage >& xStorage = mrMedium.GetOutputStorage();
847 // Set base URI
848 OUString sPropName( "BaseURI" );
849 xInfoSet->setPropertyValue( sPropName, makeAny( mrMedium.GetBaseURL( true ) ) );
851 xInfoSet->setPropertyValue( "TargetStorage", Any( xStorage ) );
853 if( SfxObjectCreateMode::EMBEDDED == mrDocShell.GetCreateMode() )
855 OUString aName;
856 if ( mrMedium.GetItemSet() )
858 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
859 mrMedium.GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
860 if ( pDocHierarchItem )
861 aName = pDocHierarchItem->GetValue();
864 if( !aName.isEmpty() )
866 sPropName = "StreamRelPath";
867 xInfoSet->setPropertyValue( sPropName, makeAny( aName ) );
871 // initialize descriptor
872 uno::Sequence< beans::PropertyValue > aDescriptor( 1 );
873 beans::PropertyValue* pProps = aDescriptor.getArray();
875 pProps[0].Name = "FileName";
876 pProps[0].Value <<= mrMedium.GetName();
879 uno::Reference< document::XEmbeddedObjectResolver > xObjectResolver;
880 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
882 // create helper for graphic and ole export if we have a storage
883 if( xStorage.is() )
885 xObjectHelper = SvXMLEmbeddedObjectHelper::Create( xStorage, *mrDocShell.GetDoc()->GetPersist(), SvXMLEmbeddedObjectHelperMode::Write );
886 xObjectResolver = xObjectHelper.get();
888 xGraphicHelper = SvXMLGraphicHelper::Create( xStorage, SvXMLGraphicHelperMode::Write );
889 xGraphicStorageHandler = xGraphicHelper.get();
892 CreateStatusIndicator();
893 if(mxStatusIndicator.is())
895 sal_Int32 nProgressRange(1000000);
896 OUString aMsg(SdResId(STR_SAVE_DOC));
897 mxStatusIndicator->start(aMsg, nProgressRange);
899 // set ProgressRange
900 uno::Any aProgRange;
901 aProgRange <<= nProgressRange;
902 xInfoSet->setPropertyValue( "ProgressRange" , aProgRange);
904 // set ProgressCurrent
905 uno::Any aProgCurrent;
906 aProgCurrent <<= sal_Int32(0);
907 xInfoSet->setPropertyValue( "ProgressCurrent" , aProgCurrent);
910 XML_SERVICES const * pServiceNames = getServices( false, IsDraw(), mnStoreVer );
912 XML_SERVICEMAP aServices[5]; sal_uInt16 i = 0;
913 aServices[i ].mpService = pServiceNames->mpStyles;
914 aServices[i++].mpStream = "styles.xml";
916 aServices[i ].mpService = pServiceNames->mpContent;
917 aServices[i++].mpStream = "content.xml";
919 aServices[i ].mpService = pServiceNames->mpSettings;
920 aServices[i++].mpStream = "settings.xml";
922 if( mrDocShell.GetCreateMode() != SfxObjectCreateMode::EMBEDDED )
924 aServices[i ].mpService = pServiceNames->mpMeta;
925 aServices[i++].mpStream = "meta.xml";
928 aServices[i].mpService = nullptr;
929 aServices[i].mpStream = nullptr;
931 XML_SERVICEMAP* pServices = aServices;
933 // doc export
936 SAL_INFO( "sd.filter", "exporting substream " << pServices->mpStream );
938 uno::Reference<io::XOutputStream> xDocOut;
939 if( xStorage.is() )
941 const OUString sDocName( OUString::createFromAscii( pServices->mpStream ) );
942 uno::Reference<io::XStream> xStream =
943 xStorage->openStreamElement( sDocName,
944 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
946 DBG_ASSERT(xStream.is(), "Can't create output stream in package!");
947 if( !xStream.is() )
948 return false;
950 xDocOut = xStream->getOutputStream();
951 Reference <beans::XPropertySet > xProps( xStream, uno::UNO_QUERY );
952 if( !xDocOut.is() || !xProps.is() )
953 return false;
955 xProps->setPropertyValue( "MediaType", Any(OUString( "text/xml")));
957 // encrypt all streams
958 xProps->setPropertyValue( "UseCommonStoragePasswordEncryption",
959 uno::makeAny( true ) );
961 const OUString sStreamName( "StreamName");
962 xInfoSet->setPropertyValue( sStreamName, Any( sDocName ) );
965 xWriter->setOutputStream( xDocOut );
967 uno::Sequence< uno::Any > aArgs( 2 + ( mxStatusIndicator.is() ? 1 : 0 ) + ( xGraphicStorageHandler.is() ? 1 : 0 ) + ( xObjectResolver.is() ? 1 : 0 ) );
968 uno::Any* pArgs = aArgs.getArray();
969 *pArgs++ <<= xInfoSet;
970 if (xGraphicStorageHandler.is())
971 *pArgs++ <<= xGraphicStorageHandler;
972 if (xObjectResolver.is())
973 *pArgs++ <<= xObjectResolver;
974 if (mxStatusIndicator.is())
975 *pArgs++ <<= mxStatusIndicator;
977 *pArgs <<= xWriter;
979 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pServices->mpService ), aArgs, xContext ), uno::UNO_QUERY );
980 if( xFilter.is() )
982 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
983 if( xExporter.is() )
985 xExporter->setSourceDocument( mxModel );
986 // outputstream will be closed by SAX parser
987 bDocRet = xFilter->filter( aDescriptor );
991 pServices++;
993 while( bDocRet && pServices->mpService );
995 if(mxStatusIndicator.is())
996 mxStatusIndicator->end();
999 catch (const uno::Exception &)
1001 TOOLS_WARN_EXCEPTION( "sd.filter", "uno Exception caught while exporting");
1002 bDocRet = false;
1004 if ( !bLocked )
1005 mxModel->unlockControllers();
1007 if( xGraphicHelper )
1008 xGraphicHelper->dispose();
1009 xGraphicHelper.clear();
1011 if( xObjectHelper )
1012 xObjectHelper->dispose();
1013 xObjectHelper.clear();
1015 return bDocRet;
1018 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportFODP(SvStream &rStream)
1020 SdDLL::Init();
1022 sd::DrawDocShellRef xDocSh(new sd::DrawDocShell(SfxObjectCreateMode::EMBEDDED, false, DocumentType::Impress));
1023 xDocSh->DoInitNew();
1024 uno::Reference<frame::XModel> xModel(xDocSh->GetModel());
1026 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
1027 uno::Reference<io::XInputStream> xStream(new ::utl::OSeekableInputStreamWrapper(rStream));
1028 uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance("com.sun.star.comp.Writer.XmlFilterAdaptor"), uno::UNO_SET_THROW);
1030 css::uno::Sequence<OUString> aUserData(7);
1031 aUserData[0] = "com.sun.star.comp.filter.OdfFlatXml";
1032 aUserData[2] = "com.sun.star.comp.Impress.XMLOasisImporter";
1033 aUserData[3] = "com.sun.star.comp.Impress.XMLOasisExporter";
1034 aUserData[6] = "true";
1035 uno::Sequence<beans::PropertyValue> aAdaptorArgs(comphelper::InitPropertySequence(
1037 { "UserData", uno::Any(aUserData) },
1038 }));
1039 css::uno::Sequence<uno::Any> aOuterArgs(1);
1040 aOuterArgs[0] <<= aAdaptorArgs;
1042 uno::Reference<lang::XInitialization> xInit(xInterface, uno::UNO_QUERY_THROW);
1043 xInit->initialize(aOuterArgs);
1045 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
1046 uno::Sequence<beans::PropertyValue> aArgs(comphelper::InitPropertySequence(
1048 { "InputStream", uno::Any(xStream) },
1049 { "URL", uno::Any(OUString("private:stream")) },
1050 }));
1051 xImporter->setTargetDocument(xModel);
1053 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
1054 //SetLoading hack because the document properties will be re-initted
1055 //by the xml filter and during the init, while it's considered uninitialized,
1056 //setting a property will inform the document it's modified, which attempts
1057 //to update the properties, which throws cause the properties are uninitialized
1058 xDocSh->SetLoading(SfxLoadedFlags::NONE);
1059 bool ret = xFilter->filter(aArgs);
1060 xDocSh->SetLoading(SfxLoadedFlags::ALL);
1062 xDocSh->DoClose();
1064 return ret;
1067 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportPPTX(SvStream &rStream)
1069 SdDLL::Init();
1071 sd::DrawDocShellRef xDocSh(new sd::DrawDocShell(SfxObjectCreateMode::EMBEDDED, false, DocumentType::Impress));
1072 xDocSh->DoInitNew();
1073 uno::Reference<frame::XModel> xModel(xDocSh->GetModel());
1075 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
1076 uno::Reference<io::XInputStream> xStream(new utl::OSeekableInputStreamWrapper(rStream));
1078 uno::Reference<document::XFilter> xFilter(xMultiServiceFactory->createInstance("com.sun.star.comp.oox.ppt.PowerPointImport"), uno::UNO_QUERY_THROW);
1080 uno::Reference<document::XImporter> xImporter(xFilter, uno::UNO_QUERY_THROW);
1081 uno::Sequence<beans::PropertyValue> aArgs(comphelper::InitPropertySequence(
1083 { "InputStream", uno::makeAny(xStream) },
1084 { "InputMode", uno::makeAny(true) },
1085 }));
1086 xImporter->setTargetDocument(xModel);
1088 //SetLoading hack because the document properties will be re-initted
1089 //by the xml filter and during the init, while it's considered uninitialized,
1090 //setting a property will inform the document it's modified, which attempts
1091 //to update the properties, which throws cause the properties are uninitialized
1092 xDocSh->SetLoading(SfxLoadedFlags::NONE);
1093 bool ret = false;
1096 ret = xFilter->filter(aArgs);
1098 catch (...)
1101 xDocSh->SetLoading(SfxLoadedFlags::ALL);
1103 xDocSh->DoClose();
1105 return ret;
1109 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */