Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / xml / xmlexport.cxx
blobd6bc3870ce080a13909e39a47a09258ba619fe60
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 <comphelper/diagnose_ex.hxx>
21 #include <com/sun/star/xml/sax/InputSource.hpp>
22 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
23 #include <com/sun/star/xml/sax/Writer.hpp>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/xml/sax/XFastParser.hpp>
26 #include <com/sun/star/io/XOutputStream.hpp>
27 #include <com/sun/star/document/XFilter.hpp>
28 #include <com/sun/star/document/XExporter.hpp>
29 #include <com/sun/star/document/XImporter.hpp>
30 #include <comphelper/processfactory.hxx>
31 #include <svx/svdmodel.hxx>
32 #include <svx/xmleohlp.hxx>
33 #include <svx/xmlgrhlp.hxx>
35 #include <svx/unomodel.hxx>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
40 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent )
42 return SvxDrawingLayerExport( pModel, xOut, xComponent, "com.sun.star.comp.DrawingLayer.XMLExporter" );
45 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut, const Reference< lang::XComponent >& xComponent, const char* pExportService )
47 bool bDocRet = xOut.is();
49 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
50 rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
52 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
53 rtl::Reference<SvXMLEmbeddedObjectHelper> xObjectHelper;
55 Reference< lang::XComponent > xSourceDoc( xComponent );
56 try
58 if( !xSourceDoc.is() )
60 xSourceDoc = new SvxUnoDrawingModel( pModel );
61 pModel->setUnoModel( Reference< XInterface >::query( xSourceDoc ) );
64 uno::Reference< uno::XComponentContext> xContext( ::comphelper::getProcessComponentContext() );
66 if( bDocRet )
68 uno::Reference< xml::sax::XWriter > xWriter = xml::sax::Writer::create( xContext );
70 ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
71 if( pPersist )
73 xObjectHelper = SvXMLEmbeddedObjectHelper::Create( *pPersist, SvXMLEmbeddedObjectHelperMode::Write );
74 xObjectResolver = xObjectHelper.get();
77 xGraphicHelper = SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode::Write );
78 xGraphicStorageHandler = xGraphicHelper.get();
80 uno::Reference<xml::sax::XDocumentHandler> xHandler = xWriter;
82 // doc export
83 xWriter->setOutputStream( xOut );
85 uno::Sequence< uno::Any > aArgs( xObjectResolver.is() ? 3 : 2 );
86 auto pArgs = aArgs.getArray();
87 pArgs[0] <<= xHandler;
88 pArgs[1] <<= xGraphicStorageHandler;
89 if( xObjectResolver.is() )
90 pArgs[2] <<= xObjectResolver;
92 uno::Reference< document::XFilter > xFilter( xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pExportService ), aArgs, xContext ), uno::UNO_QUERY );
93 if( !xFilter.is() )
95 OSL_FAIL( "com.sun.star.comp.Draw.XMLExporter service missing" );
96 bDocRet = false;
99 if( bDocRet )
101 uno::Reference< document::XExporter > xExporter( xFilter, uno::UNO_QUERY );
102 if( xExporter.is() )
104 xExporter->setSourceDocument( xSourceDoc );
106 uno::Sequence< beans::PropertyValue > aDescriptor( 0 );
107 bDocRet = xFilter->filter( aDescriptor );
112 catch(uno::Exception const&)
114 DBG_UNHANDLED_EXCEPTION("svx");
115 bDocRet = false;
118 if( xGraphicHelper )
119 xGraphicHelper->dispose();
120 xGraphicHelper.clear();
121 xGraphicStorageHandler = nullptr;
123 if( xObjectHelper.is() )
124 xObjectHelper->dispose();
126 return bDocRet;
129 bool SvxDrawingLayerExport( SdrModel* pModel, const uno::Reference<io::XOutputStream>& xOut )
131 Reference< lang::XComponent > xComponent;
132 return SvxDrawingLayerExport( pModel, xOut, xComponent );
137 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent )
139 return SvxDrawingLayerImport( pModel, xInputStream, xComponent, "com.sun.star.comp.Draw.XMLOasisImporter" );
142 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream, const Reference< lang::XComponent >& xComponent, const char* pImportService )
144 bool bRet = true;
146 uno::Reference<document::XGraphicStorageHandler> xGraphicStorageHandler;
147 rtl::Reference<SvXMLGraphicHelper> xGraphicHelper;
149 Reference< document::XEmbeddedObjectResolver > xObjectResolver;
150 rtl::Reference<SvXMLEmbeddedObjectHelper> xObjectHelper;
152 Reference< lang::XComponent > xTargetDocument( xComponent );
153 if( !xTargetDocument.is() )
155 xTargetDocument = new SvxUnoDrawingModel( pModel );
156 pModel->setUnoModel( Reference< XInterface >::query( xTargetDocument ) );
159 Reference< frame::XModel > xTargetModel( xTargetDocument, UNO_QUERY );
163 // Get service factory
164 Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
166 if ( xTargetModel.is() )
167 xTargetModel->lockControllers();
170 xGraphicHelper = SvXMLGraphicHelper::Create( SvXMLGraphicHelperMode::Read );
171 xGraphicStorageHandler = xGraphicHelper.get();
173 ::comphelper::IEmbeddedHelper *pPersist = pModel->GetPersist();
174 if( pPersist )
176 xObjectHelper = SvXMLEmbeddedObjectHelper::Create(
177 *pPersist,
178 SvXMLEmbeddedObjectHelperMode::Read );
179 xObjectResolver = xObjectHelper.get();
182 // parse
183 // prepare ParserInputSource
184 xml::sax::InputSource aParserInput;
185 aParserInput.aInputStream = xInputStream;
187 // prepare filter arguments
188 Sequence<Any> aFilterArgs( 2 );
189 Any *pArgs = aFilterArgs.getArray();
190 *pArgs++ <<= xGraphicStorageHandler;
191 *pArgs++ <<= xObjectResolver;
193 // get filter
194 Reference< XInterface > xFilter = xContext->getServiceManager()->createInstanceWithArgumentsAndContext( OUString::createFromAscii( pImportService ), aFilterArgs, xContext);
195 SAL_WARN_IF( !xFilter, "svx", "Can't instantiate filter component " << pImportService);
196 uno::Reference< xml::sax::XFastParser > xFastParser( xFilter, UNO_QUERY );
197 assert(xFastParser);
199 bRet = false;
200 if( xFastParser.is() )
202 // connect model and filter
203 uno::Reference < document::XImporter > xImporter( xFilter, UNO_QUERY );
204 xImporter->setTargetDocument( xTargetDocument );
206 // finally, parser the stream
207 xFastParser->parseStream( aParserInput );
209 bRet = true;
212 catch( uno::Exception& )
214 DBG_UNHANDLED_EXCEPTION("svx");
217 if( xGraphicHelper )
218 xGraphicHelper->dispose();
219 xGraphicHelper.clear();
220 xGraphicStorageHandler = nullptr;
222 if( xObjectHelper.is() )
223 xObjectHelper->dispose();
224 xObjectHelper.clear();
225 xObjectResolver = nullptr;
227 if ( xTargetModel.is() )
228 xTargetModel->unlockControllers();
230 return bRet;
233 bool SvxDrawingLayerImport( SdrModel* pModel, const uno::Reference<io::XInputStream>& xInputStream )
235 Reference< lang::XComponent > xComponent;
236 return SvxDrawingLayerImport( pModel, xInputStream, xComponent );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */