xref: /AOO41X/main/sc/source/filter/xml/xmlwrap.cxx (revision 5d6e898735fe9aed853a5448ee977ff30fbbd453)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 
28 
29 // INCLUDE ---------------------------------------------------------------
30 
31 #include <rsc/rscsfx.hxx>
32 #include <sfx2/docfile.hxx>
33 #include <sfx2/objsh.hxx>
34 #include <tools/debug.hxx>
35 #include <vos/xception.hxx>
36 #include <comphelper/processfactory.hxx>
37 #include <unotools/streamwrap.hxx>
38 #include <svx/xmlgrhlp.hxx>
39 #include <svtools/sfxecode.hxx>
40 #include <sfx2/frame.hxx>
41 #include <svl/itemset.hxx>
42 #include <svl/stritem.hxx>
43 #include <sfx2/sfxsids.hrc>
44 #include <tools/urlobj.hxx>
45 #include <com/sun/star/container/XChild.hpp>
46 #include <com/sun/star/beans/XPropertySetInfo.hpp>
47 #include <com/sun/star/xml/sax/XErrorHandler.hpp>
48 #include <com/sun/star/xml/sax/XEntityResolver.hpp>
49 #include <com/sun/star/xml/sax/InputSource.hpp>
50 #include <com/sun/star/xml/sax/XDTDHandler.hpp>
51 #include <com/sun/star/xml/sax/XParser.hpp>
52 #include <com/sun/star/io/XActiveDataSource.hpp>
53 #include <com/sun/star/io/XActiveDataControl.hpp>
54 #include <com/sun/star/frame/XModel.hpp>
55 #include <com/sun/star/task/XStatusIndicatorFactory.hpp>
56 #include <com/sun/star/beans/PropertyAttribute.hpp>
57 #include <comphelper/extract.hxx>
58 #include <comphelper/propertysetinfo.hxx>
59 #include <comphelper/genericpropertyset.hxx>
60 #include <com/sun/star/container/XNameContainer.hpp>
61 #include <com/sun/star/lang/DisposedException.hpp>
62 #include <com/sun/star/packages/zip/ZipIOException.hpp>
63 #include <com/sun/star/embed/ElementModes.hpp>
64 #include <com/sun/star/script/vba/XVBACompatibility.hpp>
65 #include <com/sun/star/rdf/XDocumentMetadataAccess.hpp>
66 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
67 
68 #include <sfx2/DocumentMetadataAccess.hxx>
69 #include <comphelper/componentcontext.hxx>
70 #include <comphelper/documentconstants.hxx>
71 #include <svx/xmleohlp.hxx>
72 #include <rtl/logfile.hxx>
73 #include <unotools/saveopt.hxx>
74 
75 #include "document.hxx"
76 #include "xmlwrap.hxx"
77 #include "xmlimprt.hxx"
78 #include "xmlexprt.hxx"
79 #include "global.hxx"
80 #include "globstr.hrc"
81 #include "scerrors.hxx"
82 #include "XMLExportSharedData.hxx"
83 #include "docuno.hxx"
84 #include "sheetdata.hxx"
85 #include "XMLCodeNameProvider.hxx"
86 
87 #define MAP_LEN(x) x, sizeof(x) - 1
88 
89 using namespace com::sun::star;
90 using ::rtl::OUString;
91 
92 // -----------------------------------------------------------------------
93 
ScXMLImportWrapper(ScDocument & rD,SfxMedium * pM,const uno::Reference<embed::XStorage> & xStor)94 ScXMLImportWrapper::ScXMLImportWrapper(ScDocument& rD, SfxMedium* pM, const uno::Reference < embed::XStorage >& xStor ) :
95     rDoc(rD),
96     pMedium(pM),
97     xStorage(xStor)
98 {
99     DBG_ASSERT( pMedium || xStorage.is(), "ScXMLImportWrapper: Medium or Storage must be set" );
100 }
101 
102 //UNUSED2008-05  uno::Reference <task::XStatusIndicator> ScXMLImportWrapper::GetStatusIndicator(
103 //UNUSED2008-05          uno::Reference < frame::XModel> & rModel)
104 //UNUSED2008-05  {
105 //UNUSED2008-05      DBG_ERROR( "The status indicator from medium must be used!" );
106 //UNUSED2008-05
107 //UNUSED2008-05      uno::Reference<task::XStatusIndicator> xStatusIndicator;
108 //UNUSED2008-05
109 //UNUSED2008-05      if (rModel.is())
110 //UNUSED2008-05      {
111 //UNUSED2008-05          uno::Reference<frame::XController> xController( rModel->getCurrentController());
112 //UNUSED2008-05          if ( xController.is())
113 //UNUSED2008-05          {
114 //UNUSED2008-05              uno::Reference<task::XStatusIndicatorFactory> xFactory( xController->getFrame(), uno::UNO_QUERY );
115 //UNUSED2008-05              if ( xFactory.is())
116 //UNUSED2008-05              {
117 //UNUSED2008-05                  try
118 //UNUSED2008-05                  {
119 //UNUSED2008-05                      xStatusIndicator.set(xFactory->createStatusIndicator());
120 //UNUSED2008-05                  }
121 //UNUSED2008-05                  catch ( lang::DisposedException e )
122 //UNUSED2008-05                  {
123 //UNUSED2008-05                      DBG_ERROR("Exception while trying to get a Status Indicator");
124 //UNUSED2008-05                  }
125 //UNUSED2008-05              }
126 //UNUSED2008-05          }
127 //UNUSED2008-05      }
128 //UNUSED2008-05      return xStatusIndicator;
129 //UNUSED2008-05  }
130 
GetStatusIndicator()131 uno::Reference <task::XStatusIndicator> ScXMLImportWrapper::GetStatusIndicator()
132 {
133     uno::Reference<task::XStatusIndicator> xStatusIndicator;
134     if (pMedium)
135     {
136         SfxItemSet* pSet = pMedium->GetItemSet();
137         if (pSet)
138         {
139             const SfxUnoAnyItem* pItem = static_cast<const SfxUnoAnyItem*>(pSet->GetItem(SID_PROGRESS_STATUSBAR_CONTROL));
140             if (pItem)
141                 xStatusIndicator.set(pItem->GetValue(), uno::UNO_QUERY);
142         }
143     }
144     return xStatusIndicator;
145 }
146 
ImportFromComponent(uno::Reference<lang::XMultiServiceFactory> & xServiceFactory,uno::Reference<frame::XModel> & xModel,uno::Reference<uno::XInterface> & xXMLParser,xml::sax::InputSource & aParserInput,const rtl::OUString & sComponentName,const rtl::OUString & sDocName,const rtl::OUString & sOldDocName,uno::Sequence<uno::Any> & aArgs,sal_Bool bMustBeSuccessfull)147 sal_uInt32 ScXMLImportWrapper::ImportFromComponent(uno::Reference<lang::XMultiServiceFactory>& xServiceFactory,
148     uno::Reference<frame::XModel>& xModel, uno::Reference<uno::XInterface>& xXMLParser,
149     xml::sax::InputSource& aParserInput,
150     const rtl::OUString& sComponentName, const rtl::OUString& sDocName,
151     const rtl::OUString& sOldDocName, uno::Sequence<uno::Any>& aArgs,
152     sal_Bool bMustBeSuccessfull)
153 {
154     uno::Reference < io::XStream > xDocStream;
155     if ( !xStorage.is() && pMedium )
156         xStorage = pMedium->GetStorage();
157 
158     // Get data source ...
159 
160 //  uno::Reference< uno::XInterface > xPipe;
161 //  uno::Reference< io::XActiveDataSource > xSource;
162 
163     sal_Bool bEncrypted = sal_False;
164     rtl::OUString sStream(sDocName);
165     if( xStorage.is() )
166     {
167         try
168         {
169             uno::Reference < container::XNameAccess > xAccess( xStorage, uno::UNO_QUERY );
170             if ( xAccess->hasByName(sDocName) && xStorage->isStreamElement( sDocName) )
171                 xDocStream = xStorage->openStreamElement( sDocName, embed::ElementModes::READ );
172             else if (sOldDocName.getLength() && xAccess->hasByName(sOldDocName) && xStorage->isStreamElement( sOldDocName) )
173             {
174                 xDocStream = xStorage->openStreamElement( sOldDocName, embed::ElementModes::READ );
175                 sStream = sOldDocName;
176             }
177             else
178                 return sal_False;
179 
180             aParserInput.aInputStream = xDocStream->getInputStream();
181             uno::Reference < beans::XPropertySet > xSet( xDocStream, uno::UNO_QUERY );
182 
183             uno::Any aAny = xSet->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM("Encrypted") ) );
184             aAny >>= bEncrypted;
185         }
186         catch( packages::WrongPasswordException& )
187         {
188             return ERRCODE_SFX_WRONGPASSWORD;
189         }
190         catch( packages::zip::ZipIOException& )
191         {
192             return ERRCODE_IO_BROKENPACKAGE;
193         }
194         catch( uno::Exception& )
195         {
196             return SCERR_IMPORT_UNKNOWN;
197         }
198     }
199     // #99667#; no longer necessary
200 /*  else if ( pMedium )
201     {
202         // if there is a medium and if this medium has a load environment,
203         // we get an active data source from the medium.
204         pMedium->GetInStream()->Seek( 0 );
205         xSource = pMedium->GetDataSource();
206         DBG_ASSERT( xSource.is(), "got no data source from medium" );
207         if( !xSource.is() )
208             return sal_False;
209 
210         // get a pipe for connecting the data source to the parser
211         xPipe = xServiceFactory->createInstance(
212                 OUString::createFromAscii("com.sun.star.io.Pipe") );
213         DBG_ASSERT( xPipe.is(),
214                 "XMLReader::Read: com.sun.star.io.Pipe service missing" );
215         if( !xPipe.is() )
216             return sal_False;
217 
218         // connect pipe's output stream to the data source
219         uno::Reference<io::XOutputStream> xPipeOutput( xPipe, uno::UNO_QUERY );
220         xSource->setOutputStream( xPipeOutput );
221 
222         aParserInput.aInputStream =
223             uno::Reference< io::XInputStream >( xPipe, uno::UNO_QUERY );
224     }*/
225     else
226         return SCERR_IMPORT_UNKNOWN;
227 
228     // set Base URL
229     uno::Reference< beans::XPropertySet > xInfoSet;
230     if( aArgs.getLength() > 0 )
231         aArgs.getConstArray()[0] >>= xInfoSet;
232     DBG_ASSERT( xInfoSet.is(), "missing property set" );
233     if( xInfoSet.is() )
234     {
235         rtl::OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("StreamName") );
236         xInfoSet->setPropertyValue( sPropName, uno::makeAny( sStream ) );
237     }
238 
239     sal_uInt32 nReturn(0);
240     rDoc.SetRangeOverflowType(0);   // is modified by the importer if limits are exceeded
241 
242     uno::Reference<xml::sax::XDocumentHandler> xDocHandler(
243         xServiceFactory->createInstanceWithArguments(
244             sComponentName, aArgs ),
245         uno::UNO_QUERY );
246     DBG_ASSERT( xDocHandler.is(), "can't get Calc importer" );
247     uno::Reference<document::XImporter> xImporter( xDocHandler, uno::UNO_QUERY );
248     uno::Reference<lang::XComponent> xComponent( xModel, uno::UNO_QUERY );
249     if (xImporter.is())
250         xImporter->setTargetDocument( xComponent );
251 
252     // connect parser and filter
253     uno::Reference<xml::sax::XParser> xParser( xXMLParser, uno::UNO_QUERY );
254     xParser->setDocumentHandler( xDocHandler );
255 
256     // parse
257 /*  if( xSource.is() )
258     {
259         uno::Reference<io::XActiveDataControl> xSourceControl( xSource, uno::UNO_QUERY );
260         if( xSourceControl.is() )
261             xSourceControl->start();
262     }*/
263 
264     try
265     {
266         xParser->parseStream( aParserInput );
267     }
268     catch( xml::sax::SAXParseException& r )
269     {
270         // sax parser sends wrapped exceptions,
271         // try to find the original one
272         xml::sax::SAXException aSaxEx = *(xml::sax::SAXException*)(&r);
273         sal_Bool bTryChild = sal_True;
274 
275         while( bTryChild )
276         {
277             xml::sax::SAXException aTmp;
278             if ( aSaxEx.WrappedException >>= aTmp )
279                 aSaxEx = aTmp;
280             else
281                 bTryChild = sal_False;
282         }
283 
284         packages::zip::ZipIOException aBrokenPackage;
285         if ( aSaxEx.WrappedException >>= aBrokenPackage )
286             return ERRCODE_IO_BROKENPACKAGE;
287         else if( bEncrypted )
288             nReturn = ERRCODE_SFX_WRONGPASSWORD;
289         else
290         {
291 
292 #ifdef DBG_UTIL
293             ByteString aError( "SAX parse exception catched while importing:\n" );
294             aError += ByteString( String( r.Message), RTL_TEXTENCODING_ASCII_US );
295             DBG_ERROR( aError.GetBuffer() );
296 #endif
297 
298             String sErr( String::CreateFromInt32( r.LineNumber ));
299             sErr += ',';
300             sErr += String::CreateFromInt32( r.ColumnNumber );
301 
302             if( sDocName.getLength() )
303             {
304                 nReturn = *new TwoStringErrorInfo(
305                                 (bMustBeSuccessfull ? SCERR_IMPORT_FILE_ROWCOL
306                                                     : SCWARN_IMPORT_FILE_ROWCOL),
307                                 sDocName, sErr,
308                                 ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
309             }
310             else
311             {
312                 DBG_ASSERT( bMustBeSuccessfull, "Warnings are not supported" );
313                 nReturn = *new StringErrorInfo( SCERR_IMPORT_FORMAT_ROWCOL, sErr,
314                                  ERRCODE_BUTTON_OK | ERRCODE_MSG_ERROR );
315             }
316         }
317     }
318     catch( xml::sax::SAXException& r )
319     {
320         packages::zip::ZipIOException aBrokenPackage;
321         if ( r.WrappedException >>= aBrokenPackage )
322             return ERRCODE_IO_BROKENPACKAGE;
323         else if( bEncrypted )
324             nReturn = ERRCODE_SFX_WRONGPASSWORD;
325         else
326         {
327 
328 #ifdef DBG_UTIL
329             ByteString aError( "SAX exception catched while importing:\n" );
330             aError += ByteString( String( r.Message), RTL_TEXTENCODING_ASCII_US );
331             DBG_ERROR( aError.GetBuffer() );
332 #endif
333             (void)r;    // avoid warning in product version
334 
335             nReturn = SCERR_IMPORT_FORMAT;
336         }
337     }
338     catch( packages::zip::ZipIOException& r )
339     {
340 #ifdef DBG_UTIL
341         ByteString aError( "Zip exception catched while importing:\n" );
342         aError += ByteString( String( r.Message), RTL_TEXTENCODING_ASCII_US );
343         DBG_ERROR( aError.GetBuffer() );
344 #endif
345         (void)r;    // avoid warning in product version
346 
347         nReturn = ERRCODE_IO_BROKENPACKAGE;
348     }
349     catch( io::IOException& r )
350     {
351 #ifdef DBG_UTIL
352         ByteString aError( "IO exception catched while importing:\n" );
353         aError += ByteString( String( r.Message), RTL_TEXTENCODING_ASCII_US );
354         DBG_ERROR( aError.GetBuffer() );
355 #endif
356         (void)r;    // avoid warning in product version
357 
358         nReturn = SCERR_IMPORT_OPEN;
359     }
360     catch( uno::Exception& r )
361     {
362 #ifdef DBG_UTIL
363         ByteString aError( "uno exception catched while importing:\n" );
364         aError += ByteString( String( r.Message), RTL_TEXTENCODING_ASCII_US );
365         DBG_ERROR( aError.GetBuffer() );
366 #endif
367         (void)r;    // avoid warning in product version
368 
369         nReturn = SCERR_IMPORT_UNKNOWN;
370     }
371 
372     // #i31130# Can't use getImplementation here to get the ScXMLImport from xDocHandler,
373     // because when OOo 1.x files are loaded, xDocHandler is the OOo2OasisTransformer.
374     // So the overflow warning ErrorCode is now stored in the document.
375     // Export works differently, there getImplementation still works.
376 
377     if (rDoc.HasRangeOverflow() && !nReturn)
378         nReturn = rDoc.GetRangeOverflowType();
379 
380     // free the component
381     xParser->setDocumentHandler( NULL );
382 
383     // success!
384     return nReturn;
385 }
386 
Import(sal_Bool bStylesOnly,ErrCode & nError)387 sal_Bool ScXMLImportWrapper::Import(sal_Bool bStylesOnly, ErrCode& nError)
388 {
389     RTL_LOGFILE_CONTEXT_AUTHOR ( aLog, "sc", "sb99857", "ScXMLImportWrapper::Import" );
390 
391     uno::Reference<lang::XMultiServiceFactory> xServiceFactory =
392                                         comphelper::getProcessServiceFactory();
393     DBG_ASSERT( xServiceFactory.is(), "got no service manager" );
394     if( !xServiceFactory.is() )
395         return sal_False;
396 
397     xml::sax::InputSource aParserInput;
398     if (pMedium)
399         aParserInput.sSystemId = OUString(pMedium->GetName());
400 
401     if ( !xStorage.is() && pMedium )
402         xStorage = pMedium->GetStorage();
403 
404     // get parser
405     uno::Reference<uno::XInterface> xXMLParser(
406         xServiceFactory->createInstance(
407             OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Parser" )) ));
408     DBG_ASSERT( xXMLParser.is(), "com.sun.star.xml.sax.Parser service missing" );
409     if( !xXMLParser.is() )
410         return sal_False;
411 
412     // get filter
413     SfxObjectShell* pObjSh = rDoc.GetDocumentShell();
414     if ( pObjSh )
415     {
416         rtl::OUString sEmpty;
417         uno::Reference<frame::XModel> xModel(pObjSh->GetModel());
418 
419         /** property map for export info set */
420         comphelper::PropertyMapEntry aImportInfoMap[] =
421         {
422             { MAP_LEN( "ProgressRange" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
423             { MAP_LEN( "ProgressMax" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
424             { MAP_LEN( "ProgressCurrent" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
425             { MAP_LEN( "NumberStyles" ), 0, &::getCppuType((uno::Reference<container::XNameAccess> *)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
426             { MAP_LEN( "PrivateData" ), 0, &::getCppuType( (uno::Reference<uno::XInterface> *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
427             { MAP_LEN( "BaseURI" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
428             { MAP_LEN( "StreamRelPath" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
429             { MAP_LEN( "StreamName" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
430             { MAP_LEN( "BuildId" ), 0, &::getCppuType( (OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
431             { MAP_LEN( "VBACompatibilityMode" ), 0, &::getBooleanCppuType(), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
432             { MAP_LEN( "ScriptConfiguration" ), 0, &::getCppuType((uno::Reference<container::XNameAccess> *)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
433             { MAP_LEN( "OrganizerMode" ), 0, &::getBooleanCppuType(),
434                 ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
435 
436             { NULL, 0, 0, NULL, 0, 0 }
437         };
438         uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aImportInfoMap ) ) );
439 
440         // ---- get BuildId from parent container if available
441 
442         uno::Reference< container::XChild > xChild( xModel, uno::UNO_QUERY );
443         if( xChild.is() )
444         {
445             uno::Reference< beans::XPropertySet > xParentSet( xChild->getParent(), uno::UNO_QUERY );
446             if( xParentSet.is() )
447             {
448                 uno::Reference< beans::XPropertySetInfo > xPropSetInfo( xParentSet->getPropertySetInfo() );
449                 OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("BuildId" ) );
450                 if( xPropSetInfo.is() && xPropSetInfo->hasPropertyByName(sPropName) )
451                 {
452                     xInfoSet->setPropertyValue( sPropName, xParentSet->getPropertyValue(sPropName) );
453                 }
454             }
455         }
456 
457         // -------------------------------------
458 
459         uno::Reference<task::XStatusIndicator> xStatusIndicator(GetStatusIndicator());
460         if (xStatusIndicator.is())
461         {
462             sal_Int32 nProgressRange(1000000);
463             xStatusIndicator->start(rtl::OUString(ScGlobal::GetRscString(STR_LOAD_DOC)), nProgressRange);
464             xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ProgressRange")), uno::makeAny(nProgressRange));
465         }
466 
467         // Set base URI
468         OSL_ENSURE( pMedium, "There is no medium to get MediaDescriptor from!\n" );
469         ::rtl::OUString aBaseURL = pMedium ? pMedium->GetBaseURL() : ::rtl::OUString();
470         rtl::OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("BaseURI") );
471         xInfoSet->setPropertyValue( sPropName, uno::makeAny( aBaseURL ) );
472 
473         // TODO/LATER: do not do it for embedded links
474         OUString aName;
475         if( SFX_CREATE_MODE_EMBEDDED == pObjSh->GetCreateMode() )
476         {
477             if ( pMedium && pMedium->GetItemSet() )
478             {
479                 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
480                     pMedium->GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
481                 if ( pDocHierarchItem )
482                     aName = pDocHierarchItem->GetValue();
483             }
484             else
485                 aName = ::rtl::OUString::createFromAscii( "dummyObjectName" );
486 
487             if( aName.getLength() )
488             {
489                 sPropName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamRelPath"));
490                 xInfoSet->setPropertyValue( sPropName, uno::makeAny( aName ) );
491             }
492         }
493 
494         if (bStylesOnly)
495         {
496             ::rtl::OUString const sOrganizerMode(
497                 RTL_CONSTASCII_USTRINGPARAM("OrganizerMode"));
498             xInfoSet->setPropertyValue(sOrganizerMode, uno::makeAny(sal_True));
499         }
500 
501         sal_Bool bOasis = ( SotStorage::GetVersion( xStorage ) > SOFFICE_FILEFORMAT_60 );
502 
503         if (!bStylesOnly && bOasis)
504         {
505             // RDF metadata: ODF >= 1.2
506             try
507             {
508                 ::comphelper::ComponentContext aContext( xServiceFactory );
509                 const uno::Reference< rdf::XDocumentMetadataAccess > xDMA(
510                     xModel, uno::UNO_QUERY_THROW );
511                 const uno::Reference< rdf::XURI > xBaseURI(
512                     ::sfx2::createBaseURI( aContext.getUNOContext(), xStorage, aBaseURL, aName ) );
513                 const uno::Reference< task::XInteractionHandler > xHandler(
514                     pObjSh->GetMedium()->GetInteractionHandler() );
515                 xDMA->loadMetadataFromStorage( xStorage, xBaseURI, xHandler );
516             }
517             catch (lang::WrappedTargetException & e)
518             {
519                 ucb::InteractiveAugmentedIOException iaioe;
520                 if ( e.TargetException >>= iaioe )
521                 {
522                     nError = SCERR_IMPORT_UNKNOWN;
523                 }
524                 else
525                 {
526                     nError = SCWARN_IMPORT_FEATURES_LOST;
527                 }
528             }
529             catch (uno::Exception &)
530             {
531                 nError = SCWARN_IMPORT_FEATURES_LOST;
532             }
533         }
534 
535         // #i103539#: always read meta.xml for generator
536         sal_uInt32 nMetaRetval(0);
537         uno::Sequence<uno::Any> aMetaArgs(1);
538         uno::Any* pMetaArgs = aMetaArgs.getArray();
539         pMetaArgs[0] <<= xInfoSet;
540 
541         RTL_LOGFILE_CONTEXT_TRACE( aLog, "meta import start" );
542 
543         nMetaRetval = ImportFromComponent(
544             xServiceFactory, xModel, xXMLParser, aParserInput,
545             bOasis ? rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisMetaImporter"))
546                    : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLMetaImporter")),
547             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("meta.xml")),
548             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Meta.xml")), aMetaArgs,
549             sal_False);
550 
551         RTL_LOGFILE_CONTEXT_TRACE( aLog, "meta import end" );
552 
553         SvXMLGraphicHelper* pGraphicHelper = NULL;
554         uno::Reference< document::XGraphicObjectResolver > xGrfContainer;
555 
556         uno::Reference< document::XEmbeddedObjectResolver > xObjectResolver;
557         SvXMLEmbeddedObjectHelper *pObjectHelper = NULL;
558 
559         if( xStorage.is() )
560         {
561             pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_READ );
562             xGrfContainer = pGraphicHelper;
563 
564             if( pObjSh )
565             {
566                 pObjectHelper = SvXMLEmbeddedObjectHelper::Create(xStorage, *pObjSh, EMBEDDEDOBJECTHELPER_MODE_READ, sal_False );
567                 xObjectResolver = pObjectHelper;
568             }
569         }
570         uno::Sequence<uno::Any> aStylesArgs(4);
571         uno::Any* pStylesArgs = aStylesArgs.getArray();
572         pStylesArgs[0] <<= xInfoSet;
573         pStylesArgs[1] <<= xGrfContainer;
574         pStylesArgs[2] <<= xStatusIndicator;
575         pStylesArgs[3] <<= xObjectResolver;
576 
577         sal_uInt32 nSettingsRetval(0);
578         if (!bStylesOnly)
579         {
580             //  Settings must be loaded first because of the printer setting,
581             //  which is needed in the page styles (paper tray).
582 
583             uno::Sequence<uno::Any> aSettingsArgs(1);
584             uno::Any* pSettingsArgs = aSettingsArgs.getArray();
585             pSettingsArgs[0] <<= xInfoSet;
586 
587             RTL_LOGFILE_CONTEXT_TRACE( aLog, "settings import start" );
588 
589             nSettingsRetval = ImportFromComponent(xServiceFactory, xModel, xXMLParser, aParserInput,
590                 bOasis ? rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisSettingsImporter"))
591                        : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLSettingsImporter")),
592                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("settings.xml")),
593                 sEmpty, aSettingsArgs, sal_False);
594 
595             RTL_LOGFILE_CONTEXT_TRACE( aLog, "settings import end" );
596         }
597 
598         sal_uInt32 nStylesRetval(0);
599         {
600             RTL_LOGFILE_CONTEXT_TRACE( aLog, "styles import start" );
601 
602             nStylesRetval = ImportFromComponent(xServiceFactory, xModel, xXMLParser, aParserInput,
603                 bOasis ? rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisStylesImporter"))
604                        : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLStylesImporter")),
605                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("styles.xml")),
606                 sEmpty, aStylesArgs, sal_True);
607 
608             RTL_LOGFILE_CONTEXT_TRACE( aLog, "styles import end" );
609         }
610 
611         sal_uInt32 nDocRetval(0);
612         if (!bStylesOnly)
613         {
614             uno::Sequence<uno::Any> aDocArgs(4);
615             uno::Any* pDocArgs = aDocArgs.getArray();
616             pDocArgs[0] <<= xInfoSet;
617             pDocArgs[1] <<= xGrfContainer;
618             pDocArgs[2] <<= xStatusIndicator;
619             pDocArgs[3] <<= xObjectResolver;
620 
621             RTL_LOGFILE_CONTEXT_TRACE( aLog, "content import start" );
622 
623             nDocRetval = ImportFromComponent(xServiceFactory, xModel, xXMLParser, aParserInput,
624                 bOasis ? rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisContentImporter"))
625                        : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLContentImporter")),
626                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("content.xml")),
627                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Content.xml")), aDocArgs,
628                 sal_True);
629 
630             RTL_LOGFILE_CONTEXT_TRACE( aLog, "content import end" );
631         }
632         if( pGraphicHelper )
633             SvXMLGraphicHelper::Destroy( pGraphicHelper );
634 
635         if( pObjectHelper )
636             SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
637 
638         if (xStatusIndicator.is())
639             xStatusIndicator->end();
640 
641         sal_Bool bRet(sal_False);
642         if (bStylesOnly)
643         {
644             if (nStylesRetval)
645                 nError = nStylesRetval;
646             else
647                 bRet = sal_True;
648         }
649         else
650         {
651             if (nDocRetval)
652             {
653                 nError = nDocRetval;
654                 if (nDocRetval == SCWARN_IMPORT_RANGE_OVERFLOW ||
655                     nDocRetval == SCWARN_IMPORT_ROW_OVERFLOW ||
656                     nDocRetval == SCWARN_IMPORT_COLUMN_OVERFLOW ||
657                     nDocRetval == SCWARN_IMPORT_SHEET_OVERFLOW)
658                     bRet = sal_True;
659             }
660             else if (nStylesRetval)
661                 nError = nStylesRetval;
662             else if (nMetaRetval)
663                 nError = nMetaRetval;
664             else if (nSettingsRetval)
665                 nError = nSettingsRetval;
666             else
667                 bRet = sal_True;
668         }
669 
670         // set BuildId on XModel for later OLE object loading
671         if( xInfoSet.is() )
672         {
673             uno::Reference< beans::XPropertySet > xModelSet( xModel, uno::UNO_QUERY );
674             if( xModelSet.is() )
675             {
676                 uno::Reference< beans::XPropertySetInfo > xModelSetInfo( xModelSet->getPropertySetInfo() );
677                 OUString sBuildPropName( RTL_CONSTASCII_USTRINGPARAM("BuildId" ) );
678                 if( xModelSetInfo.is() && xModelSetInfo->hasPropertyByName(sBuildPropName) )
679                 {
680                     xModelSet->setPropertyValue( sBuildPropName, xInfoSet->getPropertyValue(sBuildPropName) );
681                 }
682             }
683 
684             // Set Code Names
685             uno::Any aAny = xInfoSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("ScriptConfiguration") ));
686             uno::Reference <container::XNameAccess> xCodeNameAccess;
687             if( aAny >>= xCodeNameAccess )
688                 XMLCodeNameProvider::set( xCodeNameAccess, &rDoc );
689 
690             // VBA compatibility
691             bool bVBACompat = false;
692             if ( (xInfoSet->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("VBACompatibilityMode"))) >>= bVBACompat) && bVBACompat )
693             {
694                 /*  Set library container to VBA compatibility mode, this
695                     forces loading the Basic project, which in turn creates the
696                     VBA Globals object and does all related initialization. */
697                 if ( xModelSet.is() ) try
698                 {
699                     uno::Reference< script::vba::XVBACompatibility > xVBACompat( xModelSet->getPropertyValue(
700                         OUString( RTL_CONSTASCII_USTRINGPARAM( "BasicLibraries" ) ) ), uno::UNO_QUERY_THROW );
701                     xVBACompat->setVBACompatibilityMode( sal_True );
702                 }
703                 catch( uno::Exception& )
704                 {
705                 }
706             }
707         }
708 
709         // Don't test bStylesRetval and bMetaRetval, because it could be an older file which not contain such streams
710         return bRet;//!bStylesOnly ? bDocRetval : bStylesRetval;
711     }
712     return sal_False;
713 }
714 
lcl_HasValidStream(ScDocument & rDoc)715 bool lcl_HasValidStream(ScDocument& rDoc)
716 {
717     SfxObjectShell* pObjSh = rDoc.GetDocumentShell();
718     if ( pObjSh->IsDocShared() )
719         return false;                       // never copy stream from shared file
720 
721     // don't read remote file again
722     // (could instead re-use medium directly in that case)
723     SfxMedium* pSrcMed = rDoc.GetDocumentShell()->GetMedium();
724     if ( !pSrcMed || pSrcMed->IsRemote() )
725         return false;
726 
727     SCTAB nTabCount = rDoc.GetTableCount();
728     for (SCTAB nTab=0; nTab<nTabCount; ++nTab)
729         if (rDoc.IsStreamValid(nTab))
730             return true;
731     return false;
732 }
733 
ExportToComponent(uno::Reference<lang::XMultiServiceFactory> & xServiceFactory,uno::Reference<frame::XModel> & xModel,uno::Reference<uno::XInterface> & xWriter,uno::Sequence<beans::PropertyValue> & aDescriptor,const rtl::OUString & sName,const rtl::OUString & sMediaType,const rtl::OUString & sComponentName,const sal_Bool bPlainText,uno::Sequence<uno::Any> & aArgs,ScMySharedData * & pSharedData)734 sal_Bool ScXMLImportWrapper::ExportToComponent(uno::Reference<lang::XMultiServiceFactory>& xServiceFactory,
735     uno::Reference<frame::XModel>& xModel, uno::Reference<uno::XInterface>& xWriter,
736     uno::Sequence<beans::PropertyValue>& aDescriptor, const rtl::OUString& sName,
737     const rtl::OUString& sMediaType, const rtl::OUString& sComponentName,
738     const sal_Bool bPlainText, uno::Sequence<uno::Any>& aArgs, ScMySharedData*& pSharedData)
739 {
740     sal_Bool bRet(sal_False);
741     uno::Reference<io::XOutputStream> xOut;
742     uno::Reference<io::XStream> xStream;
743 
744     if ( !xStorage.is() && pMedium )
745         xStorage = pMedium->GetOutputStorage();
746 
747     if( xStorage.is() )
748     {
749         // #96807#; trunc stream before use, because it could be an existing stream
750         // and the new content could be shorter than the old content. In this case
751         // would not all be over written by the new content and the xml file
752         // would not be valid.
753         xStream = xStorage->openStreamElement( sName, embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
754         uno::Reference < beans::XPropertySet > xSet( xStream, uno::UNO_QUERY );
755         if (xSet.is())
756         {
757             xSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("MediaType")), uno::makeAny(sMediaType));
758             OUString aUseCommonPassPropName( RTL_CONSTASCII_USTRINGPARAM("UseCommonStoragePasswordEncryption") );
759             if (bPlainText)
760                 xSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Compressed")), uno::makeAny(sal_False));
761 
762             // even plain stream should be encrypted in encrypted documents
763             xSet->setPropertyValue( aUseCommonPassPropName, uno::makeAny(sal_True) );
764         }
765 
766         xOut = xStream->getOutputStream();
767     }
768     // #99667#; no longer necessary
769 /*  else if ( pMedium )
770     {
771         xOut = pMedium->GetDataSink();
772     }*/
773 
774     // set Base URL
775     uno::Reference< beans::XPropertySet > xInfoSet;
776     if( aArgs.getLength() > 0 )
777         aArgs.getConstArray()[0] >>= xInfoSet;
778     DBG_ASSERT( xInfoSet.is(), "missing property set" );
779     if( xInfoSet.is() )
780     {
781         rtl::OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("StreamName") );
782         xInfoSet->setPropertyValue( sPropName, uno::makeAny( sName ) );
783     }
784 
785     uno::Reference<io::XActiveDataSource> xSrc( xWriter, uno::UNO_QUERY );
786     xSrc->setOutputStream( xOut );
787 
788     uno::Reference<document::XFilter> xFilter(
789         xServiceFactory->createInstanceWithArguments( sComponentName , aArgs ),
790             uno::UNO_QUERY );
791     DBG_ASSERT( xFilter.is(), "can't get exporter" );
792     uno::Reference<document::XExporter> xExporter( xFilter, uno::UNO_QUERY );
793     uno::Reference<lang::XComponent> xComponent( xModel, uno::UNO_QUERY );
794     if (xExporter.is())
795         xExporter->setSourceDocument( xComponent );
796 
797     if ( xFilter.is() )
798     {
799         ScXMLExport* pExport = static_cast<ScXMLExport*>(SvXMLExport::getImplementation(xFilter));
800         pExport->SetSharedData(pSharedData);
801 
802         // if there are sheets to copy, get the source stream
803         if ( sName.equalsAscii("content.xml") && lcl_HasValidStream(rDoc) &&
804              ( pExport->getExportFlags() & EXPORT_OASIS ) )
805         {
806             // old stream is still in this file's storage - open read-only
807 
808             // #i106854# use the document's storage directly, without a temporary SfxMedium
809             uno::Reference<embed::XStorage> xTmpStorage = rDoc.GetDocumentShell()->GetStorage();
810             uno::Reference<io::XStream> xSrcStream;
811             uno::Reference<io::XInputStream> xSrcInput;
812 
813             // #i108978# If an embedded object is saved and no events are notified, don't use the stream
814             // because without the ...DONE events, stream positions aren't updated.
815             ScSheetSaveData* pSheetData = ScModelObj::getImplementation(xModel)->GetSheetSaveData();
816             if (pSheetData && pSheetData->IsInSupportedSave())
817             {
818                 try
819                 {
820                     if (xTmpStorage.is())
821                         xSrcStream = xTmpStorage->openStreamElement( sName, embed::ElementModes::READ );
822                     if (xSrcStream.is())
823                         xSrcInput = xSrcStream->getInputStream();
824                 }
825                 catch (uno::Exception&)
826                 {
827                     // stream not available (for example, password protected) - save normally (xSrcInput is null)
828                 }
829             }
830 
831             pExport->SetSourceStream( xSrcInput );
832             bRet = xFilter->filter( aDescriptor );
833             pExport->SetSourceStream( uno::Reference<io::XInputStream>() );
834 
835             // If there was an error, reset all stream flags, so the next save attempt will use normal saving.
836             // #i110692# For embedded objects, the stream may be unavailable for one save operation (m_pAntiImpl)
837             // and become available again later. But after saving normally once, the stream positions aren't
838             // valid anymore, so the flags also have to be reset if the stream wasn't available.
839             if ( !bRet || !xSrcInput.is() )
840             {
841                 SCTAB nTabCount = rDoc.GetTableCount();
842                 for (SCTAB nTab=0; nTab<nTabCount; nTab++)
843                     if (rDoc.IsStreamValid(nTab))
844                         rDoc.SetStreamValid(nTab, sal_False);
845             }
846         }
847         else
848             bRet = xFilter->filter( aDescriptor );
849 
850         pSharedData = pExport->GetSharedData();
851 
852         //stream is closed by SAX parser
853         //if (xOut.is())
854         //    xOut->closeOutput();
855     }
856     return bRet;
857 }
858 
Export(sal_Bool bStylesOnly)859 sal_Bool ScXMLImportWrapper::Export(sal_Bool bStylesOnly)
860 {
861     RTL_LOGFILE_CONTEXT_AUTHOR ( aLog, "sc", "sb99857", "ScXMLImportWrapper::Export" );
862 
863     uno::Reference<lang::XMultiServiceFactory> xServiceFactory(comphelper::getProcessServiceFactory());
864     DBG_ASSERT( xServiceFactory.is(), "got no service manager" );
865     if( !xServiceFactory.is() )
866         return sal_False;
867 
868     uno::Reference<uno::XInterface> xWriter(xServiceFactory->createInstance(
869             OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" )) ));
870     DBG_ASSERT( xWriter.is(), "com.sun.star.xml.sax.Writer service missing" );
871     if(!xWriter.is())
872         return sal_False;
873 
874     if ( !xStorage.is() && pMedium )
875         xStorage = pMedium->GetOutputStorage();
876 
877     uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
878 
879     OUString sFileName;
880     OUString sTextMediaType(RTL_CONSTASCII_USTRINGPARAM("text/xml"));
881     if (pMedium)
882         sFileName = pMedium->GetName();
883     SfxObjectShell* pObjSh = rDoc.GetDocumentShell();
884     uno::Sequence<beans::PropertyValue> aDescriptor(1);
885     beans::PropertyValue* pProps = aDescriptor.getArray();
886     pProps[0].Name = OUString( RTL_CONSTASCII_USTRINGPARAM( "FileName" ) );
887     pProps[0].Value <<= sFileName;
888 
889     /** property map for export info set */
890     comphelper::PropertyMapEntry aExportInfoMap[] =
891     {
892         { MAP_LEN( "ProgressRange" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
893         { MAP_LEN( "ProgressMax" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
894         { MAP_LEN( "ProgressCurrent" ), 0, &::getCppuType((sal_Int32*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
895         { MAP_LEN( "WrittenNumberStyles" ), 0, &::getCppuType((uno::Sequence<sal_Int32>*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
896         { MAP_LEN( "UsePrettyPrinting" ), 0, &::getCppuType((sal_Bool*)0), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0},
897         { MAP_LEN( "BaseURI" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
898         { MAP_LEN( "StreamRelPath" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
899         { MAP_LEN( "StreamName" ), 0, &::getCppuType( (rtl::OUString *)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
900         { MAP_LEN( "StyleNames" ), 0, &::getCppuType( (uno::Sequence<rtl::OUString>*)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
901         { MAP_LEN( "StyleFamilies" ), 0, &::getCppuType( (uno::Sequence<sal_Int32>*)0 ), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
902         { MAP_LEN( "TargetStorage" ), 0, &embed::XStorage::static_type(), ::com::sun::star::beans::PropertyAttribute::MAYBEVOID, 0 },
903         { NULL, 0, 0, NULL, 0, 0 }
904     };
905     uno::Reference< beans::XPropertySet > xInfoSet( comphelper::GenericPropertySet_CreateInstance( new comphelper::PropertySetInfo( aExportInfoMap ) ) );
906 
907     if ( pObjSh && xStorage.is() )
908     {
909         pObjSh->UpdateDocInfoForSave();     // update information
910 
911         uno::Reference<frame::XModel> xModel(pObjSh->GetModel());
912         uno::Reference<task::XStatusIndicator> xStatusIndicator(GetStatusIndicator());
913         sal_Int32 nProgressRange(1000000);
914         if(xStatusIndicator.is())
915             xStatusIndicator->start(rtl::OUString(ScGlobal::GetRscString(STR_SAVE_DOC)), nProgressRange);
916         xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ProgressRange")), uno::makeAny(nProgressRange));
917 
918         SvtSaveOptions aSaveOpt;
919         sal_Bool bUsePrettyPrinting(aSaveOpt.IsPrettyPrinting());
920         xInfoSet->setPropertyValue(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("UsePrettyPrinting")), uno::makeAny(bUsePrettyPrinting));
921 
922         const OUString sTargetStorage( RTL_CONSTASCII_USTRINGPARAM("TargetStorage") );
923         xInfoSet->setPropertyValue( sTargetStorage, uno::Any( xStorage ) );
924 
925         OSL_ENSURE( pMedium, "There is no medium to get MediaDescriptor from!\n" );
926         ::rtl::OUString aBaseURL = pMedium ? pMedium->GetBaseURL( true ) : ::rtl::OUString();
927         rtl::OUString sPropName( RTL_CONSTASCII_USTRINGPARAM("BaseURI") );
928         xInfoSet->setPropertyValue( sPropName, uno::makeAny( aBaseURL ) );
929 
930         // TODO/LATER: do not do it for embedded links
931         if( SFX_CREATE_MODE_EMBEDDED == pObjSh->GetCreateMode() )
932         {
933             OUString aName = ::rtl::OUString::createFromAscii( "dummyObjectName" );
934             if ( pMedium && pMedium->GetItemSet() )
935             {
936                 const SfxStringItem* pDocHierarchItem = static_cast<const SfxStringItem*>(
937                     pMedium->GetItemSet()->GetItem(SID_DOC_HIERARCHICALNAME) );
938                 if ( pDocHierarchItem )
939                     aName = pDocHierarchItem->GetValue();
940             }
941 
942             if( aName.getLength() )
943             {
944                 sPropName = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("StreamRelPath"));
945                 xInfoSet->setPropertyValue( sPropName, uno::makeAny( aName ) );
946             }
947         }
948 
949         sal_Bool bMetaRet(pObjSh->GetCreateMode() == SFX_CREATE_MODE_EMBEDDED);
950         sal_Bool bStylesRet (sal_False);
951         sal_Bool bDocRet(sal_False);
952         sal_Bool bSettingsRet(sal_False);
953         ScMySharedData* pSharedData = NULL;
954 
955         sal_Bool bOasis = ( SotStorage::GetVersion( xStorage ) > SOFFICE_FILEFORMAT_60 );
956 
957         // RDF metadata: ODF >= 1.2
958         if ( !bStylesOnly && bOasis )
959         {
960             const uno::Reference< beans::XPropertySet > xPropSet( xStorage, uno::UNO_QUERY_THROW );
961             try
962             {
963                 ::rtl::OUString aVersion;
964                 if (( xPropSet->getPropertyValue(
965                     rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Version"))) >>= aVersion )
966                     && !aVersion.equals(ODFVER_010_TEXT)
967                     && !aVersion.equals(ODFVER_011_TEXT) )
968                 {
969                     const uno::Reference< rdf::XDocumentMetadataAccess > xDMA(
970                         xModel, uno::UNO_QUERY_THROW );
971                     xDMA->storeMetadataToStorage( xStorage );
972                 }
973             }
974             catch (beans::UnknownPropertyException &)
975             {
976             }
977             catch (uno::Exception &)
978             {
979             }
980         }
981 
982         // meta export
983         if (!bStylesOnly && !bMetaRet)
984         {
985             uno::Sequence<uno::Any> aMetaArgs(3);
986             uno::Any* pMetaArgs = aMetaArgs.getArray();
987             pMetaArgs[0] <<= xInfoSet;
988             pMetaArgs[1] <<= xHandler;
989             pMetaArgs[2] <<= xStatusIndicator;
990 
991             RTL_LOGFILE_CONTEXT_TRACE( aLog, "meta export start" );
992 
993             bMetaRet = ExportToComponent(xServiceFactory, xModel, xWriter, aDescriptor,
994                 rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("meta.xml")),
995                 sTextMediaType,
996                 bOasis ? rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisMetaExporter"))
997                        : rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLMetaExporter")),
998                 sal_True, aMetaArgs, pSharedData);
999 
1000             RTL_LOGFILE_CONTEXT_TRACE( aLog, "meta export end" );
1001         }
1002 
1003         uno::Reference< document::XEmbeddedObjectResolver > xObjectResolver;
1004         SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
1005 
1006         uno::Reference< document::XGraphicObjectResolver > xGrfContainer;
1007         SvXMLGraphicHelper* pGraphicHelper = 0;
1008 
1009         if( xStorage.is() )
1010         {
1011             pGraphicHelper = SvXMLGraphicHelper::Create( xStorage, GRAPHICHELPER_MODE_WRITE, sal_False );
1012             xGrfContainer = pGraphicHelper;
1013         }
1014 
1015         if( pObjSh )
1016         {
1017             pObjectHelper = SvXMLEmbeddedObjectHelper::Create( xStorage, *pObjSh, EMBEDDEDOBJECTHELPER_MODE_WRITE, sal_False );
1018             xObjectResolver = pObjectHelper;
1019         }
1020 
1021         // styles export
1022 
1023         {
1024             uno::Sequence<uno::Any> aStylesArgs(5);
1025             uno::Any* pStylesArgs = aStylesArgs.getArray();
1026             pStylesArgs[0] <<= xInfoSet;
1027             pStylesArgs[1] <<= xGrfContainer;
1028             pStylesArgs[2] <<= xStatusIndicator;
1029             pStylesArgs[3] <<= xHandler;
1030             pStylesArgs[4] <<= xObjectResolver;
1031 
1032             RTL_LOGFILE_CONTEXT_TRACE( aLog, "styles export start" );
1033 
1034             bStylesRet = ExportToComponent(xServiceFactory, xModel, xWriter, aDescriptor,
1035                 rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("styles.xml")),
1036                 sTextMediaType,
1037                 bOasis ? rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisStylesExporter"))
1038                        : rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLStylesExporter")),
1039                 sal_False, aStylesArgs, pSharedData);
1040 
1041             RTL_LOGFILE_CONTEXT_TRACE( aLog, "styles export end" );
1042         }
1043 
1044         // content export
1045 
1046         if (!bStylesOnly)
1047         {
1048             uno::Sequence<uno::Any> aDocArgs(5);
1049             uno::Any* pDocArgs = aDocArgs.getArray();
1050             pDocArgs[0] <<= xInfoSet;
1051             pDocArgs[1] <<= xGrfContainer;
1052             pDocArgs[2] <<= xStatusIndicator;
1053             pDocArgs[3] <<= xHandler;
1054             pDocArgs[4] <<= xObjectResolver;
1055 
1056             RTL_LOGFILE_CONTEXT_TRACE( aLog, "content export start" );
1057 
1058             bDocRet = ExportToComponent(xServiceFactory, xModel, xWriter, aDescriptor,
1059                 rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("content.xml")),
1060                 sTextMediaType,
1061                 bOasis ? rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisContentExporter"))
1062                        : rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLContentExporter")),
1063                 sal_False, aDocArgs, pSharedData);
1064 
1065             RTL_LOGFILE_CONTEXT_TRACE( aLog, "content export end" );
1066         }
1067 
1068         if( pGraphicHelper )
1069             SvXMLGraphicHelper::Destroy( pGraphicHelper );
1070 
1071         if( pObjectHelper )
1072             SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
1073 
1074         // settings export
1075 
1076         if (!bStylesOnly)
1077         {
1078             uno::Sequence<uno::Any> aSettingsArgs(3);
1079             uno::Any* pSettingsArgs = aSettingsArgs.getArray();
1080             pSettingsArgs[0] <<= xInfoSet;
1081             pSettingsArgs[1] <<= xHandler;
1082             pSettingsArgs[2] <<= xStatusIndicator;
1083 
1084             RTL_LOGFILE_CONTEXT_TRACE( aLog, "settings export start" );
1085 
1086             bSettingsRet = ExportToComponent(xServiceFactory, xModel, xWriter, aDescriptor,
1087                 rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("settings.xml")),
1088                 sTextMediaType,
1089                 bOasis ? rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLOasisSettingsExporter"))
1090                        : rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("com.sun.star.comp.Calc.XMLSettingsExporter")),
1091                 sal_False, aSettingsArgs, pSharedData);
1092 
1093             RTL_LOGFILE_CONTEXT_TRACE( aLog, "settings export end" );
1094         }
1095 
1096         if (pSharedData)
1097             delete pSharedData;
1098 
1099         if (xStatusIndicator.is())
1100             xStatusIndicator->end();
1101         return bStylesRet && ((!bStylesOnly && bDocRet && bMetaRet && bSettingsRet) || bStylesOnly);
1102     }
1103 
1104     // later: give string descriptor as parameter for doc type
1105 
1106     return sal_False;
1107 }
1108 
1109 
1110 
1111