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