xref: /aoo41x/main/svx/source/xml/xmlexport.cxx (revision f6e50924)
1*f6e50924SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*f6e50924SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*f6e50924SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*f6e50924SAndrew Rist  * distributed with this work for additional information
6*f6e50924SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*f6e50924SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*f6e50924SAndrew Rist  * "License"); you may not use this file except in compliance
9*f6e50924SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*f6e50924SAndrew Rist  *
11*f6e50924SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*f6e50924SAndrew Rist  *
13*f6e50924SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*f6e50924SAndrew Rist  * software distributed under the License is distributed on an
15*f6e50924SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f6e50924SAndrew Rist  * KIND, either express or implied.  See the License for the
17*f6e50924SAndrew Rist  * specific language governing permissions and limitations
18*f6e50924SAndrew Rist  * under the License.
19*f6e50924SAndrew Rist  *
20*f6e50924SAndrew Rist  *************************************************************/
21*f6e50924SAndrew Rist 
22*f6e50924SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svx.hxx"
26cdf0e10cSrcweir #include <tools/debug.hxx>
27cdf0e10cSrcweir #include <tools/diagnose_ex.h>
28cdf0e10cSrcweir #include <com/sun/star/xml/sax/InputSource.hpp>
29cdf0e10cSrcweir #include <com/sun/star/xml/sax/XParser.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
32cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
33cdf0e10cSrcweir #include <com/sun/star/io/XActiveDataSource.hpp>
34cdf0e10cSrcweir #include <com/sun/star/xml/sax/SAXParseException.hpp>
35cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
36cdf0e10cSrcweir #include <com/sun/star/document/XFilter.hpp>
37cdf0e10cSrcweir #include <com/sun/star/document/XExporter.hpp>
38cdf0e10cSrcweir #include <com/sun/star/document/XImporter.hpp>
39cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
40cdf0e10cSrcweir #include <unotools/streamwrap.hxx>
41cdf0e10cSrcweir #include <sot/storage.hxx>
42cdf0e10cSrcweir #include <svx/svdmodel.hxx>
43cdf0e10cSrcweir #include <svx/xmleohlp.hxx>
44cdf0e10cSrcweir #include <svx/xmlgrhlp.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include <svx/unomodel.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using ::rtl::OUString;
49cdf0e10cSrcweir using namespace ::com::sun::star;
50cdf0e10cSrcweir using namespace ::com::sun::star::uno;
51cdf0e10cSrcweir 
SvxDrawingLayerExport(SdrModel * pModel,const uno::Reference<io::XOutputStream> & xOut,const Reference<lang::XComponent> & xComponent)52cdf0e10cSrcweir sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
SvxDrawingLayerExport(SdrModel * pModel,const uno::Reference<io::XOutputStream> & xOut,const Reference<lang::XComponent> & xComponent,const char * pExportService)57cdf0e10cSrcweir sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	sal_Bool bDocRet = xOut.is();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 	Reference< document::XGraphicObjectResolver > xGraphicResolver;
62cdf0e10cSrcweir 	SvXMLGraphicHelper *pGraphicHelper = 0;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	Reference< document::XEmbeddedObjectResolver > xObjectResolver;
65cdf0e10cSrcweir 	SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     Reference< lang::XComponent > xSourceDoc( xComponent );
68cdf0e10cSrcweir 	try
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		if( !xSourceDoc.is() )
71cdf0e10cSrcweir 		{
72cdf0e10cSrcweir 			xSourceDoc = new SvxUnoDrawingModel( pModel );
73cdf0e10cSrcweir 			pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
74cdf0e10cSrcweir 		}
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 		uno::Reference< lang::XMultiServiceFactory> xServiceFactory( ::comphelper::getProcessServiceFactory() );
77cdf0e10cSrcweir 		if( !xServiceFactory.is() )
78cdf0e10cSrcweir 		{
79cdf0e10cSrcweir 			DBG_ERROR( "got no service manager" );
80cdf0e10cSrcweir 			bDocRet = sal_False;
81cdf0e10cSrcweir 		}
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 		if( bDocRet )
84cdf0e10cSrcweir 		{
85cdf0e10cSrcweir 			uno::Reference< uno::XInterface > xWriter( xServiceFactory->createInstance( OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.xml.sax.Writer" ) ) ) );
86cdf0e10cSrcweir 			if( !xWriter.is() )
87cdf0e10cSrcweir 			{
88cdf0e10cSrcweir 				DBG_ERROR( "com.sun.star.xml.sax.Writer service missing" );
89cdf0e10cSrcweir 				bDocRet = sal_False;
90cdf0e10cSrcweir 			}
91cdf0e10cSrcweir 
92cdf0e10cSrcweir             ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
93cdf0e10cSrcweir 			if( pPersist )
94cdf0e10cSrcweir 			{
95cdf0e10cSrcweir 				pObjectHelper = SvXMLEmbeddedObjectHelper::Create( *pPersist, EMBEDDEDOBJECTHELPER_MODE_WRITE );
96cdf0e10cSrcweir 				xObjectResolver = pObjectHelper;
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 			pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_WRITE );
100cdf0e10cSrcweir 			xGraphicResolver = pGraphicHelper;
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 			if( bDocRet )
103cdf0e10cSrcweir 			{
104cdf0e10cSrcweir 				uno::Reference<xml::sax::XDocumentHandler>	xHandler( xWriter, uno::UNO_QUERY );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 				// doc export
107cdf0e10cSrcweir 				uno::Reference< io::XActiveDataSource > xDocSrc( xWriter, uno::UNO_QUERY );
108cdf0e10cSrcweir 				xDocSrc->setOutputStream( xOut );
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 				uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
111cdf0e10cSrcweir 				aArgs[0] <<= xHandler;
112cdf0e10cSrcweir 				aArgs[1] <<= xGraphicResolver;
113cdf0e10cSrcweir 				if( xObjectResolver.is() )
114cdf0e10cSrcweir 					aArgs[2] <<= xObjectResolver;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 				uno::Reference< document::XFilter > xFilter( xServiceFactory->createInstanceWithArguments( OUString::createFromAscii( pExportService ), aArgs ), uno::UNO_QUERY );
117cdf0e10cSrcweir 				if( !xFilter.is() )
118cdf0e10cSrcweir 				{
119cdf0e10cSrcweir 					DBG_ERROR( "com.sun.star.comp.Draw.XMLExporter service missing" );
120cdf0e10cSrcweir 					bDocRet = sal_False;
121cdf0e10cSrcweir 				}
122cdf0e10cSrcweir 
123cdf0e10cSrcweir 				if( bDocRet )
124cdf0e10cSrcweir 				{
125cdf0e10cSrcweir 					uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
126cdf0e10cSrcweir 					if( xExporter.is() )
127cdf0e10cSrcweir 					{
128cdf0e10cSrcweir 						xExporter->setSourceDocument( xSourceDoc );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 						uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
131cdf0e10cSrcweir 						bDocRet = xFilter->filter( aDescriptor );
132cdf0e10cSrcweir 					}
133cdf0e10cSrcweir 				}
134cdf0e10cSrcweir 			}
135cdf0e10cSrcweir 		}
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 	catch(uno::Exception e)
138cdf0e10cSrcweir 	{
139cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
140cdf0e10cSrcweir 		bDocRet = sal_False;
141cdf0e10cSrcweir 	}
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	if( pGraphicHelper )
144cdf0e10cSrcweir 		SvXMLGraphicHelper::Destroy( pGraphicHelper );
145cdf0e10cSrcweir 	xGraphicResolver = 0;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	if( pObjectHelper )
148cdf0e10cSrcweir 	    SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
149cdf0e10cSrcweir 	xObjectResolver = 0;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir 	return bDocRet;
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
SvxDrawingLayerExport(SdrModel * pModel,const uno::Reference<io::XOutputStream> & xOut)154cdf0e10cSrcweir sal_Bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
155cdf0e10cSrcweir {
156cdf0e10cSrcweir 	Reference< lang::XComponent > xComponent;
157cdf0e10cSrcweir 	return SvxDrawingLayerExport( pModel, xOut, xComponent );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir //-////////////////////////////////////////////////////////////////////
161cdf0e10cSrcweir 
SvxDrawingLayerImport(SdrModel * pModel,const uno::Reference<io::XInputStream> & xInputStream,const Reference<lang::XComponent> & xComponent)162cdf0e10cSrcweir sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
163cdf0e10cSrcweir {
164cdf0e10cSrcweir 	return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
SvxDrawingLayerImport(SdrModel * pModel,const uno::Reference<io::XInputStream> & xInputStream,const Reference<lang::XComponent> & xComponent,const char * pImportService)167cdf0e10cSrcweir sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService  )
168cdf0e10cSrcweir {
169cdf0e10cSrcweir 	sal_uInt32	nRet = 0;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 	Reference< document::XGraphicObjectResolver > xGraphicResolver;
172cdf0e10cSrcweir 	SvXMLGraphicHelper *pGraphicHelper = 0;
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 	Reference< document::XEmbeddedObjectResolver > xObjectResolver;
175cdf0e10cSrcweir 	SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     Reference< lang::XComponent > xTargetDocument( xComponent );
178cdf0e10cSrcweir 	if( !xTargetDocument.is() )
179cdf0e10cSrcweir 	{
180cdf0e10cSrcweir 		xTargetDocument = new SvxUnoDrawingModel( pModel );
181cdf0e10cSrcweir 		pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
182cdf0e10cSrcweir 	}
183cdf0e10cSrcweir 
184cdf0e10cSrcweir 	Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 	try
187cdf0e10cSrcweir 	{
188cdf0e10cSrcweir 		// Get service factory
189cdf0e10cSrcweir 		Reference< lang::XMultiServiceFactory > xServiceFactory = comphelper::getProcessServiceFactory();
190cdf0e10cSrcweir 		DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" );
191cdf0e10cSrcweir 
192cdf0e10cSrcweir 		if( !xServiceFactory.is() )
193cdf0e10cSrcweir 			nRet = 1;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 		if( 0 == nRet )
196cdf0e10cSrcweir 		{
197cdf0e10cSrcweir             if ( xTargetModel.is() )
198cdf0e10cSrcweir 			    xTargetModel->lockControllers();
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 			// -------------------------------------
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 			pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_READ );
203cdf0e10cSrcweir 			xGraphicResolver = pGraphicHelper;
204cdf0e10cSrcweir 
205cdf0e10cSrcweir             ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
206cdf0e10cSrcweir 			if( pPersist )
207cdf0e10cSrcweir 			{
208cdf0e10cSrcweir 				pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
209cdf0e10cSrcweir 											*pPersist,
210cdf0e10cSrcweir 											EMBEDDEDOBJECTHELPER_MODE_READ );
211cdf0e10cSrcweir 				xObjectResolver = pObjectHelper;
212cdf0e10cSrcweir             }
213cdf0e10cSrcweir 		}
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 		// -------------------------------------
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 		if( 0 == nRet )
218cdf0e10cSrcweir 		{
219cdf0e10cSrcweir 
220cdf0e10cSrcweir 			// parse
221cdf0e10cSrcweir 			// prepare ParserInputSrouce
222cdf0e10cSrcweir 			xml::sax::InputSource aParserInput;
223cdf0e10cSrcweir 			aParserInput.aInputStream = xInputStream;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 			// get parser
226cdf0e10cSrcweir 			Reference< xml::sax::XParser > xParser( xServiceFactory->createInstance( OUString::createFromAscii("com.sun.star.xml.sax.Parser") ), UNO_QUERY );
227cdf0e10cSrcweir 			DBG_ASSERT( xParser.is(), "Can't create parser" );
228cdf0e10cSrcweir 
229cdf0e10cSrcweir 			// prepare filter arguments
230cdf0e10cSrcweir 			Sequence<Any> aFilterArgs( 2 );
231cdf0e10cSrcweir 			Any *pArgs = aFilterArgs.getArray();
232cdf0e10cSrcweir 			*pArgs++ <<= xGraphicResolver;
233cdf0e10cSrcweir 			*pArgs++ <<= xObjectResolver;
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			// get filter
236cdf0e10cSrcweir 			Reference< xml::sax::XDocumentHandler > xFilter( xServiceFactory->createInstanceWithArguments( OUString::createFromAscii( pImportService ), aFilterArgs), UNO_QUERY );
237cdf0e10cSrcweir 			DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
238cdf0e10cSrcweir 
239cdf0e10cSrcweir             nRet = 1;
240cdf0e10cSrcweir 			if( xParser.is() && xFilter.is() )
241cdf0e10cSrcweir 			{
242cdf0e10cSrcweir 				// connect parser and filter
243cdf0e10cSrcweir 				xParser->setDocumentHandler( xFilter );
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 				// connect model and filter
246cdf0e10cSrcweir 				uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
247cdf0e10cSrcweir 				xImporter->setTargetDocument( xTargetDocument );
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 				// finally, parser the stream
250cdf0e10cSrcweir 				xParser->parseStream( aParserInput );
251cdf0e10cSrcweir 
252cdf0e10cSrcweir                 nRet = 0;
253cdf0e10cSrcweir 			}
254cdf0e10cSrcweir 		}
255cdf0e10cSrcweir 	}
256cdf0e10cSrcweir 	catch( uno::Exception& r )
257cdf0e10cSrcweir 	{
258cdf0e10cSrcweir         DBG_UNHANDLED_EXCEPTION();
259cdf0e10cSrcweir 	}
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 	if( pGraphicHelper )
262cdf0e10cSrcweir 		SvXMLGraphicHelper::Destroy( pGraphicHelper );
263cdf0e10cSrcweir 	xGraphicResolver = 0;
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 	if( pObjectHelper )
266cdf0e10cSrcweir 		SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
267cdf0e10cSrcweir 	xObjectResolver = 0;
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	if ( xTargetModel.is() )
270cdf0e10cSrcweir 		xTargetModel->unlockControllers();
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	return nRet == 0;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir 
SvxDrawingLayerImport(SdrModel * pModel,const uno::Reference<io::XInputStream> & xInputStream)275cdf0e10cSrcweir sal_Bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
276cdf0e10cSrcweir {
277cdf0e10cSrcweir 	Reference< lang::XComponent > xComponent;
278cdf0e10cSrcweir 	return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
279cdf0e10cSrcweir }
280