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