bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / filter / xml / sdxmlwrp.cxx
blobcab013acb1188fc48b36b6dbc136098c43962dc4
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 <rtl/logfile.hxx>
21 #include <rtl/strbuf.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 <sfx2/docfile.hxx>
28 #include <sfx2/docfilt.hxx>
29 #include "drawdoc.hxx"
30 #include <unotools/streamwrap.hxx>
31 #include <svx/xmlgrhlp.hxx>
33 #include "../../ui/inc/DrawDocShell.hxx"
35 #include "sdxmlwrp.hxx"
36 #include "strmname.h"
37 #include <svx/xmleohlp.hxx>
38 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
39 #include <com/sun/star/document/XFilter.hpp>
40 #include <com/sun/star/document/XImporter.hpp>
41 #include <com/sun/star/document/XExporter.hpp>
42 #include <com/sun/star/lang/XServiceInfo.hpp>
43 #include <com/sun/star/document/XGraphicObjectResolver.hpp>
44 #include <com/sun/star/beans/PropertyAttribute.hpp>
45 #include <com/sun/star/container/XNameAccess.hpp>
46 #include <com/sun/star/packages/zip/ZipIOException.hpp>
48 #include <com/sun/star/xml/sax/XErrorHandler.hpp>
49 #include <com/sun/star/xml/sax/XEntityResolver.hpp>
50 #include <com/sun/star/xml/sax/InputSource.hpp>
51 #include <com/sun/star/xml/sax/XDTDHandler.hpp>
52 #include <com/sun/star/xml/sax/Parser.hpp>
53 #include <com/sun/star/xml/sax/Writer.hpp>
54 #include <com/sun/star/io/XActiveDataSource.hpp>
55 #include <com/sun/star/io/XActiveDataControl.hpp>
56 #include <comphelper/componentcontext.hxx>
57 #include <comphelper/genericpropertyset.hxx>
58 #include <comphelper/propertysetinfo.hxx>
59 #include <unotools/saveopt.hxx>
61 // include necessary for XML progress bar at load time
62 #include <svl/itemset.hxx>
63 #include <svl/stritem.hxx>
64 #include <svtools/sfxecode.hxx>
66 #include "sderror.hxx"
67 #include "sdresid.hxx"
68 #include "glob.hrc"
70 #include <sfx2/frame.hxx>
72 using namespace com::sun::star;
73 using namespace com::sun::star::uno;
74 using namespace com::sun::star::lang;
75 using namespace com::sun::star::document;
76 using namespace comphelper;
78 #define SD_XML_READERROR 1234
80 extern void TransformOOo2xDocument( SdDrawDocument* pDocument );
82 //////////////////////////////////////////////////////////////////////////////
84 #ifndef SEQTYPE
85 #if defined(__SUNPRO_CC) && (__SUNPRO_CC == 0x500)
86 #define SEQTYPE(x) (new ::com::sun::star::uno::Type( x ))
87 #else
88 #define SEQTYPE(x) &(x)
89 #endif
90 #endif
92 #define MAP_LEN(x) x, sizeof(x) - 1
94 #define XML_STRING(i, x) sal_Char const i[sizeof(x)] = x
96 XML_STRING( sXML_metaStreamName, "meta.xml");
97 XML_STRING( sXML_styleStreamName, "styles.xml" );
98 XML_STRING( sXML_contentStreamName, "content.xml" );
99 XML_STRING( sXML_oldContentStreamName, "Content.xml" );
100 XML_STRING( sXML_settingsStreamName, "settings.xml" );
102 XML_STRING( sXML_export_impress_oasis_service, "com.sun.star.comp.Impress.XMLOasisExporter" );
103 XML_STRING( sXML_export_impress_meta_oasis_service, "com.sun.star.comp.Impress.XMLOasisMetaExporter" );
104 XML_STRING( sXML_export_impress_styles_oasis_service, "com.sun.star.comp.Impress.XMLOasisStylesExporter" );
105 XML_STRING( sXML_export_impress_content_oasis_service, "com.sun.star.comp.Impress.XMLOasisContentExporter" );
106 XML_STRING( sXML_export_impress_settings_oasis_service, "com.sun.star.comp.Impress.XMLOasisSettingsExporter" );
108 XML_STRING( sXML_export_draw_oasis_service, "com.sun.star.comp.Draw.XMLOasisExporter" );
109 XML_STRING( sXML_export_draw_meta_oasis_service, "com.sun.star.comp.Draw.XMLOasisMetaExporter" );
110 XML_STRING( sXML_export_draw_styles_oasis_service, "com.sun.star.comp.Draw.XMLOasisStylesExporter" );
111 XML_STRING( sXML_export_draw_content_oasis_service, "com.sun.star.comp.Draw.XMLOasisContentExporter" );
112 XML_STRING( sXML_export_draw_settings_oasis_service, "com.sun.star.comp.Draw.XMLOasisSettingsExporter" );
114 XML_STRING( sXML_import_impress_oasis_service, "com.sun.star.comp.Impress.XMLOasisImporter" );
115 XML_STRING( sXML_import_impress_meta_oasis_service, "com.sun.star.comp.Impress.XMLOasisMetaImporter" );
116 XML_STRING( sXML_import_impress_styles_oasis_service, "com.sun.star.comp.Impress.XMLOasisStylesImporter" );
117 XML_STRING( sXML_import_impress_content_oasis_service, "com.sun.star.comp.Impress.XMLOasisContentImporter" );
118 XML_STRING( sXML_import_impress_settings_oasis_service, "com.sun.star.comp.Impress.XMLOasisSettingsImporter" );
120 XML_STRING( sXML_import_draw_oasis_service, "com.sun.star.comp.Draw.XMLOasisImporter" );
121 XML_STRING( sXML_import_draw_meta_oasis_service, "com.sun.star.comp.Draw.XMLOasisMetaImporter" );
122 XML_STRING( sXML_import_draw_styles_oasis_service, "com.sun.star.comp.Draw.XMLOasisStylesImporter" );
123 XML_STRING( sXML_import_draw_content_oasis_service, "com.sun.star.comp.Draw.XMLOasisContentImporter" );
124 XML_STRING( sXML_import_draw_settings_oasis_service, "com.sun.star.comp.Draw.XMLOasisSettingsImporter" );
126 // OOo
127 XML_STRING( sXML_export_impress_ooo_service, "com.sun.star.comp.Impress.XMLExporter" );
128 XML_STRING( sXML_export_impress_meta_ooo_service, "com.sun.star.comp.Impress.XMLMetaExporter" );
129 XML_STRING( sXML_export_impress_styles_ooo_service, "com.sun.star.comp.Impress.XMLStylesExporter" );
130 XML_STRING( sXML_export_impress_content_ooo_service, "com.sun.star.comp.Impress.XMLContentExporter" );
131 XML_STRING( sXML_export_impress_settings_ooo_service, "com.sun.star.comp.Impress.XMLSettingsExporter" );
133 XML_STRING( sXML_export_draw_ooo_service, "com.sun.star.comp.Draw.XMLExporter" );
134 XML_STRING( sXML_export_draw_meta_ooo_service, "com.sun.star.comp.Draw.XMLMetaExporter" );
135 XML_STRING( sXML_export_draw_styles_ooo_service, "com.sun.star.comp.Draw.XMLStylesExporter" );
136 XML_STRING( sXML_export_draw_content_ooo_service, "com.sun.star.comp.Draw.XMLContentExporter" );
137 XML_STRING( sXML_export_draw_settings_ooo_service, "com.sun.star.comp.Draw.XMLSettingsExporter" );
139 XML_STRING( sXML_import_impress_ooo_service, "com.sun.star.comp.Impress.XMLImporter" );
140 XML_STRING( sXML_import_impress_meta_ooo_service, "com.sun.star.comp.Impress.XMLMetaImporter" );
141 XML_STRING( sXML_import_impress_styles_ooo_service, "com.sun.star.comp.Impress.XMLStylesImporter" );
142 XML_STRING( sXML_import_impress_content_ooo_service, "com.sun.star.comp.Impress.XMLContentImporter" );
143 XML_STRING( sXML_import_impress_settings_ooo_service, "com.sun.star.comp.Impress.XMLSettingsImporter" );
145 XML_STRING( sXML_import_draw_ooo_service, "com.sun.star.comp.Draw.XMLImporter" );
146 XML_STRING( sXML_import_draw_meta_ooo_service, "com.sun.star.comp.Draw.XMLMetaImporter" );
147 XML_STRING( sXML_import_draw_styles_ooo_service, "com.sun.star.comp.Draw.XMLStylesImporter" );
148 XML_STRING( sXML_import_draw_content_ooo_service, "com.sun.star.comp.Draw.XMLContentImporter" );
149 XML_STRING( sXML_import_draw_settings_ooo_service, "com.sun.star.comp.Draw.XMLSettingsImporter" );
151 struct XML_SERVICEMAP
153 const sal_Char* mpService;
154 const sal_Char* mpStream;
157 struct XML_SERVICES
159 const sal_Char* mpAll;
160 const sal_Char* mpMeta;
161 const sal_Char* mpStyles;
162 const sal_Char* mpContent;
163 const sal_Char* mpSettings;
166 XML_SERVICES* getServices( bool bImport, bool bDraw, sal_uLong nStoreVer )
168 static XML_SERVICES gServices[] =
170 { sXML_export_impress_oasis_service, sXML_export_impress_meta_oasis_service, sXML_export_impress_styles_oasis_service, sXML_export_impress_content_oasis_service, sXML_export_impress_settings_oasis_service },
171 { sXML_export_draw_oasis_service, sXML_export_draw_meta_oasis_service, sXML_export_draw_styles_oasis_service, sXML_export_draw_content_oasis_service, sXML_export_draw_settings_oasis_service },
172 { sXML_import_impress_oasis_service, sXML_import_impress_meta_oasis_service, sXML_import_impress_styles_oasis_service, sXML_import_impress_content_oasis_service, sXML_import_impress_settings_oasis_service },
173 { sXML_import_draw_oasis_service, sXML_import_draw_meta_oasis_service, sXML_import_draw_styles_oasis_service, sXML_import_draw_content_oasis_service, sXML_import_draw_settings_oasis_service },
175 { sXML_export_impress_ooo_service, sXML_export_impress_meta_ooo_service, sXML_export_impress_styles_ooo_service, sXML_export_impress_content_ooo_service, sXML_export_impress_settings_ooo_service },
176 { sXML_export_draw_ooo_service, sXML_export_draw_meta_ooo_service, sXML_export_draw_styles_ooo_service, sXML_export_draw_content_ooo_service, sXML_export_draw_settings_ooo_service },
177 { sXML_import_impress_ooo_service, sXML_import_impress_meta_ooo_service, sXML_import_impress_styles_ooo_service, sXML_import_impress_content_ooo_service, sXML_import_impress_settings_ooo_service },
178 { sXML_import_draw_ooo_service, sXML_import_draw_meta_ooo_service, sXML_import_draw_styles_ooo_service, sXML_import_draw_content_ooo_service, sXML_import_draw_settings_ooo_service },
181 return &gServices[ (bImport ? 2 : 0) + ((nStoreVer == SOFFICE_FILEFORMAT_60) ? 4 : 0) + (bDraw ? 1 : 0 ) ];
185 // ----------------
186 // - SdXMLWrapper -
187 // ----------------
189 SdXMLFilter::SdXMLFilter( SfxMedium& rMedium, ::sd::DrawDocShell& rDocShell, sal_Bool bShowProgress, SdXMLFilterMode eFilterMode, sal_uLong nStoreVer ) :
190 SdFilter( rMedium, rDocShell, bShowProgress ), meFilterMode( eFilterMode ), mnStoreVer( nStoreVer )
194 SdXMLFilter::~SdXMLFilter(void)
198 sal_Int32 ReadThroughComponent(
199 Reference<io::XInputStream> xInputStream,
200 Reference<XComponent> xModelComponent,
201 const String& rStreamName,
202 Reference<uno::XComponentContext> & rxContext,
203 const sal_Char* pFilterName,
204 Sequence<Any> rFilterArguments,
205 const OUString& rName,
206 sal_Bool bMustBeSuccessfull,
207 sal_Bool bEncrypted )
209 DBG_ASSERT(xInputStream.is(), "input stream missing");
210 DBG_ASSERT(xModelComponent.is(), "document missing");
211 DBG_ASSERT(rxContext.is(), "factory missing");
212 DBG_ASSERT(NULL != pFilterName,"I need a service name for the component!");
214 RTL_LOGFILE_CONTEXT( aLog, "ReadThroughComponent" );
216 // prepare ParserInputSrouce
217 xml::sax::InputSource aParserInput;
218 aParserInput.sSystemId = rName;
219 aParserInput.aInputStream = xInputStream;
221 // get parser
222 Reference< xml::sax::XParser > xParser = xml::sax::Parser::create(rxContext);
223 RTL_LOGFILE_CONTEXT_TRACE( aLog, "parser created" );
225 // get filter
226 OUString aFilterName(OUString::createFromAscii(pFilterName));
227 Reference< xml::sax::XDocumentHandler > xFilter(
228 rxContext->getServiceManager()->createInstanceWithArgumentsAndContext(aFilterName, rFilterArguments, rxContext),
229 UNO_QUERY );
230 SAL_WARN_IF(!xFilter.is(), "sd", "Can't instantiate filter component: " << aFilterName);
231 if( !xFilter.is() )
232 return SD_XML_READERROR;
233 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "%s created", pFilterName );
235 // connect parser and filter
236 xParser->setDocumentHandler( xFilter );
238 // connect model and filter
239 Reference < XImporter > xImporter( xFilter, UNO_QUERY );
240 xImporter->setTargetDocument( xModelComponent );
241 // finally, parser the stream
242 RTL_LOGFILE_CONTEXT_TRACE( aLog, "parsing stream" );
245 xParser->parseStream( aParserInput );
247 catch (const xml::sax::SAXParseException& r)
249 // sax parser sends wrapped exceptions,
250 // try to find the original one
251 xml::sax::SAXException aSaxEx = *(xml::sax::SAXException*)(&r);
252 sal_Bool bTryChild = sal_True;
254 while( bTryChild )
256 xml::sax::SAXException aTmp;
257 if ( aSaxEx.WrappedException >>= aTmp )
258 aSaxEx = aTmp;
259 else
260 bTryChild = sal_False;
263 packages::zip::ZipIOException aBrokenPackage;
264 if ( aSaxEx.WrappedException >>= aBrokenPackage )
265 return ERRCODE_IO_BROKENPACKAGE;
267 if( bEncrypted )
268 return ERRCODE_SFX_WRONGPASSWORD;
270 #if OSL_DEBUG_LEVEL > 1
271 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
272 "SAX parse exception caught while importing:\n"));
273 aError.append(OUStringToOString(r.Message,
274 RTL_TEXTENCODING_ASCII_US));
275 OSL_FAIL(aError.getStr());
276 #endif
278 String sErr( OUString::number( r.LineNumber ));
279 sErr += ',';
280 sErr += OUString::number( r.ColumnNumber );
282 if( rStreamName.Len() )
284 return *new TwoStringErrorInfo(
285 (bMustBeSuccessfull ? ERR_FORMAT_FILE_ROWCOL
286 : WARN_FORMAT_FILE_ROWCOL),
287 rStreamName, sErr,
288 ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
290 else
292 DBG_ASSERT( bMustBeSuccessfull, "Warnings are not supported" );
293 return *new StringErrorInfo( ERR_FORMAT_ROWCOL, sErr,
294 ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
297 catch (const xml::sax::SAXException& r)
299 packages::zip::ZipIOException aBrokenPackage;
300 if ( r.WrappedException >>= aBrokenPackage )
301 return ERRCODE_IO_BROKENPACKAGE;
303 if( bEncrypted )
304 return ERRCODE_SFX_WRONGPASSWORD;
306 #if OSL_DEBUG_LEVEL > 1
307 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
308 "SAX exception caught while importing:\n"));
309 aError.append(OUStringToOString(r.Message,
310 RTL_TEXTENCODING_ASCII_US));
311 OSL_FAIL(aError.getStr());
312 #endif
313 return SD_XML_READERROR;
315 catch (const packages::zip::ZipIOException& r)
317 #if OSL_DEBUG_LEVEL > 1
318 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
319 "Zip exception caught while importing:\n"));
320 aError.append(OUStringToOString(r.Message,
321 RTL_TEXTENCODING_ASCII_US));
322 OSL_FAIL(aError.getStr());
323 #else
324 (void)r;
325 #endif
326 return ERRCODE_IO_BROKENPACKAGE;
328 catch (const io::IOException& r)
330 #if OSL_DEBUG_LEVEL > 1
331 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
332 "IO exception caught while importing:\n"));
333 aError.append(OUStringToOString(r.Message,
334 RTL_TEXTENCODING_ASCII_US));
335 OSL_FAIL(aError.getStr());
336 #else
337 (void)r;
338 #endif
339 return SD_XML_READERROR;
341 catch (const uno::Exception& r)
343 #if OSL_DEBUG_LEVEL > 1
344 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
345 "uno exception caught while importing:\n"));
346 aError.append(OUStringToOString(r.Message,
347 RTL_TEXTENCODING_ASCII_US));
348 OSL_FAIL(aError.getStr());
349 #else
350 (void)r;
351 #endif
352 return SD_XML_READERROR;
355 // success!
356 return 0;
359 sal_Int32 ReadThroughComponent(
360 const uno::Reference < embed::XStorage >& xStorage,
361 Reference<XComponent> xModelComponent,
362 const sal_Char* pStreamName,
363 const sal_Char* pCompatibilityStreamName,
364 Reference<uno::XComponentContext> & rxContext,
365 const sal_Char* pFilterName,
366 Sequence<Any> rFilterArguments,
367 const OUString& rName,
368 sal_Bool bMustBeSuccessfull )
370 DBG_ASSERT(xStorage.is(), "Need storage!");
371 DBG_ASSERT(NULL != pStreamName, "Please, please, give me a name!");
373 // open stream (and set parser input)
374 OUString sStreamName = OUString::createFromAscii(pStreamName);
375 sal_Bool bContainsStream = sal_False;
378 bContainsStream = xStorage->isStreamElement(sStreamName);
380 catch (const container::NoSuchElementException&)
384 if (!bContainsStream )
386 // stream name not found! Then try the compatibility name.
387 // if no stream can be opened, return immediately with OK signal
389 // do we even have an alternative name?
390 if ( NULL == pCompatibilityStreamName )
391 return 0;
393 // if so, does the stream exist?
394 sStreamName = OUString::createFromAscii(pCompatibilityStreamName);
397 bContainsStream = xStorage->isStreamElement(sStreamName);
399 catch (const container::NoSuchElementException&)
403 if (! bContainsStream )
404 return 0;
407 // set Base URL
408 uno::Reference< beans::XPropertySet > xInfoSet;
409 if( rFilterArguments.getLength() > 0 )
410 rFilterArguments.getConstArray()[0] >>= xInfoSet;
411 DBG_ASSERT( xInfoSet.is(), "missing property set" );
412 if( xInfoSet.is() )
414 OUString sPropName( "StreamName" );
415 xInfoSet->setPropertyValue( sPropName, makeAny( sStreamName ) );
420 // get input stream
421 Reference <io::XStream> xStream =
422 xStorage->openStreamElement( sStreamName, embed::ElementModes::READ );
423 Reference <beans::XPropertySet > xProps( xStream, uno::UNO_QUERY );
424 if ( !xStream.is() || ! xProps.is() )
425 return SD_XML_READERROR;
427 Any aAny = xProps->getPropertyValue( "Encrypted" );
429 sal_Bool bEncrypted = aAny.getValueType() == ::getBooleanCppuType() &&
430 *(sal_Bool *)aAny.getValue();
432 Reference <io::XInputStream> xInputStream = xStream->getInputStream();
434 // read from the stream
435 return ReadThroughComponent(
436 xInputStream, xModelComponent, sStreamName, rxContext,
437 pFilterName, rFilterArguments,
438 rName, bMustBeSuccessfull, bEncrypted );
440 catch (const packages::WrongPasswordException&)
442 return ERRCODE_SFX_WRONGPASSWORD;
444 catch (const packages::zip::ZipIOException&)
446 return ERRCODE_IO_BROKENPACKAGE;
448 catch (const uno::Exception&)
451 return SD_XML_READERROR;
454 // -----------------------------------------------------------------------------
456 sal_Bool SdXMLFilter::Import( ErrCode& nError )
458 RTL_LOGFILE_CONTEXT_AUTHOR ( aLog, "sd", "cl93746", "SdXMLFilter::Import" );
459 #ifdef TIMELOG
460 OString aFile(OUStringToOString(mrMedium.GetName(), RTL_TEXTENCODING_ASCII_US));
461 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "importing %s", aFile.getStr() );
462 #endif
464 sal_uInt32 nRet = 0;
466 // Get service factory
467 Reference< uno::XComponentContext > rxContext =
468 comphelper::getProcessComponentContext();
470 // -------------------------------------
472 SdDrawDocument* pDoc = mrDocShell.GetDoc();
473 pDoc->EnableUndo(false);
474 pDoc->NewOrLoadCompleted( NEW_DOC );
475 pDoc->CreateFirstPages();
476 pDoc->StopWorkStartupDelay();
478 // -------------------------------------
480 mxModel->lockControllers();
482 // -------------------------------------
484 /** property map for import info set */
485 PropertyMapEntry aImportInfoMap[] =
487 // necessary properties for XML progress bar at load time
488 { MAP_LEN( "ProgressRange" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
489 { MAP_LEN( "ProgressMax" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
490 { MAP_LEN( "ProgressCurrent" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
491 { MAP_LEN( "Preview" ), 0, &::getCppuType((const sal_Bool*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
492 { MAP_LEN( "PageLayouts" ), 0, SEQTYPE(::getCppuType((const uno::Reference< container::XNameAccess >*)0)), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
493 { MAP_LEN( "PrivateData" ), 0,
494 &::getCppuType( (Reference<XInterface> *)0 ),
495 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
496 { MAP_LEN( "BaseURI" ), 0,
497 &::getCppuType( (OUString *)0 ),
498 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
499 { MAP_LEN( "StreamRelPath" ), 0,
500 &::getCppuType( (OUString *)0 ),
501 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
502 { MAP_LEN( "StreamName" ), 0,
503 &::getCppuType( (OUString *)0 ),
504 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
505 { MAP_LEN( "BuildId" ), 0,
506 &::getCppuType( (OUString *)0 ),
507 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
508 { MAP_LEN( "OrganizerMode" ), 0,
509 &::getBooleanCppuType(),
510 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
511 { MAP_LEN( "SourceStorage" ), 0, &embed::XStorage::static_type(),
512 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
513 { NULL, 0, 0, NULL, 0, 0 }
516 uno::Reference< beans::XPropertySet > xInfoSet( GenericPropertySet_CreateInstance( new PropertySetInfo( aImportInfoMap ) ) );
517 xInfoSet->setPropertyValue( "Preview" , uno::makeAny( mrDocShell.GetDoc()->IsStarDrawPreviewMode() ) );
519 // ---- get BuildId from parent container if available
521 uno::Reference< container::XChild > xChild( mxModel, uno::UNO_QUERY );
522 if( xChild.is() )
524 uno::Reference< beans::XPropertySet > xParentSet( xChild->getParent(), uno::UNO_QUERY );
525 if( xParentSet.is() )
527 uno::Reference< beans::XPropertySetInfo > xPropSetInfo( xParentSet->getPropertySetInfo() );
528 OUString sPropName( "BuildId" );
529 if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName(sPropName) )
531 xInfoSet->setPropertyValue( sPropName, xParentSet->getPropertyValue(sPropName) );
536 // -------------------------------------
538 Reference< io::XActiveDataSource > xSource;
539 Reference< XInterface > xPipe;
540 Reference< document::XGraphicObjectResolver > xGraphicResolver;
541 SvXMLGraphicHelper *pGraphicHelper = 0;
542 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
543 SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
545 Reference< lang::XComponent > xModelComp( mxModel, uno::UNO_QUERY );
547 // -------------------------------------
549 // try to get an XStatusIndicator from the Medium
550 if( mbShowProgress )
552 SfxItemSet* pSet = mrMedium.GetItemSet();
553 if(pSet)
555 const SfxUnoAnyItem* pItem = static_cast<const SfxUnoAnyItem*>(
556 pSet->GetItem(SID_PROGRESS_STATUSBAR_CONTROL) );
557 if (pItem)
559 pItem->GetValue() >>= mxStatusIndicator;
563 if(mxStatusIndicator.is())
565 sal_Int32 nProgressRange(1000000);
566 sal_Int32 nProgressCurrent(0);
567 OUString aMsg = String( SdResId( STR_LOAD_DOC ) );
568 mxStatusIndicator->start(aMsg, nProgressRange);
570 // set ProgressRange
571 uno::Any aProgRange;
572 aProgRange <<= nProgressRange;
573 xInfoSet->setPropertyValue( "ProgressRange" , aProgRange);
575 // set ProgressCurrent
576 uno::Any aProgCurrent;
577 aProgCurrent <<= nProgressCurrent;
578 xInfoSet->setPropertyValue( "ProgressCurrent" , aProgCurrent);
582 // -------------------------------------
583 // get the input stream (storage or stream)
584 // -------------------------------------
586 SvStorageStreamRef xDocStream;
587 Reference<io::XInputStream> xInputStream;
588 uno::Reference < embed::XStorage > xStorage = mrMedium.GetStorage();
590 OUString sSourceStorage( "SourceStorage");
591 xInfoSet->setPropertyValue( sSourceStorage, Any( xStorage ) );
593 if( !xStorage.is() )
594 nRet = SD_XML_READERROR;
596 if( 0 == nRet )
598 pGraphicHelper = SvXMLGraphicHelper::Create( xStorage,
599 GRAPHICHELPER_MODE_READ,
600 sal_False );
601 xGraphicResolver = pGraphicHelper;
602 pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
603 xStorage, *pDoc->GetPersist(),
604 EMBEDDEDOBJECTHELPER_MODE_READ,
605 sal_False );
606 xObjectResolver = pObjectHelper;
609 // Set base URI
610 xInfoSet->setPropertyValue( "BaseURI" , makeAny( mrMedium.GetBaseURL() ) );
612 if( 0 == nRet && SFX_CREATE_MODE_EMBEDDED == mrDocShell.GetCreateMode() )
614 OUString aName;
615 if ( mrMedium.GetItemSet() )
617 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
618 mrMedium.GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
619 if ( pDocHierarchItem )
620 aName = pDocHierarchItem->GetValue();
622 else
623 aName = "dummyObjectName" ;
625 if( !aName.isEmpty() )
626 xInfoSet->setPropertyValue( "StreamRelPath", Any( aName ) );
629 if (SDXMLMODE_Organizer == meFilterMode)
630 xInfoSet->setPropertyValue("OrganizerMode", uno::makeAny(sal_True));
632 // -------------------------------------
634 if( 0 == nRet )
637 // prepare filter arguments
638 Sequence<Any> aFilterArgs( 4 );
639 Any *pArgs = aFilterArgs.getArray();
640 *pArgs++ <<= xInfoSet;
641 *pArgs++ <<= xGraphicResolver;
642 *pArgs++ <<= xObjectResolver;
643 *pArgs++ <<= mxStatusIndicator;
645 Sequence<Any> aEmptyArgs( 2 );
646 pArgs = aEmptyArgs.getArray();
647 *pArgs++ <<= xInfoSet;
648 *pArgs++ <<= mxStatusIndicator;
650 const OUString aName( mrMedium.GetName() );
652 XML_SERVICES* pServices = getServices( true, IsDraw(), mnStoreVer );
654 sal_uInt32 nWarn = 0;
655 sal_uInt32 nWarn2 = 0;
656 // read storage streams
657 // #i103539#: always read meta.xml for generator
658 nWarn = ReadThroughComponent(
659 xStorage, xModelComp, "meta.xml", "Meta.xml", rxContext,
660 pServices->mpMeta,
661 aEmptyArgs, aName, sal_False );
663 if( meFilterMode != SDXMLMODE_Organizer )
665 nWarn2 = ReadThroughComponent(
666 xStorage, xModelComp, "settings.xml", NULL, rxContext,
667 pServices->mpSettings,
668 aFilterArgs, aName, sal_False );
671 nRet = ReadThroughComponent(
672 xStorage, xModelComp, "styles.xml", NULL, rxContext,
673 pServices->mpStyles,
674 aFilterArgs, aName, sal_True );
676 if( !nRet && (meFilterMode != SDXMLMODE_Organizer) )
677 nRet = ReadThroughComponent(
678 xStorage, xModelComp, "content.xml", "Content.xml", rxContext,
679 pServices->mpContent,
680 aFilterArgs, aName, sal_True );
682 if( !nRet )
684 if( nWarn )
685 nRet = nWarn;
686 else if( nWarn2 )
687 nRet = nWarn2;
691 // -------------------------------------
692 if( pGraphicHelper )
693 SvXMLGraphicHelper::Destroy( pGraphicHelper );
694 xGraphicResolver = 0;
695 if( pObjectHelper )
696 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
697 xObjectResolver = 0;
699 if( mxStatusIndicator.is() )
700 mxStatusIndicator->end();
702 if( mxModel.is() )
703 mxModel->unlockControllers();
705 if( nRet == 0 )
706 pDoc->UpdateAllLinks();
708 switch( nRet )
710 case 0: break;
711 case SD_XML_READERROR: break;
712 case ERRCODE_IO_BROKENPACKAGE:
713 if( xStorage.is() )
715 nError = ERRCODE_IO_BROKENPACKAGE;
716 break;
718 // fall through intented
719 default:
721 // TODO/LATER: this is completely wrong! Filter code should never call ErrorHandler directly!
722 ErrorHandler::HandleError( nRet );
723 if( IsWarning( nRet ) )
724 nRet = 0;
729 // clear unused named items from item pool
731 uno::Reference< lang::XMultiServiceFactory> xModelFactory( mxModel, uno::UNO_QUERY );
732 if( xModelFactory.is() )
736 const OUString aName("~clear~" );
737 uno::Reference< container::XNameContainer > xGradient( xModelFactory->createInstance( "com.sun.star.drawing.GradientTable" ), uno::UNO_QUERY );
738 if( xGradient.is() )
739 xGradient->removeByName( aName );
741 uno::Reference< container::XNameContainer > xHatch( xModelFactory->createInstance( "com.sun.star.drawing.HatchTable" ), uno::UNO_QUERY );
742 if( xHatch.is() )
743 xHatch->removeByName( aName );
745 uno::Reference< container::XNameContainer > xBitmap( xModelFactory->createInstance( "com.sun.star.drawing.BitmapTable" ), uno::UNO_QUERY );
746 if( xBitmap.is() )
747 xBitmap->removeByName( aName );
749 uno::Reference< container::XNameContainer > xTransGradient( xModelFactory->createInstance( "com.sun.star.drawing.TransparencyGradientTable" ), uno::UNO_QUERY );
750 if( xTransGradient.is() )
751 xTransGradient->removeByName( aName );
753 uno::Reference< container::XNameContainer > xMarker( xModelFactory->createInstance( "com.sun.star.drawing.MarkerTable" ), uno::UNO_QUERY );
754 if( xMarker.is() )
755 xMarker->removeByName( aName );
757 uno::Reference< container::XNameContainer > xDashes( xModelFactory->createInstance( "com.sun.star.drawing.DashTable" ), uno::UNO_QUERY );
758 if( xDashes.is() )
759 xDashes->removeByName( aName );
761 catch (const Exception&)
763 OSL_FAIL("sd::SdXMLFilter::Import(), exception during clearing of unused named items");
767 // set BuildId on XModel for later OLE object loading
768 if( xInfoSet.is() )
770 uno::Reference< beans::XPropertySet > xModelSet( mxModel, uno::UNO_QUERY );
771 if( xModelSet.is() )
773 uno::Reference< beans::XPropertySetInfo > xModelSetInfo( xModelSet->getPropertySetInfo() );
774 const OUString sPropName( "BuildId" );
776 OUString sBuildId;
777 xInfoSet->getPropertyValue(sPropName) >>= sBuildId;
779 if( xModelSetInfo.is() && xModelSetInfo->hasPropertyByName(sPropName) )
781 xModelSet->setPropertyValue( sPropName, Any( sBuildId ) );
784 bool bTransform = false;
786 if( nRet == 0 )
788 if( !sBuildId.isEmpty() )
790 sal_Int32 nIndex = sBuildId.indexOf('$');
791 if( nIndex != -1 )
793 sal_Int32 nUPD = sBuildId.copy( 0, nIndex ).toInt32();
795 if( nUPD == 300 )
797 sal_Int32 nBuildId = sBuildId.copy( nIndex+1 ).toInt32();
798 if( (nBuildId > 0) && (nBuildId < 9316) )
799 bTransform = true; // treat OOo 3.0 beta1 as OOo 2.x
801 else if( (nUPD == 680) || ( nUPD >= 640 && nUPD <= 645 ) )
802 bTransform = true;
805 else
807 // check for binary formats
808 const SfxFilter * pFilter = mrMedium.GetFilter();
809 if( pFilter )
811 const String& rTypeName = pFilter->GetRealTypeName();
813 if( (rTypeName.CompareToAscii( RTL_CONSTASCII_STRINGPARAM("impress_StarImpress" ) ) == 0) ||
814 (rTypeName.CompareToAscii( RTL_CONSTASCII_STRINGPARAM("draw_StarDraw" ) ) == 0) )
816 bTransform = true;
822 if( bTransform )
823 TransformOOo2xDocument( pDoc );
827 pDoc->EnableUndo(true);
828 mrDocShell.ClearUndoBuffer();
829 return nRet == 0;
832 // -----------------------------------------------------------------------------
834 sal_Bool SdXMLFilter::Export()
836 #ifdef TIMELOG
837 RTL_LOGFILE_CONTEXT_AUTHOR ( aLog, "sd", "cl93746", "SdXMLFilter::Export" );
838 OString aFile(OUStringToOString(mrMedium.GetName(), RTL_TEXTENCODING_ASCII_US));
839 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "exporting %s", aFile.getStr() );
840 #endif
842 SvXMLEmbeddedObjectHelper* pObjectHelper = NULL;
843 SvXMLGraphicHelper* pGraphicHelper = NULL;
844 sal_Bool bDocRet = sal_False;
846 if( !mxModel.is() )
848 OSL_FAIL("Got NO Model in XMLExport");
849 return sal_False;
852 sal_Bool bLocked = mxModel->hasControllersLocked();
856 mxModel->lockControllers();
858 uno::Reference< lang::XServiceInfo > xServiceInfo( mxModel, uno::UNO_QUERY );
860 if( !xServiceInfo.is() || !xServiceInfo->supportsService( "com.sun.star.drawing.GenericDrawingDocument" ) )
862 OSL_FAIL( "Model is no DrawingDocument in XMLExport" );
863 return sal_False;
866 uno::Reference<uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
868 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
870 /** property map for export info set */
871 PropertyMapEntry aExportInfoMap[] =
873 { MAP_LEN( "ProgressRange" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
874 { MAP_LEN( "ProgressMax" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
875 { MAP_LEN( "ProgressCurrent" ), 0, &::getCppuType((const sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
876 { MAP_LEN( "UsePrettyPrinting"),0, &::getBooleanCppuType(), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
878 { MAP_LEN( "PageLayoutNames" ), 0, SEQTYPE(::getCppuType((const OUString*)0)), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
879 { MAP_LEN( "BaseURI" ), 0,
880 &::getCppuType( (OUString *)0 ),
881 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
882 { MAP_LEN( "StreamRelPath" ), 0,
883 &::getCppuType( (OUString *)0 ),
884 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
885 { MAP_LEN( "StreamName" ), 0,
886 &::getCppuType( (OUString *)0 ),
887 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
888 { MAP_LEN( "StyleNames" ), 0,
889 &::getCppuType( (Sequence<OUString>*)0 ),
890 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
891 { MAP_LEN( "StyleFamilies" ), 0,
892 &::getCppuType( (Sequence<sal_Int32>*)0 ),
893 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
894 { MAP_LEN( "TargetStorage" ), 0, &embed::XStorage::static_type(),
895 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
897 { NULL, 0, 0, NULL, 0, 0 }
900 uno::Reference< beans::XPropertySet > xInfoSet( GenericPropertySet_CreateInstance( new PropertySetInfo( aExportInfoMap ) ) );
903 SvtSaveOptions aSaveOpt;
904 OUString sUsePrettyPrinting("UsePrettyPrinting");
905 sal_Bool bUsePrettyPrinting( aSaveOpt.IsPrettyPrinting() );
906 xInfoSet->setPropertyValue( sUsePrettyPrinting, makeAny( bUsePrettyPrinting ) );
908 const uno::Reference < embed::XStorage >& xStorage = mrMedium.GetOutputStorage();
910 // Set base URI
911 OUString sPropName( "BaseURI" );
912 xInfoSet->setPropertyValue( sPropName, makeAny( mrMedium.GetBaseURL( true ) ) );
914 OUString sTargetStorage( "TargetStorage" );
915 xInfoSet->setPropertyValue( sTargetStorage, Any( xStorage ) );
917 if( SFX_CREATE_MODE_EMBEDDED == mrDocShell.GetCreateMode() )
919 OUString aName;
920 if ( mrMedium.GetItemSet() )
922 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
923 mrMedium.GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
924 if ( pDocHierarchItem )
925 aName = pDocHierarchItem->GetValue();
928 if( !aName.isEmpty() )
930 sPropName = OUString( "StreamRelPath" );
931 xInfoSet->setPropertyValue( sPropName, makeAny( aName ) );
935 // initialize descriptor
936 uno::Sequence< beans::PropertyValue > aDescriptor( 1 );
937 beans::PropertyValue* pProps = aDescriptor.getArray();
939 pProps[0].Name = "FileName";
940 pProps[0].Value <<= OUString( mrMedium.GetName() );
943 uno::Reference< document::XEmbeddedObjectResolver > xObjectResolver;
944 uno::Reference< document::XGraphicObjectResolver > xGrfResolver;
946 // create helper for graphic and ole export if we have a storage
947 if( xStorage.is() )
949 pObjectHelper = SvXMLEmbeddedObjectHelper::Create( xStorage, *mrDocShell.GetDoc()->GetPersist(), EMBEDDEDOBJECTHELPER_MODE_WRITE, sal_False );
950 xObjectResolver = pObjectHelper;
952 pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_WRITE, sal_False );
953 xGrfResolver = pGraphicHelper;
956 if(mbShowProgress)
958 CreateStatusIndicator();
959 if(mxStatusIndicator.is())
961 sal_Int32 nProgressRange(1000000);
962 sal_Int32 nProgressCurrent(0);
963 OUString aMsg = String( SdResId( STR_SAVE_DOC ) );
964 mxStatusIndicator->start(aMsg, nProgressRange);
966 // set ProgressRange
967 uno::Any aProgRange;
968 aProgRange <<= nProgressRange;
969 xInfoSet->setPropertyValue( "ProgressRange" , aProgRange);
971 // set ProgressCurrent
972 uno::Any aProgCurrent;
973 aProgCurrent <<= nProgressCurrent;
974 xInfoSet->setPropertyValue( "ProgressCurrent" , aProgCurrent);
978 uno::Reference< lang::XComponent > xComponent( mxModel, uno::UNO_QUERY );
980 XML_SERVICES* pServiceNames = getServices( false, IsDraw(), mnStoreVer );
982 XML_SERVICEMAP aServices[5]; sal_uInt16 i = 0;
983 aServices[i ].mpService = pServiceNames->mpStyles;
984 aServices[i++].mpStream = sXML_styleStreamName;
986 aServices[i ].mpService = pServiceNames->mpContent;
987 aServices[i++].mpStream = sXML_contentStreamName;
989 aServices[i ].mpService = pServiceNames->mpSettings;
990 aServices[i++].mpStream = sXML_settingsStreamName;
992 if( mrDocShell.GetCreateMode() != SFX_CREATE_MODE_EMBEDDED )
994 aServices[i ].mpService = pServiceNames->mpMeta;
995 aServices[i++].mpStream = sXML_metaStreamName;
998 aServices[i].mpService = NULL;
999 aServices[i].mpStream = NULL;
1001 XML_SERVICEMAP* pServices = aServices;
1003 // doc export
1006 RTL_LOGFILE_CONTEXT_TRACE1( aLog, "exporting substream %s", pServices->mpStream );
1008 uno::Reference<io::XOutputStream> xDocOut;
1009 if( xStorage.is() )
1011 const OUString sDocName( OUString::createFromAscii( pServices->mpStream ) );
1012 uno::Reference<io::XStream> xStream =
1013 xStorage->openStreamElement( sDocName,
1014 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
1016 DBG_ASSERT(xStream.is(), "Can't create output stream in package!");
1017 if( !xStream.is() )
1018 return sal_False;
1020 xDocOut = xStream->getOutputStream();
1021 Reference <beans::XPropertySet > xProps( xStream, uno::UNO_QUERY );
1022 if( !xDocOut.is() || !xProps.is() )
1023 return sal_False;
1025 uno::Any aAny; aAny <<= OUString( "text/xml");
1026 xProps->setPropertyValue( "MediaType" , aAny);
1028 // encrypt all streams
1029 xProps->setPropertyValue( "UseCommonStoragePasswordEncryption",
1030 uno::makeAny( (sal_Bool)sal_True ) );
1032 const OUString sStreamName( "StreamName");
1033 xInfoSet->setPropertyValue( sStreamName, Any( sDocName ) );
1036 uno::Reference< io::XActiveDataSource > xDocSrc( xWriter, uno::UNO_QUERY );
1037 xDocSrc->setOutputStream( xDocOut );
1039 uno::Sequence< uno::Any > aArgs( 2 + ( mxStatusIndicator.is() ? 1 : 0 ) + ( xGrfResolver.is() ? 1 : 0 ) + ( xObjectResolver.is() ? 1 : 0 ) );
1040 uno::Any* pArgs = aArgs.getArray();
1041 *pArgs++ <<= xInfoSet;
1042 if( xGrfResolver.is() ) *pArgs++ <<= xGrfResolver;
1043 if( xObjectResolver.is() ) *pArgs++ <<= xObjectResolver;
1044 if( mxStatusIndicator.is() ) *pArgs++ <<= mxStatusIndicator;
1046 *pArgs <<= xWriter;
1048 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pServices->mpService ), aArgs, xContext ), uno::UNO_QUERY );
1049 if( xFilter.is() )
1051 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
1052 if( xExporter.is() )
1054 xExporter->setSourceDocument( xComponent );
1055 // outputstream will be closed by SAX parser
1056 bDocRet = xFilter->filter( aDescriptor );
1060 pServices++;
1062 while( bDocRet && pServices->mpService );
1064 if(mbShowProgress)
1066 if(mxStatusIndicator.is())
1067 mxStatusIndicator->end();
1071 catch (const uno::Exception &e)
1073 #if OSL_DEBUG_LEVEL > 1
1074 OStringBuffer aError(RTL_CONSTASCII_STRINGPARAM(
1075 "uno Exception caught while exporting:\n"));
1076 aError.append(OUStringToOString(e.Message,
1077 RTL_TEXTENCODING_ASCII_US));
1078 OSL_FAIL(aError.getStr());
1079 #else
1080 (void)e;
1081 #endif
1082 bDocRet = sal_False;
1084 if ( !bLocked )
1085 mxModel->unlockControllers();
1087 if( pGraphicHelper )
1088 SvXMLGraphicHelper::Destroy( pGraphicHelper );
1090 if( pObjectHelper )
1091 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
1093 return bDocRet;
1096 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */