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