bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / xml / xmlexport.cxx
blobc642e0a376faafbc6c8e4f100e1c0649943fe743
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <tools/debug.hxx>
21 #include <tools/diagnose_ex.h>
22 #include <com/sun/star/xml/sax/InputSource.hpp>
23 #include <com/sun/star/xml/sax/Parser.hpp>
24 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
25 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
26 #include <com/sun/star/xml/sax/Writer.hpp>
27 #include <com/sun/star/lang/XComponent.hpp>
28 #include <com/sun/star/io/XActiveDataSource.hpp>
29 #include <com/sun/star/xml/sax/SAXParseException.hpp>
30 #include <com/sun/star/io/XOutputStream.hpp>
31 #include <com/sun/star/document/XFilter.hpp>
32 #include <com/sun/star/document/XExporter.hpp>
33 #include <com/sun/star/document/XImporter.hpp>
34 #include <comphelper/processfactory.hxx>
35 #include <unotools/streamwrap.hxx>
36 #include <sot/storage.hxx>
37 #include <svx/svdmodel.hxx>
38 #include <svx/xmleohlp.hxx>
39 #include <svx/xmlgrhlp.hxx>
41 #include <svx/unomodel.hxx>
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
46 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
48 return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
51 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
53 bool bDocRet = xOut.is();
55 Reference< document::XGraphicObjectResolver > xGraphicResolver;
56 SvXMLGraphicHelper *pGraphicHelper = 0;
58 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
59 SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
61 Reference< lang::XComponent > xSourceDoc( xComponent );
62 try
64 if( !xSourceDoc.is() )
66 xSourceDoc = new SvxUnoDrawingModel( pModel );
67 pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
70 uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
72 if( bDocRet )
74 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
76 ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
77 if( pPersist )
79 pObjectHelper = SvXMLEmbeddedObjectHelper::Create( *pPersist, EMBEDDEDOBJECTHELPER_MODE_WRITE );
80 xObjectResolver = pObjectHelper;
83 pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_WRITE );
84 xGraphicResolver = pGraphicHelper;
86 if( bDocRet )
88 uno::Reference<xml::sax::XDocumentHandler> xHandler( xWriter, uno::UNO_QUERY );
90 // doc export
91 uno::Reference< io::XActiveDataSource > xDocSrc( xWriter, uno::UNO_QUERY );
92 xDocSrc->setOutputStream( xOut );
94 uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
95 aArgs[0] <<= xHandler;
96 aArgs[1] <<= xGraphicResolver;
97 if( xObjectResolver.is() )
98 aArgs[2] <<= xObjectResolver;
100 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
101 if( !xFilter.is() )
103 OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
104 bDocRet = false;
107 if( bDocRet )
109 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
110 if( xExporter.is() )
112 xExporter->setSourceDocument( xSourceDoc );
114 uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
115 bDocRet = xFilter->filter( aDescriptor );
121 catch(uno::Exception const&)
123 DBG_UNHANDLED_EXCEPTION();
124 bDocRet = false;
127 if( pGraphicHelper )
128 SvXMLGraphicHelper::Destroy( pGraphicHelper );
129 xGraphicResolver = 0;
131 if( pObjectHelper )
132 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
133 xObjectResolver = 0;
135 return bDocRet;
138 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
140 Reference< lang::XComponent > xComponent;
141 return SvxDrawingLayerExport( pModel, xOut, xComponent );
146 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
148 return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
151 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
153 sal_uInt32 nRet = 0;
155 Reference< document::XGraphicObjectResolver > xGraphicResolver;
156 SvXMLGraphicHelper *pGraphicHelper = 0;
158 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
159 SvXMLEmbeddedObjectHelper *pObjectHelper = 0;
161 Reference< lang::XComponent > xTargetDocument( xComponent );
162 if( !xTargetDocument.is() )
164 xTargetDocument = new SvxUnoDrawingModel( pModel );
165 pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
168 Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
172 // Get service factory
173 Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
175 if ( xTargetModel.is() )
176 xTargetModel->lockControllers();
180 pGraphicHelper = SvXMLGraphicHelper::Create( GRAPHICHELPER_MODE_READ );
181 xGraphicResolver = pGraphicHelper;
183 ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
184 if( pPersist )
186 pObjectHelper = SvXMLEmbeddedObjectHelper::Create(
187 *pPersist,
188 EMBEDDEDOBJECTHELPER_MODE_READ );
189 xObjectResolver = pObjectHelper;
194 if( 0 == nRet )
197 // parse
198 // prepare ParserInputSrouce
199 xml::sax::InputSource aParserInput;
200 aParserInput.aInputStream = xInputStream;
202 // get parser
203 Reference< xml::sax::XParser > xParser = xml::sax::Parser::create( xContext );
205 // prepare filter arguments
206 Sequence<Any> aFilterArgs( 2 );
207 Any *pArgs = aFilterArgs.getArray();
208 *pArgs++ <<= xGraphicResolver;
209 *pArgs++ <<= xObjectResolver;
211 // get filter
212 Reference< xml::sax::XDocumentHandler > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext), UNO_QUERY );
213 DBG_ASSERT( xFilter.is(), "Can't instantiate filter component." );
215 nRet = 1;
216 if( xParser.is() && xFilter.is() )
218 // connect parser and filter
219 xParser->setDocumentHandler( xFilter );
221 // connect model and filter
222 uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
223 xImporter->setTargetDocument( xTargetDocument );
225 // finally, parser the stream
226 xParser->parseStream( aParserInput );
228 nRet = 0;
232 catch( uno::Exception& )
234 DBG_UNHANDLED_EXCEPTION();
237 if( pGraphicHelper )
238 SvXMLGraphicHelper::Destroy( pGraphicHelper );
239 xGraphicResolver = 0;
241 if( pObjectHelper )
242 SvXMLEmbeddedObjectHelper::Destroy( pObjectHelper );
243 xObjectResolver = 0;
245 if ( xTargetModel.is() )
246 xTargetModel->unlockControllers();
248 return nRet == 0;
251 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
253 Reference< lang::XComponent > xComponent;
254 return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */