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